001: package org.apache.ojb.ejb;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import javax.naming.Context;
019: import javax.naming.InitialContext;
020: import javax.naming.NamingException;
021: import java.util.Properties;
022:
023: /**
024: * Helper class for client side JNDI context lookup.
025: * This class lookup system properties for {@link Context#INITIAL_CONTEXT_FACTORY},
026: * {@link Context#PROVIDER_URL} and {@link Context#URL_PKG_PREFIXES}. If not set,
027: * InitialContext for JBoss (with default settings) was instantiated.
028: *
029: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
030: * @version $Id: ContextHelper.java,v 1.6.2.2 2005/12/21 22:21:38 tomdz Exp $
031: */
032: public class ContextHelper {
033: private static InitialContext ctx;
034:
035: public synchronized static Context getContext() {
036: if (ctx == null) {
037: Properties prop = System.getProperties();
038: // if jndi-properties not set as system properties, take jboss/jonas jndi
039: // properties as default
040: if (prop.getProperty(Context.INITIAL_CONTEXT_FACTORY) == null) {
041: System.out
042: .println("System property "
043: + Context.INITIAL_CONTEXT_FACTORY
044: + " is not set. Try to use default setting for JNDI lookup");
045: ctx = jbossJNDI();
046: if (ctx == null)
047: ctx = jonasJNDI();
048: if (ctx == null)
049: throw new RuntimeException(
050: "Can't initialize naming context");
051: } else {
052: try {
053: ctx = new InitialContext(prop);
054: } catch (NamingException e) {
055: e.printStackTrace();
056: }
057: }
058: try {
059: System.out.println("Used JNDI Context: "
060: + ctx.getEnvironment());
061: // System.out.println("Namespace=" + ctx.getNameInNamespace());
062: // System.out.println("Bindings: ==>");
063: // String nameSp = ctx.getNameInNamespace();
064: // NamingEnumeration enu = ctx.listBindings(nameSp);
065: // if(!enu.hasMore())
066: // {
067: // System.out.println("no bindings found for '" + ctx.getNameInNamespace() + "'");
068: // }
069: // while(enu.hasMore())
070: // {
071: // System.out.println("element: " + enu.nextElement());
072: // }
073: } catch (NamingException e) {
074: e.printStackTrace();
075: }
076: }
077:
078: return ctx;
079: }
080:
081: private static InitialContext jbossJNDI() {
082: InitialContext ctx = null;
083: try {
084: System.out.println("Try to use JBoss naming service");
085: Properties prop = jbossNamingProperties();
086: ctx = new InitialContext(prop);
087: } catch (NamingException e) {
088: System.err.println("JBoss JNDI lookup failed: "
089: + e.getMessage());
090: }
091: return ctx;
092: }
093:
094: private static InitialContext jonasJNDI() {
095: InitialContext ctx = null;
096: try {
097: System.out.println("Try to use JOnAS naming service");
098: Properties prop = jonasNamingProperties();
099: ctx = new InitialContext(prop);
100: } catch (NamingException e) {
101: System.err.println("JonAS JNDI lookup failed: "
102: + e.getMessage());
103: e.printStackTrace();
104: }
105: return ctx;
106: }
107:
108: private static Properties jbossNamingProperties() {
109: Properties prop = new Properties();
110: prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,
111: "org.jnp.interfaces.NamingContextFactory");
112: prop.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
113: prop.setProperty(Context.URL_PKG_PREFIXES,
114: "org.jboss.naming:org.jnp.interfaces");
115: return prop;
116: }
117:
118: private static Properties jonasNamingProperties() {
119: Properties prop = new Properties();
120: prop
121: .setProperty(Context.INITIAL_CONTEXT_FACTORY,
122: "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
123: //prop.setProperty(Context.PROVIDER_URL, "jrmi://localhost:2000");
124: //prop.setProperty(Context.URL_PKG_PREFIXES, "org.objectweb.jonas.naming");
125: return prop;
126: }
127: }
|