| Method from org.apache.commons.daemon.support.DaemonLoader Detail: |
public static boolean check(String cn) {
try {
/* Check the class name */
if (cn==null)
throw new NullPointerException("Null class name specified");
/* Get the ClassLoader loading this class */
ClassLoader cl=DaemonLoader.class.getClassLoader();
if (cl==null) {
System.err.println("Cannot retrieve ClassLoader instance");
return(false);
}
/* Find the required class */
Class c=cl.loadClass(cn);
/* This should _never_ happen, but doublechecking doesn't harm */
if (c==null) throw new ClassNotFoundException(cn);
/* Create a new instance of the daemon */
Object s=c.newInstance();
} 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). */
t.printStackTrace(System.err);
return(false);
}
/* The class was loaded and instantiated correctly, we can return */
return(true);
}
|
public static boolean destroy() {
try {
/* Attempt to stop the daemon */
Object arg[] = null;
destroy.invoke(daemon,arg);
/* Run garbage collector */
daemon=null;
controller=null;
System.gc();
} 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). */
t.printStackTrace(System.err);
return(false);
}
return(true);
}
|
public static boolean load(String cn,
String[] ar) {
try {
/* Make sure any previous instance is garbage collected */
System.gc();
/* Check if the underlying libray supplied a valid list of
arguments */
if (ar==null) ar=new String[0];
/* Check the class name */
if (cn==null)
throw new NullPointerException("Null class name specified");
/* Get the ClassLoader loading this class */
ClassLoader cl=DaemonLoader.class.getClassLoader();
if (cl==null) {
System.err.println("Cannot retrieve ClassLoader instance");
return(false);
}
/* Find the required class */
Class c=cl.loadClass(cn);
/* This should _never_ happen, but doublechecking doesn't harm */
if (c==null) throw new ClassNotFoundException(cn);
/* Check interface */
boolean isdaemon = false;
try {
Class dclass = cl.loadClass("org.apache.commons.daemon.Daemon");
isdaemon = dclass.isAssignableFrom(c);
} catch(Exception cnfex) {
// Swallow if Daemon not found.
}
/* Check methods */
Class[] myclass = new Class[1];
if (isdaemon) {
myclass[0] = DaemonContext.class;
} else {
myclass[0] = ar.getClass();
}
init = c.getMethod("init",myclass);
myclass = null;
start = c.getMethod("start",myclass);
stop = c.getMethod("stop",myclass);
destroy = c.getMethod("destroy",myclass);
/* Create a new instance of the daemon */
daemon=c.newInstance();
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 {
Object arg[] = new Object[1];
arg[0] = ar;
init.invoke(daemon,arg);
}
} 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). */
t.printStackTrace(System.err);
return(false);
}
/* The class was loaded and instantiated correctly, we can return */
return(true);
}
|
public static boolean start() {
try {
/* Attempt to start the daemon */
Object arg[] = null;
start.invoke(daemon,arg);
/* Set the availability flag in the controller */
if (controller != null)
controller.setAvailable(true);
} 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). */
t.printStackTrace(System.err);
return(false);
}
return(true);
}
|
public static boolean stop() {
try {
/* Set the availability flag in the controller */
if (controller != null)
controller.setAvailable(false);
/* Attempt to stop the daemon */
Object arg[] = null;
stop.invoke(daemon,arg);
/* Run garbage collector */
System.gc();
} 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). */
t.printStackTrace(System.err);
return(false);
}
return(true);
}
|
public static void version() {
System.err.println("java version \""+
System.getProperty("java.version")+
"\"");
System.err.println(System.getProperty("java.runtime.name")+
" (build "+
System.getProperty("java.runtime.version")+
")");
System.err.println(System.getProperty("java.vm.name")+
" (build "+
System.getProperty("java.vm.version")+
", "+
System.getProperty("java.vm.info")+
")");
}
|