Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Creative commons Attribution-NonCommercial license. http://creativecommons.org/licenses/by-nc/2.5/au/deed.en_GB NO WARRANTY IS GIVEN OR IMPLIED, USE AT YOUR OWN RISK.
  
  package base.plugin;
  
 import java.io.File;
 
 
Experimental support for installation of newer versions of classes at runtime. Compiled classes are installed with a version number, allowing runtime selection of which version of classes should be in use.
 
 public class PluginClassLoader extends ClassLoader {
 
 	private Hashtable<StringClass<?>> classes = new Hashtable<StringClass<?>>();
 	private int version;
 	private File folder;
 	private DataSource ds;
 
 	public PluginClassLoader(File folderint version) {
 		this. = version;
 		this. = folder;
 	}
 
 	public PluginClassLoader(DataSource dsint version) {
 		this. = version;
 		this. = ds;
 	}
 
 	public Class<?> findClass(String classNamethrows ClassNotFoundException {
 		if( == 0) {
 			return findSystemClass(className);
 		}
 
 		byte[] classByte;
 		Class<?> result = (Class<?>).get(className);
 		if(result != null) {
 			return result;
 		}
 
 		try{
 			classByte = loadClassData(className);
 			if(classByte != null) {
 				result = defineClass(classNameclassByte, 0, classByte.lengthnull);
 				.put(classNameresult);
 				return result;
 			}
 		} catch(IOException e) {
 			return null;
 		}
 
 		return findSystemClass(className);
 	}

Load a class from the data store matching the current version of the plugin.

Parameters:
className
Returns:
Throws:
java.io.IOException
 
 	private byte[] loadClassData(String classNamethrows IOException {
 		byte[] buff = null;
 
 		if( != null) {
 			File f = new File("v" +  + . + className);
 			int size = (int)f.length();
 			buff = new byte[size];
 			FileInputStream fis = new FileInputStream(f);
 			DataInputStream dis = new DataInputStream(fis);
 			dis.readFully(buff);
 			dis.close();
 		} else {
 			Connection c = null;
 			PreparedStatement s = null;
 			ResultSet r = null;
 			try {
 				c = .getConnection();
 				s = c.prepareStatement("select data from plugin_class where name=? and version=?");
 				s.setString(1, className);
				r = s.executeQuery();
				if(r.next()) {
					buff = r.getBytes(1);
				}
catch(SQLException e) {
				throw new IOException(e);
finally {
				if(r != null) { try { r.close(); } catch(Exception e) {} }
				if(s != null) { try { s.close(); } catch(Exception e) {} }
				if(c != null) { try { c.close(); } catch(Exception e) {} }
			}
		}
		return buff;
	}
	private static void installClassesFromZip(InputStream inDataSource ds) {
	}
	private static void installClassesFromZip(InputStream inFile folder) {
	}
	private static void installClassesFromFolder(InputStream inDataSource ds) {
	}
	private static void installClassesFromFolder(InputStream inFile folder) {
	}
	private static void writeClass(Connection cString namebyte[] bytes) {
	}
	private static void writeClass(File folderString namebyte[] bytes) {
	}
New to GrepCode? Check out our FAQ X