KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > jndi > spi > MultiContext


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: MultiContext.java,v 1.9 2005/04/07 15:07:08 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.jndi.spi;
29
30 import java.util.HashMap JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import javax.naming.Context JavaDoc;
36 import javax.naming.Name JavaDoc;
37 import javax.naming.NameParser JavaDoc;
38 import javax.naming.NamingEnumeration JavaDoc;
39 import javax.naming.NamingException JavaDoc;
40
41 import org.objectweb.carol.rmi.exception.NamingExceptionHelper;
42 import org.objectweb.carol.util.configuration.ConfigurationRepository;
43 import org.objectweb.carol.util.configuration.ProtocolConfiguration;
44 import org.objectweb.carol.util.configuration.TraceCarol;
45
46 /**
47  * Class <code> MultiContext </code> is the CAROL JNDI SPI Context for multi
48  * Context management.
49  * <ul>
50  * <li>For void methods, it call method on every protocols</li>
51  * <li>For other methods (returning objects), it call method on the current protocol</li>
52  * </ul>
53  * @author Guillaume Riviere
54  * @author Florent Benoit (refactoring)
55  * @see javax.naming.Context
56  * @see javax.naming.InitialContext
57  * @see org.objectweb.util.multi.ProtocolCurrent
58  */

59 public class MultiContext implements Context JavaDoc {
60
61     /**
62      * InitialContext for each protocol configuration
63      */

64     private Map JavaDoc contextsOfConfigurations = null;
65
66     /**
67      * Current Initial Context
68      */

69     private Context JavaDoc currentInitialContext = null;
70
71
72     /**
73      * Initial environment
74      */

75     private Hashtable JavaDoc environment = null;
76
77     /**
78      * Constructor, load communication framework and instaciate initial contexts
79      * @param env the environment of the InitialContext
80      * @throws NamingException if cccf.getNewContextHashtable(env) fails
81      */

82     public MultiContext(Hashtable JavaDoc env) throws NamingException JavaDoc {
83         this.environment = env;
84         if (TraceCarol.isDebugJndiCarol()) {
85             TraceCarol.debugJndiCarol("MultiContext.MultiContext(env), env = " + env);
86         }
87
88         // Get active configuration name
89
String JavaDoc activeConfigName = null;
90         try {
91             activeConfigName = ConfigurationRepository.getCurrentConfiguration().getName();
92         } catch (Error JavaDoc err) {
93             throw NamingExceptionHelper.create("Could not get a configuration", err);
94         }
95
96         // Build InitialContext for each configuration
97
ProtocolConfiguration[] protocolConfigurations = ConfigurationRepository.getConfigurations();
98         contextsOfConfigurations = new HashMap JavaDoc();
99         for (int i = 0; i < protocolConfigurations.length; i++) {
100             Context JavaDoc ctx = protocolConfigurations[i].getInitialContext(env);
101             if (protocolConfigurations[i].getName().equals(activeConfigName)) {
102                 currentInitialContext = ctx;
103             }
104             contextsOfConfigurations.put(protocolConfigurations[i], ctx);
105         }
106         if (currentInitialContext == null) {
107             throw new NamingException JavaDoc("No current initial context was set. Active environment was '" + activeConfigName + "'. Error in carol with environment = " + env);
108         }
109     }
110
111     /**
112      * Retrieves the named object.
113      * @param name the name of the object to look up
114      * @return the object bound to <tt>name</tt>
115      * @throws NamingException if a naming exception is encountered
116      */

117     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
118         return currentInitialContext.lookup(name);
119     }
120
121     /**
122      * Retrieves the named object.
123      * @param name the name of the object to look up
124      * @return the object bound to <tt>name</tt>
125      * @throws NamingException if a naming exception is encountered
126      */

127     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
128         return currentInitialContext.lookup(name);
129     }
130
131     /**
132      * Binds a name to an object.
133      * @param name the name to bind; may not be empty
134      * @param obj the object to bind; possibly null
135      * @throws NamingException if a naming exception is encountered
136      */

