View Javadoc
1 /* ========================================================================= * 2 * * 3 * The Apache Software License, Version 1.1 * 4 * * 5 * Copyright (c) 1999-2001 The Apache Software Foundation. * 6 * All rights reserved. * 7 * * 8 * ========================================================================= * 9 * * 10 * Redistribution and use in source and binary forms, with or without modi- * 11 * fication, are permitted provided that the following conditions are met: * 12 * * 13 * 1. Redistributions of source code must retain the above copyright notice * 14 * notice, this list of conditions and the following disclaimer. * 15 * * 16 * 2. Redistributions in binary form must reproduce the above copyright * 17 * notice, this list of conditions and the following disclaimer in the * 18 * documentation and/or other materials provided with the distribution. * 19 * * 20 * 3. The end-user documentation included with the redistribution, if any, * 21 * must include the following acknowlegement: * 22 * * 23 * "This product includes software developed by the Apache Software * 24 * Foundation <http://www.apache.org/>;." * 25 * * 26 * Alternately, this acknowlegement may appear in the software itself, if * 27 * and wherever such third-party acknowlegements normally appear. * 28 * * 29 * 4. The names "The Jakarta Project", "WebApp", and "Apache Software * 30 * Foundation" must not be used to endorse or promote products derived * 31 * from this software without prior written permission. For written * 32 * permission, please contact <apache@apache.org>. * 33 * * 34 * 5. Products derived from this software may not be called "Apache" nor may * 35 * "Apache" appear in their names without prior written permission of the * 36 * Apache Software Foundation. * 37 * * 38 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES * 39 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * 40 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * 41 * THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY * 42 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * 43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * 44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * 45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * 46 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * 47 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * 48 * POSSIBILITY OF SUCH DAMAGE. * 49 * * 50 * ========================================================================= * 51 * * 52 * This software consists of voluntary contributions made by many indivi- * 53 * duals on behalf of the Apache Software Foundation. For more information * 54 * on the Apache Software Foundation, please see <http://www.apache.org/>;. * 55 * * 56 * ========================================================================= */ 57 58 /* @version $Id: DaemonLoader.java,v 1.1 2002/02/19 02:45:23 remm Exp $ */ 59 60 package org.apache.commons.daemon.support; 61 62 import org.apache.commons.daemon.Daemon; 63 import org.apache.commons.daemon.DaemonContext; 64 import org.apache.commons.daemon.DaemonController; 65 66 public final class DaemonLoader { 67 68 private static Controller controller = null; 69 private static Context context = null; 70 private static Daemon daemon = null; 71 72 public static void version() { 73 System.err.println("java version \""+ 74 System.getProperty("java.version")+ 75 "\""); 76 System.err.println(System.getProperty("java.runtime.name")+ 77 " (build "+ 78 System.getProperty("java.runtime.version")+ 79 ")"); 80 System.err.println(System.getProperty("java.vm.name")+ 81 " (build "+ 82 System.getProperty("java.vm.version")+ 83 ", "+ 84 System.getProperty("java.vm.info")+ 85 ")"); 86 } 87 88 public static boolean check(String cn) { 89 try { 90 /* Check the class name */ 91 if (cn==null) 92 throw new NullPointerException("Null class name specified"); 93 94 /* Get the ClassLoader loading this class */ 95 ClassLoader cl=DaemonLoader.class.getClassLoader(); 96 if (cl==null) { 97 System.err.println("Cannot retrieve ClassLoader instance"); 98 return(false); 99 } 100 101 /* Find the required class */ 102 Class c=cl.loadClass(cn); 103 104 /* This should _never_ happen, but doublechecking doesn't harm */ 105 if (c==null) throw new ClassNotFoundException(cn); 106 107 /* Create a new instance of the daemon */ 108 Daemon s=(Daemon)c.newInstance(); 109 110 } catch (Throwable t) { 111 /* In case we encounter ANY error, we dump the stack trace and 112 return false (load, start and stop won't be called). */ 113 t.printStackTrace(System.err); 114 return(false); 115 } 116 /* The class was loaded and instantiated correctly, we can return */ 117 return(true); 118 } 119 120 public static boolean load(String cn, String ar[]) { 121 try { 122 /* Make sure any previous instance is garbage collected */ 123 System.gc(); 124 125 /* Check if the underlying libray supplied a valid list of 126 arguments */ 127 if (ar==null) ar=new String[0]; 128 129 /* Check the class name */ 130 if (cn==null) 131 throw new NullPointerException("Null class name specified"); 132 133 /* Get the ClassLoader loading this class */ 134 ClassLoader cl=DaemonLoader.class.getClassLoader(); 135 if (cl==null) { 136 System.err.println("Cannot retrieve ClassLoader instance"); 137 return(false); 138 } 139 140 /* Find the required class */ 141 Class c=cl.loadClass(cn); 142 143 /* This should _never_ happen, but doublechecking doesn't harm */ 144 if (c==null) throw new ClassNotFoundException(cn); 145 146 /* Create a new instance of the daemon */ 147 daemon=(Daemon)c.newInstance(); 148 149 /* Create a new controller instance */ 150 controller=new Controller(); 151 152 /* Set the availability flag in the controller */ 153 controller.setAvailable(false); 154 155 /* Create context */ 156 context = new Context(); 157 context.setArguments(ar); 158 context.setController(controller); 159 160 /* Now we want to call the init method in the class */ 161 daemon.init(context); 162 163 } catch (Throwable t) { 164 /* In case we encounter ANY error, we dump the stack trace and 165 return false (load, start and stop won't be called). */ 166 t.printStackTrace(System.err); 167 return(false); 168 } 169 /* The class was loaded and instantiated correctly, we can return */ 170 return(true); 171 } 172 173 public static boolean start() { 174 try { 175 /* Attempt to start the daemon */ 176 daemon.start(); 177 178 /* Set the availability flag in the controller */ 179 controller.setAvailable(true); 180 181 } catch (Throwable t) { 182 /* In case we encounter ANY error, we dump the stack trace and 183 return false (load, start and stop won't be called). */ 184 t.printStackTrace(System.err); 185 return(false); 186 } 187 return(true); 188 } 189 190 public static boolean stop() { 191 try { 192 /* Set the availability flag in the controller */ 193 controller.setAvailable(false); 194 195 /* Attempt to start the daemon */ 196 daemon.stop(); 197 198 /* Run garbage collector */ 199 daemon=null; 200 controller=null; 201 System.gc(); 202 203 } catch (Throwable t) { 204 /* In case we encounter ANY error, we dump the stack trace and 205 return false (load, start and stop won't be called). */ 206 t.printStackTrace(System.err); 207 return(false); 208 } 209 return(true); 210 } 211 212 private static native void shutdown(boolean reload); 213 214 public static class Controller implements DaemonController { 215 216 boolean available=false; 217 218 private Controller() { 219 super(); 220 this.setAvailable(false); 221 } 222 223 private boolean isAvailable() { 224 synchronized (this) { 225 return(this.available); 226 } 227 } 228 229 private void setAvailable(boolean available) { 230 synchronized (this) { 231 this.available=available; 232 } 233 } 234 235 public void shutdown() throws IllegalStateException { 236 synchronized (this) { 237 if (!this.isAvailable()) { 238 throw new IllegalStateException(); 239 } else { 240 this.setAvailable(false); 241 DaemonLoader.shutdown(false); 242 } 243 } 244 } 245 246 public void reload() throws IllegalStateException { 247 synchronized (this) { 248 if (!this.isAvailable()) { 249 throw new IllegalStateException(); 250 } else { 251 this.setAvailable(false); 252 DaemonLoader.shutdown(true); 253 } 254 } 255 } 256 257 public void fail() 258 throws IllegalStateException { 259 } 260 261 public void fail(String message) 262 throws IllegalStateException { 263 } 264 265 public void fail(Exception exception) 266 throws IllegalStateException { 267 } 268 269 public void fail(String message, Exception exception) 270 throws IllegalStateException { 271 } 272 273 } 274 275 public static class Context implements DaemonContext { 276 277 DaemonController controller = null; 278 279 String[] args = null; 280 281 public DaemonController getController() { 282 return controller; 283 } 284 285 public void setController(DaemonController controller) { 286 this.controller = controller; 287 } 288 289 public String[] getArguments() { 290 return args; 291 } 292 293 public void setArguments(String[] args) { 294 this.args = args; 295 } 296 297 } 298 299 }

This page automatically generated by Maven