001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.client;
018:
019: import org.apache.openejb.loader.OpenEJBInstance;
020: import org.apache.openejb.loader.SystemInstance;
021:
022: import javax.naming.Context;
023: import javax.naming.NamingException;
024: import javax.naming.spi.InitialContextFactory;
025: import java.util.Hashtable;
026: import java.util.Properties;
027: import java.lang.reflect.Method;
028: import java.lang.reflect.InvocationTargetException;
029:
030: /**
031: * @version $Rev: 594246 $ $Date: 2007-11-12 10:20:54 -0800 $
032: */
033: public class LocalInitialContextFactory implements
034: javax.naming.spi.InitialContextFactory {
035:
036: private static OpenEJBInstance openejb;
037: private static final String OPENEJB_EMBEDDED_REMOTABLE = "openejb.embedded.remotable";
038:
039: public Context getInitialContext(Hashtable env)
040: throws javax.naming.NamingException {
041: init(env);
042: return getIntraVmContext(env);
043: }
044:
045: private void init(Hashtable env)
046: throws javax.naming.NamingException {
047: if (openejb != null) {
048: return;
049: }
050: try {
051: Properties properties = new Properties();
052: properties.putAll(env);
053: init(properties);
054: } catch (Exception e) {
055: throw (NamingException) new NamingException(
056: "Attempted to load OpenEJB. " + e.getMessage())
057: .initCause(e);
058: }
059: }
060:
061: public void init(Properties properties) throws Exception {
062: if (openejb != null)
063: return;
064: openejb = new OpenEJBInstance();
065: if (openejb.isInitialized())
066: return;
067: SystemInstance.init(properties);
068: openejb.init(properties);
069: if (properties.getProperty(OPENEJB_EMBEDDED_REMOTABLE, "false")
070: .equalsIgnoreCase("true")) {
071: bootServerServices();
072: }
073: }
074:
075: private void bootServerServices() {
076: ClassLoader classLoader = Thread.currentThread()
077: .getContextClassLoader();
078:
079: try {
080: Class serviceManagerClass = classLoader
081: .loadClass("org.apache.openejb.server.ServiceManager");
082: Method init = serviceManagerClass.getMethod("init");
083: Method start = serviceManagerClass.getMethod("start",
084: boolean.class);
085:
086: Object serviceManager = serviceManagerClass.newInstance();
087: try {
088: init.invoke(serviceManager);
089: } catch (InvocationTargetException e) {
090: Throwable cause = e.getCause();
091: String msg = "Option Enabled '"
092: + OPENEJB_EMBEDDED_REMOTABLE
093: + "'. Error, unable to initialize ServiceManager. Cause: "
094: + cause.getClass().getName() + ": "
095: + cause.getMessage();
096: throw new IllegalStateException(msg, cause);
097: }
098: try {
099: start.invoke(serviceManager, false);
100: } catch (InvocationTargetException e) {
101: Throwable cause = e.getCause();
102: String msg = "Option Enabled '"
103: + OPENEJB_EMBEDDED_REMOTABLE
104: + "'. Error, unable to start ServiceManager. Cause: "
105: + cause.getClass().getName() + ": "
106: + cause.getMessage();
107: throw new IllegalStateException(msg, cause);
108: }
109: } catch (ClassNotFoundException e) {
110: String msg = "Enabling option '"
111: + OPENEJB_EMBEDDED_REMOTABLE
112: + "' requires class 'org.apache.openejb.server.ServiceManager' to be available. Make sure you have the openejb-server-*.jar in your classpath and at least one protocol implementation such as openejb-ejbd-*.jar.";
113: throw new IllegalStateException(msg, e);
114: } catch (NoSuchMethodException e) {
115: String msg = "Option Enabled '"
116: + OPENEJB_EMBEDDED_REMOTABLE
117: + "'. Error, 'init' and 'start' methods not found on as expected on class 'org.apache.openejb.server.ServiceManager'. This should never happen.";
118: throw new IllegalStateException(msg, e);
119: } catch (InstantiationException e) {
120: String msg = "Option Enabled '"
121: + OPENEJB_EMBEDDED_REMOTABLE
122: + "'. Error, unable to instantiate ServiceManager class 'org.apache.openejb.server.ServiceManager'.";
123: throw new IllegalStateException(msg, e);
124: } catch (IllegalAccessException e) {
125: String msg = "Option Enabled '"
126: + OPENEJB_EMBEDDED_REMOTABLE
127: + "'. Error, 'init' and 'start' methods cannot be accessed on class 'org.apache.openejb.server.ServiceManager'. The VM SecurityManager settings must be adjusted.";
128: throw new IllegalStateException(msg, e);
129: }
130: }
131:
132: private Context getIntraVmContext(Hashtable env)
133: throws javax.naming.NamingException {
134: Context context = null;
135: try {
136: InitialContextFactory factory = null;
137: ClassLoader cl = SystemInstance.get().getClassLoader();
138: Class ivmFactoryClass = Class
139: .forName(
140: "org.apache.openejb.core.ivm.naming.InitContextFactory",
141: true, cl);
142:
143: factory = (InitialContextFactory) ivmFactoryClass
144: .newInstance();
145: context = factory.getInitialContext(env);
146: } catch (Exception e) {
147: throw (NamingException) new javax.naming.NamingException(
148: "Cannot instantiate an IntraVM InitialContext. Exception: "
149: + e.getClass().getName() + " "
150: + e.getMessage()).initCause(e);
151: }
152:
153: return context;
154: }
155: }
|