Author: dblevins
Date: Wed Jan 13 18:50:13 2010
New Revision: 898876

URL: http://svn.apache.org/viewvc?rev=898876&view=rev
Log:
Abstracted out the code from LocalInitialContext that manipulates the 
ServiceManager to a more reusable class.

Added:
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/ServiceManagerProxy.java
   (with props)
Modified:
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/client/LocalInitialContext.java

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/client/LocalInitialContext.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/client/LocalInitialContext.java?rev=898876&r1=898875&r2=898876&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/client/LocalInitialContext.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/client/LocalInitialContext.java
 Wed Jan 13 18:50:13 2010
@@ -27,7 +27,7 @@
 import org.apache.openejb.spi.ContainerSystem;
 import org.apache.openejb.util.Logger;
 import org.apache.openejb.util.LogCategory;
-import org.apache.openejb.util.Debug;
+import org.apache.openejb.util.ServiceManagerProxy;
 import org.apache.openejb.core.ivm.ClientSecurity;
 import org.apache.openejb.core.ivm.naming.ContextWrapper;
 
@@ -38,32 +38,28 @@
 import java.util.Hashtable;
 import java.util.Properties;
 import java.util.List;
-import java.util.Map;
-import java.lang.reflect.Method;
-import java.lang.reflect.InvocationTargetException;
 
 /**
  * @version $Rev$ $Date$
  */
 public class LocalInitialContext extends ContextWrapper {
 
-    private static final String OPENEJB_EMBEDDED_REMOTABLE = 
"openejb.embedded.remotable";
-    private static Logger logger = 
Logger.getInstance(LogCategory.OPENEJB_STARTUP, LocalInitialContext.class);
+    public static final String OPENEJB_EMBEDDED_REMOTABLE = 
"openejb.embedded.remotable";
+    static Logger logger = Logger.getInstance(LogCategory.OPENEJB_STARTUP, 
LocalInitialContext.class);
 
     private final LocalInitialContextFactory factory;
     private Properties properties;
     private Object clientIdentity;
-    private Object serviceManager;
 
     private static final String ON_CLOSE = 
"openejb.embedded.initialcontext.close";
     private Close onClose;
     private Options options;
+    private ServiceManagerProxy serviceManager;
 
     public static enum Close {
         LOGOUT, DESTROY;
     }
 
-
     public LocalInitialContext(Hashtable env, LocalInitialContextFactory 
factory) throws NamingException {
         super(getContainerSystemEjbContext());
         properties = new Properties();
@@ -95,7 +91,9 @@
     }
 
     private void destroy() throws NamingException {
-        stopNetworkServices();
+        if (serviceManager != null) {
+            serviceManager.stop();
+        }
         tearDownOpenEJB();
     }
 
@@ -146,69 +144,12 @@
         if (!options.get(OPENEJB_EMBEDDED_REMOTABLE, false)) {
             return;
         }
-        ClassLoader classLoader = 
Thread.currentThread().getContextClassLoader();
 
         try {
-            Class serviceManagerClass = 
classLoader.loadClass("org.apache.openejb.server.ServiceManager");
-
-            Method get = serviceManagerClass.getMethod("get");
-
-            try {
-                if (get.invoke(null) != null) return;
-            } catch (InvocationTargetException e) {
-                return;
-            }
-
-            logger.info("Starting network services");
-
-            Method getManager = serviceManagerClass.getMethod("getManager");
-            Method init = serviceManagerClass.getMethod("init");
-            Method start = serviceManagerClass.getMethod("start", 
boolean.class);
-
-            try {
-                serviceManager = getManager.invoke(null);
-            } catch (InvocationTargetException e) {
-                String msg = "Option Enabled '" + OPENEJB_EMBEDDED_REMOTABLE + 
"'.  Error, unable to instantiate ServiceManager class 
'org.apache.openejb.server.ServiceManager'.";
-                throw new IllegalStateException(msg, e);
-            }
-
-            try {
-                init.invoke(serviceManager);
-            } catch (InvocationTargetException e) {
-                Throwable cause = e.getCause();
-                String msg = "Option Enabled '" + OPENEJB_EMBEDDED_REMOTABLE + 
"'.  Error, unable to initialize ServiceManager.  Cause: " + 
cause.getClass().getName() + ": " + cause.getMessage();
-                throw new IllegalStateException(msg, cause);
-            }
-            try {
-                start.invoke(serviceManager, false);
-            } catch (InvocationTargetException e) {
-                Throwable cause = e.getCause();
-                String msg = "Option Enabled '" + OPENEJB_EMBEDDED_REMOTABLE + 
"'.  Error, unable to start ServiceManager.  Cause: " + 
cause.getClass().getName() + ": " + cause.getMessage();
-                throw new IllegalStateException(msg, cause);
-            }
-        } catch (ClassNotFoundException e) {
-            String msg = "Enabling option '" + OPENEJB_EMBEDDED_REMOTABLE + "' 
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.";
-            throw new IllegalStateException(msg, e);
-        } catch (NoSuchMethodException e) {
-            String msg = "Option Enabled '" + OPENEJB_EMBEDDED_REMOTABLE + "'. 
 Error, 'init' and 'start' methods not found on as expected on class 
'org.apache.openejb.server.ServiceManager'.  This should never happen.";
-            throw new IllegalStateException(msg, e);
-        } catch (IllegalAccessException e) {
-            String msg = "Option Enabled '" + OPENEJB_EMBEDDED_REMOTABLE + "'. 
 Error, 'init' and 'start' methods cannot be accessed on class 
'org.apache.openejb.server.ServiceManager'.  The VM SecurityManager settings 
must be adjusted.";
-            throw new IllegalStateException(msg, e);
-        }
-    }
-
-
-    private void stopNetworkServices() {
-        if (serviceManager != null){
-            logger.info("Stopping network services");
-            try {
-                Method stop = serviceManager.getClass().getMethod("stop");
-                stop.invoke(serviceManager);
-                Thread.sleep(3000);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
+            serviceManager = new ServiceManagerProxy();
+            serviceManager.start();
+        } catch (ServiceManagerProxy.AlreadyStartedException e) {
+            logger.debug("Network services already started.  Ignoring option " 
+ OPENEJB_EMBEDDED_REMOTABLE);            
         }
     }
 

Added: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/ServiceManagerProxy.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/ServiceManagerProxy.java?rev=898876&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/ServiceManagerProxy.java
 (added)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/ServiceManagerProxy.java
 Wed Jan 13 18:50:13 2010
@@ -0,0 +1,106 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.openejb.util;
+
+import org.apache.openejb.client.LocalInitialContext;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ServiceManagerProxy {
+
+    static Logger logger = Logger.getInstance(LogCategory.OPENEJB_STARTUP, 
LocalInitialContext.class);
+
+    private final Object serviceManager;
+    private final Class serviceManagerClass;
+
+
+    public class AlreadyStartedException extends Exception {
+        public AlreadyStartedException(String s) {
+            super(s);
+        }
+    }
+
+    public ServiceManagerProxy() throws AlreadyStartedException {
+        ClassLoader classLoader = 
Thread.currentThread().getContextClassLoader();
+
+        try {
+            serviceManagerClass = 
classLoader.loadClass("org.apache.openejb.server.ServiceManager");
+        } catch (ClassNotFoundException e) {
+            String msg = "Enabling option '" + 
LocalInitialContext.OPENEJB_EMBEDDED_REMOTABLE + "' 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.";
+            throw new IllegalStateException(msg, e);
+        }
+
+        Method get = getMethod("get");
+        Method getManager = getMethod("getManager");
+
+        if (invoke(get, null) != null) throw new 
AlreadyStartedException("Server services already started");
+
+        serviceManager = invoke(getManager, null);
+
+        logger.info("Initializing network services");
+
+        Method init = getMethod("init");
+
+        invoke(init, serviceManager);
+    }
+
+    private Method getMethod(String name, Class... parameterTypes) {
+        try {
+            return serviceManagerClass.getMethod(name, parameterTypes);
+        } catch (NoSuchMethodException e) {
+            throw new IllegalStateException("Cannot load the ServiceManager", 
e);
+        }
+    }
+
+
+    private Object invoke(Method method, Object obj, Object... args) {
+        try {
+            return method.invoke(obj, args);
+        } catch (IllegalAccessException e) {
+            throw new IllegalStateException(e);
+        } catch (InvocationTargetException e) {
+            throw new IllegalStateException("Failed executing ServiceManager." 
+ method.getName(), e.getCause());
+        }
+    }
+
+    public void start() {
+
+        logger.info("Initializing network services");
+
+        Method start = getMethod("start", boolean.class);
+
+        invoke(start, serviceManager, false);
+    }
+
+    public void stop() {
+        logger.info("Stopping network services");
+
+        Method stop = getMethod("stop");
+
+        invoke(stop, serviceManager);
+
+        try {
+            Thread.sleep(3000);
+        } catch (InterruptedException e) {
+            Thread.interrupted();
+        }
+    }
+}

Propchange: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/ServiceManagerProxy.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to