1 31 package org.objectweb.proactive.core.rmi; 32 33 import org.apache.log4j.Logger; 34 35 36 public class RegistryHelper { 37 protected static Logger logger = Logger.getLogger(RegistryHelper.class.getName()); 38 protected final static int DEFAULT_REGISTRY_PORT = 1099; 39 40 43 protected int registryPortNumber = DEFAULT_REGISTRY_PORT; 44 protected boolean shouldCreateRegistry = true; 45 protected boolean registryChecked; 46 protected static java.rmi.registry.Registry registry; 47 48 protected static boolean registryCreator = false; 50 51 public RegistryHelper() { 55 String port = System.getProperty("proactive.rmi.port"); 56 if (port != null) { 57 setRegistryPortNumber(new Integer (port).intValue()); 58 } 59 } 60 61 public int getRegistryPortNumber() { 65 return registryPortNumber; 66 } 67 68 public void setRegistryPortNumber(int v) { 69 registryPortNumber = v; 70 registryChecked = false; 71 } 72 73 public boolean shouldCreateRegistry() { 74 return shouldCreateRegistry; 75 } 76 77 public void setShouldCreateRegistry(boolean v) { 78 shouldCreateRegistry = v; 79 } 80 81 public synchronized void initializeRegistry() 82 throws java.rmi.RemoteException { 83 try { 84 if (!shouldCreateRegistry) { 85 return; } 87 if (registryChecked) { 88 return; } 90 getOrCreateRegistry(registryPortNumber); 91 registryChecked = true; 92 } catch (Exception e) { 93 e.printStackTrace(); 94 } 95 } 96 97 98 public static java.rmi.registry.Registry getRegistry() { 99 return registry; 100 } 101 102 public static boolean getRegistryCreator() { 103 return registryCreator; 104 } 105 106 private static java.rmi.registry.Registry createRegistry(int port) 110 throws java.rmi.RemoteException { 111 registry = java.rmi.registry.LocateRegistry.createRegistry(port); 112 registryCreator = true; 113 return registry; 114 } 115 116 private static java.rmi.registry.Registry detectRegistry(int port) { 117 try { 119 registry = java.rmi.registry.LocateRegistry.getRegistry(port); 121 if (registry == null) { 122 return null; 123 } 124 125 java.rmi.Remote r = registry.lookup("blah!"); 128 logger.info("Detected an existing RMI Registry on port " + port); 129 return registry; 130 } catch (java.rmi.NotBoundException e) { 131 logger.info("Detected an existing RMI Registry on port " + port); 132 return registry; 133 } catch (java.rmi.RemoteException e) { 134 return null; 135 } 136 } 137 138 private static java.rmi.registry.Registry getOrCreateRegistry(int port) 139 throws java.rmi.RemoteException { 140 registry = detectRegistry(port); 141 if (registry != null) { 142 return registry; 143 } 144 145 try { 147 registry = createRegistry(port); 148 logger.info("Created a new registry on port " + port); 149 return registry; 150 } catch (java.rmi.RemoteException e) { 151 registry = detectRegistry(port); 154 if (registry != null) { 155 return registry; 156 } 157 logger.error("Cannot detect an existing RMI Registry on port " + 158 port + " nor create one e=" + e); 159 throw e; 160 } 161 } 162 } 163 | Popular Tags |