KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (C) 2002,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: RegistryWrapperContext.java,v 1.2 2005/03/10 12:22:02 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.jndi.registry;
29
30 import java.rmi.AlreadyBoundException JavaDoc;
31 import java.rmi.NotBoundException JavaDoc;
32 import java.rmi.Remote JavaDoc;
33 import java.rmi.RemoteException JavaDoc;
34 import java.rmi.registry.Registry JavaDoc;
35 import java.util.Hashtable JavaDoc;
36 import java.util.Properties JavaDoc;
37
38 import javax.naming.Binding JavaDoc;
39 import javax.naming.CompositeName JavaDoc;
40 import javax.naming.CompoundName JavaDoc;
41 import javax.naming.Context JavaDoc;
42 import javax.naming.Name JavaDoc;
43 import javax.naming.NameAlreadyBoundException JavaDoc;
44 import javax.naming.NameNotFoundException JavaDoc;
45 import javax.naming.NameParser JavaDoc;
46 import javax.naming.NamingEnumeration JavaDoc;
47 import javax.naming.NamingException JavaDoc;
48
49 import org.objectweb.carol.jndi.ns.JRMPRegistry;
50 import org.objectweb.carol.rmi.exception.NamingExceptionHelper;
51
52 /**
53  * Wrapper on a Registry object and implementing Context
54  * @author Guillaume Riviere
55  * @author Florent Benoit (Refactoring)
56  */

57 public class RegistryWrapperContext implements Context JavaDoc {
58
59     /**
60      * LocalRegistry for bindings and lookup
61      */

62     private Registry JavaDoc registry;
63
64     /**
65      * the JRMP JNDI environment
66      */

67     private static Hashtable JavaDoc environment = null;
68
69     /**
70      * Simple name parser
71      */

72     private static final NameParser JavaDoc NAME_PARSER = new SimpleNameParser();
73
74     /**
75      * Create a local context for the registry
76      * @param env hashtable used
77      */

78     public RegistryWrapperContext(Hashtable JavaDoc env) {
79         registry = JRMPRegistry.getRegistry();
80         environment = env;
81         environment.put(Context.INITIAL_CONTEXT_FACTORY, "org.objectweb.carol.jndi.spi.JRMPContextWrapperFactory");
82     }
83
84     /**
85      * Retrieves the named object.
86      * @param name the name of the object to look up
87      * @return the object bound to <tt>name</tt>
88      * @throws NamingException if a naming exception is encountered
89      */

90     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
91         if (name.isEmpty()) {
92             return this;
93         }
94         Remote JavaDoc obj;
95         try {
96             obj = registry.lookup(name.get(0));
97         } catch (NotBoundException JavaDoc e) {
98             NameNotFoundException JavaDoc nnfe = new NameNotFoundException JavaDoc(name.get(0));
99             nnfe.setRootCause(e);
100             throw nnfe;
101         } catch (Exception JavaDoc e) {
102             throw NamingExceptionHelper.create("Cannot lookup name '" + name + "' : " + e.getMessage(), e);
103         }
104         return obj;
105     }
106
107     /**
108      * Retrieves the named object.
109      * @param name the name of the object to look up
110      * @return the object bound to <tt>name</tt>
111      * @throws NamingException if a naming exception is encountered
112      */

113     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
114         return lookup(new CompositeName JavaDoc(name));
115     }
116
117     /**
118      * Binds a name to an object.
119      * @param name the name to bind; may not be empty
120      * @param obj the object to bind; possibly null
121      * @throws NamingException if a naming exception is encountered
122      */

123     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
124         if (name.isEmpty()) {
125             throw new NamingException JavaDoc("Cannot bind empty name");
126         }
127
128         if (!(obj instanceof Remote JavaDoc)) {
129             throw new NamingException JavaDoc(
130                     "Can only bind object which implements Remote interface. This is not the case for object '" + obj
131                             + "' with name '" + name + "'.");
132         }
133
134         try {
135             registry.bind(name.get(0), (Remote JavaDoc) obj);
136         } catch (AlreadyBoundException JavaDoc e) {
137             NamingException JavaDoc ne = new NameAlreadyBoundException JavaDoc(name.get(0));
138             ne.setRootCause(e);
139             throw ne;
140         } catch (Exception JavaDoc e) {
141             NamingException JavaDoc ne = new NamingException JavaDoc();
142             ne.setRootCause(e);
143             throw ne;
144         }
145     }
146
147     /**
148      * Binds a name to an object.
149      * @param name the name to bind; may not be empty
150      * @param obj the object to bind; possibly null
151      * @throws NamingException if a naming exception is encountered
152      */

