1 31 package org.objectweb.proactive.core.jini; 32 33 import java.io.File ; 34 import java.io.IOException ; 35 36 import net.jini.core.discovery.LookupLocator; 37 import net.jini.core.lookup.ServiceMatches; 38 import net.jini.core.lookup.ServiceRegistrar; 39 import net.jini.core.lookup.ServiceTemplate; 40 41 import net.jini.discovery.DiscoveryEvent; 42 import net.jini.discovery.DiscoveryListener; 43 import net.jini.discovery.LookupDiscovery; 44 45 import org.apache.log4j.Logger; 46 47 import org.objectweb.proactive.core.runtime.jini.JiniRuntime; 48 49 50 62 public class ServiceLocatorHelper implements DiscoveryListener { 63 protected static Logger logger = Logger.getLogger(ServiceLocatorHelper.class.getName()); 64 protected static int MAX_RETRY = 8; 65 protected static long MAX_WAIT = 10000L; 66 private static String DEFAULT_POLICY = System.getProperty("user.dir") + 67 System.getProperty("file.separator") + "proactive.java.policy"; 68 private static final String FILE_SEPARATOR = System.getProperty( 69 "file.separator"); 70 private static String policy; 71 private static String DEFAULT_RMID_LOCATION = System.getProperty( 72 "java.home") + FILE_SEPARATOR + "bin" + FILE_SEPARATOR + "rmid"; 73 private static String DEFAULT_RMID_PARAMS = "-J-Djava.security.policy="; 74 protected static LookupLocator lookup = null; 75 protected static ServiceRegistrar registrar = null; 76 77 80 protected boolean shouldCreateServiceLocator = true; 81 protected boolean locatorChecked; 82 protected static boolean multicastLocator = false; 83 private static String host = null; 84 85 static { 86 try { 87 host = java.net.InetAddress.getLocalHost().getCanonicalHostName(); 88 String policyLocation = System.getProperty("java.security.policy"); 89 if(policyLocation != null) policy = getAbsolutePath(policyLocation); 90 else policy = DEFAULT_POLICY; 91 DEFAULT_RMID_PARAMS = DEFAULT_RMID_PARAMS.concat(policy); 92 } catch (java.net.UnknownHostException e) { 93 logger.fatal("Lookup failed: " + e.getMessage()); 94 e.printStackTrace(); 95 System.exit(1); 96 } 97 } 98 99 private static String grpName = "public"; 100 private static final String tmpDir = createTempDirectory(host); 101 private static java.io.File jiniLockFile = null; 102 private static final String jiniLockFileLocation = System.getProperty( 103 "user.dir") + System.getProperty("file.separator") + host + 104 "jiniLockFile"; 105 106 public ServiceLocatorHelper() { 110 if (logger.isDebugEnabled()) { 111 logger.debug("ServiceLocatorHelper() constructor"); 112 } 113 } 114 115 public boolean shouldCreateServiceLocator() { 119 return shouldCreateServiceLocator; 120 } 121 122 public void setShouldCreateServiceLocator(boolean v) { 123 shouldCreateServiceLocator = v; 124 } 125 126 130 public void setMulticastLocator(boolean v) { 131 ServiceLocatorHelper.multicastLocator = v; 132 } 133 134 137 public synchronized void initializeServiceLocator() { 138 if (!shouldCreateServiceLocator) { 139 return; } 141 if (locatorChecked) { 142 return; } 144 try { 145 getOrCreateServiceLocator(); 146 if (jiniLockFile != null) { 148 if (jiniLockFile.exists()) { 149 jiniLockFile.delete(); 150 } 151 } 152 } catch (java.io.IOException e) { 153 if (jiniLockFile != null) { 154 if (jiniLockFile.exists()) { 155 jiniLockFile.delete(); 156 } 157 } 158 e.printStackTrace(); 159 System.exit(1); 160 } 161 locatorChecked = true; 162 } 163 164 168 171 public void discovered(DiscoveryEvent evt) { 172 ServiceRegistrar[] registrars = evt.getRegistrars(); 176 177 for (int n = 0; n < registrars.length; n++) { 181 ServiceLocatorHelper.registrar = registrars[n]; 182 } 184 185 } 189 190 193 public void discarded(DiscoveryEvent evt) { 194 } 198 199 203 207 protected static void delDirectory(java.io.File dir) { 208 java.io.File [] files = dir.listFiles(); 209 if (files != null) { 210 for (int i = 0; i < files.length; i++) { 211 delDirectory(files[i]); 212 } 213 } 214 logger.info("deleting " + dir.getPath() + " ..."); 215 dir.delete(); 216 if (dir.exists()) { 217 logger.warn("We cannot delete this file : " + dir.getPath()); 218 logger.warn( 219 "... You should delete it before running a new ServiceLocator ..."); 220 } 221 } 222 223 227 231 private void displayServices() { 232 try { 233 logger.info(">> found a service locator (registrar) : " + 235 ServiceLocatorHelper.registrar); 236 logger.info(">> >> ServiceID : " + 237 ServiceLocatorHelper.registrar.getServiceID()); 238 239 logger.info(">> >> >> Groups : "); 240 241 String [] groups = ServiceLocatorHelper.registrar.getGroups(); 242 for (int i = 0; i < groups.length; i++) { 243 logger.info(">> >> >> >> " + i + ") " + groups[i]); 244 } 245 246 logger.info(">> >> >> Locator : " + 247 ServiceLocatorHelper.registrar.getLocator()); 248 249 ServiceTemplate template = new ServiceTemplate(null, 250 new Class [] { JiniRuntime.class }, null); 251 ServiceMatches matches = ServiceLocatorHelper.registrar.lookup(template, 252 Integer.MAX_VALUE); 253 254 logger.info(">> >> >> " + matches.items.length + " required "); 255 logger.info(">> >> >> " + matches.totalMatches + " founded "); 256 257 for (int i = 0; i < matches.items.length; i++) { 258 logger.info(">> >> >> >> Object (" + i + ") found : "); 259 logger.info(">> >> >> >> >> ID : " + 260 matches.items[i].serviceID); 261 logger.info(">> >> >> >> >> Service : " + 262 matches.items[i].service); 263 logger.info(">> >> >> >> >> Attributs :"); 264 265 for (int j = 0; j < matches.items[i].attributeSets.length; 266 j++) { 267 logger.info(">> >> >> >> >> >> Attr : " + 268 matches.items[i].attributeSets[j]); 269 } 270 271 logger.info( 272 "--------------------------------------------------------------------------------------"); 273 } 274 } catch (java.rmi.RemoteException e) { 275 e.printStackTrace(); 276 } 277 } 278 279 private static String createTempDirectory(String host) { 280 try { 281 java.io.File fTmp = java.io.File.createTempFile("proactive-", 282 "-" + host); 283 String tmpDirPath = fTmp.getAbsolutePath(); 284 285 return tmpDirPath; 289 } catch (Exception e) { 290 logger.fatal("Cannot create the TEMP directory : " + e.toString()); 291 e.printStackTrace(); 292 System.exit(1); 293 return null; 294 } 295 } 296 297 301 private void getOrCreateServiceLocator() throws java.io.IOException { 302 if ((System.getSecurityManager() == null) && 303 !("false".equals(System.getProperty("proactive.securitymanager")))) { 304 System.setSecurityManager(new java.rmi.RMISecurityManager ()); 305 } 306 if (multicastLocator) { 307 LookupDiscovery discover = new LookupDiscovery(LookupDiscovery.ALL_GROUPS); 309 discover.addDiscoveryListener(this); 310 try { 312 Thread.sleep(MAX_WAIT); 313 if (ServiceLocatorHelper.registrar == null) { 314 createServiceLocator(); 315 } 316 } catch (InterruptedException e) { 317 } 318 } else { 319 logger.info("Lookup : jini://" + host); 321 try { 322 lookup = new LookupLocator("jini://" + host); 323 logger.info("Lookup.getRegistrar() on " + host); 324 ServiceLocatorHelper.registrar = lookup.getRegistrar(); 325 } catch (java.net.MalformedURLException e) { 326 throw new java.io.IOException ("Lookup failed: " + 327 e.getMessage()); 328 } catch (java.io.IOException e) { 329 logger.error("Registrar search failed: " + e.getMessage()); 330 if (MAX_RETRY-- > 0) { 331 String [] env = new String [2]; 333 334 env[0] = DEFAULT_RMID_LOCATION; 335 env[1] = DEFAULT_RMID_PARAMS; 336 Runtime.getRuntime().exec(env); 337 338 createServiceLocator(); 339 getOrCreateServiceLocator(); 340 } else { 341 if (jiniLockFile.exists()) { 343 jiniLockFile.delete(); 344 } 345 throw new java.io.IOException ( 346 "\nCannot run a ServiceLocator : Have you launched the rmid deamon on " + 347 host); 348 } 349 } catch (java.lang.ClassNotFoundException e) { 350 if (jiniLockFile.exists()) { 351 jiniLockFile.delete(); 352 } 353 throw new java.io.IOException ("Registrar search failed: " + 354 e.toString()); 355 } 356 357 logger.info("Registrar found on " + host); 358 359 } 362 } 363 364 367 private static void createServiceLocator() { 368 if (!createLockFile()) { 373 try { 374 Thread.sleep(2000); 375 } catch (Exception e) { 376 e.printStackTrace(); 377 } 378 return; 379 } 380 logger.info("creating lock file"); 381 logger.info( 382 "No ServiceLocator founded ... we launch a ServiceLocator on " + 383 host); 384 String reggieTmpDir = tmpDir + System.getProperty("file.separator") + 385 "reggie_log"; 386 delDirectory(new java.io.File (tmpDir)); 387 java.io.File directory = new java.io.File (tmpDir); 388 directory.mkdirs(); 389 390 if (logger.isDebugEnabled()) { 391 logger.debug( 393 "We don't use a ClassServer for the service Locator (we use the CLASSPATH)"); 394 } 395 396 String [] args = { "", policy, reggieTmpDir, }; 406 com.sun.jini.start.ServiceStarter.create(args, 407 "com.sun.jini.reggie.CreateLookup", 408 "com.sun.jini.reggie.RegistrarImpl", "lookup"); 409 } 410 411 416 private static boolean createLockFile() { 417 jiniLockFile = new java.io.File (jiniLockFileLocation); 418 try { 420 return jiniLockFile.createNewFile(); 421 } catch (java.io.IOException e) { 422 return true; 424 } 425 } 426 427 private static String getAbsolutePath(String path) { 428 if (path.startsWith("file:")) { 429 path = path.substring(5); 431 } 432 try { 433 return new File (path).getCanonicalPath(); 434 } catch (IOException e) { 435 logger.error(e.getMessage()); 436 return path; 437 } 438 } 439 } 440 | Popular Tags |