01: package org.enhydra.shark.ejb;
02:
03: import java.io.InputStream;
04: import java.net.URL;
05: import java.net.URLConnection;
06: import java.util.Hashtable;
07: import java.util.Properties;
08:
09: import javax.naming.InitialContext;
10:
11: /**
12: * Different InitialContext are created for different EJB containers.
13: * @author T.Jovanovic
14: * @version 0.1
15: */
16: public class EJBJNDIContext {
17:
18: private static EJBJNDIContext instance = null;
19:
20: private static Hashtable env;
21: private static InitialContext initialContext;
22:
23: private static final String FACTORY_INITIAL = "java.naming.factory.initial";
24: private static final String PROVIDER_URL = "java.naming.provider.url";
25: private static final String SECURITY_PRINCIPAL = "java.naming.security.principal";
26: private static final String SECURITY_CREDENTIALS = "java.naming.security.credentials";
27:
28: /**
29: *
30: */
31: public static EJBJNDIContext getInstance() {
32: if (null == instance) {
33: instance = new EJBJNDIContext();
34: }
35: return instance;
36: }
37:
38: /**
39: * Constructor is private to ensure nobody instantiate this one.
40: */
41: private EJBJNDIContext() {
42: //URL u = EJBJNDIContext.class.getClassLoader().getResource("org/enhydra/shark/ejb/shark_client.properties");
43: URL u = EJBJNDIContext.class.getClassLoader().getResource(
44: "geronimo_client.properties");
45: try {
46: URLConnection urlConnection = u.openConnection();
47: InputStream is = urlConnection.getInputStream();
48: Properties props = new Properties();
49: props.load(is);
50: is.close();
51: env = new Hashtable();
52: env.put(FACTORY_INITIAL, props.getProperty(FACTORY_INITIAL,
53: "org.openejb.client.RemoteInitialContextFactory"));
54: env.put(PROVIDER_URL, props.getProperty(PROVIDER_URL,
55: "localhost:4201"));
56: env.put(SECURITY_PRINCIPAL, props.getProperty(
57: SECURITY_PRINCIPAL, "system"));
58: env.put(SECURITY_CREDENTIALS, props.getProperty(
59: SECURITY_CREDENTIALS, "manager"));
60: } catch (Exception ex) {
61: env = null;
62: //ex.printStackTrace();
63: }
64: }
65:
66: /**
67: *
68: */
69: public InitialContext getInitialContext() {
70: try {
71: if (env == null)
72: initialContext = new InitialContext();
73: else
74: initialContext = new InitialContext(env);
75: } catch (Exception e) {
76: initialContext = null;
77: }
78: return initialContext;
79: }
80:
81: }
|