Math2mat

/home/ythoma/docs/math2mat/svn/wp1/framework/m2mGUI/src/m2m/backend/buildingblocks/BuildingBlockLoader.java

Go to the documentation of this file.
00001 
00002 package m2m.backend.buildingblocks;
00003 
00004 import java.util.Hashtable;
00005 import java.io.FileInputStream;
00006 
00007 public class BuildingBlockLoader extends ClassLoader {
00008         
00009         private static BuildingBlockLoader instance = null;
00010         
00011         private Hashtable<String,Class<?>> classes = new Hashtable<String,Class<?>>();
00012         private String m_dir;
00013         private String m_packageName;
00014         
00015         public static BuildingBlockLoader getInstance() {
00016                 if (instance == null) {
00017                         instance = new BuildingBlockLoader();
00018                 }
00019                 return instance;
00020         }
00021     
00022         public void setDir(String dir) {
00023                 this.m_dir = dir;
00024         }
00025         public void setPackageName(String name) {
00026                 this.m_packageName = name;
00027         }
00028 
00033         private byte getClassImplFromDataBase(String className)[] {
00034                 //System.out.println(\t>>>>>> Fetching the implementation of "+className);
00035                 byte result[];
00036                 try {
00037                         FileInputStream fi = 
00038                                           new FileInputStream(this.m_dir+"/"+className+".class");
00039                         result = new byte[fi.available()];
00040                         fi.read(result);
00041                         return result;
00042                 } catch (Exception e) {
00043                         /*
00044                          * If we caught an exception, either the class wasnt found or it
00045                          * was unreadable by our process.
00046                          */
00047                         return null;
00048                 }
00049         }
00050 
00056         @Override
00057         public Class<?> loadClass(String className) throws ClassNotFoundException {
00058                 @SuppressWarnings("unchecked")
00059                 Class<?> c=(Class<BuildingBlock>)loadClass(className,true);
00060                 return (c);
00061         }
00062 
00063         public BuildingBlock newInstance(String className) 
00064                           throws ClassNotFoundException {
00065                 try { 
00066                         Object o = (loadClass(className)).newInstance();
00067                         BuildingBlock b=((BuildingBlock) o); 
00068                         return b; 
00069                 } catch (Exception e) { 
00070                         //System.out.println("Caught exception : "+e);
00071                         return null; 
00072                 } 
00073         }
00074         
00080         public synchronized Class<?> loadClass(String className, boolean resolveIt)
00081                           throws ClassNotFoundException {
00082                 Class<?> result;
00083                 byte  classData[];
00084 
00085                 //System.out.println("\t>>>>>> Load class : "+className);
00086 
00087                 try{
00088                         Class<?> t = Class.forName(className);
00089                         //System.out.println("The class "+className+" is already loaded!!");
00090                         //System.out.println("");
00091                         return t;
00092                 } catch(Exception e) {
00093                 //System.out.println("The class "+className+" is not loaded yet");
00094                 }
00095 
00096                 /* Check our local cache of classes */
00097                 result = (Class<?>)this.classes.get(className);
00098                 if (result != null) {
00099                         //    System.out.println("\t>>>>>> returning cached result.");
00100                         return result;
00101                 }
00102                 //System.out.println("The class does not exist");
00103                 /* Check with the primordial class loader */
00104                 try {
00105                         result = super.findSystemClass(className);
00106                         //System.out.println("\t>>>>>> returning system class (in CLASSPATH).");
00107                         return result;
00108                 } catch (ClassNotFoundException e) {
00109                         //System.out.println("\t>>>>>> Not a system class.");
00110                 }
00111 
00112                 /* Try to load it from our repository */
00113                 classData = getClassImplFromDataBase(className);
00114                 if (classData == null) {
00115                         throw new ClassNotFoundException();
00116                 }
00117 
00118                 //System.out.println("Class name: " + className);
00119                 /* Define it (parse the class file) */
00120                 /* java 1.6 */
00121                 result = defineClass(this.m_packageName+"."+className,classData, 0, classData.length);
00122                 /* java 1.4 */
00123                 //    result = defineClass(classData, 0, classData.length);
00124                 if (result == null) {
00125                         throw new ClassFormatError();
00126                 }
00127 
00128                 if (resolveIt) {
00129                         resolveClass(result);
00130                 }
00131                 try{
00132                         this.classes.put(className, result);
00133                 } catch (NullPointerException e) {
00134                         //System.out.println("Error with a class get");
00135                 }
00136                 //System.out.println("\t>>>>>> Returning newly loaded class.");
00137                 return result;
00138         }
00139 }
 All Classes Namespaces Files Functions Variables Enumerations