org/objectweb/proactive/core/runtime/rmi/RmiRuntimeFactory.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.runtime.rmi;
00032 
00033 import java.rmi.RemoteException;
00034 
00035 import org.objectweb.proactive.core.ProActiveException;
00036 import org.objectweb.proactive.core.config.ProActiveConfiguration;
00037 import org.objectweb.proactive.core.rmi.ClassServerHelper;
00038 import org.objectweb.proactive.core.rmi.RegistryHelper;
00039 import org.objectweb.proactive.core.runtime.ProActiveRuntime;
00040 import org.objectweb.proactive.core.runtime.ProActiveRuntimeAdapterForwarderImpl;
00041 import org.objectweb.proactive.core.runtime.ProActiveRuntimeAdapterImpl;
00042 import org.objectweb.proactive.core.runtime.ProActiveRuntimeForwarder;
00043 import org.objectweb.proactive.core.runtime.RemoteProActiveRuntime;
00044 import org.objectweb.proactive.core.runtime.RuntimeFactory;
00045 import org.objectweb.proactive.core.util.UrlBuilder;
00046 
00047 
00048 public class RmiRuntimeFactory extends RuntimeFactory {
00049     //protected final static int MAX_RETRY = 5;
00050     //protected java.util.Random random;
00051     protected static RegistryHelper registryHelper = new RegistryHelper();
00052     protected static ClassServerHelper classServerHelper = new ClassServerHelper();
00053     protected static ProActiveRuntime defaultRmiRuntime = null;
00054     protected static ProActiveRuntimeForwarder defaultRmiRuntimeForwarder = null;
00055 
00056     //
00057     // -- CONSTRUCTORS -----------------------------------------------
00058     //
00059     public RmiRuntimeFactory() throws java.io.IOException {
00060         if ((System.getSecurityManager() == null) &&
00061                 !("false".equals(System.getProperty("proactive.securitymanager")))) {
00062             System.setSecurityManager(new java.rmi.RMISecurityManager());
00063         }
00064 
00065         //registryHelper.initializeRegistry();
00066     }
00067 
00068     //
00069     // -- PUBLIC METHODS -----------------------------------------------
00070     //
00071     public static void setClassServerClasspath(String v) {
00072         classServerHelper.setClasspath(v);
00073     }
00074 
00075     public static void setShouldCreateClassServer(boolean v) {
00076         classServerHelper.setShouldCreateClassServer(v);
00077     }
00078 
00079     public static void setRegistryPortNumber(int v) {
00080         registryHelper.setRegistryPortNumber(v);
00081     }
00082 
00083     public static void setShouldCreateRegistry(boolean v) {
00084         registryHelper.setShouldCreateRegistry(v);
00085     }
00086 
00087     //
00088     // -- PROTECTED METHODS -----------------------------------------------
00089     //
00090     //  protected ProActiveRuntime createRemoteRuntimeImpl(String s, boolean replacePreviousBinding) throws ProActiveException {
00091     //    return createRuntimeAdapter(s, replacePreviousBinding);
00092     //  }
00093     protected synchronized ProActiveRuntime getProtocolSpecificRuntimeImpl()
00094         throws ProActiveException {
00095         //return createRuntimeAdapter(s,false);
00096         if (defaultRmiRuntime == null) {
00097             try {
00098                 registryHelper.initializeRegistry();
00099             } catch (RemoteException e) {
00100                 e.printStackTrace();
00101             }
00102 
00103             if (ProActiveConfiguration.isForwarder()) {
00104                 defaultRmiRuntime = createRuntimeAdapterForwarder();
00105             } else {
00106                 defaultRmiRuntime = createRuntimeAdapter();
00107             }
00108         }
00109 
00110         return defaultRmiRuntime;
00111     }
00112 
00113     protected ProActiveRuntime getRemoteRuntimeImpl(String s)
00114         throws ProActiveException {
00115         //if (s == null) return null;
00116         try {
00117             RemoteProActiveRuntime remoteProActiveRuntime;
00118             String URL = UrlBuilder.removeProtocol(s, "rmi:");
00119             remoteProActiveRuntime = (RemoteProActiveRuntime) java.rmi.Naming.lookup(URL);
00120             //System.out.println(remoteProActiveRuntime.getClass().getName());
00121             return createRuntimeAdapter(remoteProActiveRuntime);
00122         } catch (java.rmi.RemoteException e) {
00123             throw new ProActiveException("Remote", e);
00124         } catch (java.rmi.NotBoundException e) {
00125             throw new ProActiveException("NotBound", e);
00126         } catch (java.net.MalformedURLException e) {
00127             throw new ProActiveException("Malformed URL:" + s, e);
00128         }
00129     }
00130 
00131     protected ProActiveRuntimeAdapterImpl createRuntimeAdapter()
00132         throws ProActiveException {
00133         RmiProActiveRuntimeImpl impl;
00134         try {
00135             impl = new RmiProActiveRuntimeImpl();
00136         } catch (java.rmi.RemoteException e) {
00137             throw new ProActiveException("Cannot create the RemoteProActiveRuntimeImpl",
00138                 e);
00139         } catch (java.rmi.AlreadyBoundException e) {
00140             throw new ProActiveException("Cannot bind remoteProactiveRuntime", e);
00141         }
00142         return new ProActiveRuntimeAdapterImpl(impl);
00143     }
00144 
00145     protected ProActiveRuntimeAdapterForwarderImpl createRuntimeAdapterForwarder()
00146         throws ProActiveException {
00147         RmiProActiveRuntimeForwarderImpl impl;
00148         try {
00149             impl = new RmiProActiveRuntimeForwarderImpl();
00150         } catch (java.rmi.RemoteException e) {
00151             throw new ProActiveException("Cannot create the RemoteProActiveRuntimeImpl",
00152                 e);
00153         } catch (java.rmi.AlreadyBoundException e) {
00154             throw new ProActiveException("Cannot bind remoteProactiveRuntime", e);
00155         }
00156         return new ProActiveRuntimeAdapterForwarderImpl(impl);
00157     }
00158 
00159     public static RegistryHelper getRegistryHelper() {
00160         return registryHelper;
00161     }
00162 }

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