You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2010/10/17 13:40:21 UTC

svn commit: r1023466 - in /commons/proper/daemon/trunk/src: java/org/apache/commons/daemon/support/DaemonLoader.java samples/SimpleApplication.sh

Author: mturk
Date: Sun Oct 17 11:40:21 2010
New Revision: 1023466

URL: http://svn.apache.org/viewvc?rev=1023466&view=rev
Log:
If classes start with @ feed them through DaemonWrapper add -start automatically

Modified:
    commons/proper/daemon/trunk/src/java/org/apache/commons/daemon/support/DaemonLoader.java
    commons/proper/daemon/trunk/src/samples/SimpleApplication.sh

Modified: commons/proper/daemon/trunk/src/java/org/apache/commons/daemon/support/DaemonLoader.java
URL: http://svn.apache.org/viewvc/commons/proper/daemon/trunk/src/java/org/apache/commons/daemon/support/DaemonLoader.java?rev=1023466&r1=1023465&r2=1023466&view=diff
==============================================================================
--- commons/proper/daemon/trunk/src/java/org/apache/commons/daemon/support/DaemonLoader.java (original)
+++ commons/proper/daemon/trunk/src/java/org/apache/commons/daemon/support/DaemonLoader.java Sun Oct 17 11:40:21 2010
@@ -96,7 +96,7 @@ public final class DaemonLoader
         return true;
     }
 
-    public static boolean load(String cn, String ar[])
+    public static boolean load(String className, String args[])
     {
         try {
             /* Make sure any previous instance is garbage collected */
@@ -104,11 +104,11 @@ public final class DaemonLoader
 
             /* Check if the underlying libray supplied a valid list of
                arguments */
-            if (ar == null)
-                ar = new String[0];
+            if (args == null)
+                args = new String[0];
 
             /* Check the class name */
-            if (cn == null)
+            if (className == null)
                 throw new NullPointerException("Null class name specified");
 
             /* Get the ClassLoader loading this class */
@@ -117,31 +117,42 @@ public final class DaemonLoader
                 System.err.println("Cannot retrieve ClassLoader instance");
                 return false;
             }
-
-            /* Find the required class */
-            Class c = cl.loadClass(cn);
-
+            Class c;
+            if (className.charAt(0) == '@') {
+                /* Wrapp the class with DaemonWrapper
+                 * and modify arguments to include the real class name.
+                 */
+                c = DaemonWrapper.class;
+                String[] a = new String[args.length + 2];
+                a[0] = "-start";
+                a[1] = className.substring(1);
+                System.arraycopy(args, 0, a, 2, args.length);
+                args = a;
+            }
+            else
+                c = cl.loadClass(className);
             /* This should _never_ happen, but doublechecking doesn't harm */
             if (c == null)
-                throw new ClassNotFoundException(cn);
-
-            /* Check interface */
+                throw new ClassNotFoundException(className);
+            /* Check interfaces */
             boolean isdaemon = false;
+
             try {
                 Class dclass =
                     cl.loadClass("org.apache.commons.daemon.Daemon");
                 isdaemon = dclass.isAssignableFrom(c);
-            } catch (Exception cnfex) {
+            }
+            catch (Exception cnfex) {
                 // Swallow if Daemon not found.
             }
 
             /* Check methods */
-            Class[]myclass = new Class[1];
+            Class[] myclass = new Class[1];
             if (isdaemon) {
                 myclass[0] = DaemonContext.class;
             }
             else {
-                myclass[0] = ar.getClass();
+                myclass[0] = args.getClass();
             }
 
             init    = c.getMethod("init", myclass);
@@ -163,7 +174,7 @@ public final class DaemonLoader
 
                 /* Create context */
                 Context context = new Context();
-                context.setArguments(ar);
+                context.setArguments(args);
                 context.setController(controller);
 
                 /* Now we want to call the init method in the class */
@@ -173,20 +184,23 @@ public final class DaemonLoader
             }
             else {
                 Object arg[] = new Object[1];
-                arg[0] = ar;
+                arg[0] = args;
                 init.invoke(daemon, arg);
             }
 
-        } catch (InvocationTargetException e) {
-          Throwable thrown = e.getCause();
-          /* DaemonInitExceptions can fail with a nicer message */
-          if (thrown instanceof DaemonInitException) {
-            failed(((DaemonInitException) thrown).getMessageWithCause());
-          } else {
-            thrown.printStackTrace(System.err);
-          }
-          return false;
-        } catch (Throwable t) {
+        }
+        catch (InvocationTargetException e) {
+            Throwable thrown = e.getCause();
+            /* DaemonInitExceptions can fail with a nicer message */
+            if (thrown instanceof DaemonInitException) {
+                failed(((DaemonInitException) thrown).getMessageWithCause());
+            }
+            else {
+                thrown.printStackTrace(System.err);
+            }
+            return false;
+        }
+        catch (Throwable t) {
             /* In case we encounter ANY error, we dump the stack trace and
              * return false (load, start and stop won't be called).
              */

Modified: commons/proper/daemon/trunk/src/samples/SimpleApplication.sh
URL: http://svn.apache.org/viewvc/commons/proper/daemon/trunk/src/samples/SimpleApplication.sh?rev=1023466&r1=1023465&r2=1023466&view=diff
==============================================================================
--- commons/proper/daemon/trunk/src/samples/SimpleApplication.sh (original)
+++ commons/proper/daemon/trunk/src/samples/SimpleApplication.sh Sun Oct 17 11:40:21 2010
@@ -19,7 +19,7 @@
 #
 # Adapt the following lines to your configuration
 JAVA_HOME=/opt/java6
-PROGRAM=SampleApplication
+PROGRAM=SimpleApplication
 CLASSPATH=`pwd`/$PROGRAM.jar:`pwd`/commons-daemon-1.0.4-dev.jar
 
 case "$1" in
@@ -31,8 +31,7 @@ case "$1" in
         -nodetach \
         -errfile "&2" \
         -pidfile `pwd`/$PROGRAM.pid \
-        org.apache.commons.daemon.support.DaemonWrapper \
-        -start $PROGRAM \
+        @$PROGRAM \
         -start-method main \
         $*
     exit $?
@@ -46,8 +45,7 @@ case "$1" in
         -nodetach \
         -errfile "&2" \
         -pidfile `pwd`/$PROGRAM.pid \
-        org.apache.commons.daemon.support.DaemonWrapper \
-        -start $PROGRAM \
+        @$PROGRAM \
         -start-method main \
         $*
     exit $?