KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > StartNode


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

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 /**
46  * <p>
47  * This class is a utility class allowing to start a ProActive node with a JVM.
48  * It is very useful to start a node on a given host that will receive later
49  * active objects created by other distributed applications.
50  * </p><p>
51  * This class has a main method and can be used directly from the java command.
52  * <br>
53  * use<br>
54  * &nbsp;&nbsp;&nbsp;java org.objectweb.proactive.StartNode<br>
55  * to print the options from command line or see the java doc of the main method.
56  * </p><p>
57  * A node represents the minimum services ProActive needs to work with a remote JVM.
58  * Any JVM that is going to interact with active objects has at least one associated
59  * node. The node must have a remote implementation that allow an object to remotely
60  * invoke its methods.
61  * </p>
62  *
63  * @author ProActive Team
64  * @version 1.0, 2001/10/23
65  * @since ProActive 0.9
66  *
67  */

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 JavaDoc NO_REBIND_OPTION_NAME = "-noRebind";
74     protected static final String JavaDoc NO_CLASS_SERVER_OPTION_NAME = "-noClassServer";
75     protected static final String JavaDoc NO_REGISTRY_OPTION_NAME = "-noRegistry";
76     protected static final String JavaDoc MULTICAST_LOCATOR_NAME = "-multicastLocator";
77 // private static final String FS = System.getProperty("file.separator");
78
// private static final String XML_LOCATION = System.getProperty("user.dir") +
79
// FS + ".." + FS + ".." + FS + "descriptors" + FS +
80
// "RemoteGlobusSetup.xml";
81
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 JavaDoc classpath;
87     protected String JavaDoc 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 JavaDoc 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     //
107
// -- CONSTRUCTORS -----------------------------------------------
108
//
109
protected StartNode() {
110     }
111
112     private StartNode(String JavaDoc[] 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         /*
124         // debug
125         System.out.println("Node name = "+nodeURL);
126         if (noRebind)
127           System.out.println(" - NoRebind");
128         if (noClassServer)
129           System.out.println(" - No ClassServer");
130         else System.out.println("ClassServer classpath = "+classpath);
131         */

132     }
133
134     //
135
// -- PUBLIC METHODS -----------------------------------------------
136
//
137

138     /**
139      * Starts a ProActive node on the localhost host
140      * usage: java org.objectweb.proactive.StartNode &lt;node URL> [options]<br>
141      * where options are amongst<br>
142      * <ul>
143      * <li>noClassServer : indicates not to create a ClassServer for JINI.
144      * By default a ClassServer is automatically created
145      * to serve class files on demand.</li>
146      * <li>noRebind : indicates not to use rebind when registering the
147      * node to the RMIRegistry. If a node of the same name
148      * already exists, the creation of the new node will fail.</li>
149      * </ul>
150      * for instance: java org.objectweb.proactive.StartNode //localhost/node1<br>
151      * java org.objectweb.proactive.StartNode //localhost/node2 -noClassServer -noRebind<br>
152      */

153     public static void main(String JavaDoc[] args) {
154 // if ((args.length != 0) && (args[0].compareTo("-g") == 0)) {
155
// try {
156
// //ProActiveDescriptor pad = ProActive.getProactiveDescriptor("file://Z:/test/ProActive/classes/GlobusSetupWithRlogin.xml");
157
// ProActiveDescriptor pad = ProActive.getProactiveDescriptor(
158
// "file:" + XML_LOCATION);
159
// ExternalProcess rLoginProcess = pad.getProcess("rLoginProcess");
160
// JVMProcess jvmProcess = (JVMProcess) pad.getProcess("globusJVM");
161
// jvmProcess.setParameters(args[1]);
162
// //pad.activateMappings();
163
// rLoginProcess.startProcess();
164
// //gp.startNodeWithGlobus(args[1]);
165
// } catch (Exception e) {
166
// e.printStackTrace();
167
// }
168
// } else {
169
try {
170                 new StartNode(args).run();
171             } catch (Exception JavaDoc e) {
172                 e.printStackTrace();
173                 logger.fatal(e.toString());
174             }
175         //}
176
}
177
178     //
179
// -- PROTECTED METHODS -----------------------------------------------
180
//
181

