Author: jlmonteiro
Date: Thu Jul 15 09:37:38 2010
New Revision: 964353

URL: http://svn.apache.org/viewvc?rev=964353&view=rev
Log:
OPENEJB-1305: patch from Andy. Hook standalone shutdown process.
Cool patch! Thanks Andy

Modified:
    
openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/Main.java
    
openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/Server.java
    
openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/SimpleServiceManager.java
    
openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/admin/AdminDaemon.java

Modified: 
openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/Main.java
URL: 
http://svn.apache.org/viewvc/openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/Main.java?rev=964353&r1=964352&r2=964353&view=diff
==============================================================================
--- 
openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/Main.java
 (original)
+++ 
openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/Main.java
 Thu Jul 15 09:37:38 2010
@@ -64,8 +64,8 @@ public class Main {
             Map<String, Properties> serviceEntries = 
finder.mapAvailableProperties(ServerService.class.getName());
             services = serviceEntries.keySet();
             for (String service : services) {
-                options.addOption(option(null, service+"-port", "int", 
"cmd.start.opt.port", service));
-                options.addOption(option(null, service+"-bind", "host", 
"cmd.start.opt.bind", service));
+                options.addOption(option(null, service + "-port", "int", 
"cmd.start.opt.port", service));
+                options.addOption(option(null, service + "-bind", "host", 
"cmd.start.opt.bind", service));
             }
         } catch (IOException e) {
             services = Collections.EMPTY_SET;
@@ -115,17 +115,17 @@ public class Main {
             String[] opts = {"port", "bind"};
             for (String opt : opts) {
                 String option = service + "-" + opt;
-                if (line.hasOption(option)){
-                    props.setProperty(service+"."+opt, 
line.getOptionValue(option));
+                if (line.hasOption(option)) {
+                    props.setProperty(service + "." + opt, 
line.getOptionValue(option));
                 }
             }
         }
 
         try {
-            SystemInstance system = SystemInstance.get();
+            final SystemInstance system = SystemInstance.get();
             File libs = system.getHome().getDirectory("lib");
             system.getClassPath().addJarsToPath(libs);
-            initServer();
+            initServer(system);
         } catch (DontStartServerException ignored) {
         } catch (Exception e) {
             e.printStackTrace();
@@ -134,7 +134,7 @@ public class Main {
 
     private static void help(Options options) {
         HelpFormatter formatter = new HelpFormatter();
-        formatter.printHelp("start [options]", 
"\n"+i18n("cmd.start.description"), options, "\n");
+        formatter.printHelp("start [options]", "\n" + 
i18n("cmd.start.description"), options, "\n");
     }
 
     private static Option option(String shortOpt, String longOpt, String 
description) {
@@ -142,21 +142,23 @@ public class Main {
     }
 
     private static Option option(String shortOpt, String longOpt, String 
argName, String description, Object... details) {
-        return 
OptionBuilder.withLongOpt(longOpt).withArgName(argName).hasArg().withDescription(i18n(description,details)).create(shortOpt);
+        return 
OptionBuilder.withLongOpt(longOpt).withArgName(argName).hasArg().withDescription(i18n(description,
 details)).create(shortOpt);
     }
 
     private static String i18n(String key, Object... details) {
         return messages.format(key, details);
     }
 
+    private static void initServer(final SystemInstance system) throws 
Exception {
+        Server server = Server.getInstance();
+        server.init(system.getProperties());
 
-    private static void initServer() throws Exception {
-        Server server = new Server();
-        server.init(SystemInstance.get().getProperties());
+        system.setComponent(Server.class, server);
         server.start();
     }
 }
 
 class DontStartServerException extends Exception {
+
     private static final long serialVersionUID = 1L;
 }

Modified: 
openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/Server.java
URL: 
http://svn.apache.org/viewvc/openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/Server.java?rev=964353&r1=964352&r2=964353&view=diff
==============================================================================
--- 
openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/Server.java
 (original)
+++ 
openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/Server.java
 Thu Jul 15 09:37:38 2010
@@ -35,14 +35,14 @@ import org.apache.openejb.util.Propertie
  */
 public class Server implements Service {
     // FIXME: Remove it completely once we ensure PropertiesService (below) 
works well
+
     Properties props;
-    
     private PropertiesService propertiesService;
+    private static Server server;
+    private static ServiceManager manager;
 
-    static Server server;
-    private ServiceManager manager;
+    public static Server getInstance() {
 
-    public static Server getServer() {
         if (server == null) {
             server = new Server();
         }
@@ -51,12 +51,13 @@ public class Server implements Service {
     }
 
     // TODO: Remove it once init() suits our (initialisation) needs 
+    @Override
     public void init(Properties props) throws Exception {
         this.props = props;
 
         SystemInstance system = SystemInstance.get();
         File home = system.getHome().getDirectory();
-        system.setProperty("openejb.deployments.classpath.include", 
".*/"+home.getName()+"/lib/.*");
+        system.setProperty("openejb.deployments.classpath.include", ".*/" + 
home.getName() + "/lib/.*");
         system.setProperty("openejb.deployments.classpath.require.descriptor", 
"true");
         system.setProperty("openejb.deployments.classpath.filter.systemapps", 
"false");
 
@@ -66,9 +67,10 @@ public class Server implements Service {
             System.out.println("[init] OpenEJB Remote Server");
         }
 
-        if (manager == null){
+        if (manager == null) {
             manager = ServiceManager.getManager();
         }
+
         manager.init();
     }
 
@@ -85,7 +87,7 @@ public class Server implements Service {
             System.out.println("[init] OpenEJB Remote Server");
         }
 
-        manager.init();
+        manager.init();        
     }
 
     public void start() throws Exception {
@@ -93,26 +95,25 @@ public class Server implements Service {
     }
 
     public void stop() throws Exception {
-        manager.stop();
         OpenEJB.destroy();
+        manager.stop();
     }
 
     public void addService(URI uri) {
-
     }
 
     public static class ServerServiceFactory {
+
         public ServerService createService(URI location) throws IOException {
             return null;
         }
     }
 
     public void setServiceManager(ServiceManager serviceManager) {
-        this.manager = serviceManager;
+        manager = serviceManager;
     }
-    
+
     public void setPropertiesService(PropertiesService propertiesService) {
         this.propertiesService = propertiesService;
     }
 }
-

Modified: 
openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/SimpleServiceManager.java
URL: 
http://svn.apache.org/viewvc/openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/SimpleServiceManager.java?rev=964353&r1=964352&r2=964353&view=diff
==============================================================================
--- 
openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/SimpleServiceManager.java
 (original)
+++ 
openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/SimpleServiceManager.java
 Thu Jul 15 09:37:38 2010
@@ -23,13 +23,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
-import javax.naming.Binding;
-import javax.naming.NamingEnumeration;
-import javax.naming.NamingException;
-import javax.resource.spi.ResourceAdapter;
 
 import org.apache.openejb.loader.SystemInstance;
-import org.apache.openejb.spi.ContainerSystem;
 import org.apache.xbean.finder.ResourceFinder;
 
 /**
@@ -39,7 +34,6 @@ import org.apache.xbean.finder.ResourceF
 public class SimpleServiceManager extends ServiceManager {
 
     private ServerService[] daemons;
-
     private boolean stop = false;
 
     public SimpleServiceManager() {
@@ -61,9 +55,9 @@ public class SimpleServiceManager extend
     // Each contains the class name of the daemon implamentation
     // The port to use
     // whether it's turned on
-
     // May be reusable elsewhere, move if another use occurs
     public static class ServiceFinder {
+
         private final ResourceFinder resourceFinder;
         private ClassLoader classLoader;
 
@@ -141,7 +135,7 @@ public class SimpleServiceManager extend
                     printRow(d.getName(), d.getIP(), d.getPort() + "");
                 }
             } catch (Exception e) {
-                logger.fatal("Service Start Failed: "+d.getName() + " " + 
d.getIP() + " " + d.getPort() + ": " + e.getMessage());
+                logger.fatal("Service Start Failed: " + d.getName() + " " + 
d.getIP() + " " + d.getPort() + ": " + e.getMessage());
                 if (display) {
                     printRow(d.getName(), "----", "FAILED");
                 }
@@ -151,17 +145,19 @@ public class SimpleServiceManager extend
             System.out.println("-------");
             System.out.println("Ready!");
         }
-        if (!block) return;
+        if (!block) {
+            return;
+        }
 
         /*
-        * This will cause the user thread (the thread that keeps the
-        *  vm alive) to go into a state of constant waiting.
-        *  Each time the thread is woken up, it checks to see if
-        *  it should continue waiting.
-        *
-        *  To stop the thread (and the VM), just call the stop method
-        *  which will set 'stop' to true and notify the user thread.
-        */
+         * This will cause the user thread (the thread that keeps the
+         *  vm alive) to go into a state of constant waiting.
+         *  Each time the thread is woken up, it checks to see if
+         *  it should continue waiting.
+         *
+         *  To stop the thread (and the VM), just call the stop method
+         *  which will set 'stop' to true and notify the user thread.
+         */
         try {
             while (!stop) {
 
@@ -175,36 +171,14 @@ public class SimpleServiceManager extend
 
     @Override
     public synchronized void stop() throws ServiceException {
-        logger.info("Received stop signal");
+        logger.info("Stopping server services");
         stop = true;
 
-        try {
-            ContainerSystem containerSystem = 
SystemInstance.get().getComponent(ContainerSystem.class);
-            NamingEnumeration<Binding> namingEnumeration = null;
-            try {
-                namingEnumeration = 
containerSystem.getJNDIContext().listBindings("openejb/resourceAdapter");
-            } catch (NamingException ignored) {
-                // no resource adapters were created
-            }
-            while (namingEnumeration != null && 
namingEnumeration.hasMoreElements()) {
-                Binding binding = namingEnumeration.nextElement();
-                Object object = binding.getObject();
-                ResourceAdapter resourceAdapter = (ResourceAdapter) object;
-                try {
-                    resourceAdapter.stop();
-                } catch (Exception e) {
-                    logger.fatal("ResourceAdapter Shutdown Failed: 
"+binding.getName(), e);
-                }
-            }
-        } catch (Throwable e) {
-            logger.fatal("Unable to get ResourceAdapters from JNDI.  Stop must 
be called on them for proper vm shutdown.", e);
-        }
-
         for (int i = 0; i < daemons.length; i++) {
             try {
                 daemons[i].stop();
             } catch (ServiceException e) {
-                logger.fatal("Service Shutdown Failed: 
"+daemons[i].getName()+".  Exception: "+e.getMessage(), e);
+                logger.fatal("Service Shutdown Failed: " + 
daemons[i].getName() + ".  Exception: " + e.getMessage(), e);
             }
         }
         notifyAll();

Modified: 
openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/admin/AdminDaemon.java
URL: 
http://svn.apache.org/viewvc/openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/admin/AdminDaemon.java?rev=964353&r1=964352&r2=964353&view=diff
==============================================================================
--- 
openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/admin/AdminDaemon.java
 (original)
+++ 
openejb/branches/openejb-3.1.x/server/openejb-server/src/main/java/org/apache/openejb/server/admin/AdminDaemon.java
 Thu Jul 15 09:37:38 2010
@@ -25,12 +25,16 @@ import org.apache.openejb.server.Service
 import org.apache.openejb.server.Server;
 import org.apache.openejb.client.RequestMethodConstants;
 import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.util.LogCategory;
+import org.apache.openejb.util.Logger;
 
 public class AdminDaemon implements ServerService {
 
+    @Override
     public void init(Properties props) throws Exception {
     }
 
+    @Override
     public void service(Socket socket) throws ServiceException, IOException {
         InputStream in = null;
 
@@ -39,54 +43,68 @@ public class AdminDaemon implements Serv
 
             byte requestType = (byte) in.read();
 
-            if (requestType == -1) {
-                return;
-            }
-
             switch (requestType) {
+                case -1:
+                    return;
                 case RequestMethodConstants.STOP_REQUEST_Quit:
                 case RequestMethodConstants.STOP_REQUEST_quit:
                 case RequestMethodConstants.STOP_REQUEST_Stop:
                 case RequestMethodConstants.STOP_REQUEST_stop:
                     Server server = 
SystemInstance.get().getComponent(Server.class);
                     server.stop();
-
+                    break;
+                default:
+                    //If this turns up in the logs then it is time to take 
action
+                    Logger.getInstance(LogCategory.OPENEJB_SERVER, 
AdminDaemon.class).warning("Invalid Server Socket request: " + requestType);
+                    break;
             }
 
-        } catch (SecurityException e) {
-
         } catch (Throwable e) {
-
+            Logger.getInstance(LogCategory.OPENEJB_SERVER, 
AdminDaemon.class).warning("Server Socket request failed", e);
         } finally {
-            try {
-                if (in != null) in.close();
-                if (socket != null) socket.close();
-            } catch (Throwable t) {
+            if (null != in) {
+                try {
+                    in.close();
+                } catch (Throwable t) {
+                    //Ignore
+                }
+            }
 
+            if (null != socket) {
+                try {
+                    socket.close();
+                } catch (Throwable t) {
+                    //Ignore
+                }
             }
         }
     }
 
+    @Override
     public void service(InputStream in, OutputStream out) throws 
ServiceException, IOException {
         throw new UnsupportedOperationException("Method not implemented: 
service(InputStream in, OutputStream out)");
     }
-    
+
+    @Override
     public void start() throws ServiceException {
     }
 
+    @Override
     public void stop() throws ServiceException {
     }
 
+    @Override
     public int getPort() {
         return 0;
     }
 
+    @Override
     public String getIP() {
         return "";
     }
 
+    @Override
     public String getName() {
         return "admin thread";
     }
-
 }


Reply via email to