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 java.io.BufferedReader;
047: import java.io.InputStreamReader;
048: import java.io.IOException;
049: import java.lang.reflect.InvocationTargetException;
050: import java.lang.reflect.Method;
051: import java.util.Enumeration;
052: import java.util.Properties;
053:
054: import org.tanukisoftware.wrapper.WrapperManager;
055: import org.tanukisoftware.wrapper.WrapperServiceException;
056: import org.tanukisoftware.wrapper.WrapperWin32Service;
057: import org.tanukisoftware.wrapper.event.WrapperControlEvent;
058: import org.tanukisoftware.wrapper.event.WrapperEvent;
059: import org.tanukisoftware.wrapper.event.WrapperEventListener;
060:
061: /**
062: * @author Leif Mortenson <leif@tanukisoftware.com>
063: */
064: public abstract class AbstractActionApp implements WrapperEventListener {
065: private DeadlockPrintStream m_out;
066: private DeadlockPrintStream m_err;
067:
068: private Thread m_runner;
069: private Thread m_consoleRunner;
070:
071: private boolean m_users;
072: private boolean m_groups;
073:
074: private boolean m_nestedExit;
075:
076: private long m_eventMask = 0xffffffffffffffffL;
077: private String m_serviceName = "testWrapper";
078:
079: /*---------------------------------------------------------------
080: * Constructors
081: *-------------------------------------------------------------*/
082: protected AbstractActionApp() {
083: m_runner = new Thread("WrapperActionTest_Runner") {
084: public void run() {
085: while (true) {
086: if (m_users) {
087: System.out.println("The current user is: "
088: + WrapperManager.getUser(m_groups));
089: System.out
090: .println("The current interactive user is: "
091: + WrapperManager
092: .getInteractiveUser(m_groups));
093: }
094: synchronized (AbstractActionApp.class) {
095: try {
096: AbstractActionApp.class.wait(5000);
097: } catch (InterruptedException e) {
098: }
099: }
100: }
101: }
102: };
103: m_runner.setDaemon(true);
104: m_runner.start();
105: }
106:
107: /*---------------------------------------------------------------
108: * WrapperEventListener Methods
109: *-------------------------------------------------------------*/
110: /**
111: * Called whenever a WrapperEvent is fired. The exact set of events that a
112: * listener will receive will depend on the mask supplied when
113: * WrapperManager.addWrapperEventListener was called to register the
114: * listener.
115: *
116: * Listener implementations should never assume that they will only receive
117: * events of a particular type. To assure that events added to future
118: * versions of the Wrapper do not cause problems with user code, events
119: * should always be tested with "if ( event instanceof {EventClass} )"
120: * before casting it to a specific event type.
121: *
122: * @param event WrapperEvent which was fired.
123: */
124: public void fired(WrapperEvent event) {
125: System.out.println("Received event: " + event);
126: if (event instanceof WrapperControlEvent) {
127: System.out.println(" Consume and ignore.");
128: ((WrapperControlEvent) event).consume();
129: }
130: }
131:
132: /*---------------------------------------------------------------
133: * Methods
134: *-------------------------------------------------------------*/
135: protected boolean isNestedExit() {
136: return m_nestedExit;
137: }
138:
139: protected void setEventMask(long eventMask) {
140: m_eventMask = eventMask;
141: }
142:
143: protected void setServiceName(String serviceName) {
144: m_serviceName = serviceName;
145: }
146:
147: protected void prepareSystemOutErr() {
148: m_out = new DeadlockPrintStream(System.out);
149: System.setOut(m_out);
150: m_err = new DeadlockPrintStream(System.err);
151: System.setErr(m_err);
152: }
153:
154: protected boolean doAction(String action) {
155: if (action.equals("stop0")) {
156: WrapperManager.stop(0);
157:
158: } else if (action.equals("stop1")) {
159: WrapperManager.stop(1);
160:
161: } else if (action.equals("exit0")) {
162: System.exit(0);
163:
164: } else if (action.equals("exit1")) {
165: System.exit(1);
166:
167: } else if (action.equals("nestedexit1")) {
168: m_nestedExit = true;
169: WrapperManager.stop(1);
170:
171: } else if (action.equals("stopimmediate0")) {
172: WrapperManager.stopImmediate(0);
173: } else if (action.equals("stopimmediate1")) {
174: WrapperManager.stopImmediate(1);
175: } else if (action.equals("stopandreturn0")) {
176: WrapperManager.stopAndReturn(0);
177: } else if (action.equals("halt0")) {
178: // Execute runtime.halt(0) using reflection so this class will
179: // compile on 1.2.x versions of Java.
180: Method haltMethod;
181: try {
182: haltMethod = Runtime.class.getMethod("halt",
183: new Class[] { Integer.TYPE });
184: } catch (NoSuchMethodException e) {
185: System.out
186: .println("halt not supported by current JVM.");
187: haltMethod = null;
188: }
189:
190: if (haltMethod != null) {
191: Runtime runtime = Runtime.getRuntime();
192: try {
193: haltMethod.invoke(runtime,
194: new Object[] { new Integer(0) });
195: } catch (IllegalAccessException e) {
196: System.out.println("Unable to call runitme.halt: "
197: + e.getMessage());
198: } catch (InvocationTargetException e) {
199: System.out.println("Unable to call runitme.halt: "
200: + e.getMessage());
201: }
202: }
203: } else if (action.equals("halt1")) {
204: // Execute runtime.halt(1) using reflection so this class will
205: // compile on 1.2.x versions of Java.
206: Method haltMethod;
207: try {
208: haltMethod = Runtime.class.getMethod("halt",
209: new Class[] { Integer.TYPE });
210: } catch (NoSuchMethodException e) {
211: System.out
212: .println("halt not supported by current JVM.");
213: haltMethod = null;
214: }
215:
216: if (haltMethod != null) {
217: Runtime runtime = Runtime.getRuntime();
218: try {
219: haltMethod.invoke(runtime,
220: new Object[] { new Integer(1) });
221: } catch (IllegalAccessException e) {
222: System.out.println("Unable to call runitme.halt: "
223: + e.getMessage());
224: } catch (InvocationTargetException e) {
225: System.out.println("Unable to call runitme.halt: "
226: + e.getMessage());
227: }
228: }
229: } else if (action.equals("restart")) {
230: WrapperManager.restart();
231:
232: } else if (action.equals("restartandreturn")) {
233: WrapperManager.restartAndReturn();
234:
235: } else if (action.equals("access_violation")) {
236: WrapperManager.accessViolation();
237:
238: } else if (action.equals("access_violation_native")) {
239: WrapperManager.accessViolationNative();
240:
241: } else if (action.equals("appear_hung")) {
242: WrapperManager.appearHung();
243:
244: } else if (action.equals("dump")) {
245: WrapperManager.requestThreadDump();
246:
247: } else if (action.equals("deadlock_out")) {
248: System.out
249: .println("Deadlocking System.out and System.err ...");
250: m_out.setDeadlock(true);
251: m_err.setDeadlock(true);
252:
253: } else if (action.equals("users")) {
254: if (!m_users) {
255: System.out
256: .println("Begin polling the current and interactive users.");
257: m_users = true;
258: } else if (m_groups) {
259: System.out.println("Stop polling for group info.");
260: m_groups = false;
261: } else {
262: System.out
263: .println("Stop polling the current and interactive users.");
264: m_users = false;
265: }
266:
267: synchronized (AbstractActionApp.class) {
268: AbstractActionApp.class.notifyAll();
269: }
270: } else if (action.equals("groups")) {
271: if ((!m_users) || (!m_groups)) {
272: System.out
273: .println("Begin polling the current and interactive users with group info.");
274: m_users = true;
275: m_groups = true;
276: } else {
277: System.out.println("Stop polling for group info.");
278: m_groups = false;
279: }
280:
281: synchronized (AbstractActionApp.class) {
282: AbstractActionApp.class.notifyAll();
283: }
284: } else if (action.equals("console")) {
285: if (m_consoleRunner == null) {
286: m_consoleRunner = new Thread("console-runner") {
287: public void run() {
288: System.out.println();
289: System.out
290: .println("Start prompting for actions.");
291: try {
292: BufferedReader r = new BufferedReader(
293: new InputStreamReader(System.in));
294: String line;
295: try {
296: do {
297: System.out
298: .println("Input an action (return stops prompting):");
299: line = r.readLine();
300: if ((line != null)
301: && (!line.equals(""))) {
302: System.out
303: .println("Read action: "
304: + line);
305: if (!doAction(line)) {
306: System.out
307: .println("Unknown action: "
308: + line);
309: }
310: }
311: } while ((line != null)
312: && (!line.equals("")));
313: } catch (IOException e) {
314: e.printStackTrace();
315: }
316: } finally {
317: System.out
318: .println("Stop prompting for actions.");
319: System.out.println();
320: m_consoleRunner = null;
321: }
322: }
323: };
324: m_consoleRunner.setDaemon(true);
325: m_consoleRunner.start();
326: }
327: } else if (action.equals("idle")) {
328: System.out.println("Run idle.");
329: m_users = false;
330: m_groups = false;
331:
332: synchronized (AbstractActionApp.class) {
333: AbstractActionApp.class.notifyAll();
334: }
335: } else if (action.equals("properties")) {
336: System.out.println("Dump System Properties:");
337: Properties props = System.getProperties();
338: for (Enumeration en = props.propertyNames(); en
339: .hasMoreElements();) {
340: String name = (String) en.nextElement();
341: System.out.println(" " + name + "="
342: + props.getProperty(name));
343: }
344: System.out.println();
345: } else if (action.equals("configuration")) {
346: System.out.println("Dump Wrapper Properties:");
347: Properties props = WrapperManager.getProperties();
348: for (Enumeration en = props.propertyNames(); en
349: .hasMoreElements();) {
350: String name = (String) en.nextElement();
351: System.out.println(" " + name + "="
352: + props.getProperty(name));
353: }
354: System.out.println();
355: } else if (action.equals("listener")) {
356: System.out.println("Updating Event Listeners:");
357: WrapperManager.removeWrapperEventListener(this );
358: WrapperManager.addWrapperEventListener(this , m_eventMask);
359: } else if (action.equals("service_list")) {
360: WrapperWin32Service[] services = WrapperManager
361: .listServices();
362: if (services == null) {
363: System.out
364: .println("Services not supported by current platform.");
365: } else {
366: System.out.println("Registered Services:");
367: for (int i = 0; i < services.length; i++) {
368: System.out.println(" " + services[i]);
369: }
370: }
371: } else if (action.equals("service_interrogate")) {
372: try {
373: WrapperWin32Service service = WrapperManager
374: .sendServiceControlCode(
375: m_serviceName,
376: WrapperManager.SERVICE_CONTROL_CODE_INTERROGATE);
377: System.out.println("Service after interrogate: "
378: + service);
379: } catch (WrapperServiceException e) {
380: e.printStackTrace();
381: }
382: } else if (action.equals("service_start")) {
383: try {
384: WrapperWin32Service service = WrapperManager
385: .sendServiceControlCode(
386: m_serviceName,
387: WrapperManager.SERVICE_CONTROL_CODE_START);
388: System.out.println("Service after start: " + service);
389: } catch (WrapperServiceException e) {
390: e.printStackTrace();
391: }
392: } else if (action.equals("service_stop")) {
393: try {
394: WrapperWin32Service service = WrapperManager
395: .sendServiceControlCode(
396: m_serviceName,
397: WrapperManager.SERVICE_CONTROL_CODE_STOP);
398: System.out.println("Service after stop: " + service);
399: } catch (WrapperServiceException e) {
400: e.printStackTrace();
401: }
402: } else if (action.equals("service_user")) {
403: try {
404: for (int i = 128; i < 256; i += 10) {
405: WrapperWin32Service service = WrapperManager
406: .sendServiceControlCode(m_serviceName, i);
407: System.out.println("Service after user code " + i
408: + ": " + service);
409: }
410: } catch (WrapperServiceException e) {
411: e.printStackTrace();
412: }
413: } else {
414: // Unknown action
415: return false;
416:
417: }
418:
419: return true;
420: }
421: }
|