KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > spi > webxml > WebXmlInitialContext


1 package org.enhydra.spi.webxml;
2
3 import java.rmi.Remote JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.Hashtable JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.io.File JavaDoc;
9 import java.io.FileInputStream JavaDoc;
10
11 import javax.naming.Binding JavaDoc;
12 import javax.naming.CompositeName JavaDoc;
13 import javax.naming.Context JavaDoc;
14 import javax.naming.InvalidNameException JavaDoc;
15 import javax.naming.Name JavaDoc;
16 import javax.naming.NameAlreadyBoundException JavaDoc;
17 import javax.naming.NameClassPair JavaDoc;
18 import javax.naming.NameNotFoundException JavaDoc;
19 import javax.naming.NameParser JavaDoc;
20 import javax.naming.NamingEnumeration JavaDoc;
21 import javax.naming.NamingException JavaDoc;
22 import javax.naming.NotContextException JavaDoc;
23 import javax.naming.OperationNotSupportedException JavaDoc;
24 import javax.naming.Reference JavaDoc;
25 import javax.naming.Referenceable JavaDoc;
26 import javax.naming.spi.ObjectFactory JavaDoc;
27 import org.objectweb.carol.util.configuration.TraceCarol;
28 import org.enhydra.spi.webxml.util.*;
29
30
31 /**
32  * Class <code>WebXmlInitialContext</code> is theJNDI SPI Context for local
33  * context, for unpacked application's web.xml file.
34  * This context is accessible only in local jvm, this is a singleton (the close
35  * method do nothing). This context binds and returns Local Java Refferences.
36  * It also reads Enhydra application's configuration file (web.xml) and puts
37  * application parameters into java:comp/env/ context of Application's JNDI
38  * context.
39  *
40  * @author
41  * @version 1.0, 08/05/2004
42  */

43
44     /**
45      * WebXmlInitialContext class.
46      */

