All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.docshare.mvc.YangClassLoader Maven / Gradle / Ivy

Go to download

An efficient, fast, convenient, easy to learn, easy to use MVC framework and ORM framework. It is seamless compatible with JSTL and supports FreeMarker. It can run independently, and it can also be applied to traditional Java Web projects. It is an efficient, fast, convenient, easy to learn and easy to use MVC framework and ORM framework. It is seamless compatible with JSTL and supports FreeMarker. It can be run on its own, or it can be applied to traditional Java Web projects

There is a newer version: 2023.06.19
Show newest version
package org.docshare.mvc;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import org.docshare.log.Log;

class YangClassLoader extends ClassLoader{
    private int version;
	private Object root;
	private String reloadPackage;
	public YangClassLoader(int v,String clsRoot,String reloadPackage) {
    	this.version = v;
    	if(! clsRoot.endsWith("/")){
    		clsRoot+= "/";
    	}
    	this.root = clsRoot;
    	this.reloadPackage = reloadPackage;
	}
	public String name2Path(String cname){
		return root + cname.replace(".", "/")+".class";
	}

	@Override
	public Class loadClass(String name) throws ClassNotFoundException {
    	

        if(!name.startsWith(reloadPackage))
                return YangClassLoader.class.getClassLoader().loadClass(name);
        try {
        	Log.d("YangClassLoader v"+version +", load "+name);
//            String url = "file:C:/data/projects/tutorials/web/WEB-INF/" +
//                            "classes/reflection/MyObject.class";
//            URL myUrl = new URL(url);
//            URLConnection connection = myUrl.openConnection();
//            InputStream input = connection.getInputStream();
        	
        
        	//FileInputStream input = new FileInputStream(name2Path(name));
        	//Log.d(name2Path(name));
        	Log.d("YangClassLoader "+getClass().getResource(name2Path(name)));
        	
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        	InputStream input = getClass().getResourceAsStream(name2Path(name));
        	if(input == null){
        		return null;
        	}
        	//int data = input.read();
        	byte[] buf =new byte[1024];
            int num = input.read(buf);
            
        	while(num > 0){
                buffer.write(buf,0,num);
                num = input.read(buf);
            }

            input.close();

            byte[] classData = buffer.toByteArray();

            Class ret = defineClass(name,
                    classData, 0, classData.length);
            
            MethodAccessCacher.put(name, ret);
            return ret;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy