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.jini;
00032
00033 import java.io.File;
00034 import java.io.IOException;
00035
00036 import org.apache.log4j.Logger;
00037 import org.objectweb.proactive.core.runtime.RemoteProActiveRuntime;
00038 import org.objectweb.proactive.core.util.UrlBuilder;
00039 import org.objectweb.proactive.core.util.log.Loggers;
00040 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00041
00042 import net.jini.core.discovery.LookupLocator;
00043 import net.jini.core.lookup.ServiceMatches;
00044 import net.jini.core.lookup.ServiceRegistrar;
00045 import net.jini.core.lookup.ServiceTemplate;
00046 import net.jini.discovery.DiscoveryEvent;
00047 import net.jini.discovery.DiscoveryListener;
00048 import net.jini.discovery.LookupDiscovery;
00049
00050
00063 public class ServiceLocatorHelper implements DiscoveryListener {
00064 protected static Logger logger = ProActiveLogger.getLogger(Loggers.JINI);
00065 protected static int MAX_RETRY = 8;
00066 protected static long MAX_WAIT = 10000L;
00067 private static String DEFAULT_POLICY = System.getProperty("user.dir") +
00068 System.getProperty("file.separator") + "proactive.java.policy";
00069 private static final String FILE_SEPARATOR = System.getProperty(
00070 "file.separator");
00071 private static String policy;
00072 private static String DEFAULT_RMID_LOCATION = System.getProperty(
00073 "java.home") + FILE_SEPARATOR + "bin" + FILE_SEPARATOR + "rmid";
00074 private static String DEFAULT_RMID_PARAMS = "-J-Djava.security.policy=";
00075 protected static LookupLocator lookup = null;
00076 protected static ServiceRegistrar registrar = null;
00077
00081 protected boolean shouldCreateServiceLocator = true;
00082 protected boolean locatorChecked;
00083 protected static boolean multicastLocator = false;
00084 private static String host = null;
00085
00086 static {
00087 try {
00088 host = UrlBuilder.getHostNameorIP(java.net.InetAddress.getLocalHost());
00089 String policyLocation = System.getProperty("java.security.policy");
00090 if (policyLocation != null) {
00091 policy = getAbsolutePath(policyLocation);
00092 } else {
00093 policy = DEFAULT_POLICY;
00094 }
00095 DEFAULT_RMID_PARAMS = DEFAULT_RMID_PARAMS.concat(policy);
00096 } catch (java.net.UnknownHostException e) {
00097 logger.fatal("Lookup failed: " + e.getMessage());
00098 e.printStackTrace();
00099 System.exit(1);
00100 }
00101 }
00102
00103 private static String grpName = "public";
00104 private static final String tmpDir = createTempDirectory(host);
00105 private static java.io.File jiniLockFile = null;
00106 private static final String jiniLockFileLocation = System.getProperty(
00107 "user.dir") + System.getProperty("file.separator") + host +
00108 "jiniLockFile";
00109
00110
00111
00112
00113 public ServiceLocatorHelper() {
00114 if (logger.isDebugEnabled()) {
00115 logger.debug("ServiceLocatorHelper() constructor");
00116 }
00117 }
00118
00119
00120
00121
00122 public boolean shouldCreateServiceLocator() {
00123 return shouldCreateServiceLocator;
00124 }
00125
00126 public void setShouldCreateServiceLocator(boolean v) {
00127 shouldCreateServiceLocator = v;
00128 }
00129
00134 public void setMulticastLocator(boolean v) {
00135 ServiceLocatorHelper.multicastLocator = v;
00136 }
00137
00141 public synchronized void initializeServiceLocator() {
00142 if (!shouldCreateServiceLocator) {
00143 return;
00144 }
00145 if (locatorChecked) {
00146 return;
00147 }
00148 try {
00149 getOrCreateServiceLocator();
00150
00151 if (jiniLockFile != null) {
00152 if (jiniLockFile.exists()) {
00153 jiniLockFile.delete();
00154 }
00155 }
00156 } catch (java.io.IOException e) {
00157 if (jiniLockFile != null) {
00158 if (jiniLockFile.exists()) {
00159 jiniLockFile.delete();
00160 }
00161 }
00162 e.printStackTrace();
00163 System.exit(1);
00164 }
00165 locatorChecked = true;
00166 }
00167
00168
00169
00170
00171
00175 public void discovered(DiscoveryEvent evt) {
00176
00177
00178
00179 ServiceRegistrar[] registrars = evt.getRegistrars();
00180
00181
00182
00183
00184 for (int n = 0; n < registrars.length; n++) {
00185 ServiceLocatorHelper.registrar = registrars[n];
00186
00187 }
00188
00189
00190
00191
00192 }
00193
00197 public void discarded(DiscoveryEvent evt) {
00198
00199
00200
00201 }
00202
00203
00204
00205
00206
00211 protected static void delDirectory(java.io.File dir) {
00212 java.io.File[] files = dir.listFiles();
00213 if (files != null) {
00214 for (int i = 0; i < files.length; i++) {
00215 delDirectory(files[i]);
00216 }
00217 }
00218 logger.info("deleting " + dir.getPath() + " ...");
00219 dir.delete();
00220 if (dir.exists()) {
00221 logger.warn("We cannot delete this file : " + dir.getPath());
00222 logger.warn(
00223 "... You should delete it before running a new ServiceLocator ...");
00224 }
00225 }
00226
00227
00228
00229
00230
00235 private void displayServices() {
00236 try {
00237
00238 logger.info(">> found a service locator (registrar) : " +
00239 ServiceLocatorHelper.registrar);
00240 logger.info(">> >> ServiceID : " +
00241 ServiceLocatorHelper.registrar.getServiceID());
00242
00243 logger.info(">> >> >> Groups : ");
00244
00245 String[] groups = ServiceLocatorHelper.registrar.getGroups();
00246 for (int i = 0; i < groups.length; i++) {
00247 logger.info(">> >> >> >> " + i + ") " + groups[i]);
00248 }
00249
00250 logger.info(">> >> >> Locator : " +
00251 ServiceLocatorHelper.registrar.getLocator());
00252
00253 ServiceTemplate template = new ServiceTemplate(null,
00254 new Class[] { RemoteProActiveRuntime.class }, null);
00255 ServiceMatches matches = ServiceLocatorHelper.registrar.lookup(template,
00256 Integer.MAX_VALUE);
00257
00258 logger.info(">> >> >> " + matches.items.length + " required ");
00259 logger.info(">> >> >> " + matches.totalMatches + " founded ");
00260
00261 for (int i = 0; i < matches.items.length; i++) {
00262 logger.info(">> >> >> >> Object (" + i + ") found : ");
00263 logger.info(">> >> >> >> >> ID : " +
00264 matches.items[i].serviceID);
00265 logger.info(">> >> >> >> >> Service : " +
00266 matches.items[i].service);
00267 logger.info(">> >> >> >> >> Attributs :");
00268
00269 for (int j = 0; j < matches.items[i].attributeSets.length;
00270 j++) {
00271 logger.info(">> >> >> >> >> >> Attr : " +
00272 matches.items[i].attributeSets[j]);
00273 }
00274
00275 logger.info(
00276 "--------------------------------------------------------------------------------------");
00277 }
00278 } catch (java.rmi.RemoteException e) {
00279 e.printStackTrace();
00280 }
00281 }
00282
00283 private static String createTempDirectory(String host) {
00284 try {
00285 java.io.File fTmp = java.io.File.createTempFile("proactive-",
00286 "-" + host);
00287 String tmpDirPath = fTmp.getAbsolutePath();
00288
00289
00290
00291
00292 return tmpDirPath;
00293 } catch (Exception e) {
00294 logger.fatal("Cannot create the TEMP directory : " + e.toString());
00295 e.printStackTrace();
00296 System.exit(1);
00297 return null;
00298 }
00299 }
00300
00305 private void getOrCreateServiceLocator() throws java.io.IOException {
00306 if ((System.getSecurityManager() == null) &&
00307 !("false".equals(System.getProperty("proactive.securitymanager")))) {
00308 System.setSecurityManager(new java.rmi.RMISecurityManager());
00309 }
00310 if (multicastLocator) {
00311
00312 LookupDiscovery discover = new LookupDiscovery(LookupDiscovery.ALL_GROUPS);
00313 discover.addDiscoveryListener(this);
00314
00315 try {
00316 Thread.sleep(MAX_WAIT);
00317 if (ServiceLocatorHelper.registrar == null) {
00318 createServiceLocator();
00319 }
00320 } catch (InterruptedException e) {
00321 }
00322 } else {
00323
00324 logger.info("Lookup : jini://" + host);
00325 try {
00326 lookup = new LookupLocator("jini://" + host);
00327 logger.info("Lookup.getRegistrar() on " + host);
00328 ServiceLocatorHelper.registrar = lookup.getRegistrar();
00329 } catch (java.net.MalformedURLException e) {
00330 throw new java.io.IOException("Lookup failed: " +
00331 e.getMessage());
00332 } catch (java.io.IOException e) {
00333 logger.error("Registrar search failed: " + e.getMessage());
00334 if (MAX_RETRY-- > 0) {
00335
00336 String[] env = new String[2];
00337
00338 env[0] = DEFAULT_RMID_LOCATION;
00339 env[1] = DEFAULT_RMID_PARAMS;
00340 Runtime.getRuntime().exec(env);
00341
00342 createServiceLocator();
00343 getOrCreateServiceLocator();
00344 } else {
00345
00346 if (jiniLockFile.exists()) {
00347 jiniLockFile.delete();
00348 }
00349 throw new java.io.IOException(
00350 "\nCannot run a ServiceLocator : Have you launched the rmid deamon on " +
00351 host);
00352 }
00353 } catch (java.lang.ClassNotFoundException e) {
00354 if (jiniLockFile.exists()) {
00355 jiniLockFile.delete();
00356 }
00357 throw new java.io.IOException("Registrar search failed: " +
00358 e.toString());
00359 }
00360
00361 logger.info("Registrar found on " + host);
00362
00363
00364
00365 }
00366 }
00367
00371 private static void createServiceLocator() {
00372
00373
00374
00375
00376 if (!createLockFile()) {
00377 try {
00378 Thread.sleep(2000);
00379 } catch (Exception e) {
00380 e.printStackTrace();
00381 }
00382 return;
00383 }
00384 logger.info("creating lock file");
00385 logger.info(
00386 "No ServiceLocator founded ... we launch a ServiceLocator on " +
00387 host);
00388 String reggieTmpDir = tmpDir + System.getProperty("file.separator") +
00389 "reggie_log";
00390 delDirectory(new java.io.File(tmpDir));
00391 java.io.File directory = new java.io.File(tmpDir);
00392 directory.mkdirs();
00393
00394 if (logger.isDebugEnabled()) {
00395
00396 logger.debug(
00397 "We don't use a ClassServer for the service Locator (we use the CLASSPATH)");
00398 }
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409 String[] args = { "", policy, reggieTmpDir, };
00410 com.sun.jini.start.ServiceStarter.create(args,
00411 "com.sun.jini.reggie.CreateLookup",
00412 "com.sun.jini.reggie.RegistrarImpl", "lookup");
00413 }
00414
00420 private static boolean createLockFile() {
00421 jiniLockFile = new java.io.File(jiniLockFileLocation);
00422
00423 try {
00424 return jiniLockFile.createNewFile();
00425 } catch (java.io.IOException e) {
00426
00427 return true;
00428 }
00429 }
00430
00431 private static String getAbsolutePath(String path) {
00432 if (path.startsWith("file:")) {
00433
00434 path = path.substring(5);
00435 }
00436 try {
00437 return new File(path).getCanonicalPath();
00438 } catch (IOException e) {
00439 logger.error(e.getMessage());
00440 return path;
00441 }
00442 }
00443 }