1 31 package org.objectweb.proactive; 32 33 import org.apache.log4j.Logger; 34 import org.objectweb.proactive.core.Constants; 35 import org.objectweb.proactive.core.UniqueID; 36 import org.objectweb.proactive.core.config.ProActiveConfiguration; 37 import org.objectweb.proactive.core.node.Node; 38 import org.objectweb.proactive.core.node.NodeException; 39 import org.objectweb.proactive.core.node.NodeFactory; 40 import org.objectweb.proactive.core.runtime.RuntimeFactory; 41 import org.objectweb.proactive.core.runtime.jini.JiniRuntimeFactory; 42 import org.objectweb.proactive.core.runtime.rmi.RemoteRuntimeFactory; 43 44 45 68 public class StartNode { 69 public static final int DEFAULT_CLASSFILE_SERVER_PORT = 2001; 70 static Logger logger ; 71 protected static final int DEFAULT_PORT = 1099; 72 protected static final int MAX_RETRY = 3; 73 protected static final String NO_REBIND_OPTION_NAME = "-noRebind"; 74 protected static final String NO_CLASS_SERVER_OPTION_NAME = "-noClassServer"; 75 protected static final String NO_REGISTRY_OPTION_NAME = "-noRegistry"; 76 protected static final String MULTICAST_LOCATOR_NAME = "-multicastLocator"; 77 protected boolean noClassServer = false; 82 protected boolean noRebind = false; 83 protected boolean noRegistry = false; 84 protected boolean multicastLocator = false; 85 protected int registryPortNumber = DEFAULT_PORT; 86 protected String classpath; 87 protected String nodeURL; 88 89 static { 90 ProActiveConfiguration.load(); 91 logger = Logger.getLogger(StartNode.class.getName()); 92 if (logger.isDebugEnabled()) { 93 logger.debug("Loading ProActive class"); 94 } 95 try { 96 Class.forName("org.objectweb.proactive.ProActive"); 97 } catch (ClassNotFoundException e) { 98 if (logger.isDebugEnabled()) { 99 logger.fatal("Loading of ProActive class FAILED"); 100 } 101 e.printStackTrace(); 102 System.exit(1); 103 } 104 } 105 106 protected StartNode() { 110 } 111 112 private StartNode(String [] args) { 113 if (args.length == 0) { 114 nodeURL = null; 115 registryPortNumber = DEFAULT_PORT; 116 } else { 117 nodeURL = args[0]; 118 registryPortNumber = getPort(nodeURL, DEFAULT_PORT); 119 checkOptions(args, 1); 120 readClassPath(args, 1); 121 } 122 123 132 } 133 134 138 153 public static void main(String [] args) { 154 try { 170 new StartNode(args).run(); 171 } catch (Exception e) { 172 e.printStackTrace(); 173 logger.fatal(e.toString()); 174 } 175 } 177 178 182 186 protected void checkOptions(String [] args, int start) { 187 for (int i = start; i < args.length; i++) 188 checkOption(args[i]); 189 } 190 191 195 protected void readClassPath(String [] args, int start) { 196 if (noClassServer) { 197 return; 198 } 199 200 for (int i = start; i < args.length; i++) { 202 String s = args[i]; 203 if (s.charAt(0) != '-') { 204 classpath = s; 205 break; 206 } 207 } 208 } 209 210 214 protected void setProperties() { 215 } 220 221 225 protected void createNode(String nodeURL, boolean noRebind) 226 throws NodeException { 227 int exceptionCount = 0; 228 while (true) { 229 try { 230 Node node = null; 231 if (nodeURL == null) { 232 node = NodeFactory.getDefaultNode(); 233 } else { 234 node = NodeFactory.createNode(nodeURL, !noRebind,null,null); 236 } 237 238 logger.info("OK. Node " + node.getNodeInformation().getName() + 240 " is created in VM id=" + UniqueID.getCurrentVMID()); 241 break; 242 } catch (NodeException e) { 243 exceptionCount++; 244 if (exceptionCount == MAX_RETRY) { 245 throw e; 246 } else { 247 logger.error("Error, retrying (" + exceptionCount + ")"); 248 try { 249 Thread.sleep(1000); 250 } catch (InterruptedException e2) { 251 } 252 } 253 } 255 } 257 } 259 260 265 protected void run() throws java.io.IOException , NodeException { 266 setProperties(); 267 RemoteRuntimeFactory.setShouldCreateClassServer(!noClassServer); 269 RemoteRuntimeFactory.setShouldCreateRegistry(!noRegistry); 270 RemoteRuntimeFactory.setRegistryPortNumber(registryPortNumber); 271 if (RuntimeFactory.JINI_ENABLED) { 272 JiniRuntimeFactory.setMulticastLocator(multicastLocator); 273 } 275 276 createNode(nodeURL, noRebind); 278 } 279 280 284 protected void checkOption(String option) { 285 if (NO_REBIND_OPTION_NAME.equals(option)) { 286 noRebind = true; 287 } else if (NO_CLASS_SERVER_OPTION_NAME.equals(option)) { 288 noClassServer = true; 289 } else if (NO_REGISTRY_OPTION_NAME.equals(option)) { 290 noRegistry = true; 291 } else if (MULTICAST_LOCATOR_NAME.equals(option)) { 292 multicastLocator = true; 293 } 294 } 295 296 300 protected static int getPort(String nodeURL, int defaultValue) { 301 int deb = nodeURL.lastIndexOf(":"); 302 if (deb > -1) { 303 try { 305 return Integer.parseInt(nodeURL.substring(deb + 1, 307 nodeURL.lastIndexOf("/"))); 308 } catch (NumberFormatException e) { 309 return defaultValue; 310 } 311 } 312 return defaultValue; 313 } 314 315 private void printUsage() { 319 String localhost = "localhost"; 320 try { 321 localhost = java.net.InetAddress.getLocalHost().getCanonicalHostName(); 322 } catch (java.net.UnknownHostException e) { 323 logger.error("InetAddress failed: " + e.getMessage()); 324 e.printStackTrace(); 325 } catch (java.lang.SecurityException e) { 326 logger.error("InetAddress failed: " + e.getMessage()); 327 e.printStackTrace(); 328 } 329 330 logger.info("usage: java " + this.getClass().getName() + 331 " <node URL> [options]"); 332 logger.info(" - options"); 333 logger.info(" " + NO_CLASS_SERVER_OPTION_NAME + 334 " : indicates not to create a ClassServer for JINI and RMI."); 335 logger.info( 336 " By default a ClassServer is automatically created"); 337 logger.info(" to serve class files on demand."); 338 logger.info(" " + NO_REBIND_OPTION_NAME + 339 " : indicates not to use rebind when registering the"); 340 logger.info( 341 " node to the RMIRegistry. If a node of the same name"); 342 logger.info( 343 " already exists, the creation of the new node will fail."); 344 logger.info(" for instance: java " + StartNode.class.getName() + " " + 345 Constants.RMI_PROTOCOL_IDENTIFIER + "//" + localhost + "/node1"); 346 logger.info(" java " + StartNode.class.getName() + " " + 347 Constants.RMI_PROTOCOL_IDENTIFIER + "://" + localhost + "/node2 " + 348 NO_CLASS_SERVER_OPTION_NAME + " " + NO_REBIND_OPTION_NAME); 349 logger.info(" java " + StartNode.class.getName() + " " + 350 Constants.JINI_PROTOCOL_IDENTIFIER + "://" + localhost + "/node3"); 351 logger.info(" java " + StartNode.class.getName() + " " + 352 Constants.JINI_PROTOCOL_IDENTIFIER + "://" + localhost + "/node4 " + 353 MULTICAST_LOCATOR_NAME); 354 } 355 } 356 | Popular Tags |