KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.BufferedReader JavaDoc;
47 import java.io.InputStreamReader JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.lang.reflect.InvocationTargetException JavaDoc;
50 import java.lang.reflect.Method JavaDoc;
51 import java.util.Enumeration JavaDoc;
52 import java.util.Properties JavaDoc;
53
54 import org.tanukisoftware.wrapper.WrapperManager;
55 import org.tanukisoftware.wrapper.WrapperServiceException;
56 import org.tanukisoftware.wrapper.WrapperWin32Service;
57 import org.tanukisoftware.wrapper.event.WrapperControlEvent;
58 import org.tanukisoftware.wrapper.event.WrapperEvent;
59 import org.tanukisoftware.wrapper.event.WrapperEventListener;
60
61 /**
62  * @author Leif Mortenson <leif@tanukisoftware.com>
63  */

64 public abstract class AbstractActionApp
65     implements WrapperEventListener
66 {
67     private DeadlockPrintStream m_out;
68     private DeadlockPrintStream m_err;
69     
70     private Thread JavaDoc m_runner;
71     private Thread JavaDoc m_consoleRunner;
72     
73     private boolean m_users;
74     private boolean m_groups;
75     
76     private boolean m_nestedExit;
77     
78     private long m_eventMask = 0xffffffffffffffffL;
79     private String JavaDoc m_serviceName = "testWrapper";
80     
81     /*---------------------------------------------------------------
82      * Constructors
83      *-------------------------------------------------------------*/

84     protected AbstractActionApp() {
85         m_runner = new Thread JavaDoc( "WrapperActionTest_Runner" )
86         {
87             public void run()
88             {
89                 while ( true )
90                 {
91                     if ( m_users )
92                     {
93                         System.out.println( "The current user is: "
94                             + WrapperManager.getUser( m_groups ) );
95                         System.out.println( "The current interactive user is: "
96                             + WrapperManager.getInteractiveUser( m_groups ) );
97                     }
98                     synchronized( AbstractActionApp.class )
99                     {
100                         try
101                         {
102                             AbstractActionApp.class.wait( 5000 );
103                         }
104                         catch ( InterruptedException JavaDoc e )
105                         {
106                         }
107                     }
108                 }
109             }
110         };
111         m_runner.setDaemon( true );
112         m_runner.start();
113     }
114     
115     /*---------------------------------------------------------------
116      * WrapperEventListener Methods
117      *-------------------------------------------------------------*/

118     /**
119      * Called whenever a WrapperEvent is fired. The exact set of events that a
120      * listener will receive will depend on the mask supplied when
121      * WrapperManager.addWrapperEventListener was called to register the
122      * listener.
123      *
124      * Listener implementations should never assume that they will only receive
125      * events of a particular type. To assure that events added to future
126      * versions of the Wrapper do not cause problems with user code, events
127      * should always be tested with "if ( event instanceof {EventClass} )"
128      * before casting it to a specific event type.
129      *
130      * @param event WrapperEvent which was fired.
131      */

132     public void fired( WrapperEvent event )
133     {
134         System.out.println( "Received event: " + event );
135         if ( event instanceof WrapperControlEvent )
136         {
137             System.out.println( " Consume and ignore." );
138             ((WrapperControlEvent)event).consume();
139         }
140     }
141     
142     /*---------------------------------------------------------------
143      * Methods
144      *-------------------------------------------------------------*/

145     protected boolean isNestedExit()
146     {
147         return m_nestedExit;
148     }
149     
150     protected void setEventMask( long eventMask )
151     {
152         m_eventMask = eventMask;
153     }
154     
155     protected void setServiceName( String JavaDoc serviceName )
156     {
157         m_serviceName = serviceName;
158     }
159     
160     protected void prepareSystemOutErr()
161     {
162         m_out = new DeadlockPrintStream( System.out );
163         System.setOut( m_out );
164         m_err = new DeadlockPrintStream( System.err );
165         System.setErr( m_err );
166     }
167     protected boolean doAction( String JavaDoc action )
168     {
169         if ( action.equals( "stop0" ) )
170         {
171             WrapperManager.stop( 0 );
172             
173         }
174         else if ( action.equals( "stop1" ) )
175         {
176             WrapperManager.stop( 1 );
177             
178         }
179         else if ( action.equals( "exit0" ) )
180         {
181             System.exit( 0 );
182             
183         }
184         else if ( action.equals( "exit1" ) )
185         {
186             System.exit( 1 );
187             
188         }
189         else if ( action.equals( "nestedexit1" ) )
190         {
191             m_nestedExit = true;
192             WrapperManager.stop( 1 );
193             
194         }
195         else if ( action.equals( "stopimmediate0" ) )
196         {
197             WrapperManager.stopImmediate( 0 );
198         }
199         else if ( action.equals( "stopimmediate1" ) )
200         {
201             WrapperManager.stopImmediate( 1 );
202         }
203         else if ( action.equals( "stopandreturn0" ) )
204         {
205             WrapperManager.stopAndReturn( 0 );
206         }
207         else if ( action.equals( "halt0" ) )
208         {
209             // Execute runtime.halt(0) using reflection so this class will
210
// compile on 1.2.x versions of Java.
211
Method JavaDoc haltMethod;
212             try
213             {
214                 haltMethod = Runtime JavaDoc.class.getMethod( "halt", new Class JavaDoc[] { Integer.TYPE } );
215             }
216             catch ( NoSuchMethodException JavaDoc e )
217             {
218                 System.out.println( "halt not supported by current JVM." );
219                 haltMethod = null;
220             }
221             
222             if ( haltMethod != null )
223             {
224                 Runtime JavaDoc runtime = Runtime.getRuntime();
225                 try
226                 {
227                     haltMethod.invoke( runtime, new Object JavaDoc[] { new Integer JavaDoc( 0 ) } );
228                 }
229                 catch ( IllegalAccessException JavaDoc e )
230                 {
231                     System.out.println( "Unable to call runitme.halt: " + e.getMessage() );
232                 }
233                 catch ( InvocationTargetException JavaDoc e )
234                 {
235                     System.out.println( "Unable to call runitme.halt: " + e.getMessage() );
236                 }
237             }
238         }
239         else if ( action.equals( "halt1" ) )
240         {
241             // Execute runtime.halt(1) using reflection so this class will
242
// compile on 1.2.x versions of Java.
243
Method JavaDoc haltMethod;
244             try
245             {
246                 haltMethod = Runtime JavaDoc.class.getMethod( "halt", new Class JavaDoc[] { Integer.TYPE } );
247             }
248             catch ( NoSuchMethodException JavaDoc e )
249             {
250                 System.out.println( "halt not supported by current JVM." );
251                 haltMethod = null;
252             }
253             
254             if ( haltMethod != null )
255             {
256                 Runtime JavaDoc runtime = Runtime.getRuntime();
257                 try
258                 {
259                     haltMethod.invoke( runtime, new Object JavaDoc[] { new Integer JavaDoc( 1 ) } );
260                 }
261                 catch ( IllegalAccessException JavaDoc e )
262                 {
263                     System.out.println( "Unable to call runitme.halt: " + e.getMessage() );
264                 }
265                 catch ( InvocationTargetException JavaDoc e )
266                 {
267                     System.out.println( "Unable to call runitme.halt: " + e.getMessage() );
268                 }
269             }
270         }
271         else if ( action.equals( "restart" ) )
272         {
273             WrapperManager.restart();
274             
275         }
276         else if ( action.equals( "restartandreturn" ) )
277         {
278             WrapperManager.restartAndReturn();
279             
280         }
281         else if ( action.equals( "access_violation" ) )
282         {
283             WrapperManager.accessViolation();
284             
285         }
286         else if ( action.equals( "access_violation_native" ) )
287         {
288             WrapperManager.accessViolationNative();
289             
290         }
291         else if ( action.equals( "appear_hung" ) )
292         {
293             WrapperManager.appearHung();
294             
295         }
296         else if ( action.equals( "dump" ) )
297         {
298             WrapperManager.requestThreadDump();
299             
300         }
301         else if ( action.equals( "deadlock_out" ) )
302         {
303             System.out.println( "Deadlocking System.out and System.err ..." );
304             m_out.setDeadlock( true );
305             m_err.setDeadlock( true );
306             
307         }
308         else if ( action.equals( "users" ) )
309         {
310             if ( !m_users )
311             {
312                 System.out.println( "Begin polling the current and interactive users." );
313                 m_users = true;
314             }
315             else if ( m_groups )
316             {
317                 System.out.println( "Stop polling for group info." );
318                 m_groups = false;
319             }
320             else
321             {
322                 System.out.println( "Stop polling the current and interactive users." );
323                 m_users = false;
324             }
325             
326             synchronized( AbstractActionApp.class )
327             {
328                 AbstractActionApp.class.notifyAll();
329             }
330         }
331         else if ( action.equals( "groups" ) )
332         {
333             if ( ( !m_users ) || ( !m_groups ) )
334             {
335                 System.out.println( "Begin polling the current and interactive users with group info." );
336                 m_users = true;
337                 m_groups = true;
338             }
339             else
340             {
341                 System.out.println( "Stop polling for group info." );
342                 m_groups = false;
343             }
344             
345             synchronized( AbstractActionApp.class )
346             {
347                 AbstractActionApp.class.notifyAll();
348             }
349         }
350         else if ( action.equals( "console" ) )
351         {
352             if ( m_consoleRunner == null )
353             {
354                 m_consoleRunner = new Thread JavaDoc( "console-runner" )
355                 {
356                     public void run()
357                     {
358                         System.out.println();
359                         System.out.println( "Start prompting for actions." );
360                         try
361                         {
362                             BufferedReader JavaDoc r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
363                             String JavaDoc line;
364                             try
365                             {
366                                 do {
367                                     System.out.println("Input an action (return stops prompting):");
368                                     line = r.readLine();
369                                     if ((line != null) && (!line.equals(""))) {
370                                         System.out.println("Read action: " + line );
371                                         if ( !doAction( line ) )
372                                         {
373                                             System.out.println( "Unknown action: " + line );
374                                         }
375                                     }
376                                 } while ((line != null) && (!line.equals("")));
377                             }
378                             catch ( IOException JavaDoc e )
379                             {
380                                 e.printStackTrace();
381                             }
382                         }
383                         finally
384                         {
385                             System.out.println( "Stop prompting for actions." );
386                             System.out.println();
387                             m_consoleRunner = null;
388                         }
389                     }
390                 };
391                 m_consoleRunner.setDaemon( true );
392                 m_consoleRunner.start();
393             }
394         }
395         else if ( action.equals( "idle" ) )
396         {
397             System.out.println( "Run idle." );
398             m_users = false;
399             m_groups = false;
400             
401             synchronized( AbstractActionApp.class )
402             {
403                 AbstractActionApp.class.notifyAll();
404             }
405         }
406         else if ( action.equals( "properties" ) )
407         {
408             System.out.println( "Dump System Properties:" );
409             Properties JavaDoc props = System.getProperties();
410             for ( Enumeration JavaDoc en = props.propertyNames(); en.hasMoreElements(); )
411             {
412                 String JavaDoc name = (String JavaDoc)en.nextElement();
413                 System.out.println( " " + name + "=" + props.getProperty( name ) );
414             }
415             System.out.println();
416         }
417         else if ( action.equals( "configuration" ) )
418         {
419             System.out.println( "Dump Wrapper Properties:" );
420             Properties JavaDoc props = WrapperManager.getProperties();
421             for ( Enumeration JavaDoc en = props.propertyNames(); en.hasMoreElements(); )
422             {
423                 String JavaDoc name = (String JavaDoc)en.nextElement();
424                 System.out.println( " " + name + "=" + props.getProperty( name ) );
425             }
426             System.out.println();
427         }
428         else if ( action.equals( "listener" ) )
429         {
430             System.out.println( "Updating Event Listeners:" );
431             WrapperManager.removeWrapperEventListener( this );
432             WrapperManager.addWrapperEventListener( this, m_eventMask );
433         }
434         else if ( action.equals( "service_list" ) )
435         {
436             WrapperWin32Service[] services = WrapperManager.listServices();
437             if ( services == null )
438             {
439                 System.out.println( "Services not supported by current platform." );
440             }
441             else
442             {
443                 System.out.println( "Registered Services:" );
444                 for ( int i = 0; i < services.length; i++ )
445                 {
446                     System.out.println( " " + services[i] );
447                 }
448             }
449         }
450         else if ( action.equals( "service_interrogate" ) )
451         {
452             try
453             {
454                 WrapperWin32Service service = WrapperManager.sendServiceControlCode(
455                     m_serviceName, WrapperManager.SERVICE_CONTROL_CODE_INTERROGATE );
456                 System.out.println( "Service after interrogate: " + service );
457             }
458             catch ( WrapperServiceException e )
459             {
460                 e.printStackTrace();
461             }
462         }
463         else if ( action.equals( "service_start" ) )
464         {
465             try
466             {
467                 WrapperWin32Service service = WrapperManager.sendServiceControlCode(
468                     m_serviceName, WrapperManager.SERVICE_CONTROL_CODE_START );
469                 System.out.println( "Service after start: " + service );
470             }
471             catch ( WrapperServiceException e )
472             {
473                 e.printStackTrace();
474             }
475         }
476         else if ( action.equals( "service_stop" ) )
477         {
478             try
479             {
480                 WrapperWin32Service service = WrapperManager.sendServiceControlCode(
481                     m_serviceName, WrapperManager.SERVICE_CONTROL_CODE_STOP );
482                 System.out.println( "Service after stop: " + service );
483             }
484             catch ( WrapperServiceException e )
485             {
486                 e.printStackTrace();
487             }
488         }
489         else if ( action.equals( "service_user" ) )
490         {
491             try
492             {
493                 for ( int i = 128; i < 256; i+=10 )
494                 {
495                     WrapperWin32Service service = WrapperManager.sendServiceControlCode(
496                         m_serviceName, i );
497                     System.out.println( "Service after user code " + i + ": " + service );
498                 }
499             }
500             catch ( WrapperServiceException e )
501             {
502                 e.printStackTrace();
503             }
504         }
505         else
506         {
507             // Unknown action
508
return false;
509         
510         }
511         
512         return true;
513     }
514 }
515
516
Popular Tags