org/objectweb/proactive/core/runtime/jini/JiniRuntimeFactory.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.jini;
00032 
00033 import java.rmi.RemoteException;
00034 
00035 import org.objectweb.proactive.core.ProActiveException;
00036 import org.objectweb.proactive.core.jini.ServiceLocatorHelper;
00037 import org.objectweb.proactive.core.runtime.ProActiveRuntime;
00038 import org.objectweb.proactive.core.runtime.ProActiveRuntimeAdapterImpl;
00039 import org.objectweb.proactive.core.runtime.RemoteProActiveRuntime;
00040 import org.objectweb.proactive.core.runtime.RuntimeFactory;
00041 import org.objectweb.proactive.core.util.UrlBuilder;
00042 
00043 import net.jini.core.discovery.LookupLocator;
00044 import net.jini.core.entry.Entry;
00045 import net.jini.core.lookup.ServiceRegistrar;
00046 import net.jini.core.lookup.ServiceTemplate;
00047 import net.jini.lookup.entry.Name;
00048 
00049 
00050 public class JiniRuntimeFactory extends RuntimeFactory {
00051     protected static int MAX_RETRY = 3;
00052 
00053     //private final static long WAITFOR = 100000L;
00054     protected static ServiceLocatorHelper serviceLocatorHelper = new ServiceLocatorHelper();
00055     private static ProActiveRuntime defaultJiniRuntime = null;
00056 
00057     static {
00058         if (JiniRuntimeFactory.class.getClassLoader() != null) {
00059             if (runtimeLogger.isDebugEnabled()) {
00060                 runtimeLogger.debug("JiniRuntimeFactory created with " +
00061                     JiniRuntimeFactory.class.getClassLoader().getClass()
00062                                             .getName());
00063             }
00064         }
00065     }
00066 
00067     //
00068     // -- CONSTRUCTORS -----------------------------------------------
00069     //
00070     public JiniRuntimeFactory() throws java.io.IOException {
00071         // Obligatoire d'avoir le security manager fixe
00072         if ((System.getSecurityManager() == null) &&
00073                 !("false".equals(System.getProperty("proactive.securitymanager")))) {
00074             System.setSecurityManager(new java.rmi.RMISecurityManager());
00075         }
00076 
00077         //serviceLocatorHelper.initializeServiceLocator();
00078     }
00079 
00080     //
00081     // -- PROTECTED METHODS -----------------------------------------------
00082     //
00083     protected synchronized ProActiveRuntime getProtocolSpecificRuntimeImpl()
00084         throws ProActiveException {
00085         //return createRuntimeAdapter(s,false);
00086         if (defaultJiniRuntime == null) {
00087             serviceLocatorHelper.initializeServiceLocator();
00088             defaultJiniRuntime = createRuntimeAdapter();
00089         }
00090 
00091         return defaultJiniRuntime;
00092     }
00093 
00094     protected ProActiveRuntime getRemoteRuntimeImpl(String s)
00095         throws ProActiveException {
00096         runtimeLogger.info("> JiniRuntimeFactory.getJiniRuntimeImpl(" + s +
00097             ")");
00098 
00099         String host = null;
00100         Entry[] entries;
00101         RemoteProActiveRuntime jiniRuntime = null;
00102         LookupLocator lookup = null;
00103         ServiceRegistrar registrar = null;
00104 
00105         try {
00106             host = UrlBuilder.getHostNameFromUrl(s);
00107             runtimeLogger.info("Try to find the service lookup on host: " +
00108                 host);
00109         } catch (java.net.UnknownHostException e) {
00110             runtimeLogger.fatal("Unable to locate host");
00111             e.printStackTrace();
00112         }
00113 
00114         //ServiceDiscoveryManager clientMgr = null;
00115         if (host != null) {
00116             // recherche unicast
00117             try {
00118                 lookup = new LookupLocator("jini://" + host);
00119                 runtimeLogger.info("Service lookup found ");
00120                 registrar = lookup.getRegistrar();
00121             } catch (java.net.MalformedURLException e) {
00122                 throw new ProActiveException("Lookup failed: " +
00123                     e.getMessage());
00124             } catch (java.io.IOException e) {
00125                 runtimeLogger.error("Registrar search failed: " +
00126                     e.getMessage());
00127 
00128                 if (MAX_RETRY-- > 0) {
00129                     runtimeLogger.info(
00130                         "failed to contact the service lookup, retrying ...");
00131                     getRemoteRuntimeImpl(s);
00132                 } else {
00133                     throw new ProActiveException(
00134                         "Cannot contact the lookup service for node " + s);
00135                 }
00136             } catch (java.lang.ClassNotFoundException e) {
00137                 throw new ProActiveException("Registrar search failed: " +
00138                     e.toString());
00139             }
00140 
00141             Class[] classes = new Class[] { RemoteProActiveRuntime.class };
00142 
00143             entries = new Entry[] { new Name(s) };
00144 
00145             ServiceTemplate template = new ServiceTemplate(null, classes,
00146                     entries);
00147 
00148             try {
00149                 jiniRuntime = (RemoteProActiveRuntime) registrar.lookup(template);
00150 
00151                 if (jiniRuntime == null) {
00152                     throw new ProActiveException("No service found for url: " +
00153                         s);
00154                 }
00155 
00156                 return createRuntimeAdapter(jiniRuntime);
00157             } catch (java.rmi.RemoteException e) {
00158                 throw new ProActiveException(e);
00159 
00160                 //e.printStackTrace();
00161             }
00162         } else {
00163             throw new ProActiveException("node url should not be null");
00164         }
00165 
00166         //    try {
00167         //      // recherche multicast
00168         //      LookupDiscoveryManager mgr = new LookupDiscoveryManager(LookupDiscovery.ALL_GROUPS,
00169         //                    null,null);
00170         //
00171         //      clientMgr = new ServiceDiscoveryManager(mgr,new LeaseRenewalManager());
00172         //    } catch (Exception e) {
00173         //      throw new ProActiveException("Remote",e);
00174         //    }
00175         //    Class[] classes = new Class[] {JiniRuntime.class};
00176         // construction de la template pour la recherche
00177         // on peut ne pas mettre le nom de l'objet
00178         // ensuite on recherche une certaine classe d'objet
00179         //    ServiceItem item = null;
00180         //      try {
00181         //      item = clientMgr.lookup(template, null, WAITFOR);
00182         //    } catch (Exception e) {
00183         //      throw new ProActiveException("Remote",e);
00184         //    }
00185         //
00186         //    if (item == null) {
00187         //      System.out.println("no service found");
00188         //      return null;
00189         //    } else {
00190         //      jiniRuntime = (JiniRuntime) item.service;
00191         //      if (jiniRuntime == null) {
00192         //  System.out.println("item null");
00193         //  return null;
00194         //      }
00195         //      return createRuntimeAdapter(jiniRuntime);
00196         //    }
00197     }
00198 
00199     protected ProActiveRuntimeAdapterImpl createRuntimeAdapter()
00200         throws ProActiveException {
00201         JiniRuntimeImpl impl;
00202 
00203         try {
00204             impl = new JiniRuntimeImpl();
00205         } catch (RemoteException e) {
00206             throw new ProActiveException("Cannot create the JiniRuntimeImpl", e);
00207         }
00208 
00209         return new ProActiveRuntimeAdapterImpl(impl);
00210     }
00211 
00212     public static void setMulticastLocator(boolean multicastLocator) {
00213         serviceLocatorHelper.setMulticastLocator(multicastLocator);
00214     }
00215 }

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