Save This Page
Home » commons-daemon-1.0.2-src » org.apache.commons.daemon.support » [javadoc | source]
    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  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   /* @version $Id: DaemonLoader.java 912665 2010-02-22 17:16:00Z sebb $ */
   19   
   20   package org.apache.commons.daemon.support;
   21   
   22   import org.apache.commons.daemon.DaemonContext;
   23   import org.apache.commons.daemon.DaemonController;
   24   
   25   import java.lang.reflect.Method;
   26   
   27   public final class DaemonLoader {
   28   
   29       private static Controller controller = null;
   30       private static Context context = null;
   31       private static Object daemon = null;
   32       /* Methods to call */
   33       private static Method init = null;
   34       private static Method start = null;
   35       private static Method stop = null;
   36       private static Method destroy = null;
   37   
   38       public static void version() {
   39           System.err.println("java version \""+
   40                              System.getProperty("java.version")+
   41                              "\"");
   42           System.err.println(System.getProperty("java.runtime.name")+
   43                              " (build "+
   44                              System.getProperty("java.runtime.version")+
   45                              ")");
   46           System.err.println(System.getProperty("java.vm.name")+
   47                              " (build "+
   48                              System.getProperty("java.vm.version")+
   49                              ", "+
   50                              System.getProperty("java.vm.info")+
   51                              ")");
   52       }
   53   
   54       public static boolean check(String cn) {
   55           try {
   56               /* Check the class name */
   57               if (cn==null)
   58                   throw new NullPointerException("Null class name specified");
   59   
   60               /* Get the ClassLoader loading this class */
   61               ClassLoader cl=DaemonLoader.class.getClassLoader();
   62               if (cl==null) {
   63                   System.err.println("Cannot retrieve ClassLoader instance");
   64                   return(false);
   65               }
   66   
   67               /* Find the required class */
   68               Class c=cl.loadClass(cn);
   69   
   70               /* This should _never_ happen, but doublechecking doesn't harm */
   71               if (c==null) throw new ClassNotFoundException(cn);
   72   
   73               /* Create a new instance of the daemon */
   74               Object s=c.newInstance();
   75   
   76           } catch (Throwable t) {
   77               /* In case we encounter ANY error, we dump the stack trace and
   78                  return false (load, start and stop won't be called). */
   79               t.printStackTrace(System.err);
   80               return(false);
   81           }
   82           /* The class was loaded and instantiated correctly, we can return */
   83           return(true);
   84       }
   85   
   86       public static boolean load(String cn, String ar[]) {
   87           try {
   88               /* Make sure any previous instance is garbage collected */
   89               System.gc();
   90   
   91               /* Check if the underlying libray supplied a valid list of
   92                  arguments */
   93               if (ar==null) ar=new String[0];
   94   
   95               /* Check the class name */
   96               if (cn==null)
   97                   throw new NullPointerException("Null class name specified");
   98   
   99               /* Get the ClassLoader loading this class */
  100               ClassLoader cl=DaemonLoader.class.getClassLoader();
  101               if (cl==null) {
  102                   System.err.println("Cannot retrieve ClassLoader instance");
  103                   return(false);
  104               }
  105   
  106               /* Find the required class */
  107               Class c=cl.loadClass(cn);
  108   
  109               /* This should _never_ happen, but doublechecking doesn't harm */
  110               if (c==null) throw new ClassNotFoundException(cn);
  111   
  112               /* Check interface */
  113               boolean isdaemon = false;
  114               try {
  115                 Class dclass = cl.loadClass("org.apache.commons.daemon.Daemon");
  116                 isdaemon = dclass.isAssignableFrom(c);
  117               } catch(Exception cnfex) {
  118                 // Swallow if Daemon not found.
  119               }
  120   
  121               /* Check methods */
  122               Class[] myclass = new Class[1];
  123               if (isdaemon) {
  124                 myclass[0] = DaemonContext.class;
  125               } else {
  126                 myclass[0] = ar.getClass();
  127               }
  128   
  129               init = c.getMethod("init",myclass);
  130   
  131               myclass = null;
  132               start = c.getMethod("start",myclass);
  133   
  134               stop = c.getMethod("stop",myclass);
  135   
  136               destroy = c.getMethod("destroy",myclass);
  137   
  138               /* Create a new instance of the daemon */
  139               daemon=c.newInstance();
  140   
  141               if (isdaemon) {
  142                 /* Create a new controller instance */
  143                 controller=new Controller();
  144   
  145                 /* Set the availability flag in the controller */
  146                 controller.setAvailable(false);
  147   
  148                 /* Create context */
  149                 context = new Context();
  150                 context.setArguments(ar);
  151                 context.setController(controller);
  152   
  153                 /* Now we want to call the init method in the class */
  154                 Object arg[] = new Object[1];
  155                 arg[0] = context;
  156                 init.invoke(daemon,arg);
  157               } else {
  158                 Object arg[] = new Object[1];
  159                 arg[0] = ar;
  160                 init.invoke(daemon,arg);
  161               }
  162   
  163           } catch (Throwable t) {
  164               /* In case we encounter ANY error, we dump the stack trace and
  165                  return false (load, start and stop won't be called). */
  166               t.printStackTrace(System.err);
  167               return(false);
  168           }
  169           /* The class was loaded and instantiated correctly, we can return */
  170           return(true);
  171       }
  172   
  173       public static boolean start() {
  174           try {
  175               /* Attempt to start the daemon */
  176               Object arg[] = null;
  177               start.invoke(daemon,arg);
  178   
  179               /* Set the availability flag in the controller */
  180               if (controller != null)
  181                 controller.setAvailable(true);
  182   
  183           } catch (Throwable t) {
  184               /* In case we encounter ANY error, we dump the stack trace and
  185                  return false (load, start and stop won't be called). */
  186               t.printStackTrace(System.err);
  187               return(false);
  188           }
  189           return(true);
  190       }
  191   
  192       public static boolean stop() {
  193           try {
  194               /* Set the availability flag in the controller */
  195               if (controller != null)
  196                 controller.setAvailable(false);
  197   
  198               /* Attempt to stop the daemon */
  199               Object arg[] = null;
  200               stop.invoke(daemon,arg);
  201   
  202               /* Run garbage collector */
  203               System.gc();
  204   
  205           } catch (Throwable t) {
  206               /* In case we encounter ANY error, we dump the stack trace and
  207                  return false (load, start and stop won't be called). */
  208               t.printStackTrace(System.err);
  209               return(false);
  210           }
  211           return(true);
  212       }
  213   
  214       public static boolean destroy() {
  215           try {
  216               /* Attempt to stop the daemon */
  217               Object arg[] = null;
  218               destroy.invoke(daemon,arg);
  219   
  220               /* Run garbage collector */
  221               daemon=null;
  222               controller=null;
  223               System.gc();
  224   
  225           } catch (Throwable t) {
  226               /* In case we encounter ANY error, we dump the stack trace and
  227                  return false (load, start and stop won't be called). */
  228               t.printStackTrace(System.err);
  229               return(false);
  230           }
  231           return(true);
  232       }
  233   
  234       private static native void shutdown(boolean reload);
  235   
  236       public static class Controller implements DaemonController {
  237   
  238           boolean available=false;
  239   
  240           private Controller() {
  241               super();
  242               this.setAvailable(false);
  243           }
  244   
  245           private boolean isAvailable() {
  246               synchronized (this) {
  247                   return(this.available);
  248               }
  249           }
  250   
  251           private void setAvailable(boolean available) {
  252               synchronized (this) {
  253                   this.available=available;
  254               }
  255           }
  256   
  257           public void shutdown() throws IllegalStateException {
  258               synchronized (this) {
  259                   if (!this.isAvailable()) {
  260                       throw new IllegalStateException();
  261                   } else {
  262                       this.setAvailable(false);
  263                       DaemonLoader.shutdown(false);
  264                   }
  265               }
  266           }
  267   
  268           public void reload() throws IllegalStateException {
  269               synchronized (this) {
  270                   if (!this.isAvailable()) {
  271                       throw new IllegalStateException();
  272                   } else {
  273                       this.setAvailable(false);
  274                       DaemonLoader.shutdown(true);
  275                   }
  276               }
  277           }
  278   
  279           public void fail()
  280               throws IllegalStateException {
  281           }
  282   
  283           public void fail(String message)
  284               throws IllegalStateException {
  285           }
  286   
  287           public void fail(Exception exception)
  288               throws IllegalStateException {
  289           }
  290   
  291           public void fail(String message, Exception exception)
  292               throws IllegalStateException {
  293           }
  294   
  295       }
  296   
  297       public static class Context implements DaemonContext {
  298   
  299           DaemonController controller = null;
  300   
  301           String[] args = null;
  302   
  303           public DaemonController getController() {
  304               return controller;
  305           }
  306   
  307           public void setController(DaemonController controller) {
  308               this.controller = controller;
  309           }
  310   
  311           public String[] getArguments() {
  312               return args;
  313           }
  314   
  315           public void setArguments(String[] args) {
  316               this.args = args;
  317           }
  318   
  319       }
  320   
  321   }

Save This Page
Home » commons-daemon-1.0.2-src » org.apache.commons.daemon.support » [javadoc | source]