1 16 17 18 19 package org.apache.commons.daemon.support; 20 21 import org.apache.commons.daemon.Daemon; 22 import org.apache.commons.daemon.DaemonContext; 23 import org.apache.commons.daemon.DaemonController; 24 25 import java.lang.reflect.Method ; 26 27 public final class DaemonLoader { 28 29 private static Controller controller = null; 30 private static Context context = null; 31 private static Object daemon = null; 32 33 private static Method init = null; 34 private static Method start = null; 35 private static Method stop = null; 36 private static Method destroy = null; 37 38 public static void version() { 39 System.err.println("java version \""+ 40 System.getProperty("java.version")+ 41 "\""); 42 System.err.println(System.getProperty("java.runtime.name")+ 43 " (build "+ 44 System.getProperty("java.runtime.version")+ 45 ")"); 46 System.err.println(System.getProperty("java.vm.name")+ 47 " (build "+ 48 System.getProperty("java.vm.version")+ 49 ", "+ 50 System.getProperty("java.vm.info")+ 51 ")"); 52 } 53 54 public static boolean check(String cn) { 55 try { 56 57 if (cn==null) 58 throw new NullPointerException ("Null class name specified"); 59 60 61 ClassLoader cl=DaemonLoader.class.getClassLoader(); 62 if (cl==null) { 63 System.err.println("Cannot retrieve ClassLoader instance"); 64 return(false); 65 } 66 67 68 Class c=cl.loadClass(cn); 69 70 71 if (c==null) throw new ClassNotFoundException (cn); 72 73 74 Object s=c.newInstance(); 75 76 } catch (Throwable t) { 77 79 t.printStackTrace(System.err); 80 return(false); 81 } 82 83 return(true); 84 } 85 86 public static boolean load(String cn, String ar[]) { 87 try { 88 89 System.gc(); 90 91 93 if (ar==null) ar=new String [0]; 94 95 96 if (cn==null) 97 throw new NullPointerException ("Null class name specified"); 98 99 100 ClassLoader cl=DaemonLoader.class.getClassLoader(); 101 if (cl==null) { 102 System.err.println("Cannot retrieve ClassLoader instance"); 103 return(false); 104 } 105 106 107 Class c=cl.loadClass(cn); 108 109 110 if (c==null) throw new ClassNotFoundException (cn); 111 112 113 boolean isdaemon = false; 114 try { 115 Class dclass = cl.loadClass("org.apache.commons.daemon.Daemon"); 116 isdaemon = dclass.isAssignableFrom(c); 117 } catch(Exception cnfex) { 118 } 120 121 122 Class [] myclass = new Class [1]; 123 if (isdaemon) { 124 myclass[0] = DaemonContext.class; 125 } else { 126 myclass[0] = ar.getClass(); 127 } 128 129 init = c.getMethod("init",myclass); 130 131 myclass = null; 132 start = c.getMethod("start",myclass); 133 134 stop = c.getMethod("stop",myclass); 135 136 destroy = c.getMethod("destroy",myclass); 137 138 139 daemon=c.newInstance(); 140 141 if (isdaemon) { 142 143 controller=new Controller(); 144 145 146 controller.setAvailable(false); 147 148 149 context = new Context(); 150 context.setArguments(ar); 151 context.setController(controller); 152 153 154 Object arg[] = new Object [1]; 155 arg[0] = context; 156 init.invoke(daemon,arg); 157 } else { 158 Object arg[] = new Object [1]; 159 arg[0] = ar; 160 init.invoke(daemon,arg); 161 } 162 163 } catch (Throwable t) { 164 166 t.printStackTrace(System.err); 167 return(false); 168 } 169 170 return(true); 171 } 172 173 public static boolean start() { 174 try { 175 176 Object arg[] = null; 177 start.invoke(daemon,arg); 178 179 180 if (controller != null) 181 controller.setAvailable(true); 182 183 } catch (Throwable t) { 184 186 t.printStackTrace(System.err); 187 return(false); 188 } 189 return(true); 190 } 191 192 public static boolean stop() { 193 try { 194 195 if (controller != null) 196 controller.setAvailable(false); 197 198 199 Object arg[] = null; 200 stop.invoke(daemon,arg); 201 202 203 System.gc(); 204 205 } catch (Throwable t) { 206 208 t.printStackTrace(System.err); 209 return(false); 210 } 211 return(true); 212 } 213 214 public static boolean destroy() { 215 try { 216 217 Object arg[] = null; 218 destroy.invoke(daemon,arg); 219 220 221 daemon=null; 222 controller=null; 223 System.gc(); 224 225 } catch (Throwable t) { 226 228 t.printStackTrace(System.err); 229 return(false); 230 } 231 return(true); 232 } 233 234 private static native void shutdown(boolean reload); 235 236 public static class Controller implements DaemonController { 237 238 boolean available=false; 239 240 private Controller() { 241 super(); 242 this.setAvailable(false); 243 } 244 245 private boolean isAvailable() { 246 synchronized (this) { 247 return(this.available); 248 } 249 } 250 251 private void setAvailable(boolean available) { 252 synchronized (this) { 253 this.available=available; 254 } 255 } 256 257 public void shutdown() throws IllegalStateException { 258 synchronized (this) { 259 if (!this.isAvailable()) { 260 throw new IllegalStateException (); 261 } else { 262 this.setAvailable(false); 263 DaemonLoader.shutdown(false); 264 } 265 } 266 } 267 268 public void reload() throws IllegalStateException { 269 synchronized (this) { 270 if (!this.isAvailable()) { 271 throw new IllegalStateException (); 272 } else { 273 this.setAvailable(false); 274 DaemonLoader.shutdown(true); 275 } 276 } 277 } 278 279 public void fail() 280 throws IllegalStateException { 281 } 282 283 public void fail(String message) 284 throws IllegalStateException { 285 } 286 287 public void fail(Exception exception) 288 throws IllegalStateException { 289 } 290 291 public void fail(String message, Exception exception) 292 throws IllegalStateException { 293 } 294 295 } 296 297 public static class Context implements DaemonContext { 298 299 DaemonController controller = null; 300 301 String [] args = null; 302 303 public DaemonController getController() { 304 return controller; 305 } 306 307 public void setController(DaemonController controller) { 308 this.controller = controller; 309 } 310 311 public String [] getArguments() { 312 return args; 313 } 314 315 public void setArguments(String [] args) { 316 this.args = args; 317 } 318 319 } 320 321 } 322 | Popular Tags |