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.net.UnknownHostException;
00034
00035 import org.apache.log4j.Logger;
00036 import org.objectweb.proactive.core.config.ProActiveConfiguration;
00037 import org.objectweb.proactive.core.util.UrlBuilder;
00038 import org.objectweb.proactive.core.util.log.Loggers;
00039 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00040
00041
00042 public class ClassServer implements Runnable {
00043 protected static Logger logger = ProActiveLogger.getLogger(Loggers.CLASSLOADING);
00044 public static final int DEFAULT_SERVER_BASE_PORT = 2010;
00045 protected static int DEFAULT_SERVER_PORT_INCREMENT = 2;
00046 protected static int MAX_RETRY = 500;
00047 private static java.util.Random random = new java.util.Random();
00048 protected static int port;
00049
00050 static {
00051 String newport;
00052
00053 if (System.getProperty("proactive.http.port") != null) {
00054 newport = System.getProperty("proactive.http.port");
00055 } else {
00056 newport = new Integer(DEFAULT_SERVER_BASE_PORT).toString();
00057 System.setProperty("proactive.http.port", newport);
00058 }
00059 }
00060
00061 protected String hostname;
00062 private java.net.ServerSocket server = null;
00063 protected String paths;
00064
00071 protected ClassServer() throws java.io.IOException {
00072 this(0, null);
00073 }
00074
00075 protected ClassServer(int port_) throws java.io.IOException {
00076 if (port == 0) {
00077 port = boundServerSocket(Integer.parseInt(System.getProperty(
00078 "proactive.http.port")), MAX_RETRY);
00079 } else {
00080 port = port_;
00081 server = new java.net.ServerSocket(port);
00082 }
00083
00084 hostname = java.net.InetAddress.getLocalHost().getHostAddress();
00085
00086 newListener();
00087
00088
00089
00090 }
00091
00099 protected ClassServer(int port_, String paths) throws java.io.IOException {
00100 this(port_);
00101 this.paths = paths;
00102 printMessage();
00103 }
00104
00109 public ClassServer(String paths) throws java.io.IOException {
00110 this(0, paths);
00111 }
00112
00113
00114
00115
00116 public static boolean isPortAlreadyBound(int port) {
00117 java.net.Socket socket = null;
00118
00119 try {
00120 socket = new java.net.Socket(java.net.InetAddress.getLocalHost(),
00121 port);
00122
00123
00124 return true;
00125 } catch (java.io.IOException e) {
00126 return false;
00127 } finally {
00128 try {
00129 if (socket != null) {
00130 socket.close();
00131 }
00132 } catch (java.io.IOException e) {
00133 }
00134 }
00135 }
00136
00137 private void printMessage() {
00138 if (logger.isDebugEnabled()) {
00139 logger.info(
00140 "To use this ClassFileServer set the property java.rmi.server.codebase to http://" +
00141 hostname + ":" + port + "/");
00142 }
00143
00144 if (this.paths == null) {
00145 logger.info(
00146 " --> This ClassFileServer is reading resources from classpath " +
00147 port);
00148 } else {
00149 logger.info(
00150 " --> This ClassFileServer is reading resources from the following paths");
00151
00152
00153 logger.info(paths);
00154
00155
00156 }
00157 }
00158
00159 public static int getServerSocketPort() {
00160 if (ProActiveConfiguration.osgiServletEnabled()) {
00161 return ClassServerServlet.getPort();
00162 }
00163 return port;
00164 }
00165
00166 public String getHostname() {
00167 return hostname;
00168 }
00169
00170 public static String getUrl() {
00171 try {
00172 if (ProActiveConfiguration.osgiServletEnabled()) {
00173 return ClassServerServlet.getUrl();
00174 } else {
00175 return UrlBuilder.buildUrl(UrlBuilder.getHostNameorIP(
00176 java.net.InetAddress.getLocalHost()), "", "http:", port);
00177 }
00178 } catch (UnknownHostException e) {
00179 e.printStackTrace();
00180 }
00181 return UrlBuilder.buildUrl("localhost", "", "http:", port);
00182 }
00183
00190 public void run() {
00191 java.net.Socket socket = null;
00192
00193
00194 while (true) {
00195 try {
00196 socket = server.accept();
00197
00198 HTTPRequestHandler service = (new HTTPRequestHandler(socket,
00199 paths));
00200 service.start();
00201 } catch (java.io.IOException e) {
00202 System.out.println("Class Server died: " + e.getMessage());
00203 e.printStackTrace();
00204
00205 return;
00206 }
00207 }
00208 }
00209
00210 private void newListener() {
00211 (new Thread(this, "ClassServer-" + hostname + ":" + port)).start();
00212 }
00213
00214 private int boundServerSocket(int basePortNumber, int numberOfTry)
00215 throws java.io.IOException {
00216 for (int i = 0; i < numberOfTry; i++) {
00217 try {
00218 server = new java.net.ServerSocket(basePortNumber);
00219 return basePortNumber;
00220 } catch (java.io.IOException e) {
00221 basePortNumber += random.nextInt(DEFAULT_SERVER_PORT_INCREMENT);
00222 System.setProperty("proactive.http.port", basePortNumber + "");
00223 }
00224 }
00225
00226 throw new java.io.IOException(
00227 "ClassServer cannot create a ServerSocket after " + numberOfTry +
00228 " attempts !!!");
00229 }
00230 }