KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > node > NodeFactory


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.core.node;
32
33 import java.net.UnknownHostException JavaDoc;
34
35 import org.apache.log4j.Logger;
36 import org.objectweb.proactive.ProActive;
37 import org.objectweb.proactive.core.ProActiveException;
38 import org.objectweb.proactive.core.UniqueID;
39 import org.objectweb.proactive.core.config.ProActiveConfiguration;
40 import org.objectweb.proactive.core.runtime.ProActiveRuntime;
41 import org.objectweb.proactive.core.runtime.RuntimeFactory;
42 import org.objectweb.proactive.core.util.UrlBuilder;
43 import org.objectweb.proactive.ext.security.PolicyServer;
44
45
46 /**
47  * <p>
48  * The <code>NodeFactory</code> provides a generic way to create and lookup <code>Node</code>
49  * without protocol specific code (such as RMI or Jini).
50  * </p><p>
51  * <code>NodeFactory</code> provides a set of static methods to create and lookup <code>Node</code>.
52  * To create a node it is only necessary to associate the protocol in the node url.
53  * For instance :
54  * </p>
55  * <pre>
56  * rmi://localhost/node1
57  * jini://localhost/node2
58  * </pre>
59  * <p>
60  * As long as a protocol specific factory has been registered for the
61  * given protocol, the creation of the node will be delegated to the right factory.
62  * </p><p>
63  * This class also provide the concept of default node and default protocol. When the protocol is not
64  * specified in the node URL, the default protocol is used. When an active object is created in the local
65  * JVM but without being attached to any node, a default node is created to hold that active object.
66  * </p>
67  *
68  * @author ProActive Team
69  * @version 1.1, 2002/08/28
70  * @since ProActive 0.9
71  *
72  */