153     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
154         bind(new CompositeName JavaDoc(name), obj);
155     }
156
157     /**
158      * Binds a name to an object, overwriting any existing binding. All
159      * intermediate contexts and the target context (that named by all but
160      * terminal atomic component of the name) must already exist.
161      * @param name the name to bind; may not be empty
162      * @param obj the object to bind; possibly null
163      * @throws NamingException if a naming exception is encountered
164      */

165     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
166         if (name.isEmpty()) {
167             throw new NamingException JavaDoc("Cannot rebind empty name");
168         }
169         if (!(obj instanceof Remote JavaDoc)) {
170             throw new NamingException JavaDoc(
171                     "Can only rebind object which implements Remote interface. This is not the case for object '" + obj
172                             + "' with name '" + name + "'.");
173         }
174
175         try {
176             registry.rebind(name.get(0), (Remote JavaDoc) obj);
177         } catch (Exception JavaDoc e) {
178             NamingException JavaDoc ne = new NamingException JavaDoc();
179             ne.setRootCause(e);
180             throw ne;
181         }
182     }
183
184     /**
185      * Binds a name to an object, overwriting any existing binding.
186      * @param name the name to bind; may not be empty
187      * @param obj the object to bind; possibly null
188      * @throws NamingException if a naming exception is encountered
189      */

190     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
191         rebind(new CompositeName JavaDoc(name), obj);
192     }
193
194     /**
195      * Unbinds the named object. Removes the terminal atomic name in
196      * <code>name</code> from the target context--that named by all but the
197      * terminal atomic part of <code>name</code>.
198      * @param name the name to unbind; may not be empty
199      * @throws NamingException if a naming exception is encountered
200      */

201     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
202         if (name.isEmpty()) {
203             throw new NamingException JavaDoc("Cannot unbind empty name");
204         }
205         try {
206             registry.unbind(name.get(0));
207         } catch (Exception JavaDoc e) {
208             NamingException JavaDoc ne = new NamingException JavaDoc();
209             ne.setRootCause(e);
210             throw ne;
211         }
212     }
213
214     /**
215      * Unbinds the named object.
216      * @param name the name to unbind; may not be empty
217      * @throws NamingException if a naming exception is encountered
218      */

219     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
220         unbind(new CompositeName JavaDoc(name));
221     }
222
223     /**
224      * Binds a new name to the object bound to an old name, and unbinds the old
225      * name. Both names are relative to this context. Any attributes associated
226      * with the old name become associated with the new name.
227      * @param oldName the name of the existing binding; may not be empty
228      * @param newName the name of the new binding; may not be empty
229      * @throws NamingException if a naming exception is encountered
230      */

231     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc {
232         bind(newName, lookup(oldName));
233         unbind(oldName);
234     }
235
236     /**
237      * Binds a new name to the object bound to an old name, and unbinds the old
238      * name.
239      * @param oldName the name of the existing binding; may not be empty
240      * @param newName the name of the new binding; may not be empty
241      * @throws NamingException if a naming exception is encountered
242      */

243     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc {
244         rename(new CompositeName JavaDoc(oldName), new CompositeName JavaDoc(newName));
245     }
246
247     /**
248      * Enumerates the names bound in the named context, along with the class
249      * names of objects bound to them. The contents of any subcontexts are not
250      * included.
251      * @param name the name of the context to list
252      * @return an enumeration of the names and class names of the bindings in
253      * this context. Each element of the enumeration is of type
254      * <tt>NameClassPair</tt>.
255      * @throws NamingException if a naming exception is encountered
256      */

257     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc {
258         if (!name.isEmpty()) {
259             throw new NamingException JavaDoc("Cannot list with a given empty name");
260         }
261         try {
262             String JavaDoc[] names = registry.list();
263             return new LocalEnumeration(this, names);
264         } catch (Exception JavaDoc e) {
265             NamingException JavaDoc ne = new NamingException JavaDoc();
266             ne.setRootCause(e);
267             throw ne;
268         }
269     }
270
271     /**
272      * Enumerates the names bound in the named context, along with the class
273      * names of objects bound to them.
274      * @param name the name of the context to list
275      * @return an enumeration of the names and class names of the bindings in
276      * this context. Each element of the enumeration is of type
277      * <tt>NameClassPair</tt>.
278      * @throws NamingException if a naming exception is encountered
279      */

