KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > daemon > support > DaemonLoader


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 /* @version $Id: DaemonLoader.java 155409 2005-02-26 12:57:06Z dirkv $ */
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 JavaDoc;
26
27 public final class DaemonLoader {
28
29     private static Controller controller = null;
30     private static Context context = null;
31     private static Object JavaDoc daemon = null;
32     /* Methods to call */
33     private static Method JavaDoc init = null;
34     private static Method JavaDoc start = null;
35     private static Method JavaDoc stop = null;
36     private static Method JavaDoc 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 JavaDoc cn) {
55         try {
56             /* Check the class name */
57             if (cn==null)
58                 throw new NullPointerException JavaDoc("Null class name specified");
59
60             /* Get the ClassLoader loading this class */
61             ClassLoader JavaDoc cl=DaemonLoader.class.getClassLoader();
62             if (cl==null) {
63                 System.err.println("Cannot retrieve ClassLoader instance");
64                 return(false);
65             }
66
67             /* Find the required class */
68             Class JavaDoc c=cl.loadClass(cn);
69
70             /* This should _never_ happen, but doublechecking doesn't harm */
71             if (c==null) throw new ClassNotFoundException JavaDoc(cn);
72
73             /* Create a new instance of the daemon */
74             Object JavaDoc s=c.newInstance();
75
76         } catch (Throwable JavaDoc t) {
77             /* In case we encounter ANY error, we dump the stack trace and
78                return false (load, start and stop won't be called). */

79             t.printStackTrace(System.err);
80             return(false);
81         }
82         /* The class was loaded and instantiated correctly, we can return */
83         return(true);
84     }
85
86     public static boolean load(String JavaDoc cn, String JavaDoc ar[]) {
87         try {
88             /* Make sure any previous instance is garbage collected */
89             System.gc();
90
91             /* Check if the underlying libray supplied a valid list of
92                arguments */

93             if (ar==null) ar=new String JavaDoc[0];
94
95             /* Check the class name */
96             if (cn==null)
97                 throw new NullPointerException JavaDoc("Null class name specified");
98
99             /* Get the ClassLoader loading this class */
100             ClassLoader JavaDoc cl=DaemonLoader.class.getClassLoader();
101             if (cl==null) {
102                 System.err.println("Cannot retrieve ClassLoader instance");
103                 return(false);
104             }
105
106             /* Find the required class */
107             Class JavaDoc c=cl.loadClass(cn);
108
109             /* This should _never_ happen, but doublechecking doesn't harm */
110             if (c==null) throw new ClassNotFoundException JavaDoc(cn);
111
112             /* Check interface */
113             boolean isdaemon = false;
114             try {
115               Class JavaDoc dclass = cl.loadClass("org.apache.commons.daemon.Daemon");
116               isdaemon = dclass.isAssignableFrom(c);
117             } catch(Exception JavaDoc cnfex) {
118               // Swallow if Daemon not found.
119
}
120
121             /* Check methods */
122             Class JavaDoc[] myclass = new Class JavaDoc[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             /* Create a new instance of the daemon */
139             daemon=c.newInstance();
140
141             if (isdaemon) {
142               /* Create a new controller instance */
143               controller=new Controller();
144
145               /* Set the availability flag in the controller */
146               controller.setAvailable(false);
147
148               /* Create context */
149               context = new Context();
150               context.setArguments(ar);
151               context.setController(controller);
152
153               /* Now we want to call the init method in the class */
154               Object JavaDoc arg[] = new Object JavaDoc[1];
155               arg[0] = context;
156               init.invoke(daemon,arg);
157             } else {
158               Object JavaDoc arg[] = new Object JavaDoc[1];
159               arg[0] = ar;
160               init.invoke(daemon,arg);
161             }
162
163         } catch (Throwable JavaDoc 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             Object JavaDoc arg[] = null;
177             start.invoke(daemon,arg);
178
179             /* Set the availability flag in the controller */
180             if (controller != null)
181               controller.setAvailable(true);
182
183         } catch (Throwable JavaDoc t) {
184             /* In case we encounter ANY error, we dump the stack trace and
185                return false (load, start and stop won't be called). */

186             t.printStackTrace(System.err);
187             return(false);
188         }
189         return(true);
190     }
191
192     public static boolean stop() {
193         try {
194             /* Set the availability flag in the controller */
195             if (controller != null)
196               controller.setAvailable(false);
197
198             /* Attempt to stop the daemon */
199             Object JavaDoc arg[] = null;
200             stop.invoke(daemon,arg);
201
202             /* Run garbage collector */
203             System.gc();
204
205         } catch (Throwable JavaDoc t) {
206             /* In case we encounter ANY error, we dump the stack trace and
207                return false (load, start and stop won't be called). */

208             t.printStackTrace(System.err);
209             return(false);
210         }
211         return(true);
212     }
213
214     public static boolean destroy() {
215         try {
216             /* Attempt to stop the daemon */
217             Object JavaDoc arg[] = null;
218             destroy.invoke(daemon,arg);
219
220             /* Run garbage collector */
221             daemon=null;
222             controller=null;
223             System.gc();
224
225         } catch (Throwable JavaDoc t) {
226             /* In case we encounter ANY error, we dump the stack trace and
227                return false (load, start and stop won't be called). */

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 JavaDoc {
258             synchronized (this) {
259                 if (!this.isAvailable()) {
260                     throw new IllegalStateException JavaDoc();
261                 } else {
262                     this.setAvailable(false);
263                     DaemonLoader.shutdown(false);
264                 }
265             }
266         }
267
268         public void reload() throws IllegalStateException JavaDoc {
269             synchronized (this) {
270                 if (!this.isAvailable()) {
271                     throw new IllegalStateException JavaDoc();
272                 } else {
273                     this.setAvailable(false);
274                     DaemonLoader.shutdown(true);
275                 }
276             }
277         }
278
279         public void fail()
280             throws IllegalStateException JavaDoc {
281         }
282
283         public void fail(String JavaDoc message)
284             throws IllegalStateException JavaDoc {
285         }
286
287         public void fail(Exception JavaDoc exception)
288             throws IllegalStateException JavaDoc {
289         }
290
291         public void fail(String JavaDoc message, Exception JavaDoc exception)
292             throws IllegalStateException JavaDoc {
293         }
294
295     }
296
297     public static class Context implements DaemonContext {
298
299         DaemonController controller = null;
300
301         String JavaDoc[] 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 JavaDoc[] getArguments() {
312             return args;
313         }
314
315         public void setArguments(String JavaDoc[] args) {
316             this.args = args;
317         }
318
319     }
320
321 }
322
Popular Tags