org/objectweb/proactive/core/body/ProActiveMetaObjectFactory.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.body;
00032 
00033 import java.io.ByteArrayInputStream;
00034 import java.io.ByteArrayOutputStream;
00035 import java.io.IOException;
00036 import java.io.ObjectInputStream;
00037 import java.io.ObjectOutputStream;
00038 import java.io.Serializable;
00039 import java.util.HashMap;
00040 import java.util.Map;
00041 
00042 import org.apache.log4j.Logger;
00043 import org.objectweb.proactive.Body;
00044 import org.objectweb.proactive.core.ProActiveException;
00045 import org.objectweb.proactive.core.ProActiveRuntimeException;
00046 import org.objectweb.proactive.core.UniqueID;
00047 import org.objectweb.proactive.core.body.ft.protocols.FTManager;
00048 import org.objectweb.proactive.core.body.ft.protocols.FTManagerFactory;
00049 import org.objectweb.proactive.core.body.ft.protocols.cic.managers.FTManagerCIC;
00050 import org.objectweb.proactive.core.body.ft.protocols.cic.managers.HalfFTManagerCIC;
00051 import org.objectweb.proactive.core.body.ft.protocols.pmlrb.managers.FTManagerPMLRB;
00052 import org.objectweb.proactive.core.body.ft.protocols.pmlrb.managers.HalfFTManagerPMLRB;
00053 import org.objectweb.proactive.core.body.migration.MigrationManager;
00054 import org.objectweb.proactive.core.body.migration.MigrationManagerFactory;
00055 import org.objectweb.proactive.core.body.reply.ReplyReceiver;
00056 import org.objectweb.proactive.core.body.reply.ReplyReceiverFactory;
00057 import org.objectweb.proactive.core.body.request.BlockingRequestQueue;
00058 import org.objectweb.proactive.core.body.request.Request;
00059 import org.objectweb.proactive.core.body.request.RequestFactory;
00060 import org.objectweb.proactive.core.body.request.RequestQueueFactory;
00061 import org.objectweb.proactive.core.body.request.RequestReceiver;
00062 import org.objectweb.proactive.core.body.request.RequestReceiverFactory;
00063 import org.objectweb.proactive.core.component.ComponentParameters;
00064 import org.objectweb.proactive.core.component.identity.ProActiveComponent;
00065 import org.objectweb.proactive.core.component.identity.ProActiveComponentFactory;
00066 import org.objectweb.proactive.core.component.identity.ProActiveComponentImpl;
00067 import org.objectweb.proactive.core.component.request.SynchronousComponentRequestReceiver;
00068 import org.objectweb.proactive.core.group.spmd.ProActiveSPMDGroupManager;
00069 import org.objectweb.proactive.core.group.spmd.ProActiveSPMDGroupManagerFactory;
00070 import org.objectweb.proactive.core.mop.MethodCall;
00071 import org.objectweb.proactive.core.util.ThreadStore;
00072 import org.objectweb.proactive.core.util.ThreadStoreFactory;
00073 import org.objectweb.proactive.core.util.log.Loggers;
00074 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00075 import org.objectweb.proactive.ext.security.ProActiveSecurityManager;
00076 
00077 
00078 // TODO JAVADOC SHOULD BE REWRITTEN 
00116 public class ProActiveMetaObjectFactory implements MetaObjectFactory,
00117     java.io.Serializable, Cloneable {
00118     public static final String COMPONENT_PARAMETERS_KEY = "component-parameters";
00119     public static final String SYNCHRONOUS_COMPOSITE_COMPONENT_KEY = "synchronous-composite";
00120     protected static Logger logger = ProActiveLogger.getLogger(Loggers.MOP);
00121 
00122     //
00123     // -- PRIVATE MEMBERS -----------------------------------------------
00124     //
00125     // private static final MetaObjectFactory instance = new ProActiveMetaObjectFactory();
00126     private static MetaObjectFactory instance = new ProActiveMetaObjectFactory();
00127     public Map parameters = new HashMap();
00128 
00129     //
00130     // -- PROTECTED MEMBERS -----------------------------------------------
00131     //
00132     protected RequestFactory requestFactoryInstance;
00133     protected ReplyReceiverFactory replyReceiverFactoryInstance;
00134     protected RequestReceiverFactory requestReceiverFactoryInstance;
00135     protected RequestQueueFactory requestQueueFactoryInstance;
00136     protected MigrationManagerFactory migrationManagerFactoryInstance;
00137     protected RemoteBodyFactory remoteBodyFactoryInstance;
00138     protected ThreadStoreFactory threadStoreFactoryInstance;
00139     protected ProActiveSPMDGroupManagerFactory proActiveSPMDGroupManagerFactoryInstance;
00140     protected ProActiveComponentFactory componentFactoryInstance;
00141     protected ProActiveSecurityManager proActiveSecurityManager;
00142     protected FTManagerFactory ftmanagerFactoryInstance;
00143 
00144     //
00145     // -- CONSTRUCTORS -----------------------------------------------
00146     //
00147     protected ProActiveMetaObjectFactory() {
00148         requestFactoryInstance = newRequestFactorySingleton();
00149         replyReceiverFactoryInstance = newReplyReceiverFactorySingleton();
00150         requestReceiverFactoryInstance = newRequestReceiverFactorySingleton();
00151         requestQueueFactoryInstance = newRequestQueueFactorySingleton();
00152         migrationManagerFactoryInstance = newMigrationManagerFactorySingleton();
00153         remoteBodyFactoryInstance = newRemoteBodyFactorySingleton();
00154         threadStoreFactoryInstance = newThreadStoreFactorySingleton();
00155         proActiveSPMDGroupManagerFactoryInstance = newProActiveSPMDGroupManagerFactorySingleton();
00156         ftmanagerFactoryInstance = newFTManagerFactorySingleton();
00157     }
00158 
00164     public ProActiveMetaObjectFactory(Map parameters) {
00165         this.parameters = parameters;
00166         if (parameters.containsKey(COMPONENT_PARAMETERS_KEY)) {
00167             ComponentParameters initialComponentParameters = (ComponentParameters) parameters.get(COMPONENT_PARAMETERS_KEY);
00168             componentFactoryInstance = newComponentFactorySingleton(initialComponentParameters);
00169             requestFactoryInstance = newRequestFactorySingleton();
00170             replyReceiverFactoryInstance = newReplyReceiverFactorySingleton();
00171             requestReceiverFactoryInstance = newRequestReceiverFactorySingleton();
00172             requestQueueFactoryInstance = newRequestQueueFactorySingleton();
00173             migrationManagerFactoryInstance = newMigrationManagerFactorySingleton();
00174             remoteBodyFactoryInstance = newRemoteBodyFactorySingleton();
00175             threadStoreFactoryInstance = newThreadStoreFactorySingleton();
00176             proActiveSPMDGroupManagerFactoryInstance = newProActiveSPMDGroupManagerFactorySingleton();
00177             ftmanagerFactoryInstance = newFTManagerFactorySingleton();
00178         }
00179     }
00180 
00181     //
00182     // -- PUBLICS METHODS -----------------------------------------------
00183     //
00184     public static MetaObjectFactory newInstance() {
00185         return instance;
00186     }
00187 
00188     public static void setNewInstance(MetaObjectFactory mo) {
00189         instance = mo;
00190     }
00191 
00196     public Map getParameters() {
00197         return parameters;
00198     }
00199 
00200     //
00201     // -- implements MetaObjectFactory -----------------------------------------------
00202     //
00203     public RequestFactory newRequestFactory() {
00204         return requestFactoryInstance;
00205     }
00206 
00207     public ReplyReceiverFactory newReplyReceiverFactory() {
00208         return replyReceiverFactoryInstance;
00209     }
00210 
00211     public RequestReceiverFactory newRequestReceiverFactory() {
00212         return requestReceiverFactoryInstance;
00213     }
00214 
00215     public RequestQueueFactory newRequestQueueFactory() {
00216         return requestQueueFactoryInstance;
00217     }
00218 
00219     public MigrationManagerFactory newMigrationManagerFactory() {
00220         return migrationManagerFactoryInstance;
00221     }
00222 
00223     public RemoteBodyFactory newRemoteBodyFactory() {
00224         return remoteBodyFactoryInstance;
00225     }
00226 
00227     public ThreadStoreFactory newThreadStoreFactory() {
00228         return threadStoreFactoryInstance;
00229     }
00230 
00231     public ProActiveSPMDGroupManagerFactory newProActiveSPMDGroupManagerFactory() {
00232         return proActiveSPMDGroupManagerFactoryInstance;
00233     }
00234 
00235     public ProActiveComponentFactory newComponentFactory() {
00236         return componentFactoryInstance;
00237     }
00238 
00239     public FTManagerFactory newFTManagerFactory() {
00240         return ftmanagerFactoryInstance;
00241     }
00242 
00243     //
00244     // -- PROTECTED METHODS -----------------------------------------------
00245     //
00246     protected RequestFactory newRequestFactorySingleton() {
00247         return new RequestFactoryImpl();
00248     }
00249 
00250     protected ReplyReceiverFactory newReplyReceiverFactorySingleton() {
00251         return new ReplyReceiverFactoryImpl();
00252     }
00253 
00254     protected RequestReceiverFactory newRequestReceiverFactorySingleton() {
00255         return new RequestReceiverFactoryImpl();
00256     }
00257 
00258     protected RequestQueueFactory newRequestQueueFactorySingleton() {
00259         return new RequestQueueFactoryImpl();
00260     }
00261 
00262     protected MigrationManagerFactory newMigrationManagerFactorySingleton() {
00263         return new MigrationManagerFactoryImpl();
00264     }
00265 
00266     protected RemoteBodyFactory newRemoteBodyFactorySingleton() {
00267         return new RemoteBodyFactoryImpl();
00268     }
00269 
00270     protected ThreadStoreFactory newThreadStoreFactorySingleton() {
00271         return new ThreadStoreFactoryImpl();
00272     }
00273 
00274     protected ProActiveSPMDGroupManagerFactory newProActiveSPMDGroupManagerFactorySingleton() {
00275         return new ProActiveSPMDGroupManagerFactoryImpl();
00276     }
00277 
00278     protected ProActiveComponentFactory newComponentFactorySingleton(
00279         ComponentParameters initialComponentParameters) {
00280         return new ProActiveComponentFactoryImpl(initialComponentParameters);
00281     }
00282 
00283     protected FTManagerFactory newFTManagerFactorySingleton() {
00284         return new FTManagerFactoryImpl();
00285     }
00286 
00287     //  //
00288     //  // -- INNER CLASSES -----------------------------------------------
00289     //  //
00290     protected static class RequestFactoryImpl implements RequestFactory,
00291         java.io.Serializable {
00292         public Request newRequest(MethodCall methodCall,
00293             UniversalBody sourceBody, boolean isOneWay, long sequenceID) {
00294             //########### exemple de code pour les nouvelles factories
00295             //                  if(System.getProperty("migration.stategy").equals("locationserver")){
00296             //                            return new RequestWithLocationServer(methodCall, sourceBody,
00297             //                isOneWay, sequenceID, LocationServerFactory.getLocationServer());
00298             //                  }else{
00299             return new org.objectweb.proactive.core.body.request.RequestImpl(methodCall,
00300                 sourceBody, isOneWay, sequenceID);
00301             //}
00302         }
00303     }
00304 
00305     // end inner class RequestFactoryImpl
00306     protected static class ReplyReceiverFactoryImpl
00307         implements ReplyReceiverFactory, java.io.Serializable {
00308         public ReplyReceiver newReplyReceiver() {
00309             return new org.objectweb.proactive.core.body.reply.ReplyReceiverImpl();
00310         }
00311     }
00312 
00313     // end inner class ReplyReceiverFactoryImpl
00314     protected class RequestReceiverFactoryImpl implements RequestReceiverFactory,
00315         java.io.Serializable {
00316         public RequestReceiver newRequestReceiver() {
00317             if (parameters.containsKey(SYNCHRONOUS_COMPOSITE_COMPONENT_KEY) &&
00318                     ((Boolean) parameters.get(
00319                         ProActiveMetaObjectFactory.SYNCHRONOUS_COMPOSITE_COMPONENT_KEY)).booleanValue()) {
00320                 return new SynchronousComponentRequestReceiver();
00321             }
00322             return new org.objectweb.proactive.core.body.request.RequestReceiverImpl();
00323         }
00324     }
00325 
00326     // end inner class RequestReceiverFactoryImpl
00327     protected class RequestQueueFactoryImpl implements RequestQueueFactory,
00328         java.io.Serializable {
00329         public BlockingRequestQueue newRequestQueue(UniqueID ownerID) {
00330             if ("true".equals(parameters.get(
00331                             SYNCHRONOUS_COMPOSITE_COMPONENT_KEY))) {
00332                 return null;
00333             }
00334 
00335             //if (componentFactoryInstance != null) {
00336             // COMPONENTS
00337             // we need a request queue for components
00338             //return new ComponentRequestQueueImpl(ownerID);
00339             //} else {
00340             return new org.objectweb.proactive.core.body.request.BlockingRequestQueueImpl(ownerID);
00341             //}
00342         }
00343     }
00344 
00345     // end inner class RequestQueueFactoryImpl
00346     protected static class MigrationManagerFactoryImpl
00347         implements MigrationManagerFactory, java.io.Serializable {
00348         public MigrationManager newMigrationManager() {
00349             //########### example de code pour les nouvelles factories
00350             //                  if(System.getProperty("migration.stategy").equals("locationserver")){
00351             //                          return new MigrationManagerWithLocationServer(LocationServerFactory.getLocationServer());
00352             //                  }else{
00353             return new org.objectweb.proactive.core.body.migration.MigrationManagerImpl();
00354             //}
00355         }
00356     }
00357 
00358     // end inner class MigrationManagerFactoryImpl
00359     protected static class RemoteBodyFactoryImpl implements RemoteBodyFactory,
00360         java.io.Serializable {
00361         public UniversalBody newRemoteBody(UniversalBody body) {
00362             try {
00363                 if ("ibis".equals(System.getProperty(
00364                                 "proactive.communication.protocol"))) {
00365                     if (logger.isDebugEnabled()) {
00366                         logger.debug(
00367                             "Using ibis factory for creating remote body");
00368                     }
00369                     return new org.objectweb.proactive.core.body.ibis.IbisBodyAdapter(body);
00370                 } else if ("http".equals(System.getProperty(
00371                                 "proactive.communication.protocol"))) {
00372                     if (logger.isDebugEnabled()) {
00373                         logger.debug(
00374                             "Using http factory for creating remote body");
00375                     }
00376 
00377                     return new org.objectweb.proactive.core.body.http.HttpBodyAdapter(body);
00378                 } else if ("rmissh".equals(System.getProperty(
00379                                 "proactive.communication.protocol"))) {
00380                     if (logger.isDebugEnabled()) {
00381                         logger.debug(
00382                             "Using rmissh factory for creating remote body");
00383                     }
00384                     return new org.objectweb.proactive.core.body.rmi.SshRmiBodyAdapter(body);
00385                 } else {
00386                     if (logger.isDebugEnabled()) {
00387                         logger.debug(
00388                             "Using rmi factory for creating remote body");
00389                     }
00390                     return new org.objectweb.proactive.core.body.rmi.RmiBodyAdapter(body);
00391                 }
00392             } catch (ProActiveException e) {
00393                 throw new ProActiveRuntimeException("Cannot create Remote body adapter ",
00394                     e);
00395             }
00396         }
00397     }
00398 
00399     // end inner class RemoteBodyFactoryImpl
00400     protected static class ThreadStoreFactoryImpl implements ThreadStoreFactory,
00401         java.io.Serializable {
00402         public ThreadStore newThreadStore() {
00403             return new org.objectweb.proactive.core.util.ThreadStoreImpl();
00404         }
00405     }
00406 
00407     // end inner class ThreadStoreFactoryImpl
00408     protected static class ProActiveSPMDGroupManagerFactoryImpl
00409         implements ProActiveSPMDGroupManagerFactory, java.io.Serializable {
00410         public ProActiveSPMDGroupManager newProActiveSPMDGroupManager() {
00411             return new ProActiveSPMDGroupManager();
00412         }
00413     }
00414 
00415     // end inner class ProActiveGroupManagerFactoryImpl
00416     protected class ProActiveComponentFactoryImpl
00417         implements ProActiveComponentFactory, java.io.Serializable {
00418         // COMPONENTS
00419         private ComponentParameters componentParameters;
00420 
00421         public ProActiveComponentFactoryImpl(
00422             ComponentParameters initialComponentParameters) {
00423             this.componentParameters = initialComponentParameters;
00424         }
00425 
00426         public ProActiveComponent newProActiveComponent(Body myBody) {
00427             return new ProActiveComponentImpl(componentParameters, myBody);
00428         }
00429     }
00430 
00431     // FAULT-TOLERANCE
00432     protected class FTManagerFactoryImpl implements FTManagerFactory,
00433         Serializable {
00434         public FTManager newFTManager(int protocolSelector) {
00435             switch (protocolSelector) {
00436             case FTManagerFactory.PROTO_CIC:
00437                 return new FTManagerCIC();
00438             case FTManagerFactory.PROTO_PML:
00439                 return new FTManagerPMLRB();
00440             default:
00441                 logger.error("Error while creating fault-tolerance manager : " +
00442                     "no protocol is associated to selector value " +
00443                     protocolSelector);
00444                 return null;
00445             }
00446         }
00447 
00448         public FTManager newHalfFTManager(int protocolSelector) {
00449             switch (protocolSelector) {
00450             case FTManagerFactory.PROTO_CIC:
00451                 return new HalfFTManagerCIC();
00452             case FTManagerFactory.PROTO_PML:
00453                 return new HalfFTManagerPMLRB();
00454             default:
00455                 logger.error("Error while creating fault-tolerance manager : " +
00456                     "no protocol is associated to selector value " +
00457                     protocolSelector);
00458                 return null;
00459             }
00460         }
00461     }
00462 
00463     // SECURITY
00464     public void setProActiveSecurityManager(ProActiveSecurityManager psm) {
00465         this.proActiveSecurityManager = psm;
00466     }
00467 
00468     public ProActiveSecurityManager getProActiveSecurityManager() {
00469         return proActiveSecurityManager;
00470     }
00471 
00472     public Object clone() throws CloneNotSupportedException {
00473         ProActiveMetaObjectFactory clone = null;
00474 
00475         try {
00476             ByteArrayOutputStream bout = new ByteArrayOutputStream();
00477             ObjectOutputStream out = new ObjectOutputStream(bout);
00478 
00479             out.writeObject(this);
00480             out.flush();
00481             bout.close();
00482 
00483             bout.close();
00484 
00485             ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(
00486                         bout.toByteArray()));
00487 
00488             clone = (ProActiveMetaObjectFactory) ois.readObject();
00489         } catch (IOException e) {
00490             e.printStackTrace();
00491         } catch (ClassNotFoundException e) {
00492             e.printStackTrace();
00493         }
00494 
00495         return clone;
00496     }
00497 }

Generated on Mon Jan 22 15:16:06 2007 for ProActive by  doxygen 1.5.1