KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > jndi > registry > ManageableRegistry


1 /**
2  * Copyright (C) 2004,2005 - INRIA (www.inria.fr)
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is developed inside the ObjectWeb Consortium,
7  * http://www.objectweb.org
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  * --------------------------------------------------------------------------
25  * $Id: ManageableRegistry.java,v 1.5 2005/03/15 09:55:13 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.jndi.registry;
29
30 import java.rmi.AccessException JavaDoc;
31 import java.rmi.AlreadyBoundException JavaDoc;
32 import java.rmi.NotBoundException JavaDoc;
33 import java.rmi.Remote JavaDoc;
34 import java.rmi.RemoteException JavaDoc;
35 import java.rmi.registry.Registry JavaDoc;
36 import java.rmi.server.RMIClientSocketFactory JavaDoc;
37 import java.rmi.server.RMIServerSocketFactory JavaDoc;
38 import java.rmi.server.RMISocketFactory JavaDoc;
39 import java.rmi.server.ServerNotActiveException JavaDoc;
40 import java.util.Enumeration JavaDoc;
41 import java.util.Hashtable JavaDoc;
42
43 import sun.rmi.registry.RegistryImpl;
44
45 /**
46  * JRMP Registry without checks for bind
47  * @author Guillaume Rivière
48  */

