1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
49
50 public class SimpleContext extends DelegatingContext {
51
52
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
58 public static final String SIMPLE_SPACE = "org.osjava.sj.space";
59
60
61
62
63
64
65
66
67
68
69
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
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 }