KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > body > ProActiveMetaObjectFactory


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.body;
32
33
34 /**
35  * THIS JAVADOC SHOULD BE REWRITTEN
36  * <p>
37  * This class provides singleton instances of all default factories
38  * creating MetaObjects used in the Body.
39  * </p>
40  *
41  * <b>Since version 1.8, it is also possible to parameterized the factories on a per-object basis. </b>
42  * In that case, public ProActiveMetaObjectFactory(Hashtable parameters) is the constructor to use.
43  * <p>
44  * One can inherit from this class in order to provide custom implementation
45  * of one or several factories. This class provide a default implementation that
46  * makes the factories a singleton. One instance of each mata object factory is
47  * created when this object is built and the same instance is returned each time
48  * somebody ask for an instance.
49  * </p>
50  * <p>
51  * In order to change one meta object factory following that singleton pattern,
52  * only the protected method <code>newXXXSingleton</code> has to be overwritten.
53  * The method <code>newXXXSingleton</code> is guarantee to be called only once at
54  * construction time of this object.
55  * </p>
56  * <p>
57  * In order to change one meta object factory that does not follow the singleton
58  * pattern, the public method <code>newXXX</code> has to be overwritten in order
59  * to return a new instance of the factory each time. The default implementation
60  * of each <code>newXXX</code> method if to return the singleton instance of the
61  * factory created from <code>newXXXSingleton</code> method call.
62  * </p>
63  * <p>
64  * Each sub class of this class should be implemented as a singleton and provide
65  * a static method <code>newInstance</code> for this purpose.
66  * </p>
67  *
68  * @author ProActive Team
69  * @version 1.0, 2002/05
70  * @since ProActive 0.9.2
71  */