49 public class ManageableRegistry extends RegistryImpl {
50
51     /**
52      * Initial capacity of the hashtable
53      */

54     private static final int INITIAL_CAPACITY = 101;
55
56     /**
57      * Hashtable containing objects of the registry
58      */

59     private Hashtable JavaDoc registryObjects = new Hashtable JavaDoc(INITIAL_CAPACITY);
60
61     /**
62      * Verbosity
63      */

64     private static boolean verbose = false;
65
66     /**
67      * Build a new registry on a given port
68      * @param port given port number
69      * @param csf client socket factory
70      * @param ssf server socket factory
71      * @throws RemoteException if the registry cannot be built
72      */

73     private ManageableRegistry(int port, RMIClientSocketFactory JavaDoc csf, RMIServerSocketFactory JavaDoc ssf) throws RemoteException JavaDoc {
74         super(port, csf, ssf);
75     }
76
77     /**
78      * Build a new registry on a given port
79      * @param port given port number
80      * @throws RemoteException if the registry cannot be built
81      */

82     private ManageableRegistry(int port) throws RemoteException JavaDoc {
83         this(port, RMISocketFactory.getSocketFactory(), RMISocketFactory.getSocketFactory());
84     }
85
86     /**
87      * Set verbosity
88      * @param v true/false
89      */

90     public void setVerbose(boolean v) {
91         System.out.println("RegistryManager.setVerbose(" + v + ")");
92         verbose = v;
93     }
94
95     /**
96      * Retrieves the named object.
97      * @param name the name of the object to look up
98      * @return the object bound to <tt>name</tt>
99      * @throws RemoteException if a naming exception is encountered
100      * @throws NotBoundException if object is not bound
101      */

102     public Remote JavaDoc lookup(String JavaDoc name) throws RemoteException JavaDoc, NotBoundException JavaDoc {
103         if (verbose) {
104             try {
105                 System.out.println("ManageableRegistry.lookup(" + name + ") from client: " + getClientHost());
106             } catch (ServerNotActiveException JavaDoc e) {
107                 e.printStackTrace();
108             }
109         }
110         synchronized (registryObjects) {
111             Remote JavaDoc obj = (Remote JavaDoc) registryObjects.get(name);
112             if (obj == null) {
113                 throw new NotBoundException JavaDoc(name);
114             }
115             return obj;
116         }
117     }
118
119     /**
120      * Binds a name to an object, overwriting any existing binding. All
121      * intermediate contexts and the target context (that named by all but
122      * terminal atomic component of the name) must already exist.
123      * @param name the name to bind; may not be empty
124      * @param obj the object to bind; possibly null
125      * @throws RemoteException if a bind cannot be done exception is encountered
126      * @throws AlreadyBoundException if object is already bound
127      * @throws AccessException if cannot bind
128      */

129     public void bind(String JavaDoc name, Remote JavaDoc obj) throws RemoteException JavaDoc, AlreadyBoundException JavaDoc, AccessException JavaDoc {
130         if (verbose) {
131             try {
132                 System.out.println("ManageableRegistry.bind(" + name + ", obj)" + " from client: " + getClientHost());
133             } catch (ServerNotActiveException JavaDoc e) {
134                 e.printStackTrace();
135             }
136         }
137         synchronized (registryObjects) {
138             Remote JavaDoc curr = (Remote JavaDoc) registryObjects.get(name);
139             if (curr != null) {
140                 throw new AlreadyBoundException JavaDoc(name);
141             }
142             registryObjects.put(name, obj);
143         }
144     }
145
146     /**
147      * Unbinds the named object. Removes the terminal atomic name in
148      * <code>name</code> from the target context--that named by all but the
149      * terminal atomic part of <code>name</code>.
150      * @param name the name to unbind; may not be empty
151      * @throws RemoteException if a naming exception is encountered
152      * @throws NotBoundException if object was not bound
153      * @throws AccessException if unbind is not authorized
154      */

155     public void unbind(String JavaDoc name) throws RemoteException JavaDoc, NotBoundException JavaDoc, AccessException JavaDoc {
156         if (verbose) {
157             try {
158                 System.out.println("ManageableRegistry.unbind(" + name + ")" + " from client: " + getClientHost());
159             } catch (ServerNotActiveException JavaDoc e) {
160                 e.printStackTrace();
161             }
162         }
163         synchronized (registryObjects) {
164             Remote JavaDoc obj = (Remote JavaDoc) registryObjects.get(name);
165             if (obj == null) {
166                 throw new NotBoundException JavaDoc(name);
167             }
168             registryObjects.remove(name);
169         }
170     }
171
172     /**
173      * ReBinds a name to an object, overwriting any existing binding. All
174      * intermediate contexts and the target context (that named by all but
175      * terminal atomic component of the name) must already exist.
176      * @param name the name to bind; may not be empty
177      * @param obj the object to bind; possibly null
178      * @throws RemoteException if a bind cannot be done exception is encountered
179      * @throws AccessException if cannot bind
180      */

181     public void rebind(String JavaDoc name, Remote JavaDoc obj) throws RemoteException JavaDoc, AccessException JavaDoc {
182         if (verbose) {
183             try {
184                 System.out.println("ManageableRegistry.rebind(" + name + ", obj)" + " from client: " + getClientHost());
185             } catch (ServerNotActiveException JavaDoc e) {
186                 e.printStackTrace();
187             }
188         }
189         registryObjects.put(name, obj);
190     }
191
192     /**
193      * Enumerates the names bound in the named context, along with the class
194      * names of objects bound to them. The contents of any subcontexts are not
195      * included.
196      * @return the names in this context.
197      * @throws RemoteException if a naming exception is encountered
198      */

199     public String JavaDoc[] list() throws RemoteException JavaDoc {
200
201         if (verbose) {
202             try {
203                 System.out.println("ManageableRegistry.list()" + " from client: " + getClientHost());
204             } catch (ServerNotActiveException JavaDoc e) {
205                 // TODO Auto-generated catch block
206
e.printStackTrace();
207             }
208         }
209         String JavaDoc[] names;
210         synchronized (registryObjects) {
211             int i = registryObjects.size();
212             names = new String JavaDoc[i];
213             Enumeration JavaDoc e = registryObjects.keys();
214             while ((--i) >= 0) {
215                 names[i] = (String JavaDoc) e.nextElement();
216             }
217         }
218         return names;
219     }
220
221     /**
222      * Create a new registry on given port and use exported object port given
223      * @param port registry port
224      * @param objectPort exported objects port
225      * @return a new Registry object
226      * @throws RemoteException if registry cannot be built
227      */

228     public static Registry JavaDoc createManagableRegistry(int port, int objectPort) throws RemoteException JavaDoc {
229         // used fixed port factory only if user want set the port
230
if (objectPort > 0) {
231             RMISocketFactory JavaDoc socketFactory = RMIFixedPortFirewallSocketFactory.register(objectPort);
232             return new ManageableRegistry(port, socketFactory, socketFactory);
233         } else {
234             return new ManageableRegistry(port);
235         }
236     }
237
238     /**
239      * Remove objects of the registry
240      */

241     public void purge() {
242         registryObjects.clear();
243     }
244
245     /**
246      * Start a new Registry
247      * @param args arguments for starting registry
248      */

249     public static void main(String JavaDoc[] args) {
250         try {
251             int regPort = Registry.REGISTRY_PORT;
252             if (args.length >= 1) {
253                 regPort = Integer.parseInt(args[0]);
254             }
255             createManagableRegistry(regPort, 0);
256             System.out.println("ManageableRegistry started on port " + regPort);
257             // The registry should not exiting because of the Manager binded
258

259         } catch (Exception JavaDoc e) {
260             e.printStackTrace();
261             System.exit(-1);
262         }
263     }
264
265 }
Popular Tags