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.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
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
00069
00070 public JiniRuntimeFactory() throws java.io.IOException {
00071
00072 if ((System.getSecurityManager() == null) &&
00073 !("false".equals(System.getProperty("proactive.securitymanager")))) {
00074 System.setSecurityManager(new java.rmi.RMISecurityManager());
00075 }
00076
00077
00078 }
00079
00080
00081
00082
00083 protected synchronized ProActiveRuntime getProtocolSpecificRuntimeImpl()
00084 throws ProActiveException {
00085
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
00115 if (host != null) {
00116
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
00161 }
00162 } else {
00163 throw new ProActiveException("node url should not be null");
00164 }
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
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 }