forked from fanruan/design
MoMeak
7 years ago
11 changed files with 646 additions and 372 deletions
@ -0,0 +1,76 @@
|
||||
package com.fr.start; |
||||
import com.fr.start.server.FRTomcat; |
||||
|
||||
import javax.servlet.ServletException; |
||||
|
||||
import org.apache.catalina.Context; |
||||
import org.apache.catalina.LifecycleException; |
||||
import org.apache.catalina.core.AprLifecycleListener; |
||||
import org.apache.catalina.core.StandardServer; |
||||
import org.apache.catalina.startup.Tomcat; |
||||
|
||||
public class Booter { |
||||
|
||||
private static Tomcat tomcat; |
||||
|
||||
public static Tomcat getTomcat() { |
||||
return tomcat; |
||||
} |
||||
|
||||
private static StandardServer server; |
||||
|
||||
private static AprLifecycleListener listener; |
||||
|
||||
public static void main(String[] args) { |
||||
|
||||
tomcat = new FRTomcat(); |
||||
// 主机名,或ip
|
||||
// tomcat.setHostname("localhost");
|
||||
// 设置端口,80为默认端口
|
||||
tomcat.setPort(8071); |
||||
// tomcat用于存储自身的信息,可以随意指定,最好包含在项目目录下
|
||||
tomcat.setBaseDir("/Users/momeak/Documents/Working/develop/others/tomcatsrc"); |
||||
// 建立server参照tomcat文件结构
|
||||
server = (StandardServer) tomcat.getServer(); |
||||
listener = new AprLifecycleListener(); |
||||
server.addLifecycleListener(listener); |
||||
// 将appBase设为本项目所在目录
|
||||
//tomcat.getHost().setAppBase(".");
|
||||
// tomcat.getHost().setAppBase(System.getProperty("user.dir") + File.separator + ".");
|
||||
tomcat.getHost().setAppBase("/Users/momeak/Documents/Working/develop/others/tomcatsrc"); |
||||
|
||||
|
||||
// 第二个参数对应docBase为web应用路径,目录下应有WEB-INF,WEB-INF下要有web.xml
|
||||
// 启动tomcat
|
||||
try { |
||||
tomcat.start(); |
||||
Context ct1 = tomcat.addWebapp("/WebReport", "/Users/momeak/Documents/Working/develop/others/tomcatsrc/WebReport"); |
||||
} catch (LifecycleException e) { |
||||
e.printStackTrace(); |
||||
} catch (ServletException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
// Context ct1 = tomcat.addWebapp("/examples", "/Users/momeak/Documents/Working/develop/others/tomcatsrc/examples");
|
||||
|
||||
// Context ct = tomcat.addWebapp("", "/Users/momeak/Documents/Working/develop/others/tomcatsrc/webapps/ROOT");
|
||||
|
||||
// tomcat.getServer().await();
|
||||
System.out.println("启动成功"); |
||||
try { |
||||
Thread.sleep(5000); |
||||
} catch (InterruptedException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
// try {
|
||||
// tomcat.stop();
|
||||
// } catch (Exception e) {
|
||||
// System.exit(1);
|
||||
// }
|
||||
|
||||
System.out.println("123"); |
||||
System.exit(1); |
||||
|
||||
} |
||||
|
||||
|
||||
} |
@ -1 +0,0 @@
|
||||
/*
* Copyright (c) 2001-2014,FineReport Inc, All Rights Reserved.
*/
package com.fr.start;
import org.mortbay.http.SocketListener;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.WebApplicationContext;
import org.mortbay.jetty.servlet.XMLConfiguration;
/**
* Created with IntelliJ IDEA.
* User: THX
* Date: 13-8-22
* Time: 上午9:42
* To change this template use File | Settings | File Templates.
*/
public class JettyFRHost {
public static void main(String[] args) throws Exception {
Server server = new Server();
SocketListener listener = new SocketListener();
listener.setPort(8076);
server.addListener(listener);
server.setWebApplicationConfigurationClassNames(new String[]{XMLConfiguration.class.getName()});
WebApplicationContext webapp = server.addWebApplication("/WebReport", "D:\\fineReport\\WebReport");
// WebAppContext context = new WebAppContext();
// context.setDescriptor("d:/fineReport/WebReport/" + "/WEB-INF/web.xml");
// context.setResourceBase("d:/fineReport/WebReport/");
// context.setContextPath("/WebReport");
// context.setParentLoaderPriority(true);
// server.setHandler(context);
if (!webapp.isStarted()) {
webapp.start();
}
server.start();
}
} |
@ -0,0 +1,56 @@
|
||||
package com.fr.start; |
||||
|
||||
import java.io.File; |
||||
import javax.servlet.ServletException; |
||||
import org.apache.catalina.Context; |
||||
import org.apache.catalina.LifecycleException; |
||||
import org.apache.catalina.core.AprLifecycleListener; |
||||
import org.apache.catalina.core.StandardServer; |
||||
import org.apache.catalina.startup.Tomcat; |
||||
|
||||
public class TomcatFRHost { |
||||
private static Tomcat tomcat; |
||||
|
||||
public static Tomcat getTomcat() { |
||||
return tomcat; |
||||
} |
||||
|
||||
private static StandardServer server; |
||||
|
||||
private static AprLifecycleListener listener; |
||||
|
||||
public static void main(String[] args) throws Exception { |
||||
tomcat = new Tomcat(); |
||||
// 主机名,或ip
|
||||
// tomcat.setHostname("localhost");
|
||||
// 设置端口,80为默认端口
|
||||
tomcat.setPort(8071); |
||||
// tomcat用于存储自身的信息,可以随意指定,最好包含在项目目录下
|
||||
tomcat.setBaseDir("."); |
||||
// 建立server参照tomcat文件结构
|
||||
server = (StandardServer) tomcat.getServer(); |
||||
listener = new AprLifecycleListener(); |
||||
server.addLifecycleListener(listener); |
||||
// 将appBase设为本项目所在目录
|
||||
//tomcat.getHost().setAppBase(".");
|
||||
tomcat.getHost().setAppBase( |
||||
System.getProperty("user.dir") + File.separator + "."); |
||||
|
||||
// 第二个参数对应docBase为web应用路径,目录下应有WEB-INF,WEB-INF下要有web.xml
|
||||
// 启动tomcat
|
||||
try { |
||||
tomcat.start(); |
||||
Context ct1 = tomcat.addWebapp("/WebReport", "/Users/momeak/Documents/Working/develop/others/tomcatsrc/WebReport"); |
||||
} catch (LifecycleException e) { |
||||
e.printStackTrace(); |
||||
} catch (ServletException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
// Context ct1 = tomcat.addWebapp("/examples", "/Users/momeak/Documents/Working/develop/others/tomcatsrc/examples");
|
||||
|
||||
// Context ct = tomcat.addWebapp("", "/Users/momeak/Documents/Working/develop/others/tomcatsrc/webapps/ROOT");
|
||||
|
||||
// tomcat.getServer().await();
|
||||
System.out.println("启动成功"); |
||||
} |
||||
} |
@ -0,0 +1,133 @@
|
||||
package com.fr.start.server; |
||||
import java.lang.reflect.InvocationTargetException; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
import java.util.logging.Level; |
||||
import java.util.logging.Logger; |
||||
import javax.servlet.ServletException; |
||||
import org.apache.catalina.Context; |
||||
import org.apache.catalina.Host; |
||||
import org.apache.catalina.core.ContainerBase; |
||||
import org.apache.catalina.core.StandardContext; |
||||
import org.apache.catalina.core.StandardHost; |
||||
import org.apache.catalina.startup.ContextConfig; |
||||
import org.apache.catalina.startup.Tomcat; |
||||
|
||||
public class FRTomcat extends Tomcat{ |
||||
|
||||
private final Map<String, Logger> frpinnedLoggers = new HashMap<String, Logger>(); |
||||
private boolean frsilent = false; |
||||
|
||||
|
||||
public Context addWebapp(String contextPath, String docBase) throws ServletException { |
||||
silence(host, contextPath); |
||||
|
||||
Context ctx = createContext(host, contextPath); |
||||
if (ctx instanceof StandardContext) { |
||||
// ((StandardContext)ctx).setDelegate(true);
|
||||
} |
||||
ctx.setPath(contextPath); |
||||
ctx.setDocBase(docBase); |
||||
|
||||
ctx.addLifecycleListener(new DefaultWebXmlListener()); |
||||
ctx.setConfigFile(getWebappConfigFile(docBase, contextPath)); |
||||
|
||||
ContextConfig ctxCfg = new ContextConfig(); |
||||
ctx.addLifecycleListener(ctxCfg); |
||||
|
||||
ctxCfg.setDefaultWebXml(noDefaultWebXmlPath()); |
||||
|
||||
if (host == null) { |
||||
getHost().addChild(ctx); |
||||
} else { |
||||
host.addChild(ctx); |
||||
} |
||||
|
||||
return ctx; |
||||
} |
||||
|
||||
private void silence(Host host, String contextPath) { |
||||
String loggerName = getLoggerName(host, contextPath); |
||||
Logger logger = Logger.getLogger(loggerName); |
||||
frpinnedLoggers.put(loggerName, logger); |
||||
if (frsilent) { |
||||
logger.setLevel(Level.WARNING); |
||||
} else { |
||||
logger.setLevel(Level.INFO); |
||||
} |
||||
} |
||||
|
||||
private String getLoggerName(Host host, String contextName) { |
||||
if (host == null) { |
||||
host = getHost(); |
||||
} |
||||
StringBuilder loggerName = new StringBuilder(); |
||||
loggerName.append(ContainerBase.class.getName()); |
||||
loggerName.append(".["); |
||||
// Engine name
|
||||
loggerName.append(host.getParent().getName()); |
||||
loggerName.append("].["); |
||||
// Host name
|
||||
loggerName.append(host.getName()); |
||||
loggerName.append("].["); |
||||
// Context name
|
||||
if (contextName == null || contextName.equals("")) { |
||||
loggerName.append("/"); |
||||
} else if (contextName.startsWith("##")) { |
||||
loggerName.append("/"); |
||||
loggerName.append(contextName); |
||||
} |
||||
loggerName.append(']'); |
||||
|
||||
return loggerName.toString(); |
||||
} |
||||
|
||||
private Context createContext(Host host, String url) { |
||||
String contextClass = StandardContext.class.getName(); |
||||
if (host == null) { |
||||
host = this.getHost(); |
||||
} |
||||
if (host instanceof StandardHost) { |
||||
contextClass = ((StandardHost) host).getContextClass(); |
||||
} |
||||
try { |
||||
return (Context) Class.forName(contextClass).getConstructor() |
||||
.newInstance(); |
||||
} catch (InstantiationException e) { |
||||
throw new IllegalArgumentException( |
||||
"Can't instantiate context-class " + contextClass |
||||
+ " for host " + host + " and url " |
||||
+ url, e); |
||||
} catch (IllegalAccessException e) { |
||||
throw new IllegalArgumentException( |
||||
"Can't instantiate context-class " + contextClass |
||||
+ " for host " + host + " and url " |
||||
+ url, e); |
||||
} catch (IllegalArgumentException e) { |
||||
throw new IllegalArgumentException( |
||||
"Can't instantiate context-class " + contextClass |
||||
+ " for host " + host + " and url " |
||||
+ url, e); |
||||
} catch (InvocationTargetException e) { |
||||
throw new IllegalArgumentException( |
||||
"Can't instantiate context-class " + contextClass |
||||
+ " for host " + host + " and url " |
||||
+ url, e); |
||||
} catch (NoSuchMethodException e) { |
||||
throw new IllegalArgumentException( |
||||
"Can't instantiate context-class " + contextClass |
||||
+ " for host " + host + " and url " |
||||
+ url, e); |
||||
} catch (SecurityException e) { |
||||
throw new IllegalArgumentException( |
||||
"Can't instantiate context-class " + contextClass |
||||
+ " for host " + host + " and url " |
||||
+ url, e); |
||||
} catch (ClassNotFoundException e) { |
||||
throw new IllegalArgumentException( |
||||
"Can't instantiate context-class " + contextClass |
||||
+ " for host " + host + " and url " |
||||
+ url, e); |
||||
} |
||||
} |
||||
} |
@ -1,286 +0,0 @@
|
||||
package com.fr.start.server; |
||||
|
||||
import java.awt.SystemTray; |
||||
import java.io.File; |
||||
import java.io.FileOutputStream; |
||||
import java.io.IOException; |
||||
import java.io.PrintStream; |
||||
import java.text.DateFormat; |
||||
import java.text.SimpleDateFormat; |
||||
import java.util.ArrayList; |
||||
import java.util.Calendar; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import com.fr.general.GeneralContext; |
||||
import com.fr.stable.ProductConstants; |
||||
import org.mortbay.http.SocketListener; |
||||
import org.mortbay.jetty.Server; |
||||
import org.mortbay.jetty.servlet.WebApplicationContext; |
||||
import org.mortbay.jetty.servlet.XMLConfiguration; |
||||
|
||||
import com.fr.base.Env; |
||||
import com.fr.base.FRContext; |
||||
import com.fr.dav.LocalEnv; |
||||
import com.fr.design.DesignerEnvManager; |
||||
import com.fr.general.Inter; |
||||
import com.fr.stable.StableUtils; |
||||
import com.fr.stable.project.ProjectConstants; |
||||
import com.fr.start.StartServer; |
||||
|
||||
public class JettyHost { |
||||
|
||||
private Server server; |
||||
private MultiOutputStream multiOutputStream = null; |
||||
private File outLogFile = null; |
||||
private int currentPort = -1; |
||||
|
||||
// 内置服务器一个端口下面可以有多个应用,但是content不能重名
|
||||
private Map<String, WebApplicationContext> webAppsMap = new HashMap<String, WebApplicationContext>(); |
||||
|
||||
private List<JettyServerListener> listenerList = new ArrayList<JettyServerListener>(); |
||||
|
||||
private boolean isDemoAppLoaded = false; |
||||
|
||||
public JettyHost(int port) { |
||||
this.currentPort = port; |
||||
initServer(); |
||||
|
||||
initLogFileAndOutputStream(); |
||||
|
||||
// TODO: 将HostJettyServer放到ServerTray中去
|
||||
tryStartServerTray(); |
||||
} |
||||
|
||||
private void initServer() { |
||||
// alex:不加这句话的话,jetty无法接收超过200k的参数
|
||||
System.setProperty("org.mortbay.http.HttpRequest.maxFormContentSize", "-1"); |
||||
try { |
||||
// jetty server的配置文件
|
||||
this.server = new Server("jetty.xml"); |
||||
} catch (IOException e) { |
||||
// 如果没有配置文件,那么就用默认的吧
|
||||
this.server = new Server(); |
||||
SocketListener listener = new SocketListener(); |
||||
listener.setPort(this.currentPort); |
||||
this.server.addListener(listener); |
||||
this.server.setWebApplicationConfigurationClassNames(new String[] { XMLConfiguration.class.getName() }); |
||||
} |
||||
} |
||||
|
||||
private void initLogFileAndOutputStream() { |
||||
// log文件放置的位置
|
||||
File logDir = null; |
||||
String installHome = StableUtils.getInstallHome(); |
||||
if (installHome == null) {// 没有installHome的时候,就放到user.home下面喽
|
||||
logDir = new File(ProductConstants.getEnvHome() + File.separator + ProjectConstants.LOGS_NAME); |
||||
} else { |
||||
// james:logs放在安装目录下面
|
||||
logDir = new File(installHome + File.separator + ProjectConstants.LOGS_NAME + File.separator + "jetty"); |
||||
} |
||||
StableUtils.mkdirs(logDir); |
||||
DateFormat fateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
||||
Calendar curCalendar = Calendar.getInstance(); |
||||
outLogFile = new File(logDir, "jetty_" + fateFormat.format(curCalendar.getTime()) + ".log"); |
||||
|
||||
try { |
||||
multiOutputStream = new MultiOutputStream(); |
||||
multiOutputStream.addOutputStream(new FileOutputStream(outLogFile, true)); |
||||
multiOutputStream.addOutputStream(System.out); |
||||
System.setErr(new PrintStream(multiOutputStream)); |
||||
System.setOut(new PrintStream(multiOutputStream)); |
||||
} catch (IOException ioe) { |
||||
FRContext.getLogger().error(ioe.getMessage(), ioe); |
||||
} |
||||
} |
||||
|
||||
private synchronized void addWebApplication(String context, String webappsPath) { |
||||
try { |
||||
FRContext.getLogger().info("The new Application Path is: \n" + webappsPath + ", it will be added."); |
||||
if (webAppsMap.get(context) != null) { |
||||
WebApplicationContext webapp = webAppsMap.remove(context); |
||||
try { |
||||
webapp.stop(); |
||||
webapp.destroy(); |
||||
} catch (Exception e) { |
||||
FRContext.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
WebApplicationContext webapp = this.getServer().addWebApplication(context, webappsPath); |
||||
webAppsMap.put(context, webapp); |
||||
} catch (IOException e) { |
||||
FRContext.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
|
||||
private void addAndStartWebApplication(String context, String webAppPath) { |
||||
addWebApplication(context, webAppPath); |
||||
|
||||
WebApplicationContext webapp = webAppsMap.get(context); |
||||
try { |
||||
if (!webapp.isStarted()) { |
||||
webapp.start(); |
||||
} |
||||
} catch (Exception e) { |
||||
FRContext.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Get MultiOutputStream. |
||||
*/ |
||||
public MultiOutputStream getMultiOutputStream() { |
||||
return this.multiOutputStream; |
||||
} |
||||
|
||||
/** |
||||
* Get out log file |
||||
*/ |
||||
public File getOutLogFile() { |
||||
return this.outLogFile; |
||||
} |
||||
|
||||
private Server getServer() { |
||||
if (server == null) { |
||||
initServer(); |
||||
} |
||||
|
||||
return server; |
||||
} |
||||
|
||||
/** |
||||
* Start |
||||
* |
||||
* @throws Exception |
||||
*/ |
||||
public void start() throws Exception { |
||||
getServer().start(); |
||||
|
||||
for (int i = 0; i < listenerList.size(); i++) { |
||||
JettyServerListener listener = this.getLinstener(i); |
||||
listener.started(this); |
||||
} |
||||
|
||||
FRContext.getLogger().info(Inter.getLocText("LOG-Report_Server_IS_Started")); |
||||
} |
||||
|
||||
/** |
||||
* Stop |
||||
* |
||||
* @throws Exception |
||||
*/ |
||||
public void stop() { |
||||
try { |
||||
getServer().stop(); |
||||
} catch (InterruptedException e) { |
||||
FRContext.getLogger().error(e.getMessage(), e); |
||||
} |
||||
|
||||
for (int i = 0; i < listenerList.size(); i++) { |
||||
JettyServerListener listener = this.getLinstener(i); |
||||
listener.stopped(this); |
||||
} |
||||
|
||||
getServer().destroy(); |
||||
StartServer.currentEnvChanged(); |
||||
server = null;//重置server
|
||||
} |
||||
|
||||
/** |
||||
* Is started |
||||
* |
||||
* @throws Exception |
||||
*/ |
||||
public boolean isStarted() throws Exception { |
||||
return getServer().isStarted(); |
||||
} |
||||
|
||||
public void addListener(JettyServerListener listener) { |
||||
this.listenerList.add(listener); |
||||
} |
||||
|
||||
public int getLinstenerCount() { |
||||
return this.listenerList.size(); |
||||
} |
||||
|
||||
public JettyServerListener getLinstener(int index) { |
||||
if (index < 0 || index >= this.getLinstenerCount()) { |
||||
return null; |
||||
} |
||||
|
||||
return this.listenerList.get(index); |
||||
} |
||||
|
||||
public void clearLinsteners() { |
||||
this.listenerList.clear(); |
||||
} |
||||
|
||||
/** |
||||
* 尝试启动系统托盘 |
||||
*/ |
||||
private void tryStartServerTray() { |
||||
if (SystemTray.isSupported()) { |
||||
new ServerTray(this); |
||||
} else { |
||||
FRContext.getLogger().error("Do not support the SystemTray!"); |
||||
} |
||||
} |
||||
|
||||
public void exit() { |
||||
try { |
||||
getServer().stop(); |
||||
} catch (InterruptedException e) { |
||||
FRContext.getLogger().error(e.getMessage(), e); |
||||
} |
||||
|
||||
for (int i = 0; i < listenerList.size(); i++) { |
||||
JettyServerListener listener = this.getLinstener(i); |
||||
listener.exited(this); |
||||
} |
||||
|
||||
getServer().destroy(); |
||||
StartServer.currentEnvChanged(); |
||||
|
||||
} |
||||
|
||||
public int getCurrentPort() { |
||||
return currentPort; |
||||
} |
||||
|
||||
/** |
||||
* 安装目录下的默认的WebReport,这个只执行一次,除了预览demo,其他的不要调用这个方法 |
||||
*/ |
||||
public void addAndStartInstallHomeWebApp() { |
||||
if (!isDemoAppLoaded) { |
||||
String installHome = StableUtils.getInstallHome(); |
||||
String webApplication = StableUtils.pathJoin(new String[]{installHome, ProjectConstants.WEBAPP_NAME}); |
||||
|
||||
if (new File(webApplication).isDirectory()) { |
||||
addAndStartWebApplication("/" + ProjectConstants.WEBAPP_NAME, webApplication); |
||||
} |
||||
} |
||||
isDemoAppLoaded = true; |
||||
} |
||||
|
||||
/** |
||||
* 加载Env下的报表运行环境 |
||||
*/ |
||||
public void addAndStartLocalEnvHomeWebApp() { |
||||
String name = DesignerEnvManager.getEnvManager().getCurEnvName(); |
||||
if (name.equals(Inter.getLocText("Default"))) { |
||||
isDemoAppLoaded = true; |
||||
} |
||||
Env env = FRContext.getCurrentEnv(); |
||||
if (env instanceof LocalEnv) { |
||||
String webApplication = new File(env.getPath()).getParent(); |
||||
FRContext.getLogger().info(Inter.getLocText("INFO-Reset_Webapp") + ":" + webApplication); |
||||
addAndStartWebApplication("/" + GeneralContext.getCurrentAppNameOfEnv(), webApplication); |
||||
} |
||||
} |
||||
|
||||
public boolean isDemoAppLoaded() { |
||||
return isDemoAppLoaded; |
||||
} |
||||
|
||||
} |
@ -1,18 +0,0 @@
|
||||
package com.fr.start.server; |
||||
|
||||
public interface JettyServerListener { |
||||
/** |
||||
* Started |
||||
*/ |
||||
public void started(JettyHost jettyServer); |
||||
|
||||
/** |
||||
* Stopped |
||||
*/ |
||||
public void stopped(JettyHost jettyServer); |
||||
/** |
||||
* Exited |
||||
*/ |
||||
public void exited(JettyHost jettyServer); |
||||
|
||||
} |
@ -0,0 +1,296 @@
|
||||
package com.fr.start.server; |
||||
|
||||
import java.awt.SystemTray; |
||||
import java.io.File; |
||||
import java.io.FileOutputStream; |
||||
import java.io.IOException; |
||||
import java.io.PrintStream; |
||||
import java.text.DateFormat; |
||||
import java.text.SimpleDateFormat; |
||||
import java.util.ArrayList; |
||||
import java.util.Calendar; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import com.fr.general.GeneralContext; |
||||
import com.fr.stable.ProductConstants; |
||||
import org.apache.catalina.Context; |
||||
import org.apache.catalina.LifecycleException; |
||||
import org.apache.catalina.Server; |
||||
import org.apache.catalina.core.AprLifecycleListener; |
||||
import org.apache.catalina.core.StandardServer; |
||||
import org.apache.catalina.startup.Bootstrap; |
||||
|
||||
import com.fr.base.Env; |
||||
import com.fr.base.FRContext; |
||||
import com.fr.dav.LocalEnv; |
||||
import com.fr.design.DesignerEnvManager; |
||||
import com.fr.general.Inter; |
||||
import com.fr.stable.StableUtils; |
||||
import com.fr.stable.project.ProjectConstants; |
||||
import com.fr.start.StartServer; |
||||
|
||||
public class TomcatHost { |
||||
|
||||
private static FRTomcat tomcat; |
||||
private static Bootstrap bootstrap; |
||||
private StandardServer server; |
||||
private AprLifecycleListener listener; |
||||
|
||||
// private Server server;
|
||||
private MultiOutputStream multiOutputStream = null; |
||||
private File outLogFile = null; |
||||
private int currentPort = -1; |
||||
// 内置服务器一个端口下面可以有多个应用,但是content不能重名
|
||||
private Map<String, Context> webAppsMap = new HashMap<String, Context>(); |
||||
private List<TomcatServerListener> listenerList = new ArrayList<TomcatServerListener>(); |
||||
private boolean isDemoAppLoaded = false; |
||||
|
||||
public TomcatHost(int port) { |
||||
this.currentPort = port; |
||||
initServer(); |
||||
|
||||
initLogFileAndOutputStream(); |
||||
|
||||
// TODO: 将HostTomcatServer放到ServerTray中去
|
||||
tryStartServerTray(); |
||||
} |
||||
|
||||
public static FRTomcat getTomcat() { |
||||
return tomcat; |
||||
} |
||||
|
||||
private void initServer() { |
||||
try { |
||||
//直接用自定义的,不用server.xml
|
||||
this.tomcat = new FRTomcat(); |
||||
this.tomcat.setPort(this.currentPort); |
||||
this.tomcat.setBaseDir(ProductConstants.getEnvHome()); |
||||
this.server = (StandardServer) tomcat.getServer(); |
||||
this.listener = new AprLifecycleListener(); |
||||
this.server.addLifecycleListener(listener); |
||||
this.tomcat.getHost().setAppBase(ProductConstants.getEnvHome() + File.separator + "."); |
||||
} catch (Exception e) { |
||||
//todo 最好加一个用server.xml
|
||||
FRContext.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
|
||||
private void initLogFileAndOutputStream() { |
||||
// log文件放置的位置
|
||||
File logDir = null; |
||||
String installHome = StableUtils.getInstallHome(); |
||||
if (installHome == null) {// 没有installHome的时候,就放到user.home下面喽
|
||||
logDir = new File(ProductConstants.getEnvHome() + File.separator + ProjectConstants.LOGS_NAME); |
||||
} else { |
||||
// james:logs放在安装目录下面
|
||||
logDir = new File(installHome + File.separator + ProjectConstants.LOGS_NAME + File.separator + "tomcat"); |
||||
} |
||||
StableUtils.mkdirs(logDir); |
||||
DateFormat fateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
||||
Calendar curCalendar = Calendar.getInstance(); |
||||
outLogFile = new File(logDir, "tomcat_" + fateFormat.format(curCalendar.getTime()) + ".log"); |
||||
|
||||
try { |
||||
multiOutputStream = new MultiOutputStream(); |
||||
multiOutputStream.addOutputStream(new FileOutputStream(outLogFile, true)); |
||||
multiOutputStream.addOutputStream(System.out); |
||||
System.setErr(new PrintStream(multiOutputStream)); |
||||
System.setOut(new PrintStream(multiOutputStream)); |
||||
} catch (IOException ioe) { |
||||
FRContext.getLogger().error(ioe.getMessage(), ioe); |
||||
} |
||||
} |
||||
|
||||
private synchronized void addWebApplication(String context, String webappsPath) { |
||||
FRContext.getLogger().info("The new Application Path is: \n" + webappsPath + ", it will be added."); |
||||
if (webAppsMap.get(context) != null) { |
||||
Context webapp = webAppsMap.remove(context); |
||||
try { |
||||
if (isStarted()) { |
||||
stop(); |
||||
} |
||||
} catch (Exception e) { |
||||
FRContext.getLogger().error(e.getMessage(), e); |
||||
} |
||||
|
||||
} |
||||
try { |
||||
if (!isStarted()) { |
||||
start(); |
||||
} |
||||
Context webapp = tomcat.addWebapp(context, webappsPath); |
||||
webAppsMap.put(context, webapp); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
private void addAndStartWebApplication(String context, String webAppPath) { |
||||
addWebApplication(context, webAppPath); |
||||
} |
||||
|
||||
/** |
||||
* Get MultiOutputStream. |
||||
*/ |
||||
public MultiOutputStream getMultiOutputStream() { |
||||
return this.multiOutputStream; |
||||
} |
||||
|
||||
/** |
||||
* Get out log file |
||||
*/ |
||||
public File getOutLogFile() { |
||||
return this.outLogFile; |
||||
} |
||||
|
||||
private Server getServer() { |
||||
if (server == null) { |
||||
initServer(); |
||||
} |
||||
|
||||
return server; |
||||
} |
||||
|
||||
/** |
||||
* Start |
||||
* |
||||
* @throws Exception |
||||
*/ |
||||
public void start() throws Exception { |
||||
|
||||
tomcat.start(); |
||||
// bootstrap.setCatalinaHome("/Users/momeak/Documents/Working/develop/others/tomcatsrc/WebReport");
|
||||
// bootstrap.init();
|
||||
// bootstrap.setAwait(false);
|
||||
//
|
||||
// bootstrap.start();
|
||||
|
||||
for (int i = 0; i < listenerList.size(); i++) { |
||||
TomcatServerListener listener = TomcatHost.this.getLinstener(i); |
||||
listener.started(this); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Stop |
||||
* |
||||
* @throws Exception |
||||
*/ |
||||
public void stop() throws Exception { |
||||
|
||||
tomcat.stop(); |
||||
// bootstrap.stop();
|
||||
|
||||
|
||||
for (int i = 0; i < listenerList.size(); i++) { |
||||
TomcatServerListener listener = this.getLinstener(i); |
||||
listener.stopped(this); |
||||
} |
||||
|
||||
StartServer.currentEnvChanged(); |
||||
server = null;//重置server
|
||||
} |
||||
|
||||
/** |
||||
* Is started |
||||
* |
||||
* @throws Exception |
||||
*/ |
||||
public boolean isStarted() throws Exception { |
||||
return getServer().getState().isAvailable(); |
||||
} |
||||
|
||||
public void addListener(TomcatServerListener listener) { |
||||
this.listenerList.add(listener); |
||||
} |
||||
|
||||
public int getLinstenerCount() { |
||||
return this.listenerList.size(); |
||||
} |
||||
|
||||
public TomcatServerListener getLinstener(int index) { |
||||
if (index < 0 || index >= this.getLinstenerCount()) { |
||||
return null; |
||||
} |
||||
|
||||
return this.listenerList.get(index); |
||||
} |
||||
|
||||
public void clearLinsteners() { |
||||
this.listenerList.clear(); |
||||
} |
||||
|
||||
/** |
||||
* 尝试启动系统托盘 |
||||
*/ |
||||
private void tryStartServerTray() { |
||||
if (SystemTray.isSupported()) { |
||||
new ServerTray(this); |
||||
} else { |
||||
FRContext.getLogger().error("Do not support the SystemTray!"); |
||||
} |
||||
} |
||||
|
||||
public void exit() { |
||||
try { |
||||
getServer().stop(); |
||||
} catch (LifecycleException e) { |
||||
FRContext.getLogger().error(e.getMessage(), e); |
||||
} |
||||
|
||||
for (int i = 0; i < listenerList.size(); i++) { |
||||
TomcatServerListener listener = this.getLinstener(i); |
||||
listener.exited(this); |
||||
} |
||||
|
||||
try { |
||||
getServer().destroy(); |
||||
} catch (LifecycleException e) { |
||||
FRContext.getLogger().error(e.getMessage(), e); |
||||
} |
||||
StartServer.currentEnvChanged(); |
||||
|
||||
} |
||||
|
||||
public int getCurrentPort() { |
||||
return currentPort; |
||||
} |
||||
|
||||
/** |
||||
* 安装目录下的默认的WebReport,这个只执行一次,除了预览demo,其他的不要调用这个方法 |
||||
*/ |
||||
public void addAndStartInstallHomeWebApp() { |
||||
if (!isDemoAppLoaded) { |
||||
String installHome = StableUtils.getInstallHome(); |
||||
String webApplication = StableUtils.pathJoin(new String[]{installHome, ProjectConstants.WEBAPP_NAME}); |
||||
|
||||
if (new File(webApplication).isDirectory()) { |
||||
addAndStartWebApplication("/" + ProjectConstants.WEBAPP_NAME, webApplication); |
||||
} |
||||
} |
||||
isDemoAppLoaded = true; |
||||
} |
||||
|
||||
/** |
||||
* 加载Env下的报表运行环境 |
||||
*/ |
||||
public void addAndStartLocalEnvHomeWebApp() { |
||||
String name = DesignerEnvManager.getEnvManager().getCurEnvName(); |
||||
if (name.equals(Inter.getLocText("Default"))) { |
||||
isDemoAppLoaded = true; |
||||
} |
||||
Env env = FRContext.getCurrentEnv(); |
||||
if (env instanceof LocalEnv) { |
||||
String webApplication = new File(env.getPath()).getParent(); |
||||
FRContext.getLogger().info(Inter.getLocText("INFO-Reset_Webapp") + ":" + webApplication); |
||||
addAndStartWebApplication("/" + GeneralContext.getCurrentAppNameOfEnv(), webApplication); |
||||
} |
||||
} |
||||
|
||||
public boolean isDemoAppLoaded() { |
||||
return isDemoAppLoaded; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,18 @@
|
||||
package com.fr.start.server; |
||||
|
||||
public interface TomcatServerListener { |
||||
/** |
||||
* Started |
||||
*/ |
||||
public void started(TomcatHost tomcatServer); |
||||
|
||||
/** |
||||
* Stopped |
||||
*/ |
||||
public void stopped(TomcatHost tomcatServer); |
||||
/** |
||||
* Exited |
||||
*/ |
||||
public void exited(TomcatHost tomcatServer); |
||||
|
||||
} |
Loading…
Reference in new issue