You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jf...@apache.org on 2003/06/11 23:24:11 UTC

cvs commit: jakarta-commons-sandbox/daemon/src/java/org/apache/commons/daemon/support DaemonLoader.java

jfclere     2003/06/11 14:24:11

  Modified:    daemon/src/java/org/apache/commons/daemon/support
                        DaemonLoader.java
  Log:
  Use reflexion.
  
  Revision  Changes    Path
  1.2       +64 -22    jakarta-commons-sandbox/daemon/src/java/org/apache/commons/daemon/support/DaemonLoader.java
  
  Index: DaemonLoader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/daemon/src/java/org/apache/commons/daemon/support/DaemonLoader.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DaemonLoader.java	19 Feb 2002 02:45:23 -0000	1.1
  +++ DaemonLoader.java	11 Jun 2003 21:24:11 -0000	1.2
  @@ -2,7 +2,7 @@
    *                                                                           *
    *                 The Apache Software License,  Version 1.1                 *
    *                                                                           *
  - *          Copyright (c) 1999-2001 The Apache Software Foundation.          *
  + *          Copyright (c) 1999-2003 The Apache Software Foundation.          *
    *                           All rights reserved.                            *
    *                                                                           *
    * ========================================================================= *
  @@ -63,11 +63,18 @@
   import org.apache.commons.daemon.DaemonContext;
   import org.apache.commons.daemon.DaemonController;
   
  +import java.lang.reflect.Method;
  +
   public final class DaemonLoader {
   
       private static Controller controller = null;
       private static Context context = null;
  -    private static Daemon daemon = null;
  +    private static Object daemon = null;
  +    /* Methods to call */
  +    private static Method init = null;
  +    private static Method start = null;
  +    private static Method stop = null;
  +    private static Method destroy = null;
   
       public static void version() {
           System.err.println("java version \""+
  @@ -105,7 +112,7 @@
               if (c==null) throw new ClassNotFoundException(cn);
   
               /* Create a new instance of the daemon */
  -            Daemon s=(Daemon)c.newInstance();
  +            Object s=c.newInstance();
   
           } catch (Throwable t) {
               /* In case we encounter ANY error, we dump the stack trace and
  @@ -143,22 +150,53 @@
               /* This should _never_ happen, but doublechecking doesn't harm */
               if (c==null) throw new ClassNotFoundException(cn);
   
  -            /* Create a new instance of the daemon */
  -            daemon=(Daemon)c.newInstance();
  -
  -            /* Create a new controller instance */
  -            controller=new Controller();
  +            /* Check interface */
  +            Class[] interf = c.getInterfaces();
  +            boolean isdaemon = false;
  +            if ( interf != null ) {
  +              for(int i=0;i<interf.length;i++) {
  +                if (interf[i].getName().equals("org.apache.commons.daemon.Daemon"))
  +                  isdaemon = true;
  +              }
  +            }
  +            /* Check methods */
  +            Method[] method = c.getMethods();
  +            if ( method != null) {
  +              for(int i=0;i<method.length;i++) {
  +                if (method[i].getName().equals("start"))
  +                  start = method[i];
  +                if (method[i].getName().equals("init"))
  +                  init = method[i];
  +                if (method[i].getName().equals("stop"))
  +                  stop = method[i];
  +                if (method[i].getName().equals("destroy"))
  +                  destroy = method[i];
   
  -            /* Set the availability flag in the controller */
  -            controller.setAvailable(false);
  +              }
  +            }
   
  -            /* Create context */
  -            context = new Context();
  -            context.setArguments(ar);
  -            context.setController(controller);
  +            /* Create a new instance of the daemon */
  +            daemon=c.newInstance();
   
  -            /* Now we want to call the init method in the class */
  -            daemon.init(context);
  +            if (isdaemon) {
  +              /* Create a new controller instance */
  +              controller=new Controller();
  +
  +              /* Set the availability flag in the controller */
  +              controller.setAvailable(false);
  +
  +              /* Create context */
  +              context = new Context();
  +              context.setArguments(ar);
  +              context.setController(controller);
  +
  +              /* Now we want to call the init method in the class */
  +              Object arg[] = new Object[1];
  +              arg[0] = context;
  +              init.invoke(daemon,arg);
  +            } else {
  +                init.invoke(daemon,ar);
  +            }
   
           } catch (Throwable t) {
               /* In case we encounter ANY error, we dump the stack trace and
  @@ -173,10 +211,12 @@
       public static boolean start() {
           try {
               /* Attempt to start the daemon */
  -            daemon.start();
  +            Object arg[] = null;
  +            start.invoke(daemon,arg);
   
               /* Set the availability flag in the controller */
  -            controller.setAvailable(true);
  +            if (controller != null)
  +              controller.setAvailable(true);
   
           } catch (Throwable t) {
               /* In case we encounter ANY error, we dump the stack trace and
  @@ -190,10 +230,12 @@
       public static boolean stop() {
           try {
               /* Set the availability flag in the controller */
  -            controller.setAvailable(false);
  +            if (controller != null)
  +              controller.setAvailable(false);
   
  -            /* Attempt to start the daemon */
  -            daemon.stop();
  +            /* Attempt to stop the daemon */
  +            Object arg[] = null;
  +            stop.invoke(daemon,arg);
   
               /* Run garbage collector */
               daemon=null;
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org