137     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
138         for (Iterator JavaDoc it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) {
139             ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next();
140             ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration);
141             ((Context JavaDoc) contextsOfConfigurations.get(protocolConfiguration)).bind(name, obj);
142             ConfigurationRepository.setCurrentConfiguration(old);
143         }
144     }
145
146     /**
147      * Binds a name to an object.
148      * @param name the name to bind; may not be empty
149      * @param obj the object to bind; possibly null
150      * @throws NamingException if a naming exception is encountered
151      */

152     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
153         for (Iterator JavaDoc it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) {
154             ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next();
155             ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration);
156             ((Context JavaDoc) contextsOfConfigurations.get(protocolConfiguration)).bind(name, obj);
157             ConfigurationRepository.setCurrentConfiguration(old);
158         }
159     }
160
161     /**
162      * Binds a name to an object, overwriting any existing binding.
163      * @param name the name to bind; may not be empty
164      * @param obj the object to bind; possibly null
165      * @throws NamingException if a naming exception is encountered
166      */

167     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
168         for (Iterator JavaDoc it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) {
169             ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next();
170             ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration);
171             ((Context JavaDoc) contextsOfConfigurations.get(protocolConfiguration)).rebind(name, obj);
172             ConfigurationRepository.setCurrentConfiguration(old);
173         }
174     }
175
176     /**
177      * Binds a name to an object, overwriting any existing binding. All
178      * intermediate contexts and the target context (that named by all but
179      * terminal atomic component of the name) must already exist.
180      * @param name the name to bind; may not be empty
181      * @param obj the object to bind; possibly null
182      * @throws NamingException if a naming exception is encountered
183      */

184     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
185         for (Iterator JavaDoc it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) {
186             ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next();
187             ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration);
188             ((Context JavaDoc) contextsOfConfigurations.get(protocolConfiguration)).rebind(name, obj);
189             ConfigurationRepository.setCurrentConfiguration(old);
190         }
191     }
192
193     /**
194      * Unbinds the named object.
195      * @param name the name to unbind; may not be empty
196      * @throws NamingException if a naming exception is encountered
197      */

198     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
199         for (Iterator JavaDoc it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) {
200             ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next();
201             ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration);
202             ((Context JavaDoc) contextsOfConfigurations.get(protocolConfiguration)).unbind(name);
203             ConfigurationRepository.setCurrentConfiguration(old);
204         }
205     }
206
207     /**
208      * Unbinds the named object. Removes the terminal atomic name in
209      * <code>name</code> from the target context--that named by all but the
210      * terminal atomic part of <code>name</code>.
211      * @param name the name to unbind; may not be empty
212      * @throws NamingException if a naming exception is encountered
213      */

214     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
215         for (Iterator JavaDoc it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) {
216             ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next();
217             ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration);
218             ((Context JavaDoc) contextsOfConfigurations.get(protocolConfiguration)).unbind(name);
219             ConfigurationRepository.setCurrentConfiguration(old);
220         }
221     }
222
223     /**
224      * Binds a new name to the object bound to an old name, and unbinds the old
225      * name.
226      * @param oldName the name of the existing binding; may not be empty
227      * @param newName the name of the new binding; may not be empty
228      * @throws NamingException if a naming exception is encountered
229      */

230     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc {
231         for (Iterator JavaDoc it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) {
232             ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next();
233             ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration);
234             ((Context JavaDoc) contextsOfConfigurations.get(protocolConfiguration)).rename(oldName, newName);
235             ConfigurationRepository.setCurrentConfiguration(old);
236         }
237     }
238
239     /**
240      * Binds a new name to the object bound to an old name, and unbinds the old
241      * name. Both names are relative to this context. Any attributes associated
242      * with the old name become associated with the new name.
243      * @param oldName the name of the existing binding; may not be empty
244      * @param newName the name of the new binding; may not be empty
245      * @throws NamingException if a naming exception is encountered
246      */

