00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030  
00031 package org.objectweb.proactive.core.runtime;
00032 
00033 import org.apache.log4j.Logger;
00034 import org.objectweb.proactive.core.Constants;
00035 import org.objectweb.proactive.core.ProActiveException;
00036 import org.objectweb.proactive.core.UniqueID;
00037 import org.objectweb.proactive.core.config.ProActiveConfiguration;
00038 import org.objectweb.proactive.core.rmi.ClassServerHelper;
00039 import org.objectweb.proactive.core.util.log.Loggers;
00040 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00041 
00042 
00070 public abstract class RuntimeFactory {
00071     public static Logger runtimeLogger = ProActiveLogger.getLogger(Loggers.RUNTIME);
00072 
00073     
00074     
00075 
00077     private static java.util.HashMap<String, String> protocolFactoryMapping = new java.util.HashMap<String, String>();
00078     private static java.util.HashMap<String, RuntimeFactory> instanceFactoryMapping = new java.util.HashMap<String, RuntimeFactory>();
00079 
00080     
00081     
00082  
00083 
00084     static {
00085         ProActiveConfiguration.load();
00086         createClassServer();
00087         registerProtocolFactories();
00088         
00089     }
00090 
00091     
00092     
00093     
00094 
00102     public static synchronized void setFactory(String protocol,
00103         String factoryClassName) {
00104         if (runtimeLogger.isDebugEnabled()) {
00105             runtimeLogger.debug("protocol =  " + protocol + " " +
00106                 factoryClassName);
00107         }
00108         protocolFactoryMapping.put(protocol, factoryClassName);
00109     }
00110 
00118     public static synchronized void setFactory(String protocol,
00119         RuntimeFactory factoryObject) {
00120         protocolFactoryMapping.put(protocol, factoryObject.getClass().getName());
00121         instanceFactoryMapping.put(protocol, factoryObject);
00122     }
00123 
00128     public static boolean isRuntimeLocal(ProActiveRuntime proActiveRuntime) {
00129         return proActiveRuntime.getVMInformation().getVMID().equals(UniqueID.getCurrentVMID());
00130     }
00131 
00138     public static synchronized ProActiveRuntime getDefaultRuntime()
00139         throws ProActiveException {
00140         ProActiveRuntime defaultRuntime = null;
00141         try {
00142             
00143             defaultRuntime = getProtocolSpecificRuntime(System.getProperty(
00144                         "proactive.communication.protocol") + ":");
00145             if (runtimeLogger.isDebugEnabled()) {
00146                 runtimeLogger.debug("default runtime = " +
00147                     defaultRuntime.getURL());
00148             }
00149         } catch (ProActiveException e) {
00150             
00151             if (runtimeLogger.isDebugEnabled()) {
00152                 runtimeLogger.debug("Error with the default ProActiveRuntime");
00153             }
00154             throw new ProActiveException("Error when getting the default ProActiveRuntime",
00155                 e);
00156         }
00157         return defaultRuntime;
00158     }
00159 
00168     public static ProActiveRuntime getProtocolSpecificRuntime(String protocol)
00169         throws ProActiveException {
00170         RuntimeFactory factory = getFactory(protocol);
00171         ProActiveRuntime proActiveRuntime = factory.getProtocolSpecificRuntimeImpl();
00172         if (proActiveRuntime == null) {
00173             throw new ProActiveException(
00174                 "Cannot create a ProActiveRuntime based on " + protocol);
00175         }
00176         return proActiveRuntime;
00177     }
00178 
00187     public static ProActiveRuntime getRuntime(String proActiveRuntimeURL,
00188         String protocol) throws ProActiveException {
00189         runtimeLogger.debug("proActiveRunTimeURL " + proActiveRuntimeURL + " " +
00190             protocol);
00191 
00192         
00193         
00194         RuntimeFactory factory = getFactory(protocol);
00195 
00196         
00197         if (runtimeLogger.isDebugEnabled()) {
00198             runtimeLogger.debug("factory = " + factory);
00199         }
00200         return factory.getRemoteRuntimeImpl(proActiveRuntimeURL);
00201     }
00202 
00203     
00204     
00205     
00206 
00213     protected ProActiveRuntimeAdapterImpl createRuntimeAdapter(
00214         RemoteProActiveRuntime remoteProActiveRuntime)
00215         throws ProActiveException {
00216         return new ProActiveRuntimeAdapterImpl(remoteProActiveRuntime);
00217     }
00218 
00226     protected abstract ProActiveRuntime getProtocolSpecificRuntimeImpl()
00227         throws ProActiveException;
00228 
00232     protected abstract ProActiveRuntime getRemoteRuntimeImpl(String s)
00233         throws ProActiveException;
00234 
00240     protected abstract ProActiveRuntimeAdapterImpl createRuntimeAdapter()
00241         throws ProActiveException;
00242 
00243     
00244     
00245     
00246     private static void createClassServer() {
00247         try {
00248             new ClassServerHelper().initializeClassServer();
00249         } catch (Exception e) {
00250             if (runtimeLogger.isInfoEnabled()) {
00251                 runtimeLogger.info("Error with the ClassServer : " +
00252                     e.getMessage());
00253             }
00254         }
00255     }
00256 
00257     private static void registerProtocolFactories() {
00258         setFactory(Constants.JINI_PROTOCOL_IDENTIFIER,
00259             "org.objectweb.proactive.core.runtime.jini.JiniRuntimeFactory");
00260 
00261         setFactory(Constants.IBIS_PROTOCOL_IDENTIFIER,
00262             "org.objectweb.proactive.core.runtime.ibis.IbisRuntimeFactory");
00263 
00264         setFactory(Constants.XMLHTTP_PROTOCOL_IDENTIFIER,
00265             "org.objectweb.proactive.core.runtime.http.HttpRuntimeFactory");
00266 
00267         setFactory(Constants.RMISSH_PROTOCOL_IDENTIFIER,
00268             "org.objectweb.proactive.core.runtime.rmi.SshRmiRuntimeFactory");
00269 
00270         setFactory(Constants.RMI_PROTOCOL_IDENTIFIER,
00271             "org.objectweb.proactive.core.runtime.rmi.RmiRuntimeFactory");
00272     }
00273 
00274     private static RuntimeFactory createRuntimeFactory(Class factoryClass,
00275         String protocol) throws ProActiveException {
00276         try {
00277             RuntimeFactory nf = (RuntimeFactory) factoryClass.newInstance();
00278             instanceFactoryMapping.put(protocol, nf);
00279             return nf;
00280         } catch (Exception e) {
00281             e.printStackTrace();
00282             throw new ProActiveException("Error while creating the factory " +
00283                 factoryClass.getName() + " for the protocol " + protocol);
00284         }
00285     }
00286 
00287     private static RuntimeFactory createRuntimeFactory(
00288         String factoryClassName, String protocol) throws ProActiveException {
00289         Class factoryClass = null;
00290         if (runtimeLogger.isDebugEnabled()) {
00291             runtimeLogger.debug("factoryClassName " + factoryClassName);
00292         }
00293         try {
00294             factoryClass = Class.forName(factoryClassName);
00295         } catch (ClassNotFoundException e) {
00296             e.printStackTrace();
00297             throw new ProActiveException(
00298                 "Error while getting the class of the factory " +
00299                 factoryClassName + " for the protocol " + protocol);
00300         }
00301         return createRuntimeFactory(factoryClass, protocol);
00302     }
00303 
00304     private static synchronized RuntimeFactory getFactory(String protocol)
00305         throws ProActiveException {
00306         if (runtimeLogger.isDebugEnabled()) {
00307             runtimeLogger.debug("protocol = " + protocol);
00308         }
00309 
00310         RuntimeFactory factory = instanceFactoryMapping.get(protocol);
00311         if (factory != null) {
00312             return factory;
00313         }
00314 
00315         String factoryClassName = protocolFactoryMapping.get(protocol);
00316         if (runtimeLogger.isDebugEnabled()) {
00317             runtimeLogger.debug("factoryClassName  = " + factoryClassName);
00318         }
00319         if (factoryClassName != null) {
00320             return createRuntimeFactory(factoryClassName, protocol);
00321         }
00322         throw new ProActiveException(
00323             "No RuntimeFactory is registered for the protocol " + protocol);
00324     }
00325 }