- package com.zwl.desk.ntvExt
- {
- import flash.desktop.NativeProcess;
- import flash.desktop.NativeProcessStartupInfo;
- import flash.events.ProgressEvent;
- import flash.filesystem.File;
- import flash.filesystem.FileMode;
- import flash.filesystem.FileStream;
- import flash.system.Capabilities;
- import mx.controls.Alert;
- import mx.controls.TextArea;
- import mx.utils.StringUtil;
- /**
- *
- * @author zwl
- * Tomcat管理控制类,通过AIR的NativeProcess嵌入启动Tomcat7.x。便于扩展AIR的桌面应用功能。
- */
- public class TomcatMng
- {
- public var tomcatHomeDir:File;
- public var javaHomeDir:File;
- [Bindable]
- public var log:TextArea = null;
- private var canRun:Boolean = false;//运行tomcat条件是否就绪标记
- public function TomcatMng(log:TextArea)
- {
- this.log = log;//设置日志组件
- init();
- }
- public function startTomcat():void
- {
- if (!canRun){
- return;
- }
- if (log!=null){
- log.text = "start tomcat...\\n";
- }
- run(new NativeProcess(), "start");
- }
- public function stopTomcat():void
- {
- if (!canRun){
- return;
- }
- if (log!=null){
- log.text = "stop tomcat...\\n";
- }
- run(new NativeProcess(), "stop");
- }
- /**
- * 初始化启动环境
- * @return 运行tomcat条件是否就绪
- *
- */
- private function init():Boolean
- {
- var xml:XML = readExtConfig();
- if (xml == null){
- return false;
- }
- if (!(StringUtil.trim(xml.active)=="true")){
- return false;
- }
- if (StringUtil.trim(xml.javaHome)==""){
- return false;
- }
- if (StringUtil.trim(xml.tomcatHome)==""){
- return false;
- }
- javaHomeDir = File.applicationDirectory.resolvePath(xml.javaHome);
- if (!javaHomeDir.exists){
- return false;
- }
- tomcatHomeDir = File.applicationDirectory.resolvePath(xml.tomcatHome);
- if (!tomcatHomeDir.exists){
- return false;
- }
- if (!NativeProcess.isSupported){
- Alert.show("myeDesk不支持本地进程扩展,请使用myeDeskExt启动。","警告");
- return false;
- }
- canRun = true;//就绪标记
- return true;
- }
- /**
- * 读取配置文件
- * @return XML文件内容例如:
- <?xml version="1.0" encoding="utf-8"?>
- <config>
- <active>true</active>
- <javaHome>native\\jdk</javaHome>
- <tomcatHome>native\\tomcat</tomcatHome>
- </config>
- */
- private function readExtConfig():XML
- {
- var file:File = new File( File.applicationDirectory.resolvePath("etc/extcfg.xml" ).nativePath);
- if (file.exists){
- var fileStream:FileStream = new FileStream();
- fileStream.open(file, FileMode.READ);
- var xml:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
- fileStream.close();
- return xml;
- }else{
- return null;
- }
- }
- private function run(ntvPrc:NativeProcess, arg:String):void
- {
- //----得到JVM执行文件-------
- var jvmFile:File;
- if (Capabilities.os.toLowerCase().indexOf("win") >= 0){
- jvmFile = javaHomeDir.resolvePath("bin/java.exe");
- }else{//mac或其它os
- jvmFile = javaHomeDir.resolvePath("bin/java");
- }
- if (!jvmFile.exists){
- return;
- }
- try{
- var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
- nativeProcessStartupInfo.executable = jvmFile;
- nativeProcessStartupInfo.workingDirectory = tomcatHomeDir.resolvePath("bin");
- //----设置jvm启动参数-----
- var prcArgs:Vector.<String> = new Vector.<String>();
- //---设置jvm的区域和语言,防止控制台中文乱码-----
- prcArgs[0] = "-Dfile.encoding=UTF8";
- prcArgs[1] = "-Ddefault.client.encoding=UTF8";
- prcArgs[2] = "-Duser.language=zh";
- prcArgs[3] = "-Duser.region=CN";
- //-----设置jvm内存参数,对于桌面应用,这些参数固定无妨,就不麻烦搞成可配置的了^_^-------
- prcArgs[4] = "-Xms128m";
- prcArgs[5] = "-Xmx256m";
- prcArgs[6] = "-XX:PermSize=64M";
- prcArgs[7] = "-XX:MaxPermSize=256m";
- //-----设置tomcat启动参数-------
- prcArgs[8] = "-Dcatalina.home="+tomcatHomeDir.nativePath;
- prcArgs[9] = "-classpath";
- prcArgs[10] = javaHomeDir.resolvePath("lib/tools.jar;").nativePath+
- tomcatHomeDir.resolvePath("bin/commons-daemon.jar;").nativePath+
- tomcatHomeDir.resolvePath("bin/tomcat-juli.jar;").nativePath+
- tomcatHomeDir.resolvePath("bin/bootstrap.jar").nativePath;
- prcArgs[11] = "org.apache.catalina.startup.Bootstrap";
- //----设置start或stop参数----
- prcArgs[12] = arg;
- nativeProcessStartupInfo.arguments = prcArgs;
- //----注册回调并启动进程------
- ntvPrc.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA,
- outputHandler);
- ntvPrc.addEventListener(ProgressEvent.STANDARD_ERROR_DATA,
- errorOutputHandler);
- ntvPrc.start(nativeProcessStartupInfo);
- }catch (e:Error){
- Alert.show(e.message, "错误");
- }
- }
- private function outputHandler(event:ProgressEvent):void
- {
- var ntvPrc:NativeProcess = event.target as NativeProcess;
- var outStd:String = ntvPrc.standardOutput.readUTFBytes(ntvPrc.standardOutput.bytesAvailable);
- if (log!=null){
- log.text=log.text+outStd;
- }
- }
- private function errorOutputHandler(event:ProgressEvent):void
- {
- var ntvPrc:NativeProcess = event.target as NativeProcess;
- var outErr:String = ntvPrc.standardError.readUTFBytes(ntvPrc.standardError.bytesAvailable);
- if (log!=null){
- log.text=log.text+outErr;
- }
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/221020136552.html
来源: http://www.codesnippet.cn/detail/221020136552.html