org/objectweb/proactive/core/rmi/RegistryHelper.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.rmi;
00032 
00033 
00034 import org.apache.log4j.Logger;
00035 import org.objectweb.proactive.core.util.log.Loggers;
00036 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00037 
00038 
00039 public class RegistryHelper {
00040     static Logger logger = ProActiveLogger.getLogger(Loggers.RMI);
00041     protected final static int DEFAULT_REGISTRY_PORT = 1099;
00042 
00046     protected int registryPortNumber = DEFAULT_REGISTRY_PORT;
00047     protected boolean shouldCreateRegistry = true;
00048     protected boolean registryChecked;
00049     protected static java.rmi.registry.Registry registry;
00050 
00051     //following boolean is used to know which runtime has created RMI registry
00052     protected static boolean registryCreator = false;
00053 
00054     //
00055     // -- Constructors -----------------------------------------------
00056     //
00057     public RegistryHelper() {
00058         String port = System.getProperty("proactive.rmi.port");
00059         if (port != null) {
00060             setRegistryPortNumber(new Integer(port).intValue());
00061         }
00062     }
00063 
00064     //
00065     // -- PUBLIC METHODS -----------------------------------------------
00066     //
00067     public int getRegistryPortNumber() {
00068         return registryPortNumber;
00069     }
00070 
00071     public void setRegistryPortNumber(int v) {
00072         registryPortNumber = v;
00073         registryChecked = false;
00074     }
00075 
00076     public boolean shouldCreateRegistry() {
00077         return shouldCreateRegistry;
00078     }
00079 
00080     public void setShouldCreateRegistry(boolean v) {
00081         shouldCreateRegistry = v;
00082     }
00083 
00084     public synchronized void initializeRegistry()
00085         throws java.rmi.RemoteException {
00086         try {
00087             if (!shouldCreateRegistry) {
00088                 return; // don't bother
00089             }
00090             if (registryChecked) {
00091                 return; // already done for this VM
00092             }
00093             getOrCreateRegistry(registryPortNumber);
00094             registryChecked = true;
00095         } catch (Exception e) {
00096             e.printStackTrace();
00097         }
00098     }
00099 
00100     public static java.rmi.registry.Registry getRegistry() {
00101         return registry;
00102     }
00103 
00104     public static boolean getRegistryCreator() {
00105         return registryCreator;
00106     }
00107 
00108     //
00109     // -- PRIVATE METHODS -----------------------------------------------
00110     // 
00111     private static java.rmi.registry.Registry createRegistry(int port)
00112         throws java.rmi.RemoteException {
00113         registry = java.rmi.registry.LocateRegistry.createRegistry(port);
00114         registryCreator = true;
00115         return registry;
00116     }
00117 
00118     private static java.rmi.registry.Registry detectRegistry(int port) {
00119         // java.rmi.registry.Registry registry = null;
00120         try {
00121             // whether an effective registry exists or not we should get a reference
00122             registry = java.rmi.registry.LocateRegistry.getRegistry(port);
00123             if (registry == null) {
00124                 return null;
00125             }
00126 
00127             // doing a lookup should produce ConnectException if registry doesn't exist
00128             // and no exception or NotBoundException if the registry does exist.
00129             java.rmi.Remote r = registry.lookup("blah!");
00130             logger.info("Detected an existing RMI Registry on port " + port);
00131             return registry;
00132         } catch (java.rmi.NotBoundException e) {
00133             logger.info("Detected an existing RMI Registry on port " + port);
00134             return registry;
00135         } catch (java.rmi.RemoteException e) {
00136             return null;
00137         }
00138     }
00139 
00140     private static java.rmi.registry.Registry getOrCreateRegistry(int port)
00141         throws java.rmi.RemoteException {
00142         registry = detectRegistry(port);
00143         if (registry != null) {
00144             return registry;
00145         }
00146 
00147         // no registry created
00148         try {
00149             registry = createRegistry(port);
00150             logger.info("Created a new registry on port " + port);
00151             return registry;
00152         } catch (java.rmi.RemoteException e) {
00153             // problem to bind the registry : may be somebody created one in the meantime
00154             // try to find the rmi registry one more time
00155             registry = detectRegistry(port);
00156             if (registry != null) {
00157                 return registry;
00158             }
00159             logger.error("Cannot detect an existing RMI Registry on port " +
00160                 port + " nor create one e=" + e);
00161             throw e;
00162         }
00163     }
00164 }

Generated on Mon Jan 22 15:16:09 2007 for ProActive by  doxygen 1.5.1