KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > client > naming > CoadunationContext


1 /*
2  * <Add library description here>
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * CoadunationContext.java
20  *
21  * The implementation of the Coadunation client context. Used to lookup RMI
22  * information.
23  *
24  * Revision: $ID
25  */

26
27 // package path
28
package com.rift.coad.client.naming;
29
30 // java imports
31
import java.io.Serializable JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.Properties JavaDoc;
34 import javax.naming.CompoundName JavaDoc;
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 import javax.naming.InitialContext JavaDoc;
41 import org.omg.CORBA.ORB JavaDoc;
42
43 // logging import
44
import org.apache.log4j.Logger;
45
46 import com.rift.coad.lib.interceptor.credentials.Login;
47
48 /**
49  * The implementation of the Coadunation client context. Used to lookup RMI
50  * information.
51  *
52  * @author Brett Chaldecott
53  */

54 public class CoadunationContext implements Context JavaDoc {
55     
56     /**
57      * This object is responsible for parsing the string value passed in. Valid
58      * names must be formated as "this/is/valid".
59      *
60      * @author Brett Chaldecott
61      */

62     public class NamingParser implements NameParser JavaDoc,Serializable JavaDoc {
63         
64         /**
65          * Creates a new instance of NamingParser
66          */

67         public NamingParser() {
68         }
69         
70         
71         /**
72          * The method responsible for parsing the string value into a name value.
73          *
74          * @return The parse name object.
75          * @param name The name to parse.
76          * @exception NamingException
77          */

78         public Name JavaDoc parse(String JavaDoc name) throws NamingException JavaDoc {
79             return new CompoundName JavaDoc(name,syntax);
80         }
81     }
82     
83     // The classes static variables
84
private static Properties JavaDoc syntax = new Properties JavaDoc();
85     static {
86         syntax.setProperty("jndi.syntax.direction","left_to_right");
87         syntax.setProperty("jndi.syntax.separator","/");
88         syntax.setProperty("jndi.syntax.ignorecase","false");
89         syntax.setProperty("jndi.syntax.escape","\\");
90     }
91     protected static Logger log =
92             Logger.getLogger(CoadunationContext.class.getName());
93     
94     // class private member variables
95
private ORB JavaDoc orb = null;
96     private Hashtable JavaDoc env = null;
97     private Context JavaDoc primaryContext = null;
98     private Name JavaDoc prefix = null;
99     
100     /**
101      * Creates a new instance of CoadunationContext
102      *
103      * @param env The environment.
104      * @param orb The orb reference.
105      */

106     public CoadunationContext(ORB JavaDoc orb,Hashtable JavaDoc env) {
107         this.orb = orb;
108         this.env = env;
109         try {
110             prefix = new NamingParser().parse("");
111         } catch (Exception JavaDoc ex) {
112             log.error("Failed to parse the name : " + ex.getMessage(),ex);
113         }
114     }
115     
116     /**
117      * Creates a new instance of CoadunationContext
118      */

119     public CoadunationContext(Hashtable JavaDoc env,Name JavaDoc prefix) {
120         this.env = env;
121         this.prefix = prefix;
122     }
123     
124     /**
125      * Adds a new environment property to the environment of this context.
126      *
127      * @return The previous value of the property or null.
128      * @param propName The property to replace or add.
129      * @param propValue The new property value.
130      */

131     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal) throws
132             NamingException JavaDoc {
133         throw new NamingException JavaDoc("Not implemented");
134     }
135     
136     
137     /**
138      * Binds a name to an object.
139      *
140      * @param name The name of the object to bind.
141      * @param obj The object to bind.
142      * @exception NamingException
143      */

144     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
145         throw new NamingException JavaDoc("Not implemented");
146     }
147     
148     
149     /**
150      * Binds a name to an object.
151      */

152     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
153         throw new NamingException JavaDoc("Not implemented");
154     }
155     
156     
157     /**
158      * Closes this context.
159      */

160     public void close() throws NamingException JavaDoc {
161         if (primaryContext != null) {
162             primaryContext.close();
163             primaryContext = null;
164         }
165     }
166     
167     
168     /**
169      * Composes the name of this context with a name relative to this context.
170      *
171      * @return The compisit name.
172      * @param name The name to add to the prefix.
173      * @param prefix The prefix of the current context.
174      * @exception NamingException
175      */