280     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc {
281         return list(new CompositeName JavaDoc(name));
282     }
283
284     /**
285      * Enumerates the names bound in the named context, along with the objects
286      * bound to them. The contents of any subcontexts are not included.
287      * @param name the name of the context to list
288      * @return an enumeration of the bindings in this context. Each element of
289      * the enumeration is of type <tt>Binding</tt>.
290      * @throws NamingException if a naming exception is encountered
291      */

292     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
293         if (!name.isEmpty()) {
294             throw new NamingException JavaDoc("can not list");
295         }
296         try {
297             String JavaDoc[] names = registry.list();
298             return new LocalEnumeration(this, names);
299         } catch (RemoteException JavaDoc e) {
300             NamingException JavaDoc ne = new NamingException JavaDoc();
301             ne.setRootCause(e);
302             throw ne;
303         }
304     }
305
306     /**
307      * Enumerates the names bound in the named context, along with the objects
308      * bound to them.
309      * @param name the name of the context to list
310      * @return an enumeration of the bindings in this context. Each element of
311      * the enumeration is of type <tt>Binding</tt>.
312      * @throws NamingException if a naming exception is encountered
313      */

314     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc {
315         return listBindings(new CompositeName JavaDoc(name));
316     }
317
318     /**
319      * Destroys the named context and removes it from the namespace. Any
320      * attributes associated with the name are also removed. Intermediate
321      * contexts are not destroyed.
322      * @param name the name of the context to be destroyed; may not be empty
323      * @throws NamingException if a naming exception is encountered
324      */

325     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
326         throw new NamingException JavaDoc("destroySubcontext() method not implemented");
327     }
328
329     /**
330      * Destroys the named context and removes it from the namespace.
331      * @param name the name of the context to be destroyed; may not be empty
332      * @throws NamingException if a naming exception is encountered
333      */

334     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
335         destroySubcontext(new CompositeName JavaDoc(name));
336     }
337
338     /**
339      * Creates and binds a new context.
340      * @param name the name of the context to create; may not be empty
341      * @return the newly created context
342      * @throws NamingException if a naming exception is encountered
343      */

344     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
345         throw new NamingException JavaDoc("createSubcontext() method not implemented");
346     }
347
348     /**
349      * Creates and binds a new context.
350      * @param name the name of the context to create; may not be empty
351      * @return the newly created context
352      * @throws NamingException if a naming exception is encountered
353      */

354     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc {
355         return createSubcontext(new CompositeName JavaDoc(name));
356     }
357
358     /**
359      * Retrieves the named object, following links except for the terminal
360      * atomic component of the name.
361      * @param name the name of the object to look up
362      * @return the object bound to <tt>name</tt>, not following the terminal
363      * link (if any).
364      * @throws NamingException if a naming exception is encountered
365      */

366     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
367         return lookup(name);
368     }
369
370     /**
371      * Retrieves the named object, following links except for the terminal
372      * atomic component of the name.
373      * @param name the name of the object to look up
374      * @return the object bound to <tt>name</tt>, not following the terminal
375      * link (if any)
376      * @throws NamingException if a naming exception is encountered
377      */

378     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
379         return lookup(name);
380     }
381
382     /**
383      * Retrieves the parser associated with the named context.
384      * @param name the name of the context from which to get the parser
385      * @return a name parser that can parse compound names into their atomic
386      * components
387      * @throws NamingException if a naming exception is encountered
388      */

389     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
390         return NAME_PARSER;
391     }
392
393     /**
394      * Retrieves the parser associated with the named context.
395      * @param name the name of the context from which to get the parser
396      * @return a name parser that can parse compound names into their atomic
397      * components
398      * @throws NamingException if a naming exception is encountered
399      */

400     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
401         return NAME_PARSER;
402     }
403
404     /**
405      * Composes the name of this context with a name relative to this context.
406      * @param name a name relative to this context
407      * @param prefix the name of this context relative to one of its ancestors
408      * @return the composition of <code>prefix</code> and <code>name</code>
409      * @throws NamingException if a naming exception is encountered
410      */

411     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
412         Name JavaDoc result = (Name JavaDoc) prefix.clone();
413         return result.addAll(name);
414     }
415
416     /**
417      * Composes the name of this context with a name relative to this context.
418      * @param name a name relative to this context
419      * @param prefix the name of this context relative to one of its ancestors
420      * @return the composition of <code>prefix</code> and <code>name</code>
421      * @throws NamingException if a naming exception is encountered
422      */

