⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wrappersimpleapp.java

📁 java程序写系统的服务
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
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.
 * 
 * 
 * Portions of the Software have been derived from source code
 * developed by Silver Egg Technology under the following license:
 * 
 * Copyright (c) 2001 Silver Egg Technology
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software 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.
 */

// $Log: WrapperSimpleApp.java,v $
// Revision 1.12  2006/07/02 15:19:00  mortenson
// Make it possible to extend the WrapperSimpleApp and WrapperStartStopApp
// helper classes.
//
// Revision 1.11  2006/02/24 05:45:57  mortenson
// Update the copyright.
//
// Revision 1.10  2006/01/07 02:39:27  mortenson
// Fix a problem with the WrapperSimpleApp and WrapperStartStopApp helper
// classes where on heavily loaded systems it was possible for the Wrapper
// to get a running thread count of 0 and shutdown before the main thread
// had a chance to be started.
//
// Revision 1.9  2005/12/07 02:45:18  mortenson
// Modify the WrapperSimpleApp and WrapperStartStopApp classes so that the
// WrapperManager is always initialized immediately.  This makes the output
// clearer in the event of startup errors.
//
// Revision 1.8  2005/05/23 02:41:12  mortenson
// Update the copyright information.
//
// Revision 1.7  2005/03/24 07:23:51  mortenson
// Make it possible to control whether or not the helper classes wait for the main
// methods to complete before reporting that the application has been started.
//
// Revision 1.6  2004/02/16 04:37:20  mortenson
// Modify the WrapperSimpleApp and WrapperStartStopApp so that the main method
// of a class is located even if it exists in a parent class rather than the
// class specified.
//
// Revision 1.5  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.4  2003/06/19 05:45:02  mortenson
// Modified the suggested behavior of the WrapperListener.controlEvent() method.
//
// Revision 1.3  2003/04/03 04:05:23  mortenson
// Fix several typos in the docs.  Thanks to Mike Castle.
//
// Revision 1.2  2003/02/17 09:52:16  mortenson
// Modify the way exceptions thrown by an application's main method are
// presented to the user by the WrapperSimpleApp and WrapperStartStopApp so
// they no longer look like a problem with Wrapper configuration.
//
// Revision 1.1  2003/02/03 06:55:28  mortenson
// License transfer to TanukiSoftware.org
//

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * By default the WrapperSimpleApp will only wait for 2 seconds for the main
 *  method of the start class to complete.  This was done because the main
 *  methods of many applications never return.  It is possible to force the
 *  class to wait for the startup main method to complete by defining the
 *  following system property when launching the JVM (defaults to FALSE):
 *  -Dorg.tanukisoftware.wrapper.WrapperSimpleApp.waitForStartMain=TRUE
 * <p>
 * Using the waitForStartMain property will cause the startup to wait
 *  indefinitely.  This is fine if the main method will always return
 *  within a predefined period of time.  But if there is any chance that
 *  it could hang, then the maxStartMainWait property may be a better
 *  option.  It allows the 2 second wait time to be overridden. To wait
 *  for up to 5 minutes for the startup main method to complete, set
 *  the property to 300 as follows (defaults to 2 seconds):
 *  -Dorg.tanukisoftware.wrapper.WrapperSimpleApp.maxStartMainWait=300
 * <p>
 * It is possible to extend this class but make absolutely sure that any
 *  overridden methods call their super method or the class will fail to
 *  function correctly.  Most users will have no need to override this
 *  class.
 * <p>
 * NOTE - The main methods of many applications are designed not to
 *  return.  In these cases, you must either stick with the default 2 second
 *  startup timeout or specify a slightly longer timeout, using the
 *  maxStartMainWait property, to simulate the amount of time your application
 *  takes to start up.
 * <p>
 * WARNING - If the waitForStartMain is specified for an application
 *  whose start method never returns, the Wrapper will appear at first to be
 *  functioning correctly.  However the Wrapper will never enter a running
 *  state, this means that the Windows Service Manager and several of the
 *  Wrapper's error recovery mechanisms will not function correctly.
 *
 * @author Leif Mortenson <leif@tanukisoftware.com>
 * @version $Revision: 1.12 $
 */
