KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.BorderLayout JavaDoc;
47 import java.awt.Button JavaDoc;
48 import java.awt.Component JavaDoc;
49 import java.awt.Container JavaDoc;
50 import java.awt.Frame JavaDoc;
51 import java.awt.GridBagConstraints JavaDoc;
52 import java.awt.GridBagLayout JavaDoc;
53 import java.awt.Label JavaDoc;
54 import java.awt.List JavaDoc;
55 import java.awt.Panel JavaDoc;
56 import java.awt.ScrollPane JavaDoc;
57 import java.awt.TextField JavaDoc;
58 import java.awt.event.ActionEvent JavaDoc;
59 import java.awt.event.ActionListener JavaDoc;
60
61 import org.tanukisoftware.wrapper.WrapperActionServer;
62 import org.tanukisoftware.wrapper.WrapperManager;
63 import org.tanukisoftware.wrapper.WrapperListener;
64 import org.tanukisoftware.wrapper.event.WrapperEventListener;
65
66 /**
67  * This is a Test / Example program which can be used to test the
68  * main features of the Wrapper.
69  * <p>
70  * It is also an example of Integration Method #3, where you implement
71  * the WrapperListener interface manually.
72  * <p>
73  * <b>NOTE</b> that in most cases you will want to use Method #1, using the
74  * WrapperSimpleApp helper class to integrate your application. Please
75  * see the <a HREF="http://wrapper.tanukisoftware.org/doc/english/integrate.html">integration</a>
76  * section of the documentation for more details.
77  *
78  * @author Leif Mortenson <leif@tanukisoftware.com>
79  */