176     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
177         return null;
178     }
179     
180     
181     /**
182      * Composes the name of this context with a name relative to this context.
183      *
184      * @return The string version of the composed name.
185      * @param name The name to add to the preffix.
186      * @param prefix The prefix for this context.
187      */

188     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws
189             NamingException JavaDoc {
190         return null;
191     }
192     
193     
194     /**
195      * Creates and binds a new context to this context.
196      *
197      * @return The newly created context.
198      * @param name The name of the new sub context.
199      * @exception NamingException
200      */

201     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
202         throw new NamingException JavaDoc("Not implemented");
203     }
204     
205     
206     /**
207      * Creates and binds a new context.
208      *
209      * @return The newly create sub context.
210      * @exception name The name of the new sub context.
211      * @exception NamingException
212      */

213     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc {
214         throw new NamingException JavaDoc("Not implemented");
215     }
216     
217     
218     /**
219      * Destroys the named context and removes it from the namespace.
220      *
221      * @param name The name of the sub context to remove.
222      * @exception NamingException
223      */

224     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
225         throw new NamingException JavaDoc("Not implemented");
226     }
227     
228     
229     /**
230      * Destroys the named context and removes it from the namespace.
231      *
232      * @param name The name of the context to destroy.
233      */

234     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
235         throw new NamingException JavaDoc("Not implemented");
236     }
237     
238     
239     /**
240      * Retrieves the environment in effect for this context.
241      *
242      * @return The reference to the hash table.
243      * @exception NamingException
244      */

245     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc {
246         return null;
247     }
248     
249     
250     /**
251      * Retrieves the full name of this context within its own namespace.
252      */

253     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
254         return null;
255     }
256     
257     
258     /**
259      * Retrieves the parser associated with the named context.
260      *
261      * @return The reference to the name parser.
262      * @param name The name to return the parser for.
263      * @exception NamingException
264      */

265     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
266         return null;
267     }
268     
269     
270     /**
271      * Retrieves the parser associated with the named context.
272      */

273     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
274         return null;
275     }
276     
277     
278     /**
279      * Enumerates the names bound in the named context, along with the class
280      * names of objects bound to them.
281      */

282     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc {
283         throw new NamingException JavaDoc("Not implemented");
284     }
285     
286     
287     /**
288      * Enumerates the names bound in the named context, along with the class
289      * names of objects bound to them.
290      *
291      * @return The list of names bound to this context.
292      * @param name The list of names.
293      * @exception NamingException
294      */

295     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc {
296         throw new NamingException JavaDoc("Not implemented");
297     }
298     
299     
300     /**
301      * Enumerates the names bound in the named context, along with the objects
302      * bound to them.
303      *
304      * @return The list of bindings for the name
305      * @param name The name to perform the search below.
306      * @exception NamingException
307      */

308     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
309         throw new NamingException JavaDoc("Not implemented");
310     }
311     
312     
313     /**
314      * Enumerates the names bound in the named context, along with the objects
315      * bound to them.
316      *
317      * @return The list of binding for the name.
318      * @param name The name to perform the search for.
319      * @exception NamingException
320      */

321     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc {
322         throw new NamingException JavaDoc("Not implemented");
323     }
324     
325     
326     /**
327      * Retrieves the named object.
328      *
329      * @return The named object.
330      * @param name The name to retrieve the object for.
331      * @exception NamingException
332      */

