KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > rmi > RegistryHelper


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

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     /**
41      * settings of the registry
42      */

43     protected int registryPortNumber = DEFAULT_REGISTRY_PORT;
44     protected boolean shouldCreateRegistry = true;
45     protected boolean registryChecked;
46     protected static java.rmi.registry.Registry JavaDoc registry;
47
48     //following boolean is used to know which runtime has created RMI registry
49
protected static boolean registryCreator = false;
50
51     //
52
// -- Constructors -----------------------------------------------
53
//
54
public RegistryHelper() {
55         String JavaDoc port = System.getProperty("proactive.rmi.port");
56         if (port != null) {
57             setRegistryPortNumber(new Integer JavaDoc(port).intValue());
58         }
59     }
60
61     //
62
// -- PUBLIC METHODS -----------------------------------------------
63
//
64
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 JavaDoc {
83         try {
84             if (!shouldCreateRegistry) {
85                 return; // don't bother
86
}
87             if (registryChecked) {
88                 return; // already done for this VM
89
}
90             getOrCreateRegistry(registryPortNumber);
91             registryChecked = true;
92         } catch (Exception JavaDoc e) {
93             e.printStackTrace();
94         }
95     }
96
97    
98     public static java.rmi.registry.Registry JavaDoc getRegistry() {
99         return registry;
100     }
101
102     public static boolean getRegistryCreator() {
103         return registryCreator;
104     }
105
106     //
107
// -- PRIVATE METHODS -----------------------------------------------
108
//
109
private static java.rmi.registry.Registry JavaDoc createRegistry(int port)
110         throws java.rmi.RemoteException JavaDoc {
111         registry = java.rmi.registry.LocateRegistry.createRegistry(port);
112         registryCreator = true;
113         return registry;
114     }
115
116     private static java.rmi.registry.Registry JavaDoc detectRegistry(int port) {
117         // java.rmi.registry.Registry registry = null;
118
try {
119             // whether an effective registry exists or not we should get a reference
120
registry = java.rmi.registry.LocateRegistry.getRegistry(port);
121             if (registry == null) {
122                 return null;
123             }
124
125             // doing a lookup should produce ConnectException if registry doesn't exist
126
// and no exception or NotBoundException if the registry does exist.
127
java.rmi.Remote JavaDoc r = registry.lookup("blah!");
128             logger.info("Detected an existing RMI Registry on port " + port);
129             return registry;
130         } catch (java.rmi.NotBoundException JavaDoc e) {
131             logger.info("Detected an existing RMI Registry on port " + port);
132             return registry;
133         } catch (java.rmi.RemoteException JavaDoc e) {
134             return null;
135         }
136     }
137
138     private static java.rmi.registry.Registry JavaDoc getOrCreateRegistry(int port)
139         throws java.rmi.RemoteException JavaDoc {
140         registry = detectRegistry(port);
141         if (registry != null) {
142             return registry;
143         }
144
145         // no registry created
146
try {
147             registry = createRegistry(port);
148             logger.info("Created a new registry on port " + port);
149             return registry;
150         } catch (java.rmi.RemoteException JavaDoc e) {
151             // problem to bind the registry : may be somebody created one in the meantime
152
// try to find the rmi registry one more time
153
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