1 30 package org.objectweb.proactive.core.rmi; 31 32 import org.objectweb.proactive.core.component.asmgen.MetaObjectInterfaceClassGenerator; 33 import org.objectweb.proactive.core.component.asmgen.RepresentativeInterfaceClassGenerator; 34 35 36 42 public class ClassFileServer extends ClassServer { 43 private java.io.File [] codebases; 44 45 49 53 public ClassFileServer() throws java.io.IOException { 54 this(0, null); 55 } 56 57 61 public ClassFileServer(int port) throws java.io.IOException { 62 this(port, null); 63 } 64 65 69 public ClassFileServer(String paths) throws java.io.IOException { 70 this(0, paths); 71 } 72 73 78 public ClassFileServer(int port, String paths) throws java.io.IOException { 79 super(port); 80 if (paths != null) { 81 codebases = findClasspathRoots(paths); 82 } 83 printMessage(); 84 } 85 86 public static boolean isPortAlreadyBound(int port) { 90 java.net.Socket socket = null; 91 try { 92 socket = new java.net.Socket (java.net.InetAddress.getLocalHost(), 93 port); 94 return true; 96 } catch (java.io.IOException e) { 97 return false; 98 } finally { 99 try { 100 if (socket != null) { 101 socket.close(); 102 } 103 } catch (java.io.IOException e) { 104 } 105 } 106 } 107 108 132 public static void main(String [] args) { 133 int port = 0; 134 String classpath = null; 135 if (args.length >= 1) { 136 port = Integer.parseInt(args[0]); 137 } 138 if (args.length >= 2) { 139 classpath = args[1]; 140 } 141 try { 142 new ClassFileServer(port, classpath); 143 } catch (java.io.IOException e) { 144 logger.fatal("Unable to start ClassServer: " + e.getMessage()); 145 e.printStackTrace(); 146 } 147 } 148 149 153 163 protected byte[] getBytes(String path) 164 throws java.io.IOException , ClassNotFoundException { 165 byte[] b = null; 167 if (codebases == null) { 168 b = getBytesFromResource(path); 170 } else { 171 for (int i = 0; i < codebases.length; i++) { 172 try { 173 if (codebases[i].isDirectory()) { 174 b = getBytesFromDirectory(path, codebases[i]); 175 } else { 176 b = getBytesFromArchive(path, codebases[i]); 177 } 178 } catch (java.io.IOException e) { 179 } 180 } 181 } 182 if (b != null) { 183 return b; 184 } 185 186 b = org.objectweb.proactive.core.mop.MOPClassLoader.getMOPClassLoader() 189 .getClassData(path); 190 if (b != null) { 191 return b; 192 } 193 194 b = RepresentativeInterfaceClassGenerator.getClassData(path); 197 198 if (b != null) { 199 return b; 200 } 201 202 b = MetaObjectInterfaceClassGenerator.getClassData(path); 205 if (b != null) { 206 return b; 207 } 208 209 throw new ClassNotFoundException ("Cannot find class " + path); 210 } 211 212 216 225 private byte[] getBytesFromResource(String path) throws java.io.IOException { 226 String filename = path.replace('.', '/') + ".class"; 228 java.io.InputStream in = this.getClass().getClassLoader() 229 .getResourceAsStream(filename); 230 if (in == null) { 231 return null; 232 } 233 int length = in.available(); 234 235 if (length == -1) { 239 throw new java.io.IOException ("File length is unknown: " + 240 filename); 241 } else { 242 return getBytesFromInputStream(in, length); 243 } 244 } 245 246 256 private byte[] getBytesFromArchive(String path, java.io.File archive) 257 throws java.io.IOException { 258 String filename = path.replace('.', '/') + ".class"; 259 java.util.zip.ZipFile jarFile = new java.util.zip.ZipFile (archive); 260 java.util.zip.ZipEntry zipEntry = jarFile.getEntry(filename); 261 if (zipEntry == null) { 262 return null; 263 } 264 int length = (int) (zipEntry.getSize()); 265 266 if (length == -1) { 270 throw new java.io.IOException ("File length is unknown: " + 271 filename); 272 } else { 273 return getBytesFromInputStream(jarFile.getInputStream(zipEntry), 274 length); 275 } 276 } 277 278 288 private byte[] getBytesFromDirectory(String path, java.io.File directory) 289 throws java.io.IOException { 290 java.io.File f = new java.io.File (directory, 291 path.replace('.', java.io.File.separatorChar) + ".class"); 292 if (!f.exists()) { 293 return null; 294 } 295 int length = (int) (f.length()); 296 297 if (length == 0) { 301 throw new java.io.IOException ("File length is zero: " + path); 302 } else { 303 return getBytesFromInputStream(new java.io.FileInputStream (f), 304 length); 305 } 306 } 307 308 315 private byte[] getBytesFromInputStream(java.io.InputStream in, int length) 316 throws java.io.IOException { 317 java.io.DataInputStream din = new java.io.DataInputStream (in); 318 byte[] bytecodes = new byte[length]; 319 try { 320 din.readFully(bytecodes); 321 } finally { 322 if (din != null) { 323 din.close(); 324 } 325 } 326 return bytecodes; 327 } 328 329 private void printMessage() { 330 if (logger.isDebugEnabled()) { 331 logger.debug( 332 "To use this ClassFileServer set the property java.rmi.server.codebase to http://" + 333 hostname + ":" + port + "/"); 334 } 335 if (codebases == null) { 336 logger.info( 337 " --> This ClassFileServer is reading resources from classpath"); 338 } else { 339 logger.info( 340 " --> This ClassFileServer is reading resources from the following paths"); 341 for (int i = 0; i < codebases.length; i++) { 342 logger.info(" (" + i + ") : " + 343 codebases[i].getAbsolutePath()); 344 } 345 } 346 } 347 348 private java.io.File [] findClasspathRoots(String classpath) { 349 String pathSeparator = System.getProperty("path.separator"); 350 java.util.StringTokenizer st = new java.util.StringTokenizer (classpath, 351 pathSeparator); 352 int n = st.countTokens(); 353 java.io.File [] roots = new java.io.File [n]; 354 for (int i = 0; i < n; i++) { 355 roots[i] = new java.io.File (st.nextToken()); 356 } 357 return roots; 358 } 359 } 360 | Popular Tags |