72 import java.util.Hashtable JavaDoc;
73
74 import org.apache.log4j.Logger;
75 import org.objectweb.proactive.Body;
76 import org.objectweb.proactive.core.ProActiveException;
77 import org.objectweb.proactive.core.ProActiveRuntimeException;
78 import org.objectweb.proactive.core.UniqueID;
79 import org.objectweb.proactive.core.body.migration.MigrationManager;
80 import org.objectweb.proactive.core.body.migration.MigrationManagerFactory;
81 import org.objectweb.proactive.core.body.reply.ReplyReceiver;
82 import org.objectweb.proactive.core.body.reply.ReplyReceiverFactory;
83 import org.objectweb.proactive.core.body.request.BlockingRequestQueue;
84 import org.objectweb.proactive.core.body.request.Request;
85 import org.objectweb.proactive.core.body.request.RequestFactory;
86 import org.objectweb.proactive.core.body.request.RequestQueueFactory;
87 import org.objectweb.proactive.core.body.request.RequestReceiver;
88 import org.objectweb.proactive.core.body.request.RequestReceiverFactory;
89 import org.objectweb.proactive.core.component.ComponentParameters;
90 import org.objectweb.proactive.core.component.identity.ProActiveComponent;
91 import org.objectweb.proactive.core.component.identity.ProActiveComponentFactory;
92 import org.objectweb.proactive.core.component.identity.ProActiveComponentImpl;
93 import org.objectweb.proactive.core.component.request.ComponentRequestQueueImpl;
94 import org.objectweb.proactive.core.group.ProActiveGroupManager;
95 import org.objectweb.proactive.core.group.ProActiveGroupManagerFactory;
96 import org.objectweb.proactive.core.mop.MethodCall;
97 import org.objectweb.proactive.core.util.ThreadStore;
98 import org.objectweb.proactive.core.util.ThreadStoreFactory;
99 import org.objectweb.proactive.ext.security.ProActiveSecurityManager;
100
101
102 public class ProActiveMetaObjectFactory implements MetaObjectFactory,
103     java.io.Serializable JavaDoc {
104     public static final String JavaDoc COMPONENT_PARAMETERS_KEY = "component-parameters";
105     protected static Logger logger = Logger.getLogger(ProActiveMetaObjectFactory.class.getName());
106
107     //
108
// -- PRIVATE MEMBERS -----------------------------------------------
109
//
110
// private static final MetaObjectFactory instance = new ProActiveMetaObjectFactory();
111
private static MetaObjectFactory instance = new ProActiveMetaObjectFactory();
112     public Hashtable JavaDoc parameters;
113
114     //
115
// -- PROTECTED MEMBERS -----------------------------------------------
116
//
117
protected RequestFactory requestFactoryInstance;
118     protected ReplyReceiverFactory replyReceiverFactoryInstance;
119     protected RequestReceiverFactory requestReceiverFactoryInstance;
120     protected RequestQueueFactory requestQueueFactoryInstance;
121     protected MigrationManagerFactory migrationManagerFactoryInstance;
122     protected RemoteBodyFactory remoteBodyFactoryInstance;
123     protected ThreadStoreFactory threadStoreFactoryInstance;
124     protected ProActiveGroupManagerFactory proActiveGroupManagerFactoryInstance;
125     protected ProActiveComponentFactory componentFactoryInstance;
126     protected ProActiveSecurityManager proActiveSecurityManager;
127
128     //
129
// -- CONSTRUCTORS -----------------------------------------------
130
//
131
protected ProActiveMetaObjectFactory() {
132         requestFactoryInstance = newRequestFactorySingleton();
133         replyReceiverFactoryInstance = newReplyReceiverFactorySingleton();
134         requestReceiverFactoryInstance = newRequestReceiverFactorySingleton();
135         requestQueueFactoryInstance = newRequestQueueFactorySingleton();
136         migrationManagerFactoryInstance = newMigrationManagerFactorySingleton();
137         remoteBodyFactoryInstance = newRemoteBodyFactorySingleton();
138         threadStoreFactoryInstance = newThreadStoreFactorySingleton();
139         proActiveGroupManagerFactoryInstance = newProActiveGroupManagerFactorySingleton();
140     }
141
142     /**
143      * Constructor with parameters
144      * It is used for per-active-object configurations of ProActive factories
145      * @param parameters the parameters of the factories; these parameters can be of any type
146      */

147     public ProActiveMetaObjectFactory(Hashtable JavaDoc parameters) {
148         this.parameters = parameters;
149         if (parameters.containsKey(COMPONENT_PARAMETERS_KEY)) {
150             ComponentParameters initialComponentParameters = (ComponentParameters) parameters.get(COMPONENT_PARAMETERS_KEY);
151             componentFactoryInstance = newComponentFactorySingleton(initialComponentParameters);
152             requestFactoryInstance = newRequestFactorySingleton();
153             replyReceiverFactoryInstance = newReplyReceiverFactorySingleton();
154             requestReceiverFactoryInstance = newRequestReceiverFactorySingleton();
155             requestQueueFactoryInstance = newRequestQueueFactorySingleton();
156             migrationManagerFactoryInstance = newMigrationManagerFactorySingleton();
157             remoteBodyFactoryInstance = newRemoteBodyFactorySingleton();
158             threadStoreFactoryInstance = newThreadStoreFactorySingleton();
159             proActiveGroupManagerFactoryInstance = newProActiveGroupManagerFactorySingleton();
160         }
161     }
162
163     //
164
// -- PUBLICS METHODS -----------------------------------------------
165
//
166
public static MetaObjectFactory newInstance() {
167         return instance;
168     }
169
170     public static void setNewInstance(MetaObjectFactory mo) {
171         instance = mo;
172     }
173
174     /**
175      * getter for the parameters of the factory (per-active-object config)
176      * @return the parameters of the factory
177      */

178     public Hashtable JavaDoc getParameters() {
179         return parameters;
180     }
181
182     //
183
// -- implements MetaObjectFactory -----------------------------------------------
184
//
185
public RequestFactory newRequestFactory() {
186         return requestFactoryInstance;
187     }
188
189     public ReplyReceiverFactory newReplyReceiverFactory() {
190         return replyReceiverFactoryInstance;
191     }
192
193     public RequestReceiverFactory newRequestReceiverFactory() {
194         return requestReceiverFactoryInstance;
195     }
196
197     public RequestQueueFactory newRequestQueueFactory() {
198         return requestQueueFactoryInstance;
199     }
200
201     public MigrationManagerFactory newMigrationManagerFactory() {
202         return migrationManagerFactoryInstance;
203     }
204
205     public RemoteBodyFactory newRemoteBodyFactory() {
206         return remoteBodyFactoryInstance;
207     }
208
209     public ThreadStoreFactory newThreadStoreFactory() {
210         return threadStoreFactoryInstance;
211     }
212
213     public ProActiveGroupManagerFactory newProActiveGroupManagerFactory() {
214         return proActiveGroupManagerFactoryInstance;
215     }
216
217     public ProActiveComponentFactory newComponentFactory() {
218         return componentFactoryInstance;
219     }
220
221     //
222
// -- PROTECTED METHODS -----------------------------------------------
223
//
224
protected RequestFactory newRequestFactorySingleton() {
225         return new RequestFactoryImpl();
226     }
227
228     protected ReplyReceiverFactory newReplyReceiverFactorySingleton() {
229         return new ReplyReceiverFactoryImpl();
230     }
231
232     protected RequestReceiverFactory newRequestReceiverFactorySingleton() {
233         return new RequestReceiverFactoryImpl();
234     }
235
236     protected RequestQueueFactory newRequestQueueFactorySingleton() {
237         return new RequestQueueFactoryImpl();
238     }
239
240     protected MigrationManagerFactory newMigrationManagerFactorySingleton() {
241         return new MigrationManagerFactoryImpl();
242     }
243
244     protected RemoteBodyFactory newRemoteBodyFactorySingleton() {
245         return new RemoteBodyFactoryImpl();
246     }
247
248     protected ThreadStoreFactory newThreadStoreFactorySingleton() {
249         return new ThreadStoreFactoryImpl();
250     }
251
252     protected ProActiveGroupManagerFactory newProActiveGroupManagerFactorySingleton() {
253         return new ProActiveGroupManagerFactoryImpl();
254     }
255
256     protected ProActiveComponentFactory newComponentFactorySingleton(
257         ComponentParameters initialComponentParameters) {
258         return new ProActiveComponentFactoryImpl(initialComponentParameters);
259     }
260
261     // //
262
// // -- INNER CLASSES -----------------------------------------------
263
// //
264
protected static class RequestFactoryImpl implements RequestFactory,
265         java.io.Serializable JavaDoc {
266         public Request newRequest(MethodCall methodCall,
267             UniversalBody sourceBody, boolean isOneWay, long sequenceID) {
268             //########### exemple de code pour les nouvelles factories
269
// if(System.getProperty("migration.stategy").equals("locationserver")){
270
// return new RequestWithLocationServer(methodCall, sourceBody,
271
// isOneWay, sequenceID, LocationServerFactory.getLocationServer());
272
// }else{
273
return new org.objectweb.proactive.core.body.request.RequestImpl(methodCall,
274                 sourceBody, isOneWay, sequenceID);
275             //}
276
}
277     }
278     // end inner class RequestFactoryImpl
279

280     protected static class ReplyReceiverFactoryImpl
281         implements ReplyReceiverFactory, java.io.Serializable JavaDoc {
282         public ReplyReceiver newReplyReceiver() {
283             return new org.objectweb.proactive.core.body.reply.ReplyReceiverImpl();
284         }
285     }
286     // end inner class ReplyReceiverFactoryImpl
287

288     protected static class RequestReceiverFactoryImpl
289         implements RequestReceiverFactory, java.io.Serializable JavaDoc {
290         public RequestReceiver newRequestReceiver() {
291             return new org.objectweb.proactive.core.body.request.RequestReceiverImpl();
292         }
293     }
294     // end inner class RequestReceiverFactoryImpl
295

296     protected class RequestQueueFactoryImpl implements RequestQueueFactory,
297         java.io.Serializable JavaDoc {
298         public BlockingRequestQueue newRequestQueue(UniqueID ownerID) {
299             if (componentFactoryInstance != null) {
300                 // COMPONENTS
301
// we need a request queue for components
302
return new ComponentRequestQueueImpl(ownerID);
303             } else {
304                 return new org.objectweb.proactive.core.body.request.BlockingRequestQueueImpl(ownerID);
305             }
306         }
307     }
308     // end inner class RequestQueueFactoryImpl
309

310     protected static class MigrationManagerFactoryImpl
311         implements MigrationManagerFactory, java.io.Serializable JavaDoc {
312         public MigrationManager newMigrationManager() {
313             //########### example de code pour les nouvelles factories
314
// if(System.getProperty("migration.stategy").equals("locationserver")){
315
// return new MigrationManagerWithLocationServer(LocationServerFactory.getLocationServer());
316
// }else{
317
return new org.objectweb.proactive.core.body.migration.MigrationManagerImpl();
318             //}
319
}
320     }
321     // end inner class MigrationManagerFactoryImpl
322

323     protected static class RemoteBodyFactoryImpl implements RemoteBodyFactory,
324         java.io.Serializable JavaDoc {
325         public UniversalBody newRemoteBody(UniversalBody body) {
326             try {
327                 if ("ibis".equals(System.getProperty("proactive.communication.protocol"))) {
328                     if (logger.isDebugEnabled()) {
329                         logger.debug("Factory is ibis");
330                     }
331                     return new org.objectweb.proactive.core.body.ibis.IbisRemoteBodyAdapter(body);
332                 } else {
333                     if (logger.isDebugEnabled()) {
334                         logger.debug("Factory is rmi");
335                     }
336                     return new org.objectweb.proactive.core.body.rmi.RemoteBodyAdapter(body);
337                 }
338             } catch (ProActiveException e) {
339                 throw new ProActiveRuntimeException("Cannot create Remote body adapter ",
340                     e);
341             }
342         }
343     }
344     // end inner class RemoteBodyFactoryImpl
345

346     protected static class ThreadStoreFactoryImpl implements ThreadStoreFactory,
347         java.io.Serializable JavaDoc {
348         public ThreadStore newThreadStore() {
349             return new org.objectweb.proactive.core.util.ThreadStoreImpl();
350         }
351     }
352     // end inner class ThreadStoreFactoryImpl
353

354     protected static class ProActiveGroupManagerFactoryImpl implements ProActiveGroupManagerFactory,
355         java.io.Serializable JavaDoc {
356             public ProActiveGroupManager newProActiveGroupManager() {
357                 return new ProActiveGroupManager();
358         }
359     }
360     // end inner class ProActiveGroupManagerFactoryImpl
361

362     protected class ProActiveComponentFactoryImpl
363         implements ProActiveComponentFactory, java.io.Serializable JavaDoc {
364         // COMPONENTS
365
private ComponentParameters componentParameters;
366
367         public ProActiveComponentFactoryImpl(
368             ComponentParameters initialComponentParameters) {
369             this.componentParameters = initialComponentParameters;
370         }
371
372         public ProActiveComponent newProActiveComponent(Body myBody) {
373             return new ProActiveComponentImpl(componentParameters, myBody);
374         }
375     }
376     
377     // SECURITY
378
public void setProActiveSecurityManager(ProActiveSecurityManager psm) {
379         this.proActiveSecurityManager = psm;
380     }
381
382     public ProActiveSecurityManager getProActiveSecurityManager() {
383         return proActiveSecurityManager;
384     }
385
386 }
387
Popular Tags