1 31 package org.objectweb.proactive.core.runtime.jini; 32 33 import net.jini.core.discovery.LookupLocator; 34 import net.jini.core.entry.Entry; 35 import net.jini.core.lookup.ServiceRegistrar; 36 import net.jini.core.lookup.ServiceTemplate; 37 38 import net.jini.lookup.entry.Name; 39 40 import org.objectweb.proactive.core.ProActiveException; 41 import org.objectweb.proactive.core.jini.ServiceLocatorHelper; 42 import org.objectweb.proactive.core.runtime.ProActiveRuntime; 43 import org.objectweb.proactive.core.runtime.RuntimeFactory; 44 import org.objectweb.proactive.core.util.UrlBuilder; 45 46 47 public class JiniRuntimeFactory extends RuntimeFactory { 48 protected static int MAX_RETRY = 3; 49 private final static long WAITFOR = 100000L; 50 protected java.util.Random random; 51 protected static ServiceLocatorHelper serviceLocatorHelper = new ServiceLocatorHelper(); 52 private static ProActiveRuntime defaultJiniRuntime = null; 53 private int count; 54 55 static { 56 if (JiniRuntimeFactory.class.getClassLoader() != null) { 57 if(logger.isDebugEnabled()){ 58 logger.debug("JiniRuntimeFactory created with " + 59 JiniRuntimeFactory.class.getClassLoader().getClass().getName()); 60 } 61 } 62 } 63 64 public JiniRuntimeFactory() throws java.io.IOException { 68 if ((System.getSecurityManager() == null) && 70 !("false".equals(System.getProperty("proactive.securitymanager")))) { 71 System.setSecurityManager(new java.rmi.RMISecurityManager ()); 72 } 73 random = new java.util.Random (System.currentTimeMillis()); 74 } 76 77 protected synchronized ProActiveRuntime getProtocolSpecificRuntimeImpl() 81 throws ProActiveException { 82 if (defaultJiniRuntime == null) { 84 serviceLocatorHelper.initializeServiceLocator(); 85 defaultJiniRuntime = createRuntimeAdapter(); 86 } 87 88 return defaultJiniRuntime; 89 } 90 91 protected ProActiveRuntime getRemoteRuntimeImpl(String s) 92 throws ProActiveException { 93 logger.info("> JiniRuntimeFactory.getJiniRuntimeImpl(" + s + ")"); 94 String host = null; 95 Entry[] entries; 96 JiniRuntime jiniRuntime = null; 97 LookupLocator lookup = null; 98 ServiceRegistrar registrar = null; 99 try { 100 host = UrlBuilder.getHostNameFromUrl(s); 101 logger.info("Try to find the service lookup on host: " + host); 102 } catch (java.net.UnknownHostException e) { 103 logger.fatal("Unable to locate host"); 104 e.printStackTrace(); 105 } 106 107 if (host != null) { 109 try { 111 lookup = new LookupLocator("jini://" + host); 112 logger.info("Service lookup found "); 113 registrar = lookup.getRegistrar(); 114 } catch (java.net.MalformedURLException e) { 115 throw new ProActiveException("Lookup failed: " + 116 e.getMessage()); 117 } catch (java.io.IOException e) { 118 logger.error("Registrar search failed: " + e.getMessage()); 119 if (MAX_RETRY-- > 0) { 120 logger.info( 121 "failed to contact the service lookup, retrying ..."); 122 getRemoteRuntimeImpl(s); 123 } else { 124 throw new ProActiveException( 125 "Cannot contact the lookup service for node " + s); 126 } 127 } catch (java.lang.ClassNotFoundException e) { 128 throw new ProActiveException("Registrar search failed: " + 129 e.toString()); 130 } 131 Class [] classes = new Class [] { JiniRuntime.class }; 132 133 entries = new Entry[] { new Name(s) }; 134 135 ServiceTemplate template = new ServiceTemplate(null, classes, 136 entries); 137 try { 138 jiniRuntime = (JiniRuntime) registrar.lookup(template); 139 if (jiniRuntime == null) { 140 logger.info("No service found for url: " + s); 141 } 142 return createRuntimeAdapter(jiniRuntime); 143 } catch (java.rmi.RemoteException e) { 144 throw new ProActiveException(e); 145 } 147 } else { 148 throw new ProActiveException("node url should not be null"); 149 } 150 151 } 183 184 protected JiniRuntimeAdapter createRuntimeAdapter(JiniRuntime jiniRuntime) 185 throws ProActiveException { 186 return new JiniRuntimeAdapter(jiniRuntime); 187 } 188 189 protected JiniRuntimeAdapter createRuntimeAdapter() 190 throws ProActiveException { 191 return new JiniRuntimeAdapter(); 192 } 193 194 public static void setMulticastLocator(boolean multicastLocator) { 195 serviceLocatorHelper.setMulticastLocator(multicastLocator); 196 } 197 } 198 | Popular Tags |