1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44:
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54:
55: public class NamingManager
56: {
57: public static final String CPE = "java.naming.spi.CannotProceedException";
58:
59: private static InitialContextFactoryBuilder icfb;
60:
61:
62: static ObjectFactoryBuilder ofb;
63:
64:
65: NamingManager ()
66: {
67: }
68:
69: public static boolean hasInitialContextFactoryBuilder ()
70: {
71: return icfb != null;
72: }
73:
74: public static Context getInitialContext (Hashtable environment)
75: throws NamingException
76: {
77: InitialContextFactory icf = null;
78:
79: if (icfb != null)
80: icf = icfb.createInitialContextFactory(environment);
81: else
82: {
83: String java_naming_factory_initial = null;
84: if (environment != null)
85: java_naming_factory_initial
86: = (String) environment.get (Context.INITIAL_CONTEXT_FACTORY);
87: if (java_naming_factory_initial == null)
88: java_naming_factory_initial =
89: System.getProperty (Context.INITIAL_CONTEXT_FACTORY);
90: if (java_naming_factory_initial == null)
91: throw new
92: NoInitialContextException ("Can't find property: "
93: + Context.INITIAL_CONTEXT_FACTORY);
94:
95: try
96: {
97: icf = (InitialContextFactory)Class.forName
98: (java_naming_factory_initial, true,
99: Thread.currentThread().getContextClassLoader())
100: .newInstance ();
101: }
102: catch (Exception exception)
103: {
104: NoInitialContextException e
105: = new NoInitialContextException
106: ("Can't load InitialContextFactory class: "
107: + java_naming_factory_initial);
108: e.setRootCause(exception);
109: throw e;
110: }
111: }
112:
113: return icf.getInitialContext (environment);
114: }
115:
116: static Context getURLContext (Object refInfo,
117: Name name,
118: Context nameCtx,
119: String scheme,
120: Hashtable environment)
121: throws NamingException
122: {
123: String prefixes = null;
124: if (environment != null)
125: prefixes = (String) environment.get (Context.URL_PKG_PREFIXES);
126: if (prefixes == null)
127: prefixes = System.getProperty (Context.URL_PKG_PREFIXES);
128: if (prefixes == null)
129: {
130:
131:
132: prefixes = "com.sun.jndi.url";
133: }
134:
135: scheme = scheme + "." + scheme + "URLContextFactory";
136:
137: StringTokenizer tokens = new StringTokenizer (prefixes, ":");
138: while (tokens.hasMoreTokens ())
139: {
140: String aTry = tokens.nextToken ();
141: try
142: {
143: Class factoryClass = Class.forName (aTry + "." + scheme,
144: true,
145: Thread.currentThread().getContextClassLoader());
146: ObjectFactory factory =
147: (ObjectFactory) factoryClass.newInstance ();
148: Object obj = factory.getObjectInstance (refInfo, name,
149: nameCtx, environment);
150: Context ctx = (Context) obj;
151: if (ctx != null)
152: return ctx;
153: }
154: catch (ClassNotFoundException _1)
155: {
156:
157: }
158: catch (ClassCastException _2)
159: {
160:
161:
162:
163: }
164: catch (InstantiationException _3)
165: {
166:
167:
168: }
169: catch (IllegalAccessException _4)
170: {
171:
172: }
173: catch (NamingException _5)
174: {
175: throw _5;
176: }
177: catch (Exception _6)
178: {
179:
180: }
181: }
182:
183: return null;
184: }
185:
186: public static Context getURLContext (String scheme,
187: Hashtable environment)
188: throws NamingException
189: {
190: return getURLContext (null, null, null, scheme, environment);
191: }
192:
193: public static void setObjectFactoryBuilder (ObjectFactoryBuilder builder)
194: throws NamingException
195: {
196: SecurityManager sm = System.getSecurityManager ();
197: if (sm != null)
198: sm.checkSetFactory ();
199:
200: if (ofb != null)
201: throw new IllegalStateException ("builder already installed");
202: if (builder != null)
203: ofb = builder;
204: }
205:
206: static StringTokenizer getPlusPath (String property, Hashtable env,
207: Context nameCtx)
208: throws NamingException
209: {
210: String path = (String) env.get (property);
211: if (nameCtx == null)
212: nameCtx = getInitialContext (env);
213: String path2 = (String) nameCtx.getEnvironment ().get (property);
214: if (path == null)
215: path = path2;
216: else if (path2 != null)
217: path += ":" + path2;
218: return new StringTokenizer (path != null ? path : "", ":");
219: }
220:
221: public static Object getObjectInstance (Object refInfo,
222: Name name,
223: Context nameCtx,
224: Hashtable environment)
225: throws Exception
226: {
227: ObjectFactory factory = null;
228:
229: if (ofb != null)
230: factory = ofb.createObjectFactory (refInfo, environment);
231: else
232: {
233:
234:
235: Object ref2 = refInfo;
236: if (refInfo instanceof Referenceable)
237: ref2 = ((Referenceable) refInfo).getReference ();
238: if (ref2 instanceof Reference)
239: {
240: Reference ref = (Reference) ref2;
241:
242:
243: String fClass = ref.getFactoryClassName ();
244: if (fClass != null)
245: {
246:
247: Class k = Class.forName (fClass,
248: true,
249: Thread.currentThread().getContextClassLoader());
250: factory = (ObjectFactory) k.newInstance ();
251: }
252: else
253: {
254:
255:
256:
257: Enumeration e = ref.getAll ();
258: while (e.hasMoreElements ())
259: {
260: RefAddr ra = (RefAddr) e.nextElement ();
261: if (ra instanceof StringRefAddr
262: && "URL".equals (ra.getType ()))
263: {
264: factory
265: = (ObjectFactory) getURLContext (refInfo,
266: name,
267: nameCtx,
268: (String) ra.getContent (),
269: environment);
270: Object obj = factory.getObjectInstance (refInfo,
271: name,
272: nameCtx,
273: environment);
274: if (obj != null)
275: return obj;
276: }
277: }
278:
279:
280: factory = null;
281: }
282: }
283:
284:
285: if (factory == null)
286: {
287: StringTokenizer tokens = getPlusPath (Context.OBJECT_FACTORIES,
288: environment, nameCtx);
289:
290: while (tokens.hasMoreTokens ())
291: {
292: String klassName = tokens.nextToken ();
293: Class k = Class.forName (klassName,
294: true,
295: Thread.currentThread().getContextClassLoader());
296: factory = (ObjectFactory) k.newInstance ();
297: Object obj = factory.getObjectInstance (refInfo, name,
298: nameCtx, environment);
299: if (obj != null)
300: return obj;
301: }
302:
303:
304: return refInfo;
305: }
306: }
307:
308: if (factory == null)
309: return refInfo;
310: Object obj = factory.getObjectInstance (refInfo, name,
311: nameCtx, environment);
312: return obj == null ? refInfo : obj;
313: }
314:
315: public static void setInitialContextFactoryBuilder (InitialContextFactoryBuilder builder)
316: throws NamingException
317: {
318: SecurityManager sm = System.getSecurityManager ();
319: if (sm != null)
320: sm.checkSetFactory ();
321:
322: if (icfb != null)
323: throw new IllegalStateException ("builder already installed");
324: if (builder != null)
325: icfb = builder;
326: }
327:
328: public static Context getContinuationContext (CannotProceedException cpe)
329: throws NamingException
330: {
331: Hashtable env = cpe.getEnvironment ();
332: if (env != null)
333: env.put (CPE, cpe);
334:
335:
336: try
337: {
338: Object obj = getObjectInstance (cpe.getResolvedObj(),
339: cpe.getAltName (),
340: cpe.getAltNameCtx (),
341: env);
342: if (obj != null)
343: return (Context) obj;
344: }
345: catch (Exception _)
346: {
347: }
348:
349:
350: cpe.fillInStackTrace();
351:
352: throw cpe;
353: }
354:
355: public static Object getStateToBind (Object obj, Name name,
356: Context nameCtx, Hashtable environment)
357: throws NamingException
358: {
359: StringTokenizer tokens = getPlusPath (Context.STATE_FACTORIES,
360: environment, nameCtx);
361: while (tokens.hasMoreTokens ())
362: {
363: String klassName = tokens.nextToken ();
364: try
365: {
366: Class k = Class.forName (klassName,
367: true,
368: Thread.currentThread().getContextClassLoader());
369: StateFactory factory = (StateFactory) k.newInstance ();
370: Object o = factory.getStateToBind (obj, name, nameCtx,
371: environment);
372: if (o != null)
373: return o;
374: }
375: catch (ClassNotFoundException _1)
376: {
377:
378: }
379: catch (ClassCastException _2)
380: {
381:
382:
383:
384: }
385: catch (InstantiationException _3)
386: {
387:
388:
389: }
390: catch (IllegalAccessException _4)
391: {
392:
393: }
394: }
395:
396: return obj;
397: }
398: }