org/objectweb/proactive/core/rmi/ClassServer.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.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         //        System.out.println("URL du classServer : " + hostname + ":" + port);
00086         newListener();
00087         //        if (logger.isInfoEnabled()) {
00088         //            logger.info("communication protocol = " +System.getProperty("proactive.communication.protocol")+", http server port = " + port);
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     // -- PUBLIC METHODS -----------------------------------------------
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             // if we can connect to the port it means the server already exists
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             //for (int i = 0; i < codebases.length; i++) {
00153             logger.info(paths);
00154 
00155             //codebases[i].getAbsolutePath());
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         // accept a connection
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 }

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