View Javadoc

1   /*
2    * Copyright (c) 2003-2005, Henri Yandell
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or 
6    * without modification, are permitted provided that the 
7    * following conditions are met:
8    * 
9    * + Redistributions of source code must retain the above copyright notice, 
10   *   this list of conditions and the following disclaimer.
11   * 
12   * + Redistributions in binary form must reproduce the above copyright notice, 
13   *   this list of conditions and the following disclaimer in the documentation 
14   *   and/or other materials provided with the distribution.
15   * 
16   * + Neither the name of Simple-JNDI nor the names of its contributors 
17   *   may be used to endorse or promote products derived from this software 
18   *   without specific prior written permission.
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  package org.osjava.sj;
33  
34  import org.osjava.sj.jndi.DelegatingContext;
35  import org.osjava.sj.jndi.AbstractContext;
36  
37  import java.io.File;
38  import java.io.IOException;
39  
40  import java.util.Hashtable;
41  import javax.naming.NamingException;
42  import javax.naming.InitialContext;
43  import javax.naming.Context;
44  
45  import org.osjava.sj.loader.JndiLoader;
46  import org.osjava.sj.loader.util.Utils;
47  
48  // job is to hide the JndiLoader, apart from a jndi.properties entry
49  // can also handle switching . to / so that the delimiter may be settable
50  public class SimpleContext extends DelegatingContext {
51  
52      // root
53      public static final String SIMPLE_ROOT = "org.osjava.sj.root";
54  
55      public static final String SIMPLE_DELEGATE = "org.osjava.sj.factory";
56  
57      // option for top level space; ie) java:comp
58      public static final String SIMPLE_SPACE = "org.osjava.sj.space";
59  
60      /*
61       * 
62       * root
63       *    org.osjava.jndi.root
64       * separator, or just put them in as contexts?
65       *    org.osjava.jndi.delimiter
66       * option for top level space; ie) java:comp
67       *    org.osjava.jndi.space
68       * share the same InitialContext
69       *    org.osjava.jndi.shared
70       */
71      public SimpleContext(Hashtable env) throws NamingException {
72          super(createContext(env));
73  
74          JndiLoader loader = new JndiLoader(env);
75  
76          String root = (String) env.get(SIMPLE_ROOT);
77  
78          if(root == null) {
79              throw new IllegalStateException("Property "+SIMPLE_ROOT+" is mandatory. ");
80          }
81  
82          if(root.startsWith("file://")) {
83              root = root.substring("file://".length());
84          }
85  
86          if(!AbstractContext.isSharedAndLoaded()) {
87              Context ctxt = this;
88              String space = (String) env.get(SIMPLE_SPACE);
89              if(space != null) {
90                  // make contexts for space...
91                  String[] array = Utils.split(space, (String) env.get(JndiLoader.SIMPLE_DELIMITER) );
92                  for(int i=0; i<array.length; i++) {
93                      ctxt = ctxt.createSubcontext(array[i]);
94                  }
95              }
96  
97              try {
98                  loader.loadDirectory( new File(root), ctxt );
99              } catch(IOException ioe) {
100                 throw new NamingException("Unable to load data from directory: "+root+" due to error: "+ioe.getMessage());
101             }
102         }
103     }
104     
105     private static InitialContext createContext(Hashtable env) throws NamingException {
106 
107         copyFromSystemProperties(env, JndiLoader.SIMPLE_DELIMITER);
108         copyFromSystemProperties(env, SIMPLE_ROOT);
109         copyFromSystemProperties(env, SIMPLE_SPACE);
110         copyFromSystemProperties(env, JndiLoader.SIMPLE_SHARED);
111         copyFromSystemProperties(env, SIMPLE_DELEGATE);
112         
113         env.put("jndi.syntax.direction", "left_to_right");
114         if(!env.containsKey(JndiLoader.SIMPLE_DELIMITER)) {
115             env.put(JndiLoader.SIMPLE_DELIMITER, ".");
116         }
117         env.put("jndi.syntax.separator", env.get(JndiLoader.SIMPLE_DELIMITER));
118 
119         if(!env.containsKey(SIMPLE_DELEGATE)) {
120             env.put(SIMPLE_DELEGATE, "org.osjava.sj.memory.MemoryContextFactory");
121         }
122 
123         env.put("java.naming.factory.initial", env.get(SIMPLE_DELEGATE) );
124 
125         return new InitialContext(env);
126     }
127 
128     private static void copyFromSystemProperties(Hashtable env, String key) {
129         if(System.getProperty(key) != null) {
130             env.put(key, System.getProperty(key));
131         }
132     }
133 
134 }