73 public class NodeFactory {
74     protected static Logger logger = Logger.getLogger(NodeFactory.class.getName());
75     private static final String JavaDoc DEFAULT_NODE_NAME;
76     private static Node defaultNode = null;
77
78     static {
79         ProActiveConfiguration.load();
80
81         String JavaDoc protocol = UrlBuilder.checkProtocol(System.getProperty(
82                     "proactive.communication.protocol"));
83         String JavaDoc port = System.getProperty("proactive.rmi.port");
84         if (port != null) {
85             DEFAULT_NODE_NAME = UrlBuilder.buildUrl("localhost", "Node",
86                     protocol, new Integer JavaDoc(port).intValue());
87         } else {
88             DEFAULT_NODE_NAME = UrlBuilder.buildUrl("localhost", "Node",
89                     protocol);
90         }
91     }
92
93     // test with class loader
94
//private static final ClassLoader myClassLoader = new NodeClassLoader();
95
//
96
// -- PUBLIC METHODS - STATIC -----------------------------------------------
97
//
98

99     /**
100      * Returns the reference to the default node associated to the current JVM
101      * If no default node yet exists, it creates a new one.
102      * @return a reference to the default node associated to this JVM
103      * @exception NodeException if the default node cannot be instantiated
104      */

105     public static synchronized Node getDefaultNode() throws NodeException {
106         String JavaDoc nodeURL;
107         ProActiveRuntime defaultRuntime;
108         String JavaDoc jobID = ProActive.getJobId();
109         if (defaultNode == null) {
110             try {
111                 defaultRuntime = RuntimeFactory.getDefaultRuntime();
112                 nodeURL = defaultRuntime.createLocalNode(DEFAULT_NODE_NAME +
113                         Integer.toString(
114                             new java.util.Random JavaDoc(System.currentTimeMillis()).nextInt()),
115                         false,defaultRuntime.getPolicyServer(), "currentJVM",jobID );
116             } catch (ProActiveException e) {
117                 throw new NodeException("Cannot create the default Node", e);
118             }
119             defaultNode = new NodeImpl(defaultRuntime, nodeURL,
120                     UrlBuilder.checkProtocol(System.getProperty("proactive.communication.protocol")), jobID);
121         }
122         return defaultNode;
123     }
124
125     /**
126      * Returns true if the given node belongs to this JVM false else.
127      * @return true if the given node belongs to this JVM false else
128      */

129     public static boolean isNodeLocal(Node node) {
130         return node.getNodeInformation().getVMID().equals(UniqueID.getCurrentVMID());
131     }
132
133     /**
134      * Creates a new node on the local machine. This call can only be used
135      * to create a node on the local JVM on the local machine.
136      * The node URL can be in the form
137      * <ul>
138      * <li>///nodeName</li>
139      * <li>//localhost/nodeName</li>
140      * <li>//<i>&lt;hostname></i>/nodeName</li>
141      * </ul>
142      * where <i>&lt;hostname></i> is the name of the localhost.
143      * @param <code>nodeURL</code> the URL of the node to create
144      * @return the newly created node on the local JVM
145      * @exception NodeException if the node cannot be created
146      */

147     public static Node createNode(String JavaDoc nodeURL) throws NodeException {
148         return createNode(nodeURL, false,null,null);
149     }
150
151     /**
152      * Creates a new node on the local machine. This call can only be used
153      * to create a node on the local JVM on the local machine.
154      * The node URL can be in the form
155      * <ul>
156      * <li>///nodeName</li>
157      * <li>//localhost/nodeName</li>
158      * <li>//<i>&lt;hostname></i>/nodeName</li>
159      * </ul>
160      * where <i>&lt;hostname></i> is the name of the localhost.
161      * @param <code>nodeURL</code> the URL of the node to create
162      * @param <code>replacePreviousBinding</code>
163      * @return the newly created node on the local JVM
164      * @exception NodeException if the node cannot be created
165      */

166     public static Node createNode(String JavaDoc url, boolean replacePreviousBinding, PolicyServer ps, String JavaDoc vnname)
167         throws NodeException {
168         ProActiveRuntime proActiveRuntime;
169         String JavaDoc nodeURL;
170         String JavaDoc jobID = ProActive.getJobId();
171
172         if (logger.isDebugEnabled()) {
173             logger.debug("NodeFactory: createNode(" + url + ")");
174         }
175
176         //first look for the prototcol
177
String JavaDoc protocol = UrlBuilder.getProtocol(url);
178
179         //NodeFactory factory = getFactory(protocol);
180
//then create a node
181
try {
182             proActiveRuntime = RuntimeFactory.getProtocolSpecificRuntime(protocol);
183             nodeURL = proActiveRuntime.createLocalNode(url,
184                     replacePreviousBinding,ps,vnname, jobID);
185         } catch (ProActiveException e) {
186             throw new NodeException("Cannot create a Node based on " + url, e);
187         }
188         Node node = new NodeImpl(proActiveRuntime, nodeURL, protocol, jobID);
189         return node;
190     }
191
192     /**
193      * Returns the reference to the node located at the given url.
194      * This url can be either local or remote.
195      * @param nodeURL. The url of the node
196      * @return Node. The reference of the node
197      * @throws NodeException if the node cannot be found
198      */

199     public static Node getNode(String JavaDoc nodeURL) throws NodeException {
200         ProActiveRuntime proActiveRuntime;
201         String JavaDoc url;
202         String JavaDoc nodeName;
203         String JavaDoc jobID;
204
205         if (logger.isDebugEnabled()) {
206             logger.debug("NodeFactory: getNode() for " + nodeURL);
207         }
208
209         //do we have any association for this node?
210
String JavaDoc protocol = UrlBuilder.getProtocol(nodeURL);
211
212         //String noProtocolUrl = UrlBuilder.removeProtocol(nodeURL, protocol);
213
try {
214             url = UrlBuilder.checkUrl(nodeURL);
215             proActiveRuntime = RuntimeFactory.getRuntime(url, protocol);
216             jobID = proActiveRuntime.getJobID(url);
217         } catch (ProActiveException e) {
218             throw new NodeException("Cannot get the node based on " + nodeURL, e);
219         } catch (UnknownHostException JavaDoc e) {
220             throw new NodeException("Cannot get the node based on " + nodeURL, e);
221         }
222         Node node = new NodeImpl(proActiveRuntime, url, protocol, jobID);
223         return node;
224     }
225
226     /**
227      * Kills the node of the given url
228      * @param nodeURL
229      * @throws NodeException if a problem occurs when killing the node
230      */

231     public static void killNode(String JavaDoc nodeURL) throws NodeException {
232         ProActiveRuntime proActiveRuntime;
233         String JavaDoc url;
234
235         String JavaDoc protocol = UrlBuilder.getProtocol(nodeURL);
236         try {
237             url = UrlBuilder.checkUrl(nodeURL);
238             proActiveRuntime = RuntimeFactory.getRuntime(url, protocol);
239             proActiveRuntime.killNode(url);
240         } catch (ProActiveException e) {
241             throw new NodeException("Cannot get the node based on " + nodeURL, e);
242         } catch (UnknownHostException JavaDoc e) {
243             throw new NodeException("Cannot get the node based on " + nodeURL, e);
244         }
245     }
246 }
247
Popular Tags