1 31 package org.objectweb.proactive.core.rmi; 32 33 import org.apache.log4j.Logger; 34 35 36 53 public abstract class ClassServer implements Runnable { 54 protected static Logger logger = Logger.getLogger(ClassServer.class.getName()); 55 protected static int DEFAULT_SERVER_BASE_PORT = 2001; 56 protected static int DEFAULT_SERVER_PORT_INCREMENT = 20; 57 protected static int MAX_RETRY = 50; 58 private static java.util.Random random = new java.util.Random (); 59 private java.net.ServerSocket server = null; 60 protected int port; 61 protected String hostname; 62 63 69 protected ClassServer() throws java.io.IOException { 70 this(0); 71 } 72 73 80 protected ClassServer(int port) throws java.io.IOException { 81 if (port == 0) { 82 this.port = boundServerSockect(DEFAULT_SERVER_BASE_PORT, MAX_RETRY); 83 } else { 84 this.port = port; 85 server = new java.net.ServerSocket (port); 86 } 87 hostname = java.net.InetAddress.getLocalHost().getHostAddress(); 88 newListener(); 89 } 90 91 public int getServerSocketPort() { 92 return port; 94 } 95 96 public String getHostname() { 97 return hostname; 98 } 99 100 106 public void run() { 107 java.net.Socket socket; 108 109 try { 111 socket = server.accept(); 112 } catch (java.io.IOException e) { 113 logger.error("Class Server died: " + e.getMessage()); 114 e.printStackTrace(); 115 return; 116 } 117 118 newListener(); 120 try { 121 java.io.DataOutputStream out = new java.io.DataOutputStream (socket.getOutputStream()); 122 RequestInfo info = null; 123 try { 124 java.io.BufferedReader in = new java.io.BufferedReader (new java.io.InputStreamReader ( 126 socket.getInputStream())); 127 info = getInfo(in); 128 byte[] bytecodes = getBytes(info.path); 130 131 try { 133 out.writeBytes("HTTP/1.0 200 OK\r\n"); 134 out.writeBytes("Content-Length: " + bytecodes.length + 135 "\r\n"); 136 out.writeBytes("Content-Type: application/java\r\n\r\n"); 137 out.write(bytecodes); 138 out.flush(); 139 logger.info("ClassServer sent class " + info.path + 140 " successfully"); 141 } catch (java.io.IOException ie) { 142 return; 143 } 144 } catch (Exception e) { 145 e.printStackTrace(); 147 logger.error("!!! ClassServer failed to load class " + 148 info.path); 149 out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n"); 150 out.writeBytes("Content-Type: text/html\r\n\r\n"); 151 out.flush(); 152 } 153 } catch (java.io.IOException ex) { 154 } finally { 158 try { 159 socket.close(); 160 } catch (java.io.IOException e) { 161 } 162 } 163 } 164 165 169 180 protected abstract byte[] getBytes(String path) 181 throws java.io.IOException , ClassNotFoundException ; 182 183 187 190 private void newListener() { 191 (new Thread (this, "ClassServer-" + hostname + ":" + port)).start(); 192 } 193 194 198 private static RequestInfo getInfo(java.io.BufferedReader in) 199 throws java.io.IOException { 200 RequestInfo info = new RequestInfo(); 201 String line = null; 202 do { 203 line = in.readLine(); 204 if (line.startsWith("GET /")) { 205 info.path = getPath(line); 206 } else if (line.startsWith("Host:")) { 207 info.host = getHost(line); 208 } else { 209 } 211 } while ((line.length() != 0) && (line.charAt(0) != '\r') && 212 (line.charAt(0) != '\n')); 213 if (info.path != null) { 214 return info; 215 } else { 216 throw new java.io.IOException ("Malformed Header"); 217 } 218 } 219 220 225 private static String getPath(String line) { 226 line = line.substring(5, line.length()).trim(); 229 int index = line.indexOf(".class "); 230 if (index != -1) { 231 return line.substring(0, index).replace('/', '.'); 232 } else { 233 return null; 234 } 235 } 236 237 242 private static String getHost(String line) { 243 245 return line.substring(5, line.length()).trim(); 246 } 247 248 private int boundServerSockect(int basePortNumber, int numberOfTry) 249 throws java.io.IOException { 250 for (int i = 0; i < numberOfTry; i++) { 251 try { 252 server = new java.net.ServerSocket (basePortNumber); 253 return basePortNumber; 254 } catch (java.io.IOException e) { 255 basePortNumber += random.nextInt(DEFAULT_SERVER_PORT_INCREMENT); 256 } 257 } 258 throw new java.io.IOException ( 259 "ClassServer cannot create a ServerSocket after " + numberOfTry + 260 " attempts !!!"); 261 } 262 263 private static class RequestInfo { 264 String path; 265 String host; 266 } 267 } 268 | Popular Tags |