public class WrapperSimpleApp
    implements WrapperListener, Runnable
{
    /**
     * Application's main method
     */
    private Method m_mainMethod;
    
    /**
     * Command line arguments to be passed on to the application
     */
    private String[] m_appArgs;
    
    /**
     * Gets set to true when the thread used to launch the application
     *  actuially starts.
     */
    private boolean m_mainStarted;
    
    /**
     * Gets set to true when the thread used to launch the application
     *  completes.
     */
    private boolean m_mainComplete;
    
    /**
     * Exit code to be returned if the application fails to start.
     */
    private Integer m_mainExitCode;
    
    /**
     * Flag used to signify that the start method has completed.
     */
    private boolean m_startComplete;
    
    /*---------------------------------------------------------------
     * Constructors
     *-------------------------------------------------------------*/
    /**
     * Creates an instance of a WrapperSimpleApp.
     *
     * @param The full list of arguments passed to the JVM.
     */
    protected WrapperSimpleApp( String args[] )
    {
        
        // Initialize the WrapperManager class on startup by referencing it.
        Class wmClass = WrapperManager.class;
        
        // Get the class name of the application
        if ( args.length < 1 )
        {
            showUsage();
            WrapperManager.stop( 1 );
            return;  // Will not get here
        }
        
        // Look for the specified class by name
        Class mainClass;
        try
        {
            mainClass = Class.forName( args[0] );
        }
        catch ( ClassNotFoundException e )
        {
            System.out.println( "WrapperSimpleApp: Unable to locate the class " + args[0] + ": "
                + e );
            showUsage();
            WrapperManager.stop( 1 );
            return;  // Will not get here
        }
        catch ( LinkageError e )
        {
            System.out.println( "WrapperSimpleApp: Unable to locate the class " + args[0] + ": "
                + e );
            showUsage();
            WrapperManager.stop( 1 );
            return;  // Will not get here
        }
        
        // Look for the main method
        try
        {
            // getDeclaredMethod will return any method named main in the specified class,
            //  while getMethod will only return public methods, but it will search up the
            //  inheritance path.
            m_mainMethod = mainClass.getMethod( "main", new Class[] { String[].class } );
        }
        catch ( NoSuchMethodException e )
        {
            System.out.println(
                "WrapperSimpleApp: Unable to locate a public static main method in class "
                + args[0] + ": " + e );
            showUsage();
            WrapperManager.stop( 1 );
            return;  // Will not get here
        }
        catch ( SecurityException e )
        {
            System.out.println(
                "WrapperSimpleApp: Unable to locate a public static main method in class "
                + args[0] + ": " + e );
            showUsage();
            WrapperManager.stop( 1 );
            return;  // Will not get here
        }
        
        // Make sure that the method is public and static
        int modifiers = m_mainMethod.getModifiers();
        if ( !( Modifier.isPublic( modifiers ) && Modifier.isStatic( modifiers ) ) )
        {
            System.out.println( "WrapperSimpleApp: The main method in class " + args[0]
                + " must be declared public and static." );
            showUsage();
            WrapperManager.stop( 1 );
            return;  // Will not get here
        }
        
        // Build the application args array
        String[] appArgs = new String[args.length - 1];
        System.arraycopy( args, 1, appArgs, 0, appArgs.length );
        
        // Start the application.  If the JVM was launched from the native
        //  Wrapper then the application will wait for the native Wrapper to
        //  call the application's start method.  Otherwise the start method
        //  will be called immediately.
        WrapperManager.start( this, appArgs );
        
        // This thread ends, the WrapperManager will start the application after the Wrapper has
        //  been properly initialized by calling the start method above.
    }
    
    /*---------------------------------------------------------------
     * Runnable Methods

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -