001: package org.tanukisoftware.wrapper.test;
002:
003: /*
004: * Copyright (c) 1999, 2006 Tanuki Software Inc.
005: *
006: * Permission is hereby granted, free of charge, to any person
007: * obtaining a copy of the Java Service Wrapper and associated
008: * documentation files (the "Software"), to deal in the Software
009: * without restriction, including without limitation the rights
010: * to use, copy, modify, merge, publish, distribute, sub-license,
011: * and/or sell copies of the Software, and to permit persons to
012: * whom the Software is furnished to do so, subject to the
013: * following conditions:
014: *
015: * The above copyright notice and this permission notice shall be
016: * included in all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
020: * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
021: * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
022: * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
023: * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
024: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
025: * OTHER DEALINGS IN THE SOFTWARE.
026: *
027: *
028: * Portions of the Software have been derived from source code
029: * developed by Silver Egg Technology under the following license:
030: *
031: * Copyright (c) 2001 Silver Egg Technology
032: *
033: * Permission is hereby granted, free of charge, to any person
034: * obtaining a copy of this software and associated documentation
035: * files (the "Software"), to deal in the Software without
036: * restriction, including without limitation the rights to use,
037: * copy, modify, merge, publish, distribute, sub-license, and/or
038: * sell copies of the Software, and to permit persons to whom the
039: * Software is furnished to do so, subject to the following
040: * conditions:
041: *
042: * The above copyright notice and this permission notice shall be
043: * included in all copies or substantial portions of the Software.
044: */
045:
046: import org.tanukisoftware.wrapper.WrapperManager;
047: import org.tanukisoftware.wrapper.WrapperListener;
048:
049: /**
050: *
051: *
052: * @author Leif Mortenson <leif@tanukisoftware.com>
053: */
054: public class TestAction extends AbstractActionApp implements
055: WrapperListener {
056: private ActionRunner m_actionRunner;
057:
058: /**************************************************************************
059: * Constructors
060: *************************************************************************/
061: private TestAction() {
062: }
063:
064: /**************************************************************************
065: * WrapperListener Methods
066: *************************************************************************/
067: public Integer start(String[] args) {
068: Thread actionThread;
069:
070: System.out.println("start()");
071:
072: if (args.length <= 0)
073: printHelp("Missing action parameter.");
074:
075: prepareSystemOutErr();
076:
077: // * * Start the action thread
078: m_actionRunner = new ActionRunner(args[0]);
079: actionThread = new Thread(m_actionRunner);
080: actionThread.start();
081:
082: return null;
083: }
084:
085: public int stop(int exitCode) {
086: System.out.println("stop(" + exitCode + ")");
087:
088: if (isNestedExit()) {
089: System.out.println("calling System.exit(" + exitCode
090: + ") within stop.");
091: System.exit(exitCode);
092: }
093:
094: return exitCode;
095: }
096:
097: public void controlEvent(int event) {
098: System.out.println("controlEvent(" + event + ")");
099: if (event == WrapperManager.WRAPPER_CTRL_C_EVENT) {
100: //WrapperManager.stop(0);
101:
102: // May be called before the running is started.
103: if (m_actionRunner != null) {
104: m_actionRunner.endThread();
105: }
106: }
107: }
108:
109: /**************************************************************************
110: * Inner Classes
111: *************************************************************************/
112: private class ActionRunner implements Runnable {
113: private String m_action;
114: private boolean m_alive;
115:
116: public ActionRunner(String action) {
117: m_action = action;
118: m_alive = true;
119: }
120:
121: public void run() {
122: // Wait for 5 seconds so that the startup will complete.
123: try {
124: Thread.sleep(5000);
125: } catch (InterruptedException e) {
126: }
127:
128: if (!TestAction.this .doAction(m_action)) {
129: printHelp("\"" + m_action + "\" is an unknown action.");
130: WrapperManager.stop(0);
131: return;
132: }
133:
134: while (m_alive) {
135: // Idle some
136: try {
137: Thread.sleep(500);
138: } catch (Exception e) {
139: e.printStackTrace();
140: }
141: }
142: }
143:
144: public void endThread() {
145: m_alive = false;
146: }
147: }
148:
149: /**
150: * Prints the usage text.
151: *
152: * @param error_msg Error message to write with usage text
153: */
154: private static void printHelp(String error_msg) {
155: System.err.println("USAGE");
156: System.err.println("");
157: System.err.println("TestAction <action>");
158: System.err.println("");
159: System.err.println("[ACTIONS]");
160: System.err
161: .println(" Actions which should cause the Wrapper to exit cleanly:");
162: System.err
163: .println(" stop0 : Calls WrapperManager.stop(0)");
164: System.err
165: .println(" exit0 : Calls System.exit(0)");
166: System.err
167: .println(" stopimmediate0 : Calls WrapperManager.stopImmediate(0)");
168: System.err
169: .println(" stopandreturn0 : Calls WrapperManager.stopAndReturn(0)");
170: System.err
171: .println(" Actions which should cause the Wrapper to exit in an error state:");
172: System.err
173: .println(" stop1 : Calls WrapperManager.stop(1)");
174: System.err
175: .println(" exit1 : Calls System.exit(1)");
176: System.err
177: .println(" nestedexit1 : Calls System.exit(1) within WrapperListener.stop(1) callback");
178: System.err
179: .println(" stopimmediate1 : Calls WrapperManager.stopImmediate(1)");
180: System.err
181: .println(" Actions which should cause the Wrapper to restart the JVM:");
182: System.err
183: .println(" access_violation : Calls WrapperManager.accessViolation");
184: System.err
185: .println(" access_violation_native : Calls WrapperManager.accessViolationNative()");
186: System.err
187: .println(" appear_hung : Calls WrapperManager.appearHung()");
188: System.err
189: .println(" halt0 : Calls Runtime.getRuntime().halt(0)");
190: System.err
191: .println(" halt1 : Calls Runtime.getRuntime().halt(1)");
192: System.err
193: .println(" restart : Calls WrapperManager.restart()");
194: System.err
195: .println(" restartandreturn : Calls WrapperManager.restartAndReturn()");
196: System.err.println(" Additional Tests:");
197: System.err
198: .println(" dump : Calls WrapperManager.requestThreadDump()");
199: System.err
200: .println(" deadlock_out : Deadlocks the JVM's System.out and err streams.");
201: System.err
202: .println(" users : Start polling the current and interactive users.");
203: System.err
204: .println(" groups : Start polling the current and interactive users with groups.");
205: System.err
206: .println(" console : Prompt for actions in the console.");
207: System.err
208: .println(" idle : Do nothing just run in idle mode.");
209: System.err
210: .println(" properties : Dump all System Properties to the console.");
211: System.err
212: .println(" configuration : Dump all Wrapper Configuration Properties to the console.");
213: System.err.println("");
214: System.err.println("[EXAMPLE]");
215: System.err.println(" TestAction access_violation_native ");
216: System.err.println("");
217: System.err.println("ERROR: " + error_msg);
218: System.err.println("");
219:
220: System.exit(-1);
221: }
222:
223: /**************************************************************************
224: * Main Method
225: *************************************************************************/
226: public static void main(String[] args) {
227: System.out.println("Initializing...");
228:
229: // Start the application. If the JVM was launched from the native
230: // Wrapper then the application will wait for the native Wrapper to
231: // call the application's start method. Otherwise the start method
232: // will be called immediately.
233: WrapperManager.start(new TestAction(), args);
234: }
235: }
|