80 public class Main
81     extends AbstractActionApp
82     implements WrapperListener
83 {
84     private MainFrame m_frame;
85     
86     private DeadlockPrintStream m_out;
87     private DeadlockPrintStream m_err;
88     
89     private Thread JavaDoc m_userRunner;
90     
91     private List JavaDoc m_listenerFlags;
92     private TextField JavaDoc m_serviceName;
93     
94     /**************************************************************************
95      * Constructors
96      *************************************************************************/

97     private Main() {
98     }
99     
100     private class MainFrame extends Frame JavaDoc implements ActionListener JavaDoc
101     {
102         MainFrame()
103         {
104             super( "Wrapper Test Application" );
105             
106             init();
107             
108             setLocation( 10, 10 );
109             setSize( 750, 480 );
110             
111             setResizable( true );
112         }
113         
114         private void init()
115         {
116             GridBagLayout JavaDoc gridBag = new GridBagLayout JavaDoc();
117             GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
118             
119             Panel JavaDoc panel = new Panel JavaDoc();
120             panel.setLayout( gridBag );
121             
122             ScrollPane JavaDoc scrollPane = new ScrollPane JavaDoc();
123             scrollPane.add( panel );
124             scrollPane.getHAdjustable().setUnitIncrement( 20 );
125             scrollPane.getVAdjustable().setUnitIncrement( 20 );
126             
127             setLayout( new BorderLayout JavaDoc() );
128             add( scrollPane, BorderLayout.CENTER );
129             
130             buildCommand( panel, gridBag, c, "Stop(0)", "stop0",
131                 "Calls WrapperManager.stop( 0 ) to shutdown the JVM and Wrapper with a success exit code." );
132             
133             buildCommand( panel, gridBag, c, "Stop(1)", "stop1",
134                 "Calls WrapperManager.stop( 1 ) to shutdown the JVM and Wrapper with a failure exit code." );
135             
136             buildCommand( panel, gridBag, c, "Exit(0)", "exit0",
137                 "Calls System.exit( 0 ) to shutdown the JVM and Wrapper with a success exit code." );
138             
139             buildCommand( panel, gridBag, c, "Exit(1)", "exit1",
140                 "Calls System.exit( 1 ) to shutdown the JVM and Wrapper with a failure exit code." );
141             
142             buildCommand( panel, gridBag, c, "StopImmediate(0)", "stopimmediate0",
143                 "Calls WrapperManager.stopImmediate( 0 ) to immediately shutdown the JVM and Wrapper with a success exit code." );
144             
145             buildCommand( panel, gridBag, c, "StopImmediate(1)", "stopimmediate1",
146                 "Calls WrapperManager.stopImmediate( 1 ) to immediately shutdown the JVM and Wrapper with a failure exir code." );
147             
148             buildCommand( panel, gridBag, c, "StopAndReturn(0)", "stopandreturn0",
149                 "Calls WrapperManager.stopAndReturn( 0 ) to shutdown the JVM and Wrapper with a success exit code." );
150             
151             buildCommand( panel, gridBag, c, "Nested Exit(1)", "nestedexit1",
152                 "Calls System.exit(1) within WrapperListener.stop(1) callback." );
153             
154             buildCommand( panel, gridBag, c, "Halt(0)", "halt0",
155                 "Calls Runtime.getRuntime().halt(0) to kill the JVM, the Wrapper will restart it." );
156             
157             buildCommand( panel, gridBag, c, "Halt(1)", "halt1",
158                 "Calls Runtime.getRuntime().halt(1) to kill the JVM, the Wrapper will restart it." );
159             
160             buildCommand( panel, gridBag, c, "Restart()", "restart",
161                 "Calls WrapperManager.restart() to shutdown the current JVM and start a new one." );
162             
163             buildCommand( panel, gridBag, c, "RestartAndReturn()", "restartandreturn",
164                 "Calls WrapperManager.restartAndReturn() to shutdown the current JVM and start a new one." );
165             
166             buildCommand( panel, gridBag, c, "Access Violation", "access_violation",
167                 "Attempts to cause an access violation within the JVM, relies on a JVM bug and may not work." );
168             
169             buildCommand( panel, gridBag, c, "Native Access Violation", "access_violation_native",
170                 "Causes an access violation using native code, the JVM will crash and be restarted." );
171             
172             buildCommand( panel, gridBag, c, "Simulate JVM Hang", "appear_hung",
173                 "Makes the JVM appear to be hung as viewed from the Wrapper, it will be killed and restarted." );
174             
175             buildCommand( panel, gridBag, c, "Request Thread Dump", "dump",
176                 "Calls WrapperManager.requestThreadDump() to cause the JVM to dump its current thread state." );
177             
178             buildCommand( panel, gridBag, c, "System.out Deadlock", "deadlock_out",
179                 "Simulates a failure mode where the System.out object has become deadlocked." );
180             
181             buildCommand( panel, gridBag, c, "Poll Users", "users",
182                 "Begins calling WrapperManager.getUser() and getInteractiveUser() to monitor the current and interactive users." );
183             
184             buildCommand( panel, gridBag, c, "Poll Users with Groups", "groups",
185                 "Same as above, but includes information about the user's groups." );
186             
187             buildCommand( panel, gridBag, c, "Console", "console", "Prompt for Actions in the console." );
188             
189             buildCommand( panel, gridBag, c, "Idle", "idle", "Run idly." );
190             
191             buildCommand( panel, gridBag, c, "Dump Properties", "properties",
192                 "Dumps all System Properties to the console." );
193             
194             buildCommand( panel, gridBag, c, "Dump Configuration", "configuration",
195                 "Dumps all Wrapper Configuration Properties to the console." );
196             
197             
198             m_listenerFlags = new List JavaDoc( 2, true );
199             m_listenerFlags.add( "Service" );
200             m_listenerFlags.add( "Control" );
201             m_listenerFlags.add( "Core" );
202             
203             Panel JavaDoc flagPanel = new Panel JavaDoc();
204             flagPanel.setLayout( new BorderLayout JavaDoc() );
205             flagPanel.add( new Label JavaDoc( "Event Flags: " ), BorderLayout.WEST );
206             flagPanel.add( m_listenerFlags, BorderLayout.CENTER );
207             flagPanel.setSize( 100, 10 );
208             
209             Panel JavaDoc flagPanel2 = new Panel JavaDoc();
210             flagPanel2.setLayout( new BorderLayout JavaDoc() );
211             flagPanel2.add( flagPanel, BorderLayout.WEST );
212             
213             buildCommand( panel, gridBag, c, "Update Event Listener", "listener", flagPanel2 );
214             
215             buildCommand( panel, gridBag, c, "Service List", "service_list", "Displays a list of registered services on Windows." );
216             
217             m_serviceName = new TextField JavaDoc( "testwrapper" );
218             
219             Panel JavaDoc servicePanel = new Panel JavaDoc();
220             servicePanel.setLayout( new BorderLayout JavaDoc() );
221             servicePanel.add( new Label JavaDoc( "Interrogate Service. Service name: " ), BorderLayout.WEST );
222             servicePanel.add( m_serviceName, BorderLayout.CENTER );
223             
224             Panel JavaDoc servicePanel2 = new Panel JavaDoc();
225             servicePanel2.setLayout( new BorderLayout JavaDoc() );
226             servicePanel2.add( servicePanel, BorderLayout.WEST );
227             
228             buildCommand( panel, gridBag, c, "Service Interrogate", "service_interrogate", servicePanel2 );
229             
230             buildCommand( panel, gridBag, c, "Service Start", "service_start", "Starts the above service." );
231             
232             buildCommand( panel, gridBag, c, "Service Stop", "service_stop", "Stops the above service." );
233             
234             buildCommand( panel, gridBag, c, "Service User Code", "service_user", "Sends a series of user codes to the above service." );
235         }
236         
237         private void buildCommand( Container JavaDoc container,
238                                    GridBagLayout JavaDoc gridBag,
239                                    GridBagConstraints JavaDoc c,
240                                    String JavaDoc label,
241                                    String JavaDoc command,
242                                    Object JavaDoc description )
243         {
244             Button JavaDoc button = new Button JavaDoc( label );
245             button.setActionCommand( command );
246             
247             c.fill = GridBagConstraints.BOTH;
248             c.gridwidth = 1;
249             gridBag.setConstraints( button, c );
250             container.add( button );
251             button.addActionListener(this);
252             
253             c.gridwidth = GridBagConstraints.REMAINDER;
254             Component JavaDoc desc;
255             if ( description instanceof String JavaDoc )
256             {
257                 desc = new Label JavaDoc( (String JavaDoc)description );
258             }
259             else if ( description instanceof Component JavaDoc )
260             {
261                 desc = (Component JavaDoc)description;
262             }
263             else
264             {
265                 desc = new Label JavaDoc( description.toString() );
266             }
267             
268             gridBag.setConstraints( desc, c );
269             container.add( desc );
270         }
271         
272         public void actionPerformed( ActionEvent JavaDoc event )
273         {
274             String JavaDoc action = event.getActionCommand();
275             if ( action.equals( "listener" ) )
276             {
277                 // Create the mask.
278
long mask = 0;
279                 String JavaDoc[] flags = m_listenerFlags.getSelectedItems();
280                 for ( int i = 0; i < flags.length; i++ )
281                 {
282                     String JavaDoc flag = flags[i];
283                     if ( flag.equals( "Service" ) )
284                     {
285                         mask |= WrapperEventListener.EVENT_FLAG_SERVICE;
286                     }
287                     else if ( flag.equals( "Control" ) )
288                     {
289                         mask |= WrapperEventListener.EVENT_FLAG_CONTROL;
290                     }
291                     else if ( flag.equals( "Core" ) )
292                     {
293                         mask |= WrapperEventListener.EVENT_FLAG_CORE;
294                     }
295                 }
296                 
297                 setEventMask( mask );
298             }
299             
300             setServiceName( m_serviceName.getText() );
301             
302             Main.this.doAction( action );
303         }
304     }
305     
306     /**************************************************************************
307      * WrapperListener Methods
308      *************************************************************************/

309     public Integer JavaDoc start( String JavaDoc[] args )
310     {
311         System.out.println( "start()" );
312
313         prepareSystemOutErr();
314         
315         try
316         {
317             m_frame = new MainFrame();
318             m_frame.setVisible( true );
319         }
320         catch ( java.lang.InternalError JavaDoc e )
321         {
322             System.out.println();
323             System.out.println( "ERROR - Unable to display the Swing GUI:" );
324             System.out.println( " " + e.toString() );
325             System.out.println( "Exiting" );
326             System.out.println();
327             return new Integer JavaDoc( 1 );
328         }
329
330         try
331         {
332             int port = 9999;
333             WrapperActionServer server = new WrapperActionServer( port );
334             server.enableShutdownAction( true );
335             server.enableHaltExpectedAction( true );
336             server.enableRestartAction( true );
337             server.enableThreadDumpAction( true );
338             server.enableHaltUnexpectedAction( true );
339             server.enableAccessViolationAction( true );
340             server.enableAppearHungAction( true );
341             server.start();
342             
343             System.out.println( "ActionServer Enabled. " );
344             System.out.println( " Telnet localhost 9999" );
345             System.out.println( " Commands: " );
346             System.out.println( " S: Shutdown" );
347             System.out.println( " H: Expected Halt" );
348             System.out.println( " R: Restart" );
349             System.out.println( " D: Thread Dump" );
350             System.out.println( " U: Unexpected Halt (Simulate crash)" );
351             System.out.println( " V: Access Violation (Actual crash)" );
352             System.out.println( " G: Make the JVM appear to be hung." );
353         }
354         catch ( java.io.IOException JavaDoc e )
355         {
356             System.out.println( "Unable to open the action server socket: " + e.getMessage() );
357         }
358         
359         return null;
360     }
361     
362     public int stop( int exitCode )
363     {
364         System.out.println( "stop(" + exitCode + ")" );
365         
366         if ( m_frame != null )
367         {
368             if ( !WrapperManager.hasShutdownHookBeenTriggered() )
369             {
370                 m_frame.setVisible( false );
371                 m_frame.dispose();
372             }
373             m_frame = null;
374         }
375         
376         if ( isNestedExit() )
377         {
378             System.out.println( "calling System.exit(" + exitCode + ") within stop." );
379             System.exit( exitCode );
380         }
381         
382         return exitCode;
383     }
384     
385     public void controlEvent( int event )
386     {
387         System.out.println( "controlEvent(" + event + ")" );
388         
389         if ( ( event == WrapperManager.WRAPPER_CTRL_LOGOFF_EVENT )
390             && WrapperManager.isLaunchedAsService() )
391         {
392             System.out.println( " Ignoring logoff event" );
393             // Ignore
394
}
395         else
396         {
397             WrapperManager.stop( 0 );
398         }
399     }
400     
401     /**************************************************************************
402      * Main Method
403      *************************************************************************/

404     /**
405      * IMPORTANT: Please read the Javadocs for this class at the top of the
406      * page before you start to use this class as a template for integrating
407      * your own application. This will save you a lot of time.
408      */

409     public static void main( String JavaDoc[] args )
410     {
411         System.out.println( "Initializing..." );
412         
413         // Start the application. If the JVM was launched from the native
414
// Wrapper then the application will wait for the native Wrapper to
415
// call the application's start method. Otherwise the start method
416
// will be called immediately.
417
WrapperManager.start( new Main(), args );
418     }
419 }
420
421
Popular Tags