KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > launcher > DaemonLauncher


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.phoenix.launcher;
9
10 import com.silveregg.wrapper.WrapperListener;
11 import com.silveregg.wrapper.WrapperManager;
12 import java.util.Hashtable JavaDoc;
13 import java.util.Observable JavaDoc;
14 import java.util.Observer JavaDoc;
15
16 /**
17  * A frontend for Phoenix that starts it as a native service
18  * using the Java Service Wrapper at http://wrapper.sourceforge.net
19  *
20  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
21  * @author <a HREF="mailto:leif@tanukisoftware.com">Leif Mortenson</a>
22  */

23 public class DaemonLauncher
24     implements WrapperListener, Observer JavaDoc
25 {
26     /**
27      * In order to avoid calling the Wrapper stop method recursively, we need
28      * to keep track of whether or not the Wrapper already knows we are
29      * stopping. Necessary because of the way the shutdown process in
30      * Phoenix works. Ideally, we would unregister this Observer with
31      * CLIMain but we can't do that for security reasons.
32      */

33     private boolean m_ignoreUpdates = false;
34
35     public Integer JavaDoc start( final String JavaDoc[] args )
36     {
37         Integer JavaDoc exitCodeInteger = null;
38
39         // This startup could take a while, so tell the wrapper to be patient.
40
WrapperManager.signalStarting( 45000 );
41
42         final Hashtable JavaDoc data = new Hashtable JavaDoc();
43         data.put( Observer JavaDoc.class.getName(), this );
44
45         if( WrapperManager.isDebugEnabled() )
46         {
47             System.out.println( "DaemonLauncher: Starting up Phoenix" );
48         }
49
50         try
51         {
52             int exitCode = Main.startup( args, data, false );
53             if( exitCode != 0 )
54             {
55                 exitCodeInteger = new Integer JavaDoc( exitCode );
56             }
57
58             if( WrapperManager.isDebugEnabled() )
59             {
60                 System.out.println( "DaemonLauncher: Phoenix startup completed" );
61             }
62         }
63         catch( final Exception JavaDoc e )
64         {
65             e.printStackTrace();
66             exitCodeInteger = new Integer JavaDoc( 1 );
67         }
68
69         // We are almost up now, so reset the wait time
70
WrapperManager.signalStarting( 2000 );
71
72         return exitCodeInteger;
73     }
74
75     public int stop( final int exitCode )
76     {
77         // To avoid recursive calls, start ignoring updates.
78
m_ignoreUpdates = true;
79
80         Main.shutdown();
81         return exitCode;
82     }
83
84     public void controlEvent( final int event )
85     {
86         if( WrapperManager.isControlledByNativeWrapper() )
87         {
88             if( WrapperManager.isDebugEnabled() )
89             {
90                 System.out.println( "DaemonLauncher: controlEvent(" + event + ") - Ignored." );
91             }
92
93             // This application ignores all incoming control events.
94
// It relies on the wrapper code to handle them.
95
}
96         else
97         {
98             if( WrapperManager.isDebugEnabled() )
99             {
100                 System.out.println( "DaemonLauncher: controlEvent(" + event + ") - Stopping." );
101             }
102
103             // Not being run under a wrapper, so this isn't an NT service and should always exit.
104
// Handle the event here.
105
WrapperManager.stop( 0 );
106             // Will not get here.
107
}
108     }
109
110     /**
111      * We use an Observer rather than operating on some more meaningful
112      * event system as Observer and friends can be loaded from system
113      * ClassLoader and thus the Embeddor does not have to share a common
114      * classloader ancestor with invoker
115      */

116     public void update( final Observable JavaDoc observable, final Object JavaDoc arg )
117     {
118         if( m_ignoreUpdates )
119         {
120             // Ignore this update
121
if( WrapperManager.isDebugEnabled() )
122             {
123                 System.out.println( "DaemonLauncher: " + arg
124                                     + " request ignored because stop already called." );
125                 System.out.flush();
126             }
127         }
128         else
129         {
130             final String JavaDoc command = (null != arg) ? arg.toString() : "";
131             if( command.equals( "restart" ) )
132             {
133                 if( WrapperManager.isDebugEnabled() )
134                 {
135                     System.out.println( "DaemonLauncher: restart requested." );
136                     System.out.flush();
137                 }
138
139                 WrapperManager.restart();
140
141                 if( WrapperManager.isDebugEnabled() )
142                 {
143                     //Should never get here???
144
System.out.println( "DaemonLauncher: restart completed." );
145                     System.out.flush();
146                 }
147             }
148             else if( command.equals( "shutdown" ) )
149             {
150                 if( WrapperManager.isDebugEnabled() )
151                 {
152                     System.out.println( "DaemonLauncher: shutdown requested." );
153                     System.out.flush();
154                 }
155
156                 WrapperManager.stop( 0 );
157
158                 if( WrapperManager.isDebugEnabled() )
159                 {
160                     //Should never get here???
161
System.out.println( "DaemonLauncher: shutdown completed." );
162                     System.out.flush();
163                 }
164             }
165             else
166             {
167                 throw new IllegalArgumentException JavaDoc( "Unknown action " + command );
168             }
169         }
170     }
171
172     public static void main( final String JavaDoc[] args )
173     {
174         WrapperManager.start( new DaemonLauncher(), args );
175     }
176 }
177
Popular Tags