182     /**
183      * <i><font size="-1" color="#FF0000">**For internal use only** </font></i>
184      * Checks options from the arguments
185      */

186     protected void checkOptions(String JavaDoc[] args, int start) {
187         for (int i = start; i < args.length; i++)
188             checkOption(args[i]);
189     }
190
191     /**
192      * <i><font size="-1" color="#FF0000">**For internal use only** </font></i>
193      * Reads the classpath from the arguments
194      */

195     protected void readClassPath(String JavaDoc[] args, int start) {
196         if (noClassServer) {
197             return;
198         }
199
200         // look for classpath
201
for (int i = start; i < args.length; i++) {
202             String JavaDoc s = args[i];
203             if (s.charAt(0) != '-') {
204                 classpath = s;
205                 break;
206             }
207         }
208     }
209
210     /**
211      * <i><font size="-1" color="#FF0000">**For internal use only** </font></i>
212      * sets the properties needed for the node creation
213      */

214     protected void setProperties() {
215         // System.setProperty("sun.rmi.dgc.checkInterval","400");
216
// System.setProperty("java.rmi.dgc.leaseValue","800");
217
// System.setProperty("sun.rmi.dgc.cleanInterval","400");
218
// System.setProperty("sun.rmi.dgc.client.gcInterval","400");
219
}
220
221     /**
222      * <i><font size="-1" color="#FF0000">**For internal use only** </font></i>
223      * Creates the node at the given URL with the rebind option
224      */

225     protected void createNode(String JavaDoc 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                     //TODO allow start alone node with security parameters
235
node = NodeFactory.createNode(nodeURL, !noRebind,null,null);
236                 }
237
238                 //System.out.println("nodeurl "+node.getNodeInformation().getURL());
239
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 JavaDoc e2) {
251                     }
252                 }
253                  // end if
254
}
255              // try
256
}
257          // end while
258
}
259
260     /**
261      * <i><font size="-1" color="#FF0000">**For internal use only** </font></i>
262      * Run the complete creation of the node step by step by invoking the other
263      * helper methods
264      */

265     protected void run() throws java.io.IOException JavaDoc, NodeException {
266         setProperties();
267         // set options on node factory
268
RemoteRuntimeFactory.setShouldCreateClassServer(!noClassServer);
269         RemoteRuntimeFactory.setShouldCreateRegistry(!noRegistry);
270         RemoteRuntimeFactory.setRegistryPortNumber(registryPortNumber);
271         if (RuntimeFactory.JINI_ENABLED) {
272             JiniRuntimeFactory.setMulticastLocator(multicastLocator);
273             // System.out.println("jini not yet implemented");
274
}
275
276         // create node
277
createNode(nodeURL, noRebind);
278     }
279
280     /**
281      * <i><font size="-1" color="#FF0000">**For internal use only** </font></i>
282      * Checks one given option from the arguments
283      */

284     protected void checkOption(String JavaDoc 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     /**
297      * <i><font size="-1" color="#FF0000">**For internal use only** </font></i>
298      * Extracts the port number from the node URL
299      */

300     protected static int getPort(String JavaDoc nodeURL, int defaultValue) {
301         int deb = nodeURL.lastIndexOf(":");
302         if (deb > -1) {
303             //there is a port number specified
304
try {
305                 // System.out.println("StartNode: " + nodeURL.substring(deb + 1, nodeURL.lastIndexOf("/")));getporrt
306
return Integer.parseInt(nodeURL.substring(deb + 1,
307                         nodeURL.lastIndexOf("/")));
308             } catch (NumberFormatException JavaDoc e) {
309                 return defaultValue;
310             }
311         }
312         return defaultValue;
313     }
314
315     //
316
// -- PRIVATE METHODS -----------------------------------------------
317
//
318
private void printUsage() {
319         String JavaDoc localhost = "localhost";
320         try {
321             localhost = java.net.InetAddress.getLocalHost().getCanonicalHostName();
322         } catch (java.net.UnknownHostException JavaDoc e) {
323             logger.error("InetAddress failed: " + e.getMessage());
324             e.printStackTrace();
325         } catch (java.lang.SecurityException JavaDoc 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  // end class
357
Popular Tags