首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

java代码兑现启动tomcat

2012-07-23 
java代码实现启动tomcatpackage com.huawei.m2m.deploy.ui.actionimport java.io.BufferedReaderimport

java代码实现启动tomcat
package com.huawei.m2m.deploy.ui.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.lang.StringUtils;
import org.eclipse.gmf.runtime.common.core.util.Log;

import com.huawei.m2m.deploy.ui.DeployUiPlugin;
import com.huawei.m2m.deploy.ui.LogStatusCodes;
import com.huawei.m2m.deploy.ui.l10n.Messages;
import com.huawei.m2m.deploy.ui.util.DeployManager;

/**
* StartService
*/
public class StartService
{
    /**
     * Tomcat没启动则启动Tomcat
     * @return boolean
     */
    public boolean startTomcatService()
    {
       
        String cmd = DeployManager.getPath() + "apache-tomcat/bin/startup.bat"; //$NON-NLS-1$
        try
        {
            if (!isStartTomcat())
            {
                Process proc = Runtime.getRuntime().exec(cmd);
                StreamGobblerThread errorGobbler = new StreamGobblerThread(
                        proc.getErrorStream(), "Error"); //$NON-NLS-1$
                StreamGobblerThread outputGobbler = new StreamGobblerThread(
                        proc.getInputStream(), "Output"); //$NON-NLS-1$
                errorGobbler.start();
                outputGobbler.start();
               
                if (proc.waitFor() == 0)
                {
                    return false;
                }
            }
            return true;
        }
        catch (Exception e)
        {
            Log.error(DeployUiPlugin.getDefault(),
                    LogStatusCodes.StartService,
                    e.getMessage());
           
        }
        return false;
    }
   
    private boolean isStartTomcat()
    {
        BufferedReader br = null;
        Process pro = null;
        try
        {
            pro = Runtime.getRuntime().exec("netstat -an -p TCP"); //$NON-NLS-1$
            br = new BufferedReader(new InputStreamReader(pro.getInputStream()));
            String str = StringUtils.EMPTY;
            while ((str = br.readLine()) != null)
            {
                String temp = "LISTENING";//$NON-NLS-1$
                if (StringUtils.isNotBlank(str)
                        && str.contains(Messages.M2MDeployUtil_0)
                        && str.contains(temp))
                {
                    return true;
                }
            }
           
        }
        catch (Exception e)
        {
            Log.error(DeployUiPlugin.getDefault(),
                    LogStatusCodes.StartService,
                    e.getMessage());
        }
        finally
        {
           
            if (br != null)
            {
                try
                {
                    br.close();
                }
                catch (IOException e)
                {
                    Log.error(DeployUiPlugin.getDefault(),
                            LogStatusCodes.StartService,
                            e.getMessage());
                }
            }
            if (pro != null)
            {
                pro.destroy();
            }
        }
        return false;
    }
   
}

热点排行