333     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
334         Context JavaDoc context = getContext();
335         String JavaDoc currentName = null;
336         try {
337             if (this.env.containsKey(CoadunationInitialContextFactory.USERNAME) &&
338                     this.env.containsKey(
339                     CoadunationInitialContextFactory.PASSWORD)) {
340                 CoadunationInitialContextFactory.userLogin.set(
341                         new Login((String JavaDoc)this.env.get(
342                         CoadunationInitialContextFactory.USERNAME),
343                         (String JavaDoc)this.env.get(
344                         CoadunationInitialContextFactory.PASSWORD)));
345             } else {
346                 CoadunationInitialContextFactory.userLogin.set(null);
347             }
348             Name JavaDoc composedName = (Name JavaDoc)prefix.clone();
349             composedName.addAll(name);
350             for (int index =0; index < (composedName.size() - 1); index++ ) {
351                 currentName = composedName.get(index);
352                 context = (Context JavaDoc)context.lookup(currentName);
353             }
354             currentName = composedName.get(composedName.size() - 1);
355             Object JavaDoc result = context.lookup(currentName);
356             if (result instanceof com.sun.corba.se.impl.corba.CORBAObjectImpl) {
357                 com.sun.corba.se.impl.corba.CORBAObjectImpl obj =
358                         (com.sun.corba.se.impl.corba.CORBAObjectImpl)result;
359                 if (result instanceof Context JavaDoc) {
360                     return new CoadunationContext(env,composedName);
361                 }
362             }
363             return result;
364         } catch (NamingException JavaDoc ex) {
365             this.invalidateContext();
366             log.error("Failed to lookup the object [" + currentName + "] :" +
367                     ex.getMessage(),ex);
368             throw ex;
369         } catch (Exception JavaDoc ex) {
370             this.invalidateContext();
371             log.error("Failed to lookup the object [" + currentName + "] :" +
372                     ex.getMessage(),ex);
373             throw new NamingException JavaDoc(
374                     "Failed to lookup the object [" + currentName + "] :" +
375                     ex.getMessage());
376         }
377     }
378     
379     
380     /**
381      * Retrieves the named object.
382      *
383      * @return The object to retrieve by name.
384      * @param name The name of the object to retrieve.
385      * @exception NamingException
386      */

387     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
388         return lookup(new NamingParser().parse(name));
389     }
390     
391     
392     /**
393      * Retrieves the named object, following links except for the terminal
394      * atomic component of the name.
395      *
396      * @return The object to retrieve.
397      * @param name The name of the object to lookup.
398      * @exception NamingException
399      */

400     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
401         throw new NamingException JavaDoc("Not implemented");
402     }
403     
404     
405     /**
406      * Retrieves the named object, following links except for the terminal
407      * atomic component of the name.
408      *
409      * @return The results of the lookup link.
410      * @param name The name of the object to lookup.
411      * @exception NamingException
412      */

413     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
414         throw new NamingException JavaDoc("Not implemented");
415     }
416     
417     
418     /**
419      * Binds a name to an object, overwriting any existing binding.
420      *
421      * @param name The name to rebind.
422      * @param obj The object to rebind.
423      * @exception NamingException
424      */

425     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
426         throw new NamingException JavaDoc("Not implemented");
427     }
428     
429     
430     /**
431      * Binds a name to an object, overwriting any existing binding.
432      *
433      * @param name The name to rebind.
434      * @param obj The object to rebind.
435      * @exception NamingException
436      */

437     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
438         throw new NamingException JavaDoc("Not implemented");
439     }
440     
441     
442     /**
443      * Removes an environment property from the environment of this context.
444      *
445      * @param propName The name of the entry to remove from the environment.
446      * @exception NamingException
447      */

448     public Object JavaDoc removeFromEnvironment(String JavaDoc propName) throws NamingException JavaDoc {
449         throw new NamingException JavaDoc("Not implemented");
450     }
451     
452     
453     /**
454      * Binds a new name to the object bound to an old name, and unbinds the old
455      * name.
456      *
457      * @param oldName The old name to rename.
458      * @param newName The name to replace it with.
459      * @exception NamingException
460      */

461     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc {
462         throw new NamingException JavaDoc("Not implemented");
463     }
464     
465     
466     /**
467      * Binds a new name to the object bound to an old name, and unbinds the old
468      * name.
469      *
470      * @param oldName The old name to rename.
471      * @param newName The name to replace it with.
472      * @exception NamingException
473      */

474     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc {
475         throw new NamingException JavaDoc("Not implemented");
476     }
477     
478     
479     /**
480      * Unbinds the named object.
481      *
482      * @param name The name to unbind.
483      * @exception NamingException
484      */

485     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
486         throw new NamingException JavaDoc("Not implemented");
487     }
488     
489     
490     /**
491      * Unbinds the named objec.
492      *
493      * @param name The name to unbind.
494      * @exception NamingException
495      */

496     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
497         throw new NamingException JavaDoc("Not implemented");
498     }
499     
500     
501     /**
502      * This method returns a valid context
503      */

504     public synchronized Context JavaDoc getContext() throws NamingException JavaDoc {
505         if (primaryContext != null) {
506             return primaryContext;
507         }
508         return primaryContext = new InitialContext JavaDoc(env);
509     }
510     
511     
512     /**
513      * This method invalidates a bad context.
514      */

515     public synchronized void invalidateContext() {
516         primaryContext = null;
517     }
518 }
519
Popular Tags