247     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc {
248         for (Iterator JavaDoc it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) {
249             ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next();
250             ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration);
251             ((Context JavaDoc) contextsOfConfigurations.get(protocolConfiguration)).rename(oldName, newName);
252             ConfigurationRepository.setCurrentConfiguration(old);
253         }
254     }
255
256     /**
257      * Enumerates the names bound in the named context, along with the class
258      * names of objects bound to them.
259      * @param name the name of the context to list
260      * @return an enumeration of the names and class names of the bindings in
261      * this context. Each element of the enumeration is of type
262      * <tt>NameClassPair</tt>.
263      * @throws NamingException if a naming exception is encountered
264      */

265     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc {
266         return currentInitialContext.list(name);
267     }
268
269     /**
270      * Enumerates the names bound in the named context, along with the class
271      * names of objects bound to them. The contents of any subcontexts are not
272      * included.
273      * @param name the name of the context to list
274      * @return an enumeration of the names and class names of the bindings in
275      * this context. Each element of the enumeration is of type
276      * <tt>NameClassPair</tt>.
277      * @throws NamingException if a naming exception is encountered
278      */

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

291     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc {
292         return currentInitialContext.listBindings(name);
293     }
294
295     /**
296      * Enumerates the names bound in the named context, along with the objects
297      * bound to them. The contents of any subcontexts are not included.
298      * @param name the name of the context to list
299      * @return an enumeration of the bindings in this context. Each element of
300      * the enumeration is of type <tt>Binding</tt>.
301      * @throws NamingException if a naming exception is encountered
302      */

303     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
304         return currentInitialContext.listBindings(name);
305     }
306
307     /**
308      * Destroys the named context and removes it from the namespace.
309      * @param name the name of the context to be destroyed; may not be empty
310      * @throws NamingException if a naming exception is encountered
311      */

312     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
313         for (Iterator JavaDoc it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) {
314             ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next();
315             ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration);
316             ((Context JavaDoc) contextsOfConfigurations.get(protocolConfiguration)).destroySubcontext(name);
317             ConfigurationRepository.setCurrentConfiguration(old);
318         }
319     }
320
321     /**
322      * Destroys the named context and removes it from the namespace. Any
323      * attributes associated with the name are also removed. Intermediate
324      * contexts are not destroyed.
325      * @param name the name of the context to be destroyed; may not be empty
326      * @throws NamingException if a naming exception is encountered
327      */

328     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
329         for (Iterator JavaDoc it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) {
330             ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next();
331             ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration);
332             ((Context JavaDoc) contextsOfConfigurations.get(protocolConfiguration)).destroySubcontext(name);
333             ConfigurationRepository.setCurrentConfiguration(old);
334         }
335     }
336     /**
337      * Creates and binds a new context.
338      * @param name the name of the context to create; may not be empty
339      * @return the newly created context
340      * @throws NamingException if a naming exception is encountered
341      */

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

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

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

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

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

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

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

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

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

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

458     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc {
459         return this.environment;
460     }
461
462     /**
463      * Closes this context. This method releases this context's resources
464      * immediately, instead of waiting for them to be released automatically by
465      * the garbage collector.
466      * @throws NamingException if a naming exception is encountered
467      */

468     public void close() throws NamingException JavaDoc {
469         for (Iterator JavaDoc it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) {
470             ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next();
471             ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration);
472             ((Context JavaDoc) contextsOfConfigurations.get(protocolConfiguration)).close();
473             ConfigurationRepository.setCurrentConfiguration(old);
474         }
475
476     }
477
478     /**
479      * Retrieves the full name of this context within its own namespace.
480      * @return this context's name in its own namespace; never null
481      * @throws NamingException if a naming exception is encountered
482      */

483     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
484         return currentInitialContext.getNameInNamespace();
485     }
486
487 }
488
Popular Tags