This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-daemon.git


The following commit(s) were added to refs/heads/master by this push:
     new 36850c88 [DAEMON-479] Check wrapped daemon classes in jsvc
36850c88 is described below

commit 36850c889ccf447f40cc814734aa741cee1467fc
Author: Gary Gregory <[email protected]>
AuthorDate: Mon Nov 16 08:07:55 2026 -0500

    [DAEMON-479] Check wrapped daemon classes in jsvc
    
    Follow up to PR #317.
    
    Clarify and test DaemonLoader.check() behavior.
    
    Document plain and wrapped class handling and the limits of validation.
    Add regression tests for wrapped applications with private or throwing
    constructors and verify that an empty class name returns false.
    
    Assisted-By: OpenAI Codex
---
 src/changes/changes.xml                            |  2 +-
 .../commons/daemon/support/DaemonLoader.java       | 16 ++++--
 .../commons/daemon/support/DaemonLoaderTest.java   | 60 ++++++++++++++++++++++
 3 files changed, 74 insertions(+), 4 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 4a1dfa58..ef18c012 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -44,7 +44,7 @@
       <!-- FIX -->
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix native build 
with C23 boolean keywords (#319).</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory, xzhiyouu">jsvc. 
Prevent a heap buffer overflow when parsing jvm.cfg by rejecting configurations 
with more than 255 resolvable JVM definitions.</action>
-      <action type="fix" issue="DAEMON-479" due-to="Aryan Ganjoo">Make jsvc 
-check accept wrapped @ClassName services that DaemonLoader.load() 
supports.</action>
+      <action type="fix" dev="ggregory" due-to="Aryan Ganjoo, Gary Gregory" 
issue="DAEMON-479">Make jsvc -check accept wrapped @ClassName services that 
DaemonLoader.load() supports.</action>
       <!-- ADD -->
       <action type="add" dev="ggregory" due-to="Mohd Irshad, Gary Gregory">Add 
unit test for DaemonInitException.getMessageWithCause (#318).</action>
       <!-- UPDATE -->
diff --git a/src/main/java/org/apache/commons/daemon/support/DaemonLoader.java 
b/src/main/java/org/apache/commons/daemon/support/DaemonLoader.java
index 4e754540..9ff6b90c 100644
--- a/src/main/java/org/apache/commons/daemon/support/DaemonLoader.java
+++ b/src/main/java/org/apache/commons/daemon/support/DaemonLoader.java
@@ -71,10 +71,20 @@ public final class DaemonLoader
     }
 
     /**
-     * Checks whether the given class name can be instantiated with a 
zero-argument constructor.
+     * Checks whether a plain or wrapped class can be loaded and its daemon 
instance created.
      *
-     * @param className The class name.
-     * @return true if the given class name can be instantiated, false 
otherwise.
+     * <p>
+     * For a plain class name, loads the class and invokes its public 
zero-argument constructor.
+     * For a name prefixed with {@code @}, loads the application class and 
creates a {@link DaemonWrapper} instead of an application instance.
+     * </p>
+     *
+     * <p>
+     * This check does not invoke lifecycle methods or validate wrapper 
configuration, so success does not guarantee that the service can start.
+     * </p>
+     *
+     * @param className The class name, optionally prefixed with {@code @} to 
use {@link DaemonWrapper}.
+     * @return {@code true} if the class loads and the plain class or its 
wrapper can be instantiated; {@code false} otherwise, including for
+     *         {@code null} or an empty class name.
      */
     public static boolean check(final String className)
     {
diff --git 
a/src/test/java/org/apache/commons/daemon/support/DaemonLoaderTest.java 
b/src/test/java/org/apache/commons/daemon/support/DaemonLoaderTest.java
index 4481f719..da1ac435 100644
--- a/src/test/java/org/apache/commons/daemon/support/DaemonLoaderTest.java
+++ b/src/test/java/org/apache/commons/daemon/support/DaemonLoaderTest.java
@@ -37,16 +37,76 @@ class DaemonLoaderTest {
         }
     }
 
+    /**
+     * A wrapped application with a private constructor.
+     */
+    public static class WrappedApplicationWithPrivateConstructor {
+
+        private WrappedApplicationWithPrivateConstructor() {
+            // empty
+        }
+
+        /**
+         * Runs the application.
+         *
+         * @param args The application arguments.
+         */
+        public static void main(final String[] args) {
+            // empty
+        }
+    }
+
+    /**
+     * A wrapped application whose constructor must not be invoked.
+     */
+    public static class WrappedApplicationWithThrowingConstructor {
+
+        /**
+         * Throws an exception if invoked.
+         */
+        public WrappedApplicationWithThrowingConstructor() {
+            throw new IllegalStateException("The application constructor must 
not be invoked.");
+        }
+
+        /**
+         * Runs the application.
+         *
+         * @param args The application arguments.
+         */
+        public static void main(final String[] args) {
+            // empty
+        }
+    }
+
     @Test
     void testCheckDaemonClass() {
         assertTrue(DaemonLoader.check(WrappedApplication.class.getName()));
         
assertFalse(DaemonLoader.check("org.apache.commons.daemon.DoesNotExist"));
     }
 
+    @Test
+    void testCheckEmptyClassName() {
+        assertFalse(DaemonLoader.check(""));
+    }
+
     @Test
     void testCheckWrappedClass() {
         // jsvc -check @className must accept what DaemonLoader.load() accepts.
         assertTrue(DaemonLoader.check("@" + 
WrappedApplication.class.getName()));
         
assertFalse(DaemonLoader.check("@org.apache.commons.daemon.DoesNotExist"));
     }
+
+    @Test
+    void testCheckWrappedClassWithPrivateConstructor() {
+        final String className = 
WrappedApplicationWithPrivateConstructor.class.getName();
+        assertFalse(DaemonLoader.check(className));
+        assertTrue(DaemonLoader.check("@" + className));
+    }
+
+    @Test
+    void testCheckWrappedClassWithThrowingConstructor() {
+        final String className = 
WrappedApplicationWithThrowingConstructor.class.getName();
+        assertFalse(DaemonLoader.check(className));
+        assertTrue(DaemonLoader.check("@" + className));
+    }
 }

Reply via email to