1 /*
2 * Copyright (c) 1998-2002 The Jgroup Team.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 */
18
19 package jgroup.core.registry;
20
21 import java.rmi.RemoteException;
22 import java.rmi.server.RMIClientSocketFactory;
23 import java.rmi.server.RMIServerSocketFactory;
24
25 import jgroup.core.ConfigManager;
26 import jgroup.core.ConfigurationException;
27 import jgroup.core.JgroupException;
28
29
30 /**
31 * Class <code>RegistryFactory</code> is used to obtain a group proxy
32 * for the dependable registry service, or to create a dependable
33 * registry replica on the local machine. <p>
34 *
35 * Every instance (replica) of the dependable registry service is
36 * associated with a standard RMI registry, which is used to retrieve a
37 * group reference for the replicated remote registries.
38 *
39 * @see jgroup.relacs.registry.RelacsRegistryLocator
40 * @author Alberto Montresor
41 * @author Hein Meling
42 * @since Jgroup 0.8
43 */
44 public final class RegistryFactory
45 {
46
47 ////////////////////////////////////////////////////////////////////////////////////////////
48 // Static section
49 ////////////////////////////////////////////////////////////////////////////////////////////
50
51 /** The registry locator object needed to obtain a registry instance */
52 private static RegistryLocator locator;
53
54
55 /**
56 * Set the registry locator object.
57 */
58 public static void setLocator(RegistryLocator reglocator)
59 {
60 if (reglocator == null)
61 throw new IllegalStateException("Cannot set registry locator to null");
62 locator = reglocator;
63 }
64
65
66 ////////////////////////////////////////////////////////////////////////////////////////////
67 // Constructor
68 ////////////////////////////////////////////////////////////////////////////////////////////
69
70 /**
71 * Private constructor to prevent public construction.
72 */
73 private RegistryFactory() {}
74
75
76 ////////////////////////////////////////////////////////////////////////////////////////////
77 // Get registry methods
78 ////////////////////////////////////////////////////////////////////////////////////////////
79
80 /**
81 * Returns a proxy for the dependable registry service.
82 *
83 * @return
84 * A proxy for the dependable registry service.
85 *
86 * @exception RemoteException
87 * Raised if the proxy could not be obtained.
88 * @exception ConfigurationException
89 * Raised if there was a problem parsing the distributed system
90 * configuration file.
91 */
92 public static DependableRegistry getRegistry()
93 throws RemoteException, JgroupException
94 {
95 ConfigManager.init();
96 if (locator == null)
97 throw new IllegalStateException("Registry locator not yet initialized");
98 return locator.getRegistry();
99 }
100
101 /**
102 * @return
103 * Returns an interface clients can use to obtain
104 * a remote reference from registry.
105 */
106 public static LookupRegistry getLookupRegistry()
107 throws RemoteException, JgroupException
108 {
109 return locator.getLookupRegistry();
110 }
111
112 /**
113 * Returns a proxy for the dependable registry service.
114 *
115 * Communication with this remote registry will use the supplied
116 * <code>RMIClientSocketFactory</code> to create socket connections
117 * to the dependable registry on the distributed system and port.
118 *
119 * @param csf
120 * Client-side socket factory used to make connections to the
121 * dependable registry. If <code>csf</code> is null, then the
122 * default client-side socket factory will be used.
123 *
124 * @return
125 * A proxy for the dependable registry service.
126 *
127 * @exception RemoteException
128 * Raised if the proxy could not be obtained.
129 * @exception ConfigurationException
130 * Raised if there was a problem parsing the distributed system
131 * configuration file.
132 */
133 public static DependableRegistry getRegistry(RMIClientSocketFactory csf)
134 throws RemoteException, JgroupException
135 {
136 ConfigManager.init();
137 if (locator == null)
138 throw new IllegalStateException("Registry locator not yet initialized");
139 return locator.getRegistry(csf);
140 }
141
142
143 ////////////////////////////////////////////////////////////////////////////////////////////
144 // Create registry methods
145 ////////////////////////////////////////////////////////////////////////////////////////////
146
147 /**
148 * Creates and exports a dependable registry replica on the local
149 * host listening for incoming requests on an the port specified in
150 * the system configuration file. <p>
151 *
152 * @return
153 * The newly created dependable registry replica.
154 *
155 * @exception RemoteException
156 * Raised if the registry could not be exported.
157 * @exception JgroupException
158 * Raised if a registry instance could not be created.
159 * @exception ConfigurationException
160 * Raised if there was a problem parsing the distributed system
161 * configuration file.
162 */
163 public static DependableRegistry createRegistry()
164 throws RemoteException, JgroupException
165 {
166 ConfigManager.init();
167 if (locator == null)
168 throw new IllegalStateException("Registry locator not yet initialized");
169 return locator.createRegistry();
170 }
171
172
173 /**
174 * Creates and exports a dependable registry replica on the local
175 * host listening for incoming requests on an the port specified in
176 * the system configuration file. <p>
177 *
178 * The new instance of the dependable registry listens to incoming
179 * requests using a <code>ServerSocket</code> created from the
180 * supplied <code>RMIServerSocketFactory</code>. A client that
181 * receives a reference to this registry will then use a
182 * <code>Socket</code> created from the supplied
183 * <code>RMIClientSocketFactory</code>.
184 *
185 * @param csf
186 * Client-side <code>Socket</code> factory used to make connections
187 * to the registry.
188 * @param ssf
189 * Server-side <code>ServerSocket</code> factory used to accept
190 * connections to the registry.
191 *
192 * @return
193 * The newly created dependable registry replica.
194 *
195 * @exception RemoteException
196 * Raised if the registry could not be exported.
197 * @exception JgroupException
198 * Raised if a registry instance could not be created.
199 * @exception ConfigurationException
200 * Raised if there was a problem parsing the distributed system
201 * configuration file.
202 */
203 public static DependableRegistry createRegistry(RMIClientSocketFactory csf, RMIServerSocketFactory ssf)
204 throws RemoteException, JgroupException
205 {
206 ConfigManager.init();
207 if (locator == null)
208 throw new IllegalStateException("Registry locator not yet initialized");
209 return locator.createRegistry(csf, ssf);
210 }
211
212 } // END RegistryFactory