00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030  
00031 package org.objectweb.proactive.core.classloader;
00032 
00033 import java.io.File;
00034 import java.lang.reflect.Method;
00035 import java.net.MalformedURLException;
00036 import java.net.URL;
00037 import java.net.URLClassLoader;
00038 import java.util.ArrayList;
00039 import java.util.StringTokenizer;
00040 
00041 import org.apache.log4j.Logger;
00042 import org.objectweb.proactive.core.util.log.Loggers;
00043 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00044 
00045 
00060 public class ProActiveClassLoader extends URLClassLoader {
00061     boolean runtimeReady = false;
00062     private Object helper;
00063     private Method helper_getClassData;
00064     private final static Logger CLASSLOADER_LOGGER = ProActiveLogger.getLogger(Loggers.CLASSLOADING);
00065 
00066     public ProActiveClassLoader() {
00067         super(pathToURLs(System.getProperty("java.class.path")));
00068     }
00069 
00070     
00071 
00072 
00073 
00074     public ProActiveClassLoader(ClassLoader parent) {
00075         super(pathToURLs(System.getProperty("java.class.path")), parent);
00076         try {
00077             
00078             Class proActiveClassLoaderHelper = loadClass(
00079                     "org.objectweb.proactive.core.classloader.ProActiveClassLoaderHelper");
00080             helper = proActiveClassLoaderHelper.newInstance();
00081             helper_getClassData = proActiveClassLoaderHelper.getDeclaredMethod("getClassData",
00082                     new Class[] { String.class });
00083         } catch (Exception e) {
00084             throw new RuntimeException(e);
00085         }
00086     }
00087 
00093     protected Class<?> findClass(String name) throws ClassNotFoundException {
00094         Class c = null;
00095         try {
00096             
00097             c = super.findClass(name);
00098         } catch (ClassNotFoundException e) {
00099             if (runtimeReady) {
00100                 byte[] class_data = null;
00101                 try {
00102                     
00103                     class_data = (byte[]) helper_getClassData.invoke(helper,
00104                             new Object[] { name });
00105                     if (class_data != null) {
00106                         c = defineClass(name, class_data, 0, class_data.length,
00107                                 getClass().getProtectionDomain());
00108                     }
00109                 } catch (Exception e1) {
00110                     throw new ClassNotFoundException(name, e1);
00111                 }
00112             } else {
00113                 throw e;
00114             }
00115         }
00116         if (c != null) {
00117             return c;
00118         } else {
00119             throw new ClassNotFoundException(name);
00120         }
00121     }
00122 
00123     
00124 
00125 
00126     public Class<?> loadClass(String name) throws ClassNotFoundException {
00127         Class c = null;
00128         if ((c = findLoadedClass(name)) != null) {
00129             return c;
00130         }
00131         if (name.startsWith("java.") || name.startsWith("javax.") ||
00132                 name.startsWith("sun.") || name.startsWith("com.sun.") ||
00133                 name.startsWith("org.xml.sax") || name.startsWith("org.omg") ||
00134                 name.startsWith("org.ietf.jgss") ||
00135                 name.startsWith("org.w3c.dom")) {
00136             return getParent().loadClass(name);
00137         }
00138         if (name.equals("org.objectweb.proactive.core.ssh.http.Handler")) {
00139             
00140             throw new ClassNotFoundException(name);
00141         }
00142 
00143         
00144         if (name.endsWith("_Skel")) {
00145             
00146             throw new ClassNotFoundException(name);
00147         }
00148         c = findClass(name);
00149         if (name.equals(
00150                     "org.objectweb.proactive.core.runtime.ProActiveRuntimeImpl")) {
00151             runtimeReady = true;
00152         }
00153         if (c != null) {
00154             
00155         } else {
00156             throw new ClassNotFoundException(name);
00157         }
00158         return c;
00159     }
00160 
00172     public static URL[] pathToURLs(String _classpath) {
00173         StringTokenizer tok = new StringTokenizer(_classpath, File.pathSeparator);
00174         ArrayList<String> pathList = new ArrayList<String>();
00175 
00176         while (tok.hasMoreTokens()) {
00177             pathList.add(tok.nextToken());
00178         }
00179 
00180         URL[] urlArray = new URL[pathList.size()];
00181 
00182         int count = 0;
00183         for (int i = 0; i < pathList.size(); i++) {
00184             try {
00185                 urlArray[i] = (new File((String) pathList.get(i))).toURI()
00186                                .toURL();
00187                 count++;
00188             } catch (MalformedURLException e) {
00189                 CLASSLOADER_LOGGER.info("MalformedURLException occured for " +
00190                     urlArray[i].toString() +
00191                     " during the ProActiveClassLoader creation");
00192             }
00193         }
00194 
00195         if (count != pathList.size()) {
00196             
00197             URL[] tmpUrlArray = new URL[count];
00198             System.arraycopy(urlArray, 0, tmpUrlArray, 0, count);
00199         }
00200 
00201         return urlArray;
00202     }
00203 }