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.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
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
00128
00129
00130
00131
00132
00133
00134
00135 }
00136
00137
00138
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
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
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
00180
00181
00182
00183
00184
00185
00186
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
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
00222 }
00223
00224
00225 }
00226
00227
00228 }
00229
00235 protected void run()
00236 throws java.io.IOException, NodeException, AlreadyBoundException {
00237
00238
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
00256 createNode(nodeURL, noRebind);
00257
00258 if (Profiling.STARTNODE) {
00259 mt.stop();
00260
00261
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
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 }