org/objectweb/proactive/core/rmi/FileProcess.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.rmi;
00032 
00033 import java.io.IOException;
00034 
00035 import org.apache.log4j.Logger;
00036 import org.objectweb.proactive.core.component.gen.Utils;
00037 import org.objectweb.proactive.core.util.log.Loggers;
00038 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00039 
00040 
00045 public class FileProcess {
00046     protected static Logger logger = ProActiveLogger.getLogger(Loggers.CLASSLOADING);
00047     private java.io.File[] codebases;
00048     protected RequestInfo info;
00049 
00050     public FileProcess(String paths, RequestInfo info) {
00051         if (paths != null) {
00052             codebases = findClasspathRoots(paths);
00053         }
00054         this.info = info;
00055     }
00056 
00067     public byte[] getBytes() throws ClassNotFoundException {
00068         byte[] b = null;
00069         if (codebases == null) {
00070             try {
00071                 // reading from resources in the classpath
00072                 b = getBytesFromResource(info.getClassFileName());
00073             } catch (IOException e) {
00074                 throw new ClassNotFoundException("Cannot find class " +
00075                     info.getClassFileName(), e);
00076             }
00077         } else {
00078             for (int i = 0; i < codebases.length; i++) {
00079                 try {
00080                     if (codebases[i].isDirectory()) {
00081                         b = getBytesFromDirectory(info.getClassFileName(),
00082                                 codebases[i]);
00083                     } else {
00084                         b = getBytesFromArchive(info.getClassFileName(),
00085                                 codebases[i]);
00086                     }
00087                 } catch (java.io.IOException e) {
00088                 }
00089             }
00090         }
00091         if (b != null) {
00092             return b;
00093         }
00094 
00095         // try to get the class as a generated stub
00096         // generate it if necessary
00097         b = org.objectweb.proactive.core.mop.MOPClassLoader.getMOPClassLoader()
00098                                                            .getClassData(info.getClassFileName());
00099         if (b != null) {
00100             return b;
00101         }
00102 
00103         // COMPONENTS
00104         // try to get the class as a generated component interface reference
00105         b = Utils.getClassData(info.getClassFileName());
00106         if (b != null) {
00107             return b;
00108         }
00109         
00110 
00111         //if (info.path != null) {
00112         //    System.out.println("ClassServer sent class " + info.path +
00113         //        " successfully");
00114         //}
00115         throw new ClassNotFoundException("Cannot find class " +
00116             info.getClassFileName());
00117     }
00118 
00119     //
00120     // -- PRIVATE METHODS -----------------------------------------------
00121     //
00122 
00132     public static byte[] getBytesFromResource(String path)
00133         throws java.io.IOException {
00134         String filename = path.replace('.', '/') + ".class";
00135         java.io.InputStream in = FileProcess.class.getClassLoader()
00136                                                   .getResourceAsStream(filename);
00137         if (in == null) {
00138             return null;
00139         }
00140         int length = in.available();
00141 
00142         //if (logger.isDebugEnabled()) {
00143         //      //logger.debug("ClassFileServer reading: " + filename+"  length="+length+" from classpath");
00144         //}
00145         if (length == -1) {
00146             throw new java.io.IOException("File length is unknown: " +
00147                 filename);
00148         } else {
00149             return getBytesFromInputStream(in, length);
00150         }
00151     }
00152 
00163     private byte[] getBytesFromArchive(String path, java.io.File archive)
00164         throws java.io.IOException {
00165         String filename = path.replace('.', '/') + ".class";
00166         java.util.zip.ZipFile jarFile = new java.util.zip.ZipFile(archive);
00167         java.util.zip.ZipEntry zipEntry = jarFile.getEntry(filename);
00168         if (zipEntry == null) {
00169             return null;
00170         }
00171         int length = (int) (zipEntry.getSize());
00172 
00173         //if (logger.isDebugEnabled()) {
00174         //      //logger.debug("ClassFileServer reading: " + filename+"  length="+length+" from jar/xip file "+archive.getAbsolutePath());
00175         //}
00176         if (length == -1) {
00177             throw new java.io.IOException("File length is unknown: " +
00178                 filename);
00179         } else {
00180             return getBytesFromInputStream(jarFile.getInputStream(zipEntry),
00181                 length);
00182         }
00183     }
00184 
00195     private byte[] getBytesFromDirectory(String path, java.io.File directory)
00196         throws java.io.IOException {
00197         java.io.File f = new java.io.File(directory,
00198                 path.replace('.', java.io.File.separatorChar) + ".class");
00199         if (!f.exists()) {
00200             return null;
00201         }
00202         int length = (int) (f.length());
00203 
00204         //if (logger.isDebugEnabled()) {
00205         //      //logger.debug("ClassFileServer reading: " + f.getAbsolutePath()+"  length="+length);
00206         //}
00207         if (length == 0) {
00208             throw new java.io.IOException("File length is zero: " + path);
00209         } else {
00210             return getBytesFromInputStream(new java.io.FileInputStream(f),
00211                 length);
00212         }
00213     }
00214 
00222     private static byte[] getBytesFromInputStream(java.io.InputStream in,
00223         int length) throws java.io.IOException {
00224         java.io.DataInputStream din = new java.io.DataInputStream(in);
00225         byte[] bytecodes = new byte[length];
00226         try {
00227             din.readFully(bytecodes);
00228         } finally {
00229             if (din != null) {
00230                 din.close();
00231             }
00232         }
00233         return bytecodes;
00234     }
00235 
00236     private java.io.File[] findClasspathRoots(String classpath) {
00237         String pathSeparator = System.getProperty("path.separator");
00238         java.util.StringTokenizer st = new java.util.StringTokenizer(classpath,
00239                 pathSeparator);
00240         int n = st.countTokens();
00241         java.io.File[] roots = new java.io.File[n];
00242         for (int i = 0; i < n; i++) {
00243             roots[i] = new java.io.File(st.nextToken());
00244         }
00245         return roots;
00246     }
00247 }

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