47
48 public class WebXmlInitialContext implements Context JavaDoc {
49
50     /**
51      * WebXml Environment
52      */

53     private static Hashtable JavaDoc fileEnv = new Hashtable JavaDoc();
54
55     /**
56      * WebXml bindings
57      */

58     private static Hashtable JavaDoc bindings = new Hashtable JavaDoc();
59
60     /**
61      * Application's java:comp/env/ context.
62      */

63     private static Context JavaDoc envContext = null;
64
65     /**
66      * Path to application's web.xml.
67      */

68     private String JavaDoc filePath = null;
69
70     /**
71      * True if the file is parsed, otherwise false.
72      */

73     private boolean isFileParsed = false;
74
75     /**
76      * WebXmlBindings Name Parser
77      */

78     private static NameParser JavaDoc fileParser = new WebXmlNameParser();
79
80     public final static String JavaDoc FILE_JNDI_ABS_PATH_NAME = "fileJndiAbsPathName";
81     public final static String JavaDoc CONTEXT_FOR_ENV_BIND = "contextForEnvToBind";
82
83     /**
84      * Resolves a Remote Object.
85      * If this object is a reference, returns the reference.
86      *
87      * @param o Object to resolve.
88      * @param name Name of this object.
89      * @return <code>Referenceable</code> if o is a Reference
90      * and the inititial object o otherwise.
91      */

92     private Object JavaDoc resolveObject(Object JavaDoc o, Name JavaDoc name) {
93       try {
94         if (o instanceof Reference JavaDoc) {
95           // build of the Referenceable object with is Reference
96
Reference JavaDoc objRef = (Reference JavaDoc) o;
97           ObjectFactory JavaDoc objFact = (ObjectFactory JavaDoc) (Thread.currentThread().
98             getContextClassLoader().loadClass(objRef.getFactoryClassName())).newInstance();
99           return objFact.getObjectInstance(objRef, name, this,
100                                            this.getEnvironment());
101         }
102         else {
103           return o;
104         }
105       }
106       catch (Exception JavaDoc e) {
107         TraceCarol.error("WebXmlInitialContext.resolveObject()", e);
108         return o;
109       }
110     }
111
112     /**
113      * Encodes an Object. If the object is a referenceable, it binds this
114      * reference.
115      * @param o Object to encode.
116      * @return <code>Remote Object</code> if o is a resource, otherwise o.
117      * @exception NamingException If naming exception occurs.
118      */

119     private Object JavaDoc encodeObject(Object JavaDoc o) throws NamingException JavaDoc {
120       try {
121         if ( (! (o instanceof Remote JavaDoc)) && (o instanceof Referenceable JavaDoc)) {
122           return ( (Referenceable JavaDoc) o).getReference();
123         }
124         else if ( (! (o instanceof Remote JavaDoc)) && (o instanceof Reference JavaDoc)) {
125           return (Reference JavaDoc) o;
126         }
127         else {
128           return o;
129         }
130       }
131       catch (Exception JavaDoc e) {
132         throw new NamingException JavaDoc("" + e);
133       }
134     }
135
136     /**
137      * Constructor, loads communication framework and instaciates initial context.
138      * @exception NamingException If naming exception occurs.
139      */

140     public WebXmlInitialContext () throws NamingException JavaDoc {
141        if (TraceCarol.isDebugJndiCarol()) {
142             TraceCarol.debugJndiCarol("WebXmlInitialContext.WebXmlInitialContext()");
143         }
144         envContext = null;
145         filePath = null;
146         isFileParsed = false;
147     }
148
149     /**
150      * Constructor, loads communication framework and instaciates initial context.
151      * @param ev Context environment.
152      * @exception NamingException If naming exception occurs.
153      */

154     public WebXmlInitialContext (Hashtable JavaDoc ev) throws NamingException JavaDoc {
155        if (TraceCarol.isDebugJndiCarol()) {
156             TraceCarol.debugJndiCarol("WebXmlInitialContext.WebXmlInitialContext(Hashtable env)");
157         }
158         if (ev != null) {
159            fileEnv = (Hashtable JavaDoc)(ev.clone());
160         }
161         envContext = null;
162         filePath = null;
163         isFileParsed = false;
164     }
165
166
167     // Inital context wrapper see the Context documentation for this methods
168
public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
169       if (TraceCarol.isDebugJndiCarol()) {
170         TraceCarol.debugJndiCarol("WebXmlInitialContext.lookup(\"" + name + "\")");
171       }
172       if ( (name == null) || (name.equals(""))) {
173         return (new WebXmlInitialContext(fileEnv));
174       }
175       Object JavaDoc o = bindings.get(name);
176       if (o != null) {
177         return resolveObject(o, new CompositeName JavaDoc(name));
178       }
179       else {
180         throw new NameNotFoundException JavaDoc(name + " not found");
181       }
182     }
183
184     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
185       return lookup(name.toString());
186     }
187
188     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
189       if (TraceCarol.isDebugJndiCarol()) {
190         TraceCarol.debugJndiCarol("WebXmlInitialContext.bind(\"" + name + "\"," +
191                                   simpleClass(obj.getClass().getName()) +
192                                   " object)");
193       }
194       if (name.equals("")) {
195         throw new InvalidNameException JavaDoc("Cannot bind empty name");
196       }
197       if (name.equals(FILE_JNDI_ABS_PATH_NAME)) {
198         this.filePath = (String JavaDoc) obj;
199         if ( (this.envContext != null) && (!this.isFileParsed)) {
200           parseWebXmlFile(this.filePath);
201           this.isFileParsed = true;
202         }
203       }
204       else {
205         if (name.equals(CONTEXT_FOR_ENV_BIND)) {
206           this.envContext = (Context JavaDoc) obj;
207           if (this.filePath != null) {
208             parseWebXmlFile(this.filePath);
209             this.isFileParsed = true;
210           }
211         }
212         else {
213           if (bindings.get(name) != null) {
214             throw new NameAlreadyBoundException JavaDoc("Use rebind to override");
215           }
216           bindings.put(name, encodeObject(obj));
217         }
218       }
219     }
220
221     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
222       bind(name.toString(), obj);
223     }
224
225     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
226       if (TraceCarol.isDebugJndiCarol()) {
227         TraceCarol.debugJndiCarol("WebXmlInitialContext.rebind(\"" + name + "\"," +
228                                   simpleClass(obj.getClass().getName()) +
229                                   " object)");
230       }
231       if (name.equals("")) {
232         throw new InvalidNameException JavaDoc("Cannot bind empty name");
233       }
234       if (name.equals(FILE_JNDI_ABS_PATH_NAME)) {
235         this.filePath = (String JavaDoc) obj;
236         if (this.envContext != null) {
237           parseWebXmlFile(this.filePath);
238           this.isFileParsed = true;
239         }
240       }
241       else {
242         if (name.equals(CONTEXT_FOR_ENV_BIND)) {
243           this.envContext = (Context JavaDoc) obj;
244           if (this.filePath != null) {
245             parseWebXmlFile(this.filePath);
246             this.isFileParsed = true;
247           }
248         }
249         else {
250           bindings.put(name, encodeObject(obj));
251         }
252       }
253     }
254
255     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
256         rebind(name.toString(), obj);
257     }
258
259     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
260       if (TraceCarol.isDebugJndiCarol()) {
261         TraceCarol.debugJndiCarol("WebXmlInitialContext.unbind(\"" + name + "\")");
262       }
263       if (name.equals("")) {
264         throw new InvalidNameException JavaDoc("Cannot unbind empty name");
265       }
266       bindings.remove(name);
267     }
268
269     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
270         unbind(name.toString());
271     }
272
273     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc {
274       if (TraceCarol.isDebugJndiCarol()) {
275         TraceCarol.debugJndiCarol("WebXmlInitialContext.rename(\"" + oldName +
276                                   "\",\"" + newName + "\")");
277       }
278       if (oldName.equals("") || newName.equals(""))
279         throw new InvalidNameException JavaDoc("Cannot rename empty name");
280       if (bindings.get(newName) != null)
281         throw new NameAlreadyBoundException JavaDoc(newName + " is already bound");
282
283       Object JavaDoc oldb = bindings.remove(oldName);
284       if (oldb == null)
285         throw new NameNotFoundException JavaDoc(oldName + " not bound");
286       bindings.put(newName, oldb);
287     }
288
289     public void rename(Name JavaDoc oldname, Name JavaDoc newname) throws NamingException JavaDoc {
290         rename(oldname.toString(), newname.toString());
291     }
292
293     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc {
294       if (TraceCarol.isDebugJndiCarol()) {
295         TraceCarol.debugJndiCarol("WebXmlInitialContext.list(\"" + name + "\")");
296       }
297       if (name.equals("")) {
298         return new WebXmlNames(bindings.keys());
299       }
300
301       Object JavaDoc target = lookup(name);
302       if (target instanceof Context JavaDoc) {
303         return ( (Context JavaDoc) target).list("");
304       }
305       throw new NotContextException JavaDoc(name + " cannot be listed");
306     }
307
308     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc {
309         return list(name.toString());
310     }
311
312     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc {
313       if (TraceCarol.isDebugJndiCarol()) {
314         TraceCarol.debugJndiCarol("WebXmlInitialContext.listBindings(\"" + name +
315                                   "\")/rmi name=\"");
316       }
317       if (name.equals("")) {
318         return new WebXmlBindings(bindings.keys());
319       }
320       Object JavaDoc target = lookup(name);
321       if (target instanceof Context JavaDoc) {
322         return ( (Context JavaDoc) target).listBindings("");
323       }
324       throw new NotContextException JavaDoc(name + " cannot be listed");
325     }
326
327     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
328       return listBindings(name.toString());
329     }
330
331     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
332       TraceCarol.error("WebXmlInitialContext.destroySubcontext(\"" + name +
333                        "\"): Not supported");
334       throw new OperationNotSupportedException JavaDoc(
335           "WebXmlInitialContext.destroySubcontext(\"" + name +
336           "\"): Not supported");
337     }
338
339     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
340       destroySubcontext(name.toString());
341     }
342
343     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc {
344       TraceCarol.error("WebXmlInitialContext.createSubcontext(\"" + name +
345                        "\"): Not supported");
346       throw new OperationNotSupportedException JavaDoc(
347           "WebXmlInitialContext.createSubcontext(\"" + name +
348           "\"): Not supported");
349     }
350
351     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
352         return createSubcontext(name.toString());
353     }
354
355     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
356       if (TraceCarol.isDebugJndiCarol()) {
357         TraceCarol.debugJndiCarol("WebXmlInitialContext.lookupLink(\"" + name +
358                                   "\")");
359       }
360       return lookup(name);
361     }
362
363     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
364         return lookupLink(name.toString());
365     }
366
367     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
368       if (TraceCarol.isDebugJndiCarol()) {
369         TraceCarol.debugJndiCarol("WebXmlInitialContext.getNameParser(\"" + name +
370                                   "\")");
371       }
372       return fileParser;
373     }
374
375     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
376         return getNameParser(name.toString());
377     }
378
379     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws NamingException JavaDoc {
380       if (TraceCarol.isDebugJndiCarol()) {
381         TraceCarol.debugJndiCarol("WebXmlInitialContext.composeName(" + name +
382                                   "," + prefix + ")");
383       }
384       Name JavaDoc result = composeName(new CompositeName JavaDoc(name), new CompositeName JavaDoc(prefix));
385       return result.toString();
386     }
387
388     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
389       if (TraceCarol.isDebugJndiCarol()) {
390         TraceCarol.debugJndiCarol("WebXmlInitialContext.composeName(" + name +
391                                   "," + prefix + ")");
392       }
393       Name JavaDoc result = (Name JavaDoc) (prefix.clone());
394       result.addAll(name);
395       return result;
396     }
397
398     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal) throws NamingException JavaDoc {
399       if (TraceCarol.isDebugJndiCarol()) {
400         TraceCarol.debugJndiCarol("WebXmlInitialContext.addToEnvironment(\"" +
401                                   propName + "\"," +
402                                   simpleClass(propVal.getClass().getName()) +
403                                   " object)");
404       }
405       if (fileEnv == null) {
406         fileEnv = new Hashtable JavaDoc();
407       }
408
409       return fileEnv.put(propName, propVal);
410     }
411
412     public Object JavaDoc removeFromEnvironment(String JavaDoc propName) throws NamingException JavaDoc {
413       if (TraceCarol.isDebugJndiCarol()) {
414         TraceCarol.debugJndiCarol("WebXmlInitialContext.removeFromEnvironment(\"" +
415                                   propName + "\")");
416       }
417       if (fileEnv == null)
418         return null;
419       return fileEnv.remove(propName);
420     }
421
422     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc {
423       if (TraceCarol.isDebugJndiCarol()) {
424         TraceCarol.debugJndiCarol("WebXmlInitialContext.getEnvironment()");
425       }
426       if (fileEnv == null) {
427         fileEnv = new Hashtable JavaDoc();
428       }
429       return fileEnv;
430     }
431
432     public void close() throws NamingException JavaDoc {
433       if (TraceCarol.isDebugJndiCarol()) {
434         TraceCarol.debugJndiCarol("WebXmlInitialContext.close()");
435       }
436     }
437
438     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
439       if (TraceCarol.isDebugJndiCarol()) {
440         TraceCarol.debugJndiCarol("WebXmlInitialContext.getNameInNamespace()");
441       }
442       return "webXmlContext";
443     }
444
445     /**
446      * Just the name of the class without the package
447      *
448      * @param c Full class name.
449      * @return Name of the class without the package.
450      */

