1 31 package org.objectweb.proactive.core.runtime; 32 33 import org.apache.log4j.Logger; 34 35 import org.objectweb.proactive.core.Constants; 36 import org.objectweb.proactive.core.ProActiveException; 37 import org.objectweb.proactive.core.UniqueID; 38 import org.objectweb.proactive.core.config.ProActiveConfiguration; 39 import org.objectweb.proactive.core.rmi.ClassServerHelper; 40 41 42 69 public abstract class RuntimeFactory { 70 protected static Logger logger = Logger.getLogger(RuntimeFactory.class.getName()); 71 72 75 76 private static java.util.HashMap protocolFactoryMapping = new java.util.HashMap (); 77 private static java.util.HashMap instanceFactoryMapping = new java.util.HashMap (); 78 79 public static boolean JINI_ENABLED; 82 public static boolean IBIS_ENABLED; 83 84 static { 85 ProActiveConfiguration.load(); 86 createClassServer(); 87 JINI_ENABLED = isJiniEnabled(); 88 IBIS_ENABLED = isIbisEnabled(); 89 registerProtocolFactories(); 90 } 92 93 97 104 public static synchronized void setFactory(String protocol, 105 String factoryClassName) { 106 if (logger.isDebugEnabled()) { 107 logger.debug("protocol = " + protocol + " " + factoryClassName); 108 } 109 protocolFactoryMapping.put(protocol, factoryClassName); 110 } 111 112 119 public static synchronized void setFactory(String protocol, 120 RuntimeFactory factoryObject) { 121 protocolFactoryMapping.put(protocol, factoryObject.getClass().getName()); 122 instanceFactoryMapping.put(protocol, factoryObject); 123 } 124 125 129 public static boolean isRuntimeLocal(ProActiveRuntime proActiveRuntime) { 130 return proActiveRuntime.getVMInformation().getVMID().equals(UniqueID.getCurrentVMID()); 131 } 132 133 139 public static synchronized ProActiveRuntime getDefaultRuntime() 140 throws ProActiveException { 141 ProActiveRuntime defaultRuntime = null; 142 try { 143 defaultRuntime = getProtocolSpecificRuntime(System.getProperty( 145 "proactive.communication.protocol") + ":"); 146 if (logger.isDebugEnabled()) { 147 logger.debug("default runtime = " + defaultRuntime.getURL()); 148 } 149 } catch (ProActiveException e) { 150 if (logger.isDebugEnabled()) { 152 logger.debug("Error with the default ProActiveRuntime"); 153 } 154 throw new ProActiveException("Error when getting the default ProActiveRuntime", 155 e); 156 } 157 return defaultRuntime; 158 } 159 160 168 public static ProActiveRuntime getProtocolSpecificRuntime(String protocol) 169 throws ProActiveException { 170 RuntimeFactory factory = getFactory(protocol); 171 ProActiveRuntime proActiveRuntime = factory.getProtocolSpecificRuntimeImpl(); 172 if (proActiveRuntime == null) { 173 throw new ProActiveException( 174 "Cannot create a ProActiveRuntime based on " + protocol); 175 } 176 return proActiveRuntime; 177 } 178 179 187 public static ProActiveRuntime getRuntime(String proActiveRuntimeURL, 188 String protocol) throws ProActiveException { 189 if (logger.isDebugEnabled()) { 190 logger.debug("proActiveRunTimeURL " + proActiveRuntimeURL + " " + 191 protocol); 192 } 193 194 RuntimeFactory factory = getFactory(protocol); 197 198 if (logger.isDebugEnabled()) { 200 logger.debug("factory = " + factory); 201 } 202 return factory.getRemoteRuntimeImpl(proActiveRuntimeURL); 203 } 204 205 209 216 protected abstract ProActiveRuntime getProtocolSpecificRuntimeImpl() 217 throws ProActiveException; 218 219 222 protected abstract ProActiveRuntime getRemoteRuntimeImpl(String s) 223 throws ProActiveException; 224 225 private static void createClassServer() { 229 try { 230 new ClassServerHelper().initializeClassServer(); 231 } catch (Exception e) { 232 if (logger.isInfoEnabled()) { 233 logger.info("Error with the ClassServer : " + e.getMessage()); 234 } 235 } 236 } 237 238 private static void registerProtocolFactories() { 239 if (JINI_ENABLED) { 240 setFactory(Constants.JINI_PROTOCOL_IDENTIFIER, 241 "org.objectweb.proactive.core.runtime.jini.JiniRuntimeFactory"); 242 } 243 if (IBIS_ENABLED) { 244 setFactory(Constants.IBIS_PROTOCOL_IDENTIFIER, 245 "org.objectweb.proactive.core.runtime.ibis.RemoteRuntimeFactory"); 246 } 247 248 setFactory(Constants.RMI_PROTOCOL_IDENTIFIER, 249 "org.objectweb.proactive.core.runtime.rmi.RemoteRuntimeFactory"); 250 } 251 252 private static boolean isJiniEnabled() { 253 try { 254 Class.forName("net.jini.discovery.DiscoveryManagement"); 256 if (logger.isInfoEnabled()) { 257 logger.info("Jini enabled"); 258 } 259 return true; 260 } catch (ClassNotFoundException e) { 261 if (logger.isInfoEnabled()) { 262 logger.info("Jini disabled"); 263 } 264 return false; 265 } 266 } 267 268 private static boolean isIbisEnabled() { 269 try { 270 Class.forName("ibis.rmi.server.UnicastRemoteObject"); 272 if (logger.isInfoEnabled()) { 273 logger.info("Ibis enabled"); 274 } 275 return true; 276 } catch (ClassNotFoundException e) { 277 if (logger.isInfoEnabled()) { 278 logger.info("Ibis disabled"); 279 } 280 return false; 281 } 282 } 283 284 private static RuntimeFactory createRuntimeFactory(Class factoryClass, 285 String protocol) throws ProActiveException { 286 try { 287 RuntimeFactory nf = (RuntimeFactory) factoryClass.newInstance(); 288 instanceFactoryMapping.put(protocol, nf); 289 return nf; 290 } catch (Exception e) { 291 e.printStackTrace(); 292 throw new ProActiveException("Error while creating the factory " + 293 factoryClass.getName() + " for the protocol " + protocol); 294 } 295 } 296 297 private static RuntimeFactory createRuntimeFactory( 298 String factoryClassName, String protocol) throws ProActiveException { 299 Class factoryClass = null; 300 if (logger.isDebugEnabled()) { 301 logger.debug("factoryClassName " + factoryClassName); 302 } 303 try { 304 factoryClass = Class.forName(factoryClassName); 305 } catch (ClassNotFoundException e) { 306 e.printStackTrace(); 307 throw new ProActiveException( 308 "Error while getting the class of the factory " + 309 factoryClassName + " for the protocol " + protocol); 310 } 311 return createRuntimeFactory(factoryClass, protocol); 312 } 313 314 private static synchronized RuntimeFactory getFactory(String protocol) 315 throws ProActiveException { 316 if (logger.isDebugEnabled()) { 317 logger.debug("protocol = " + protocol); 318 } 319 320 RuntimeFactory factory = (RuntimeFactory) instanceFactoryMapping.get(protocol); 321 if (factory != null) { 322 return factory; 323 } 324 325 String factoryClassName = (String ) protocolFactoryMapping.get(protocol); 326 if (logger.isDebugEnabled()) { 327 logger.debug("factoryClassName = " + factoryClassName); 328 } 329 if (factoryClassName != null) { 330 return createRuntimeFactory(factoryClassName, protocol); 331 } 332 throw new ProActiveException( 333 "No RuntimeFactory is registered for the protocol " + protocol); 334 } 335 336 340 private static String getProtocol(String proActiveRuntimeURL) { 341 if (proActiveRuntimeURL == null) { 342 return Constants.DEFAULT_PROTOCOL_IDENTIFIER; 343 } 344 345 int n = proActiveRuntimeURL.indexOf("://"); 346 if (n <= 0) { 347 return Constants.DEFAULT_PROTOCOL_IDENTIFIER; 348 } 349 return proActiveRuntimeURL.substring(0, n + 1); 350 } 351 352 354 private static String removeProtocol(String url, String protocol) { 355 if (url.startsWith(protocol)) { 356 return url.substring(protocol.length()); 357 } 358 return url; 359 } 360 } 361 | Popular Tags |