KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > runtime > RuntimeFactory


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

69 public abstract class RuntimeFactory {
70     protected static Logger logger = Logger.getLogger(RuntimeFactory.class.getName());
71
72     // test with class loader
73
//private static final ClassLoader myClassLoader = new NodeClassLoader();
74

75     /** the table where associations Protocol - Factory are kept */
76     private static java.util.HashMap JavaDoc protocolFactoryMapping = new java.util.HashMap JavaDoc();
77     private static java.util.HashMap JavaDoc instanceFactoryMapping = new java.util.HashMap JavaDoc();
78
79     //private static ProActiveRuntime defaultRuntime = null;
80
//private static RuntimeFactory defaultRuntimeFactory;
81
public static boolean JINI_ENABLED;
82     public static boolean IBIS_ENABLED;
83
84     static {
85         ProActiveConfiguration.load();
86         createClassServer();
87         JINI_ENABLED = isJiniEnabled();
88         IBIS_ENABLED = isIbisEnabled();
89         registerProtocolFactories();
90         //getLocalRuntime();
91
}
92
93     //
94
// -- PUBLIC METHODS - STATIC -----------------------------------------------
95
//
96

97     /**
98      * Associates the factory of class <code>factoryClassName</code> as the factory to create
99      * proactiveRuntime for the given protocol. Replaces any previous association.
100      * @param <code>protocol</code> the protocol to associate the factory to
101      * @param <code>factoryClassName</code> the fully qualified name of the class of the factory
102      * responsible of creating the proActiveRuntime for that protocol
103      */

104     public static synchronized void setFactory(String JavaDoc protocol,
105         String JavaDoc factoryClassName) {
106         if (logger.isDebugEnabled()) {
107             logger.debug("protocol = " + protocol + " " + factoryClassName);
108         }
109         protocolFactoryMapping.put(protocol, factoryClassName);
110     }
111
112     /**
113      * Associates the factory of class <code>factoryClassName</code> as the factory to create
114      * proactiveRuntime for the given protocol. Replaces any previous association.
115      * @param <code>protocol</code> the protocol to associate the factory to
116      * @param <code>factoryObject</code> the class of the factory
117      * responsible of creating the proactiveRuntime for that protocol
118      */

119     public static synchronized void setFactory(String JavaDoc protocol,
120         RuntimeFactory factoryObject) {
121         protocolFactoryMapping.put(protocol, factoryObject.getClass().getName());
122         instanceFactoryMapping.put(protocol, factoryObject);
123     }
124
125     /**
126      * Returns true if the given proActiveRuntime belongs to this JVM false else.
127      * @return true if the given proActiveRuntime belongs to this JVM false else
128      */

129     public static boolean isRuntimeLocal(ProActiveRuntime proActiveRuntime) {
130         return proActiveRuntime.getVMInformation().getVMID().equals(UniqueID.getCurrentVMID());
131     }
132
133     /**
134      * Returns the reference of the only one instance of the default ProActiveRuntime associated with the local JVM.
135      * If this runtime does not yet exist, it creates it with the default protocol.
136      * @return The only one ProActiveRuntime associated with the local JVM
137      * @throws ProActiveException if the default runtime cannot be created
138      */

139     public static synchronized ProActiveRuntime getDefaultRuntime()
140         throws ProActiveException {
141         ProActiveRuntime defaultRuntime = null;
142         try {
143             //defaultRuntime = getProtocolSpecificRuntime(Constants.DEFAULT_PROTOCOL_IDENTIFIER);
144
defaultRuntime = getProtocolSpecificRuntime(System.getProperty(
145                         "proactive.communication.protocol") + ":");
146             if (logger.isDebugEnabled()) {
147                 logger.debug("default runtime = " + defaultRuntime.getURL());
148             }
149         } catch (ProActiveException e) {
150             //e.printStackTrace();
151
if (logger.isDebugEnabled()) {
152                 logger.debug("Error with the default ProActiveRuntime");
153             }
154             throw new ProActiveException("Error when getting the default ProActiveRuntime",
155                 e);
156         }
157         return defaultRuntime;
158     }
159
160     /**
161      * Returns the reference of the only one instance of the ProActiveRuntime
162      * created with the given protocol, associated with the local JVM.
163      * If this runtime does not yet exist, it creates it with the given protocol.
164      * @param protocol
165      * @return ProActiveRuntime
166      * @throws ProActiveException if this ProActiveRuntime cannot be created
167      */

168     public static ProActiveRuntime getProtocolSpecificRuntime(String JavaDoc protocol)
169         throws ProActiveException {
170         RuntimeFactory factory = getFactory(protocol);
171         ProActiveRuntime proActiveRuntime = factory.getProtocolSpecificRuntimeImpl();
172         if (proActiveRuntime == null) {
173             throw new ProActiveException(
174                 "Cannot create a ProActiveRuntime based on " + protocol);
175         }
176         return proActiveRuntime;
177     }
178
179     /**
180      * Returns a reference to the ProActiveRuntime created with the given protocol and
181      * located at the given url.This url can be either local or remote
182      * @param proActiveRuntimeURL
183      * @param protocol
184      * @return ProActiveRuntime
185      * @throws ProActiveException if the runtime cannot be found
186      */

187     public static ProActiveRuntime getRuntime(String JavaDoc proActiveRuntimeURL,
188         String JavaDoc protocol) throws ProActiveException {
189         if (logger.isDebugEnabled()) {
190             logger.debug("proActiveRunTimeURL " + proActiveRuntimeURL + " " +
191                 protocol);
192         }
193
194         //do we have any association for this node?
195
//String protocol = getProtocol(proActiveRuntimeURL);
196
RuntimeFactory factory = getFactory(protocol);
197
198         //proActiveRuntimeURL = removeProtocol(proActiveRuntimeURL, protocol);
199
if (logger.isDebugEnabled()) {
200             logger.debug("factory = " + factory);
201         }
202         return factory.getRemoteRuntimeImpl(proActiveRuntimeURL);
203     }
204
205     //
206
// -- PROTECTED METHODS -----------------------------------------------
207
//
208

209     /**
210      * Returns the reference of the only one instance of the ProActiveRuntime
211      * associated with the local JVM.
212      * If this runtime does not yet exist, it creates it with the associated protocol.
213      * @return ProActiveRuntime
214      * @throws ProActiveException if this ProActiveRuntime cannot be created
215      */

216     protected abstract ProActiveRuntime getProtocolSpecificRuntimeImpl()
217         throws ProActiveException;
218
219     /**
220      * Returns the reference to the proActiveRuntime located at s
221      */

222     protected abstract ProActiveRuntime getRemoteRuntimeImpl(String JavaDoc s)
223         throws ProActiveException;
224
225     //
226
// -- PRIVATE METHODS - STATIC -----------------------------------------------
227
//
228
private static void createClassServer() {
229         try {
230             new ClassServerHelper().initializeClassServer();
231         } catch (Exception JavaDoc e) {
232             if (logger.isInfoEnabled()) {
233                 logger.info("Error with the ClassServer : " + e.getMessage());
234             }
235         }
236     }
237
238     private static void registerProtocolFactories() {
239         if (JINI_ENABLED) {
240             setFactory(Constants.JINI_PROTOCOL_IDENTIFIER,
241                 "org.objectweb.proactive.core.runtime.jini.JiniRuntimeFactory");
242         }
243         if (IBIS_ENABLED) {
244             setFactory(Constants.IBIS_PROTOCOL_IDENTIFIER,
245                 "org.objectweb.proactive.core.runtime.ibis.RemoteRuntimeFactory");
246         }
247
248         setFactory(Constants.RMI_PROTOCOL_IDENTIFIER,
249             "org.objectweb.proactive.core.runtime.rmi.RemoteRuntimeFactory");
250     }
251
252     private static boolean isJiniEnabled() {
253         try {
254             // test if Jini is available
255
Class.forName("net.jini.discovery.DiscoveryManagement");
256             if (logger.isInfoEnabled()) {
257                 logger.info("Jini enabled");
258             }
259             return true;
260         } catch (ClassNotFoundException JavaDoc e) {
261             if (logger.isInfoEnabled()) {
262                 logger.info("Jini disabled");
263             }
264             return false;
265         }
266     }
267
268     private static boolean isIbisEnabled() {
269         try {
270             // test if Ibis is available
271
Class.forName("ibis.rmi.server.UnicastRemoteObject");
272             if (logger.isInfoEnabled()) {
273                 logger.info("Ibis enabled");
274             }
275             return true;
276         } catch (ClassNotFoundException JavaDoc e) {
277             if (logger.isInfoEnabled()) {
278                 logger.info("Ibis disabled");
279             }
280             return false;
281         }
282     }
283
284     private static RuntimeFactory createRuntimeFactory(Class JavaDoc factoryClass,
285         String JavaDoc protocol) throws ProActiveException {
286         try {
287             RuntimeFactory nf = (RuntimeFactory) factoryClass.newInstance();
288             instanceFactoryMapping.put(protocol, nf);
289             return nf;
290         } catch (Exception JavaDoc e) {
291             e.printStackTrace();
292             throw new ProActiveException("Error while creating the factory " +
293                 factoryClass.getName() + " for the protocol " + protocol);
294         }
295     }
296
297     private static RuntimeFactory createRuntimeFactory(
298         String JavaDoc factoryClassName, String JavaDoc protocol) throws ProActiveException {
299         Class JavaDoc factoryClass = null;
300         if (logger.isDebugEnabled()) {
301             logger.debug("factoryClassName " + factoryClassName);
302         }
303         try {
304             factoryClass = Class.forName(factoryClassName);
305         } catch (ClassNotFoundException JavaDoc e) {
306             e.printStackTrace();
307             throw new ProActiveException(
308                 "Error while getting the class of the factory " +
309                 factoryClassName + " for the protocol " + protocol);
310         }
311         return createRuntimeFactory(factoryClass, protocol);
312     }
313
314     private static synchronized RuntimeFactory getFactory(String JavaDoc protocol)
315         throws ProActiveException {
316         if (logger.isDebugEnabled()) {
317             logger.debug("protocol = " + protocol);
318         }
319
320         RuntimeFactory factory = (RuntimeFactory) instanceFactoryMapping.get(protocol);
321         if (factory != null) {
322             return factory;
323         }
324
325         String JavaDoc factoryClassName = (String JavaDoc) protocolFactoryMapping.get(protocol);
326         if (logger.isDebugEnabled()) {
327             logger.debug("factoryClassName = " + factoryClassName);
328         }
329         if (factoryClassName != null) {
330             return createRuntimeFactory(factoryClassName, protocol);
331         }
332         throw new ProActiveException(
333             "No RuntimeFactory is registered for the protocol " + protocol);
334     }
335
336     /**
337      * Return the protocol specified in the string
338      * The same convention as in URL is used
339      */

340     private static String JavaDoc getProtocol(String JavaDoc proActiveRuntimeURL) {
341         if (proActiveRuntimeURL == null) {
342             return Constants.DEFAULT_PROTOCOL_IDENTIFIER;
343         }
344
345         int n = proActiveRuntimeURL.indexOf("://");
346         if (n <= 0) {
347             return Constants.DEFAULT_PROTOCOL_IDENTIFIER;
348         }
349         return proActiveRuntimeURL.substring(0, n + 1);
350     }
351
352     /**
353      */

354     private static String JavaDoc removeProtocol(String JavaDoc url, String JavaDoc protocol) {
355         if (url.startsWith(protocol)) {
356             return url.substring(protocol.length());
357         }
358         return url;
359     }
360 }
361
Popular Tags