1 package org.tanukisoftware.wrapper.test; 2 3 45 46 import java.awt.BorderLayout ; 47 import java.awt.Button ; 48 import java.awt.Component ; 49 import java.awt.Container ; 50 import java.awt.Frame ; 51 import java.awt.GridBagConstraints ; 52 import java.awt.GridBagLayout ; 53 import java.awt.Label ; 54 import java.awt.List ; 55 import java.awt.Panel ; 56 import java.awt.ScrollPane ; 57 import java.awt.TextField ; 58 import java.awt.event.ActionEvent ; 59 import java.awt.event.ActionListener ; 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 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 m_userRunner; 90 91 private List m_listenerFlags; 92 private TextField m_serviceName; 93 94 97 private Main() { 98 } 99 100 private class MainFrame extends Frame implements ActionListener 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 gridBag = new GridBagLayout (); 117 GridBagConstraints c = new GridBagConstraints (); 118 119 Panel panel = new Panel (); 120 panel.setLayout( gridBag ); 121 122 ScrollPane scrollPane = new ScrollPane (); 123 scrollPane.add( panel ); 124 scrollPane.getHAdjustable().setUnitIncrement( 20 ); 125 scrollPane.getVAdjustable().setUnitIncrement( 20 ); 126 127 setLayout( new BorderLayout () ); 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 ( 2, true ); 199 m_listenerFlags.add( "Service" ); 200 m_listenerFlags.add( "Control" ); 201 m_listenerFlags.add( "Core" ); 202 203 Panel flagPanel = new Panel (); 204 flagPanel.setLayout( new BorderLayout () ); 205 flagPanel.add( new Label ( "Event Flags: " ), BorderLayout.WEST ); 206 flagPanel.add( m_listenerFlags, BorderLayout.CENTER ); 207 flagPanel.setSize( 100, 10 ); 208 209 Panel flagPanel2 = new Panel (); 210 flagPanel2.setLayout( new BorderLayout () ); 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 ( "testwrapper" ); 218 219 Panel servicePanel = new Panel (); 220 servicePanel.setLayout( new BorderLayout () ); 221 servicePanel.add( new Label ( "Interrogate Service. Service name: " ), BorderLayout.WEST ); 222 servicePanel.add( m_serviceName, BorderLayout.CENTER ); 223 224 Panel servicePanel2 = new Panel (); 225 servicePanel2.setLayout( new BorderLayout () ); 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 container, 238 GridBagLayout gridBag, 239 GridBagConstraints c, 240 String label, 241 String command, 242 Object description ) 243 { 244 Button button = new Button ( 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 desc; 255 if ( description instanceof String ) 256 { 257 desc = new Label ( (String )description ); 258 } 259 else if ( description instanceof Component ) 260 { 261 desc = (Component )description; 262 } 263 else 264 { 265 desc = new Label ( description.toString() ); 266 } 267 268 gridBag.setConstraints( desc, c ); 269 container.add( desc ); 270 } 271 272 public void actionPerformed( ActionEvent event ) 273 { 274 String action = event.getActionCommand(); 275 if ( action.equals( "listener" ) ) 276 { 277 long mask = 0; 279 String [] flags = m_listenerFlags.getSelectedItems(); 280 for ( int i = 0; i < flags.length; i++ ) 281 { 282 String 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 309 public Integer start( String [] 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 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 ( 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 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 } 395 else 396 { 397 WrapperManager.stop( 0 ); 398 } 399 } 400 401 404 409 public static void main( String [] args ) 410 { 411 System.out.println( "Initializing..." ); 412 413 WrapperManager.start( new Main(), args ); 418 } 419 } 420 421 | Popular Tags |