public static boolean load()

in src/main/java/org/apache/commons/daemon/support/DaemonLoader.java [141:255]


    public static boolean load(final String className, String[] args)
    {
        try {
            /* Check if the underlying library supplied a valid list of
               arguments */
            if (args == null) {
                args = new String[0];
            }

            /* Check the class name */
            Objects.requireNonNull(className, "className");

            /* Gets the ClassLoader loading this class */
            final ClassLoader cl = DaemonLoader.class.getClassLoader();
            if (cl == null) {
                System.err.println("Cannot retrieve ClassLoader instance");
                return false;
            }
            final Class<?> c;
            if (className.charAt(0) == '@') {
                /* Wrap the class with DaemonWrapper
                 * and modify arguments to include the real class name.
                 */
                c = DaemonWrapper.class;
                final 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 double-checking doesn't harm */
            if (c == null) {
                throw new ClassNotFoundException(className);
            }
            /* Check interfaces */
            boolean isdaemon = false;

            try {
                final Class<?> dclass = cl.loadClass("org.apache.commons.daemon.Daemon");
                isdaemon = dclass.isAssignableFrom(c);
            }
            catch (final Exception ignored) {
                // Swallow if Daemon not found.
            }

            /* Check methods */
            final Class<?>[] myclass = new Class[1];
            if (isdaemon) {
                myclass[0] = DaemonContext.class;
            }
            else {
                myclass[0] = args.getClass();
            }

            init    = c.getMethod("init", myclass);
            start   = c.getMethod("start");
            stop    = c.getMethod("stop");
            destroy = c.getMethod("destroy");

            try {
                signal = c.getMethod("signal");
            } catch (final NoSuchMethodException ignored) {
                // Signaling will be disabled.
            }

            /* Create a new instance of the daemon */
            daemon = c.getConstructor().newInstance();

            if (isdaemon) {
                // Create a new controller instance
                controller = new Controller();

                // Set the availability flag in the controller
                controller.setAvailable(false);

                /* Create context */
                final Context context = new Context();
                context.setArguments(args);
                context.setController(controller);

                // Now we want to call the init method in the class
                final Object[] arg = new Object[1];
                arg[0] = context;
                init.invoke(daemon, arg);
            }
            else {
                final Object[] arg = new Object[1];
                arg[0] = args;
                init.invoke(daemon, arg);
            }

        }
        catch (final InvocationTargetException e) {
            final Throwable thrown = e.getTargetException();
            // DaemonInitExceptions can fail with a nicer message
            if (thrown instanceof DaemonInitException) {
                failed(((DaemonInitException) thrown).getMessageWithCause());
            }
            else {
                thrown.printStackTrace(System.err);
            }
            return false;
        }
        catch (final Throwable t) {
            // In case we encounter ANY error, we dump the stack trace and
            // return false (load, start and stop won't be called).
            t.printStackTrace(System.err);
            return false;
        }
        // The class was loaded and instantiated correctly, we can return
        return true;
    }