KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > ejb > ContextHelper


1 package org.apache.ojb.ejb;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import javax.naming.Context JavaDoc;
19 import javax.naming.InitialContext JavaDoc;
20 import javax.naming.NamingException JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 /**
24  * Helper class for client side JNDI context lookup.
25  * This class lookup system properties for {@link Context#INITIAL_CONTEXT_FACTORY},
26  * {@link Context#PROVIDER_URL} and {@link Context#URL_PKG_PREFIXES}. If not set,
27  * InitialContext for JBoss (with default settings) was instantiated.
28  *
29  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
30  * @version $Id: ContextHelper.java,v 1.6.2.2 2005/12/21 22:21:38 tomdz Exp $
31  */

32 public class ContextHelper
33 {
34     private static InitialContext JavaDoc ctx;
35
36     public synchronized static Context JavaDoc getContext()
37     {
38         if(ctx == null)
39         {
40             Properties JavaDoc prop = System.getProperties();
41             // if jndi-properties not set as system properties, take jboss/jonas jndi
42
// properties as default
43
if (prop.getProperty(Context.INITIAL_CONTEXT_FACTORY) == null)
44             {
45                 System.out.println("System property " + Context.INITIAL_CONTEXT_FACTORY
46                         + " is not set. Try to use default setting for JNDI lookup");
47                 ctx = jbossJNDI();
48                 if(ctx == null) ctx = jonasJNDI();
49                 if(ctx == null) throw new RuntimeException JavaDoc("Can't initialize naming context");
50             }
51             else
52             {
53                 try
54                 {
55                     ctx = new InitialContext JavaDoc(prop);
56                 }
57                 catch (NamingException JavaDoc e)
58                 {
59                     e.printStackTrace();
60                 }
61             }
62             try
63             {
64                 System.out.println("Used JNDI Context: " + ctx.getEnvironment());
65 // System.out.println("Namespace=" + ctx.getNameInNamespace());
66
// System.out.println("Bindings: ==>");
67
// String nameSp = ctx.getNameInNamespace();
68
// NamingEnumeration enu = ctx.listBindings(nameSp);
69
// if(!enu.hasMore())
70
// {
71
// System.out.println("no bindings found for '" + ctx.getNameInNamespace() + "'");
72
// }
73
// while(enu.hasMore())
74
// {
75
// System.out.println("element: " + enu.nextElement());
76
// }
77
}
78             catch(NamingException JavaDoc e)
79             {
80                 e.printStackTrace();
81             }
82         }
83
84         return ctx;
85     }
86
87     private static InitialContext JavaDoc jbossJNDI()
88     {
89         InitialContext JavaDoc ctx = null;
90         try
91         {
92             System.out.println("Try to use JBoss naming service");
93             Properties JavaDoc prop = jbossNamingProperties();
94             ctx = new InitialContext JavaDoc(prop);
95         }
96         catch(NamingException JavaDoc e)
97         {
98             System.err.println("JBoss JNDI lookup failed: " + e.getMessage());
99         }
100         return ctx;
101     }
102
103     private static InitialContext JavaDoc jonasJNDI()
104     {
105         InitialContext JavaDoc ctx = null;
106         try
107         {
108             System.out.println("Try to use JOnAS naming service");
109             Properties JavaDoc prop = jonasNamingProperties();
110             ctx = new InitialContext JavaDoc(prop);
111         }
112         catch(NamingException JavaDoc e)
113         {
114             System.err.println("JonAS JNDI lookup failed: " + e.getMessage());
115             e.printStackTrace();
116         }
117         return ctx;
118     }
119
120     private static Properties JavaDoc jbossNamingProperties()
121     {
122         Properties JavaDoc prop = new Properties JavaDoc();
123         prop.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
124         prop.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
125         prop.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
126         return prop;
127     }
128
129     private static Properties JavaDoc jonasNamingProperties()
130     {
131         Properties JavaDoc prop = new Properties JavaDoc();
132         prop.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
133         //prop.setProperty(Context.PROVIDER_URL, "jrmi://localhost:2000");
134
//prop.setProperty(Context.URL_PKG_PREFIXES, "org.objectweb.jonas.naming");
135
return prop;
136     }
137 }
138
Popular Tags