org/objectweb/proactive/core/classloader/ProActiveClassLoader.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
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      * @see ClassLoader#ClassLoader(java.lang.ClassLoader)
00072      * @see ClassLoader#getSystemClassLoader()
00073      */
00074     public ProActiveClassLoader(ClassLoader parent) {
00075         super(pathToURLs(System.getProperty("java.class.path")), parent);
00076         try {
00077             // use a helper class so that the current class does not include any other proactive or application type 
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             // 1. look in parents and classpath
00097             c = super.findClass(name);
00098         } catch (ClassNotFoundException e) {
00099             if (runtimeReady) {
00100                 byte[] class_data = null;
00101                 try {
00102                     // 2. search for class data using helper
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      * see ClassLoader#loadClass(java.lang.String)
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             // class does not exist
00140             throw new ClassNotFoundException(name);
00141         }
00142 
00143         // FIXME temporary walkaround
00144         if (name.endsWith("_Skel")) {
00145             // do not attempt to download any 1.2- rmi skeleton
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             // System.out.println("ProActiveClassloader loaded class : " + name);
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             // A MalformedURLException occured
00197             URL[] tmpUrlArray = new URL[count];
00198             System.arraycopy(urlArray, 0, tmpUrlArray, 0, count);
00199         }
00200 
00201         return urlArray;
00202     }
00203 }

Generated on Mon Jan 22 15:16:06 2007 for ProActive by  doxygen 1.5.1