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.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
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
00096
00097 b = org.objectweb.proactive.core.mop.MOPClassLoader.getMOPClassLoader()
00098 .getClassData(info.getClassFileName());
00099 if (b != null) {
00100 return b;
00101 }
00102
00103
00104
00105 b = Utils.getClassData(info.getClassFileName());
00106 if (b != null) {
00107 return b;
00108 }
00109
00110
00111
00112
00113
00114
00115 throw new ClassNotFoundException("Cannot find class " +
00116 info.getClassFileName());
00117 }
00118
00119
00120
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
00143
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
00174
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
00205
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 }