org/objectweb/proactive/core/node/StartNode.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.node;
00032 
00033 import java.rmi.AlreadyBoundException;
00034 
00035 import org.apache.log4j.Logger;
00036 import org.objectweb.proactive.core.Constants;
00037 import org.objectweb.proactive.core.UniqueID;
00038 import org.objectweb.proactive.core.config.ProActiveConfiguration;
00039 import org.objectweb.proactive.core.runtime.jini.JiniRuntimeFactory;
00040 import org.objectweb.proactive.core.runtime.rmi.RmiRuntimeFactory;
00041 import org.objectweb.proactive.core.util.UrlBuilder;
00042 import org.objectweb.proactive.core.util.log.Loggers;
00043 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00044 import org.objectweb.proactive.core.util.profiling.PAProfilerEngine;
00045 import org.objectweb.proactive.core.util.profiling.Profiling;
00046 import org.objectweb.proactive.core.util.timer.AverageMicroTimer;
00047 
00048 
00072 public class StartNode {
00073     public static final int DEFAULT_CLASSFILE_SERVER_PORT = 2001;
00074     static Logger logger;
00075     protected static final int DEFAULT_PORT = 1099;
00076     protected static final int MAX_RETRY = 3;
00077     protected static final String NO_REBIND_OPTION_NAME = "-noRebind";
00078     protected static final String NO_CLASS_SERVER_OPTION_NAME = "-noClassServer";
00079     protected static final String NO_REGISTRY_OPTION_NAME = "-noRegistry";
00080     protected static final String MULTICAST_LOCATOR_NAME = "-multicastLocator";
00081 
00082     static {
00083         ProActiveConfiguration.load();
00084         logger = ProActiveLogger.getLogger(Loggers.DEPLOYMENT);
00085 
00086         if (logger.isDebugEnabled()) {
00087             logger.debug("Loading ProActive class");
00088         }
00089 
00090         try {
00091             Class.forName("org.objectweb.proactive.ProActive");
00092         } catch (ClassNotFoundException e) {
00093             if (logger.isDebugEnabled()) {
00094                 logger.fatal("Loading of ProActive class FAILED");
00095             }
00096 
00097             e.printStackTrace();
00098             System.exit(1);
00099         }
00100     }
00101 
00102     protected boolean noClassServer = false;
00103     protected boolean noRebind = false;
00104     protected boolean noRegistry = false;
00105     protected boolean multicastLocator = false;
00106     protected int registryPortNumber = DEFAULT_PORT;
00107     protected String classpath;
00108     protected String nodeURL;
00109 
00110     //
00111     // -- CONSTRUCTORS -----------------------------------------------
00112     //
00113     protected StartNode() {
00114     }
00115 
00116     private StartNode(String[] args) {
00117         if (args.length == 0) {
00118             nodeURL = null;
00119         } else {
00120             nodeURL = args[0];
00121             registryPortNumber = UrlBuilder.getPortFromUrl(nodeURL);
00122             checkOptions(args, 1);
00123             readClassPath(args, 1);
00124         }
00125 
00126         /*
00127            // debug
00128            System.out.println("Node name = "+nodeURL);
00129            if (noRebind)
00130              System.out.println(" - NoRebind");
00131            if (noClassServer)
00132              System.out.println(" - No ClassServer");
00133            else System.out.println("ClassServer classpath = "+classpath);
00134          */
00135     }
00136 
00137     //
00138     // -- PUBLIC METHODS -----------------------------------------------
00139     //
00140     public static void main(String[] args) {
00141         try {
00142             new StartNode(args).run();
00143         } catch (Exception e) {
00144             e.printStackTrace();
00145             logger.fatal(e.toString());
00146         }
00147     }
00148 
00149     //
00150     // -- PROTECTED METHODS -----------------------------------------------
00151     //
00152     protected void checkOptions(String[] args, int start) {
00153         for (int i = start; i < args.length; i++)
00154             checkOption(args[i]);
00155     }
00156 
00161     protected void readClassPath(String[] args, int start) {
00162         if (noClassServer) {
00163             return;
00164         }
00165 
00166         // look for classpath
00167         for (int i = start; i < args.length; i++) {
00168             String s = args[i];
00169 
00170             if (s.charAt(0) != '-') {
00171                 classpath = s;
00172 
00173                 break;
00174             }
00175         }
00176     }
00177 
00178     //    /**
00179     //     * <i><font size="-1" color="#FF0000">**For internal use only** </font></i>
00180     //     * sets the properties needed for the node creation
00181     //     */
00182     //    protected void setProperties() {
00183     //        //  System.setProperty("sun.rmi.dgc.checkInterval","400");
00184     //        //  System.setProperty("java.rmi.dgc.leaseValue","800");
00185     //        //  System.setProperty("sun.rmi.dgc.cleanInterval","400");
00186     //        //  System.setProperty("sun.rmi.dgc.client.gcInterval","400");
00187     //    }
00188     protected void createNode(String nodeURL, boolean noRebind)
00189         throws NodeException, AlreadyBoundException {
00190         int exceptionCount = 0;
00191 
00192         while (true) {
00193             try {
00194                 Node node = null;
00195 
00196                 if (nodeURL == null) {
00197                     node = NodeFactory.getDefaultNode();
00198                 } else {
00199                     //TODO allow start alone node with security parameters 
00200                     node = NodeFactory.createNode(nodeURL, !noRebind, null, null);
00201                 }
00202 
00203                 logger.info("OK. Node " + node.getNodeInformation().getName() +
00204                     " is created in VM id=" + UniqueID.getCurrentVMID());
00205 
00206                 break;
00207             } catch (NodeException e) {
00208                 exceptionCount++;
00209 
00210                 if (exceptionCount == MAX_RETRY) {
00211                     throw e;
00212                 } else {
00213                     logger.error("Error, retrying (" + exceptionCount + ")");
00214 
00215                     try {
00216                         Thread.sleep(1000);
00217                     } catch (InterruptedException e2) {
00218                     }
00219                 }
00220 
00221                 // end if
00222             }
00223 
00224             // try
00225         }
00226 
00227         // end while
00228     }
00229 
00235     protected void run()
00236         throws java.io.IOException, NodeException, AlreadyBoundException {
00237         //setProperties();
00238         // set options on node factory
00239         RmiRuntimeFactory.setShouldCreateClassServer(!noClassServer);
00240         RmiRuntimeFactory.setShouldCreateRegistry(!noRegistry);
00241         RmiRuntimeFactory.setRegistryPortNumber(registryPortNumber);
00242 
00243         if (multicastLocator) {
00244             JiniRuntimeFactory.setMulticastLocator(multicastLocator);
00245         }
00246 
00247         AverageMicroTimer mt = null;
00248 
00249         if (Profiling.STARTNODE) {
00250             mt = new AverageMicroTimer("StartNode");
00251             PAProfilerEngine.registerTimer(mt);
00252             mt.start();
00253         }
00254 
00255         // create node
00256         createNode(nodeURL, noRebind);
00257 
00258         if (Profiling.STARTNODE) {
00259             mt.stop();
00260 
00261             //  mt.dump();
00262         }
00263     }
00264 
00269     protected void checkOption(String option) {
00270         if (NO_REBIND_OPTION_NAME.equals(option)) {
00271             noRebind = true;
00272         } else if (NO_CLASS_SERVER_OPTION_NAME.equals(option)) {
00273             noClassServer = true;
00274         } else if (NO_REGISTRY_OPTION_NAME.equals(option)) {
00275             noRegistry = true;
00276         } else if (MULTICAST_LOCATOR_NAME.equals(option)) {
00277             multicastLocator = true;
00278         } else {
00279                 printUsage();
00280         }
00281     }
00282 
00283     //
00284     // -- PRIVATE METHODS -----------------------------------------------
00285     //
00286     private void printUsage() {
00287         String localhost = "localhost";
00288 
00289         try {
00290             localhost = UrlBuilder.getHostNameorIP(java.net.InetAddress.getLocalHost());
00291         } catch (java.net.UnknownHostException e) {
00292             logger.error("InetAddress failed: " + e.getMessage());
00293             e.printStackTrace();
00294         } catch (java.lang.SecurityException e) {
00295             logger.error("InetAddress failed: " + e.getMessage());
00296             e.printStackTrace();
00297         }
00298 
00299         logger.info("usage: java " + this.getClass().getName() +
00300             " <node URL> [options]");
00301         logger.info(" - options");
00302         logger.info("     " + NO_CLASS_SERVER_OPTION_NAME +
00303             " : indicates not to create a ClassServer for JINI and RMI.");
00304         logger.info(
00305             "                      By default a ClassServer is automatically created");
00306         logger.info("                      to serve class files on demand.");
00307         logger.info("     " + NO_REBIND_OPTION_NAME +
00308             "      : indicates not to use rebind when registering the");
00309         logger.info(
00310             "                      node to the RMIRegistry. If a node of the same name");
00311         logger.info(
00312             "                      already exists, the creation of the new node will fail.");
00313         logger.info("  for instance: java " + StartNode.class.getName() + " " +
00314             Constants.RMI_PROTOCOL_IDENTIFIER + "//" + localhost + "/node1");
00315         logger.info("                java " + StartNode.class.getName() + " " +
00316             Constants.RMI_PROTOCOL_IDENTIFIER + "://" + localhost + "/node2  " +
00317             NO_CLASS_SERVER_OPTION_NAME + " " + NO_REBIND_OPTION_NAME);
00318         logger.info("                java " + StartNode.class.getName() + " " +
00319             Constants.JINI_PROTOCOL_IDENTIFIER + "://" + localhost + "/node3");
00320         logger.info("                java " + StartNode.class.getName() + " " +
00321             Constants.JINI_PROTOCOL_IDENTIFIER + "://" + localhost + "/node4 " +
00322             MULTICAST_LOCATOR_NAME);
00323     }
00324 }

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