📄 wrapperactionserver.java
字号:
package org.tanukisoftware.wrapper;
/*
* Copyright (c) 1999, 2006 Tanuki Software Inc.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of the Java Service Wrapper and associated
* documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sub-license,
* and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
// $Log: WrapperActionServer.java,v $
// Revision 1.10 2006/02/24 05:45:57 mortenson
// Update the copyright.
//
// Revision 1.9 2005/09/12 03:54:30 mortenson
// Fix some spelling.
//
// Revision 1.8 2005/05/23 02:41:12 mortenson
// Update the copyright information.
//
// Revision 1.7 2004/06/30 09:02:33 mortenson
// Remove unused imports.
//
// Revision 1.6 2004/01/16 04:42:00 mortenson
// The license was revised for this version to include a copyright omission.
// This change is to be retroactively applied to all versions of the Java
// Service Wrapper starting with version 3.0.0.
//
// Revision 1.5 2003/10/30 17:13:24 mortenson
// Add an action to the WrapperActionServer which makes it possible to test
// simulate a JVM hang for testing.
//
// Revision 1.4 2003/09/03 08:55:48 mortenson
// Add some more javadocs describing how to use the class.
//
// Revision 1.3 2003/08/20 17:42:03 mortenson
// Got rid of some old messages.
//
// Revision 1.2 2003/06/08 04:15:34 mortenson
// Synchronize access to the actions map.
// Cast chars to byte so that the class will compile under Java 1.2.x
//
// Revision 1.1 2003/06/07 05:19:11 mortenson
// Add a new class, WrapperActionServer, which makes it easy to remotely control
// the Wrapper remotely by opening a socket and sending commands. See the
// javadocs of the class for more details.
import java.io.IOException;
import java.io.InterruptedIOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.ServerSocket;
import java.util.Hashtable;
import org.tanukisoftware.wrapper.WrapperManager;
/**
* If an application instantiates an instance of this class, the JVM will
* listen on the specified port for connections. When a connection is
* detected, the first byte of input will be read from the socket and
* then the connection will be immediately closed. An action will then
* be performed based on the byte read from the stream.
* <p>
* The easiest way to invoke an action manually is to telnet to the specified
* port and then type the single command key.
* <code>telnet localhost 9999</code>, for example.
* <p>
* Valid commands include:
* <ul>
* <li><b>S</b> : Shutdown cleanly.</li>
* <li><b>H</b> : Immediate forced shutdown.</li>
* <li><b>R</b> : Restart</li>
* <li><b>D</b> : Perform a Thread Dump</li>
* <li><b>U</b> : Unexpected shutdown. (Simulate a crash for testing)</li>
* <li><b>V</b> : Cause an access violation. (For testing)</li>
* <li><b>G</b> : Make the JVM appear to be hung. (For testing)</li>
* </ul>
* Additional user defined actions can be defined by calling the
* {@link #registerAction( byte command, Runnable action )} method.
* The Wrapper project reserves the right to define any upper case
* commands in the future. To avoid future conflicts, please use lower
* case for user defined commands.
* <p>
* This application will work even in most deadlock situations because the
* thread is in issolation from the rest of the application. If the JVM
* is truely hung, this class will fail to accept connections but the
* Wrapper itself will detect the hang and restart the JVM externally.
* <p>
* The following code can be used in your application to start up the
* WrapperActionServer with all default actions enabled:
* <pre>
* int port = 9999;
* WrapperActionServer server = new WrapperActionServer( port );
* server.enableShutdownAction( true );
* server.enableHaltExpectedAction( true );
* server.enableRestartAction( true );
* server.enableThreadDumpAction( true );
* server.enableHaltUnexpectedAction( true );
* server.enableAccessViolationAction( true );
* server.enableAppearHungAction( true );
* server.start();
* </pre>
* Then remember to stop the server when your application shuts down:
* <pre>
* server.stop();
* </pre>
*
* @author Leif Mortenson <leif@tanukisoftware.com>
* @version $Revision: 1.10 $
*/
public class WrapperActionServer
implements Runnable
{
/** Command to invoke a shutdown action. */
public final static byte COMMAND_SHUTDOWN = (byte)'S';
/** Command to invoke an expected halt action. */
public final static byte COMMAND_HALT_EXPECTED = (byte)'H';
/** Command to invoke a restart action. */
public final static byte COMMAND_RESTART = (byte)'R';
/** Command to invoke a thread dump action. */
public final static byte COMMAND_DUMP = (byte)'D';
/** Command to invoke an unexpected halt action. */
public final static byte COMMAND_HALT_UNEXPECTED = (byte)'U';
/** Command to invoke an access violation. */
public final static byte COMMAND_ACCESS_VIOLATION = (byte)'V';
/** Command to invoke an appear hung action. */
public final static byte COMMAND_APPEAR_HUNG = (byte)'G';
/** The address to bind the port server to. Null for any address. */
private InetAddress m_bindAddr;
/** The port to listen on for connections. */
private int m_port;
/** Reference to the worker thread. */
private Thread m_runner;
/** Flag set when the m_runner thread has been asked to stop. */
private boolean m_runnerStop = false;
/** Reference to the ServerSocket. */
private ServerSocket m_serverSocket;
/** Table of all the registered actions. */
private Hashtable m_actions = new Hashtable();
/*---------------------------------------------------------------
* Constructors
*-------------------------------------------------------------*/
/**
* Creates and starts WrapperActionServer instance bound to the
* specified port and address.
*
* @param port Port on which to listen for connections.
* @param bindAddress Address to bind to.
*/
public WrapperActionServer( int port, InetAddress bindAddress )
{
m_port = port;
m_bindAddr = bindAddress;
}
/**
* Creates and starts WrapperActionServer instance bound to the
* specified port. The socket will bind to all addresses and
* should be concidered a security risk.
*
* @param port Port on which to listen for connections.
*/
public WrapperActionServer( int port )
{
this( port, null );
}
/*---------------------------------------------------------------
* Runnable Methods
*-------------------------------------------------------------*/
/**
* Thread which will listen for connections on the socket.
*/
public void run()
{
if ( Thread.currentThread() != m_runner )
{
throw new IllegalStateException( "Private method." );
}
try
{
while ( !m_runnerStop )
{
try
{
int command;
Socket socket = m_serverSocket.accept();
try
{
// Set a short timeout of 15 seconds,
// so connections will be promptly closed if left idle.
socket.setSoTimeout( 15000 );
// Read a single byte.
command = socket.getInputStream().read();
}
finally
{
socket.close();
}
if ( command >= 0 )
{
Runnable action;
synchronized( m_actions )
{
action = (Runnable)m_actions.get( new Integer( command ) );
}
if ( action != null )
{
try
{
action.run();
}
catch ( Throwable t )
{
System.out.println(
"WrapperActionServer: Error processing action." );
t.printStackTrace();
}
}
}
}
catch ( Throwable t )
{
// Check for throwable type this way rather than with seperate catches
// to work around a problem where InterruptedException can be thrown
// when the compiler gives an error saying that it can't.
if ( m_runnerStop
&& ( ( t instanceof InterruptedException )
|| ( t instanceof SocketException )
|| ( t instanceof InterruptedIOException ) ) )
{
// This is expected, the service is being stopped.
}
else
{
System.out.println(
"WrapperActionServer: Unexpeced error." );
t.printStackTrace();
// Avoid tight thrashing
try
{
Thread.sleep( 5000 );
}
catch ( InterruptedException e )
{
// Ignore
}
}
}
}
}
finally
{
synchronized( this )
{
m_runner = null;
// Wake up the stop method if it is waiting for the runner to stop.
this.notify();
}
}
}
/*---------------------------------------------------------------
* Methods
*-------------------------------------------------------------*/
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -