KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tanukisoftware > wrapper > test > TestAction


1 package org.tanukisoftware.wrapper.test;
2
3 /*
4  * Copyright (c) 1999, 2006 Tanuki Software Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of the Java Service Wrapper and associated
8  * documentation files (the "Software"), to deal in the Software
9  * without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sub-license,
11  * and/or sell copies of the Software, and to permit persons to
12  * whom the Software is furnished to do so, subject to the
13  * following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  *
28  * Portions of the Software have been derived from source code
29  * developed by Silver Egg Technology under the following license:
30  *
31  * Copyright (c) 2001 Silver Egg Technology
32  *
33  * Permission is hereby granted, free of charge, to any person
34  * obtaining a copy of this software and associated documentation
35  * files (the "Software"), to deal in the Software without
36  * restriction, including without limitation the rights to use,
37  * copy, modify, merge, publish, distribute, sub-license, and/or
38  * sell copies of the Software, and to permit persons to whom the
39  * Software is furnished to do so, subject to the following
40  * conditions:
41  *
42  * The above copyright notice and this permission notice shall be
43  * included in all copies or substantial portions of the Software.
44  */

45
46 import org.tanukisoftware.wrapper.WrapperManager;
47 import org.tanukisoftware.wrapper.WrapperListener;
48
49 /**
50  *
51  *
52  * @author Leif Mortenson <leif@tanukisoftware.com>
53  */

54 public class TestAction
55     extends AbstractActionApp
56     implements WrapperListener
57 {
58     private ActionRunner m_actionRunner;
59     
60     /**************************************************************************
61      * Constructors
62      *************************************************************************/

63     private TestAction() {
64     }
65
66     /**************************************************************************
67      * WrapperListener Methods
68      *************************************************************************/

69     public Integer JavaDoc start(String JavaDoc[] args) {
70         Thread JavaDoc actionThread;
71
72         System.out.println("start()");
73         
74         if (args.length <= 0)
75             printHelp("Missing action parameter.");
76
77         prepareSystemOutErr();
78         
79         // * * Start the action thread
80
m_actionRunner = new ActionRunner(args[0]);
81         actionThread = new Thread JavaDoc(m_actionRunner);
82         actionThread.start();
83
84         return null;
85     }
86     
87     public int stop(int exitCode) {
88         System.out.println("stop(" + exitCode + ")");
89         
90         if (isNestedExit())
91         {
92             System.out.println("calling System.exit(" + exitCode + ") within stop.");
93             System.exit(exitCode);
94         }
95         
96         return exitCode;
97     }
98     
99     public void controlEvent(int event) {
100         System.out.println("controlEvent(" + event + ")");
101         if (event == WrapperManager.WRAPPER_CTRL_C_EVENT) {
102             //WrapperManager.stop(0);
103

104             // May be called before the running is started.
105
if (m_actionRunner != null) {
106                 m_actionRunner.endThread();
107             }
108         }
109     }
110
111     /**************************************************************************
112      * Inner Classes
113      *************************************************************************/

114     private class ActionRunner implements Runnable JavaDoc {
115         private String JavaDoc m_action;
116         private boolean m_alive;
117         
118         public ActionRunner(String JavaDoc action) {
119             m_action = action;
120             m_alive = true;
121         }
122     
123         public void run() {
124             // Wait for 5 seconds so that the startup will complete.
125
try {
126                 Thread.sleep(5000);
127             } catch (InterruptedException JavaDoc e) {}
128             
129             if (!TestAction.this.doAction(m_action)) {
130                 printHelp("\"" + m_action + "\" is an unknown action.");
131                 WrapperManager.stop(0);
132                 return;
133             }
134     
135             while (m_alive) {
136                 // Idle some
137
try {
138                     Thread.sleep(500);
139                 } catch (Exception JavaDoc e) {
140                     e.printStackTrace();
141                 }
142             }
143         }
144     
145         public void endThread( ) {
146             m_alive = false;
147         }
148     }
149     
150     /**
151      * Prints the usage text.
152      *
153      * @param error_msg Error message to write with usage text
154      */

155     private static void printHelp(String JavaDoc error_msg) {
156         System.err.println( "USAGE" );
157         System.err.println( "" );
158         System.err.println( "TestAction <action>" );
159         System.err.println( "" );
160         System.err.println( "[ACTIONS]" );
161         System.err.println( " Actions which should cause the Wrapper to exit cleanly:" );
162         System.err.println( " stop0 : Calls WrapperManager.stop(0)" );
163         System.err.println( " exit0 : Calls System.exit(0)" );
164         System.err.println( " stopimmediate0 : Calls WrapperManager.stopImmediate(0)" );
165         System.err.println( " stopandreturn0 : Calls WrapperManager.stopAndReturn(0)" );
166         System.err.println( " Actions which should cause the Wrapper to exit in an error state:" );
167         System.err.println( " stop1 : Calls WrapperManager.stop(1)" );
168         System.err.println( " exit1 : Calls System.exit(1)" );
169         System.err.println( " nestedexit1 : Calls System.exit(1) within WrapperListener.stop(1) callback" );
170         System.err.println( " stopimmediate1 : Calls WrapperManager.stopImmediate(1)" );
171         System.err.println( " Actions which should cause the Wrapper to restart the JVM:" );
172         System.err.println( " access_violation : Calls WrapperManager.accessViolation" );
173         System.err.println( " access_violation_native : Calls WrapperManager.accessViolationNative()" );
174         System.err.println( " appear_hung : Calls WrapperManager.appearHung()" );
175         System.err.println( " halt0 : Calls Runtime.getRuntime().halt(0)" );
176         System.err.println( " halt1 : Calls Runtime.getRuntime().halt(1)" );
177         System.err.println( " restart : Calls WrapperManager.restart()" );
178         System.err.println( " restartandreturn : Calls WrapperManager.restartAndReturn()" );
179         System.err.println( " Additional Tests:" );
180         System.err.println( " dump : Calls WrapperManager.requestThreadDump()" );
181         System.err.println( " deadlock_out : Deadlocks the JVM's System.out and err streams." );
182         System.err.println( " users : Start polling the current and interactive users." );
183         System.err.println( " groups : Start polling the current and interactive users with groups." );
184         System.err.println( " console : Prompt for actions in the console." );
185         System.err.println( " idle : Do nothing just run in idle mode." );
186         System.err.println( " properties : Dump all System Properties to the console." );
187         System.err.println( " configuration : Dump all Wrapper Configuration Properties to the console." );
188         System.err.println( "" );
189         System.err.println( "[EXAMPLE]" );
190         System.err.println( " TestAction access_violation_native " );
191         System.err.println( "" );
192         System.err.println( "ERROR: " + error_msg );
193         System.err.println( "" );
194
195         System.exit( -1 );
196     }
197
198     /**************************************************************************
199      * Main Method
200      *************************************************************************/

201     public static void main(String JavaDoc[] args) {
202         System.out.println("Initializing...");
203         
204         // Start the application. If the JVM was launched from the native
205
// Wrapper then the application will wait for the native Wrapper to
206
// call the application's start method. Otherwise the start method
207
// will be called immediately.
208
WrapperManager.start(new TestAction(), args);
209     }
210 }
211
212
Popular Tags