451     private String JavaDoc simpleClass(String JavaDoc c) {
452       return c.substring(c.lastIndexOf('.') + 1);
453     }
454
455     // Class for enumerating name/class pairs
456
class WebXmlNames implements NamingEnumeration JavaDoc {
457       Enumeration JavaDoc names;
458
459       WebXmlNames(Enumeration JavaDoc names) {
460         this.names = names;
461       }
462
463       public boolean hasMoreElements() {
464         return names.hasMoreElements();
465       }
466
467       public boolean hasMore() throws NamingException JavaDoc {
468         return hasMoreElements();
469       }
470
471       public Object JavaDoc nextElement() {
472         String JavaDoc name = (String JavaDoc) names.nextElement();
473         String JavaDoc className = bindings.get(name).getClass().getName();
474         return new NameClassPair JavaDoc(name, className);
475       }
476
477       public Object JavaDoc next() throws NamingException JavaDoc {
478         return nextElement();
479       }
480
481       public void close() throws NamingException JavaDoc {
482         names = null;
483       }
484     }
485
486     // Class for enumerating bindings
487
class WebXmlBindings
488         implements NamingEnumeration JavaDoc {
489       Enumeration JavaDoc names;
490
491       WebXmlBindings(Enumeration JavaDoc names) {
492         this.names = names;
493       }
494
495       public boolean hasMoreElements() {
496         return names.hasMoreElements();
497       }
498
499       public boolean hasMore() throws NamingException JavaDoc {
500         return hasMoreElements();
501       }
502
503       public Object JavaDoc nextElement() {
504         String JavaDoc name = (String JavaDoc) names.nextElement();
505         return new Binding JavaDoc(name, bindings.get(name));
506       }
507
508       public Object JavaDoc next() throws NamingException JavaDoc {
509         return nextElement();
510       }
511
512       public void close() throws NamingException JavaDoc {
513         names = null;
514         }
515     }
516
517     private void parseWebXmlFile(String JavaDoc path) {
518       //System.out.println("parseWebXmlFile");
519
File JavaDoc file = new File JavaDoc(path); // file.getCanonicalPath());
520
WebXmlUtil xmlUtil = null;
521       try {
522         xmlUtil = new WebXmlUtil(file);
523       }
524       catch (Exception JavaDoc e){
525         System.err.println("Error in forming FileInputStream");
526       }
527       try {
528         xmlUtil.parseTree(this.envContext);
529       }
530       catch (Exception JavaDoc e) {
531         System.err.println("Error in processing web.xml file");
532         e.printStackTrace();
533       }
534     }
535
536 }
537
Popular Tags