423     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws NamingException JavaDoc {
424         return composeName(new CompositeName JavaDoc(name), new CompositeName JavaDoc(prefix)).toString();
425     }
426
427     /**
428      * Adds a new environment property to the environment of this context. If
429      * the property already exists, its value is overwritten. See class
430      * description for more details on environment properties.
431      * @param propName the name of the environment property to add; may not be
432      * null
433      * @param propVal the value of the property to add; may not be null
434      * @return the previous value of the property, or null if the property was
435      * not in the environment before
436      * @throws NamingException if a naming exception is encountered
437      */

438     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal) throws NamingException JavaDoc {
439         return environment.put(propName, propVal);
440     }
441
442     /**
443      * Removes an environment property from the environment of this context. See
444      * class description for more details on environment properties.
445      * @param propName the name of the environment property to remove; may not
446      * be null
447      * @return the previous value of the property, or null if the property was
448      * not in the environment
449      * @throws NamingException if a naming exception is encountered
450      */

451     public Object JavaDoc removeFromEnvironment(String JavaDoc propName) throws NamingException JavaDoc {
452         return environment.remove(propName);
453     }
454
455     /**
456      * Retrieves the environment in effect for this context. See class
457      * description for more details on environment properties.
458      * @return the environment of this context; never null
459      * @throws NamingException if a naming exception is encountered
460      */

461     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc {
462         return (Hashtable JavaDoc) environment.clone();
463     }
464
465     /**
466      * Closes this context. This method releases this context's resources
467      * immediately, instead of waiting for them to be released automatically by
468      * the garbage collector.
469      */

470     public void close() {
471     }
472
473     /**
474      * Retrieves the full name of this context within its own namespace.
475      * @return this context's name in its own namespace; never null
476      */

477     public String JavaDoc getNameInNamespace() {
478         return "";
479     }
480
481 }
482
483 /**
484  * A very simple Compound Name parser
485  */

486
487 class SimpleNameParser implements NameParser JavaDoc {
488
489     /**
490      * Default syntax
491      */

492     private static final Properties JavaDoc SYNTAX = new Properties JavaDoc();
493
494     /**
495      * Parses a name into its components.
496      * @param name The non-null string name to parse.
497      * @return A non-null parsed form of the name using the naming convention of
498      * this parser.
499      * @exception NamingException If a naming exception was encountered.
500      */

501     public Name JavaDoc parse(String JavaDoc name) throws NamingException JavaDoc {
502         return (new CompoundName JavaDoc(name, SYNTAX));
503     }
504 }
505
506 /**
507  * Local enumeration for local context
508  */

509
510 class LocalEnumeration implements NamingEnumeration JavaDoc {
511
512     /**
513      * Initial context
514      */

515     private Context JavaDoc localContext;
516
517     /**
518      * List of names
519      */

520     private final String JavaDoc[] names;
521
522     /**
523      * Index of names
524      */

525     private int nextName;
526
527     /**
528      * Default constructor
529      * @param ctx given context
530      * @param names names to enumerate
531      */

532     public LocalEnumeration(Context JavaDoc ctx, String JavaDoc[] names) {
533         this.localContext = ctx;
534         this.names = names;
535         nextName = 0;
536     }
537
538     /**
539      * Enumeration is finished ?
540      * @return true is this is finished
541      */

542     public boolean hasMore() {
543         return (nextName < names.length);
544     }
545
546     /**
547      * @return Next object of the enumeration
548      * @throws NamingException if compositeName object cannot be built
549      */

550     public Object JavaDoc next() throws NamingException JavaDoc {
551         if (!hasMore()) {
552             throw (new java.util.NoSuchElementException JavaDoc());
553         }
554         String JavaDoc name = names[nextName++];
555         Name JavaDoc cname = (new CompositeName JavaDoc()).add(name);
556
557         Object JavaDoc obj = localContext.lookup(cname);
558         return (new Binding JavaDoc(cname.toString(), obj));
559     }
560
561     /**
562      * Enumeration is finished ?
563      * @return true is this is finished
564      */

565     public boolean hasMoreElements() {
566         return hasMore();
567     }
568
569     /**
570      * @return Next object of the enumeration
571      */

572     public Object JavaDoc nextElement() {
573         try {
574             return next();
575         } catch (NamingException JavaDoc e) {
576             throw new java.util.NoSuchElementException JavaDoc(e.toString());
577         }
578     }
579
580     /**
581      * Close the enumeration
582      */

583     public void close() {
584     }
585
586 }
Popular Tags