diff --git a/designer_base/src/com/fr/start/JettyFRHost.java b/designer_base/src/com/fr/start/JettyFRHost.java deleted file mode 100644 index 0a1e76744..000000000 --- a/designer_base/src/com/fr/start/JettyFRHost.java +++ /dev/null @@ -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(); } } \ No newline at end of file diff --git a/designer_base/src/com/fr/start/StartServer.java b/designer_base/src/com/fr/start/StartServer.java index 17e3c58d7..78026f504 100644 --- a/designer_base/src/com/fr/start/StartServer.java +++ b/designer_base/src/com/fr/start/StartServer.java @@ -21,7 +21,7 @@ import com.fr.stable.StableUtils; import com.fr.stable.StringUtils; import com.fr.stable.OperatingSystem; import com.fr.stable.project.ProjectConstants; -import com.fr.start.server.JettyHost; +import com.fr.start.server.TomcatHost; import javax.swing.*; import java.awt.*; @@ -31,9 +31,9 @@ import java.net.URISyntaxException; public class StartServer { public static boolean NEED_LOAD_ENV = true; - // 原先的jettyHost放在类JettyHost里面,很不方便操作,而且因为存在多个进程的原因, + // 原先的tomcatHost放在类TomcatHost里面,很不方便操作,而且因为存在多个进程的原因, // 原先的getInstance()方法无多大意义 - private static JettyHost jettyHost = null; + private static TomcatHost tomcatHost = null; static { GeneralContext.addEnvChangedListener(new EnvChangedListener() { @@ -81,19 +81,19 @@ public class StartServer { } private static void initDemoServerAndBrowser() { - if (jettyHost != null) { - if (!jettyHost.isDemoAppLoaded()) { - jettyHost.exit(); - jettyHost = new JettyHost(DesignerEnvManager.getEnvManager().getJettyServerPort()); - jettyHost.addAndStartInstallHomeWebApp(); + if (tomcatHost != null) { + if (!tomcatHost.isDemoAppLoaded()) { + tomcatHost.exit(); + tomcatHost = new TomcatHost(DesignerEnvManager.getEnvManager().getJettyServerPort()); + tomcatHost.addAndStartInstallHomeWebApp(); } } else { - jettyHost = new JettyHost(DesignerEnvManager.getEnvManager().getJettyServerPort()); - jettyHost.addAndStartInstallHomeWebApp(); + tomcatHost = new TomcatHost(DesignerEnvManager.getEnvManager().getJettyServerPort()); + tomcatHost.addAndStartInstallHomeWebApp(); } try { - if (!jettyHost.isStarted()) { - jettyHost.start(); + if (!tomcatHost.isStarted()) { + tomcatHost.start(); } } catch (Exception e) { FRContext.getLogger().errorWithServerLevel(e.getMessage()); @@ -112,20 +112,20 @@ public class StartServer { */ public static void browserURLWithLocalEnv(String url) { try { - if (jettyHost != null) { + if (tomcatHost != null) { if (NEED_LOAD_ENV) { - jettyHost.exit(); - jettyHost = new JettyHost(DesignerEnvManager.getEnvManager().getJettyServerPort()); - jettyHost.addAndStartLocalEnvHomeWebApp(); + tomcatHost.exit(); + tomcatHost = new TomcatHost(DesignerEnvManager.getEnvManager().getJettyServerPort()); + tomcatHost.addAndStartLocalEnvHomeWebApp(); } } else { - jettyHost = new JettyHost(DesignerEnvManager.getEnvManager().getJettyServerPort()); - jettyHost.addAndStartLocalEnvHomeWebApp(); + tomcatHost = new TomcatHost(DesignerEnvManager.getEnvManager().getJettyServerPort()); + tomcatHost.addAndStartLocalEnvHomeWebApp(); } - if (!jettyHost.isStarted()) { - jettyHost.start(); + if (!tomcatHost.isStarted()) { + tomcatHost.start(); } } catch (InterruptedException e) { FRContext.getLogger().errorWithServerLevel(e.getMessage()); @@ -137,9 +137,9 @@ public class StartServer { } } - public static JettyHost getInstance() { + public static TomcatHost getInstance() { // august: 正确的逻辑能保证jettyHost不为null,不然就有bug,不允许这儿加是否等于null判断 - return jettyHost; + return tomcatHost; } /** diff --git a/designer_base/src/com/fr/start/TomcatFRHost.java b/designer_base/src/com/fr/start/TomcatFRHost.java new file mode 100644 index 000000000..585564b90 --- /dev/null +++ b/designer_base/src/com/fr/start/TomcatFRHost.java @@ -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("启动成功"); + } +} diff --git a/designer_base/src/com/fr/start/server/FRTomcat.java b/designer_base/src/com/fr/start/server/FRTomcat.java new file mode 100644 index 000000000..46c529697 --- /dev/null +++ b/designer_base/src/com/fr/start/server/FRTomcat.java @@ -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 frpinnedLoggers = new HashMap(); + 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); + } + } +} diff --git a/designer_base/src/com/fr/start/server/JettyHost.java b/designer_base/src/com/fr/start/server/JettyHost.java deleted file mode 100644 index 95427d63d..000000000 --- a/designer_base/src/com/fr/start/server/JettyHost.java +++ /dev/null @@ -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 webAppsMap = new HashMap(); - - private List listenerList = new ArrayList(); - - 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; - } - -} \ No newline at end of file diff --git a/designer_base/src/com/fr/start/server/JettyServerListener.java b/designer_base/src/com/fr/start/server/JettyServerListener.java deleted file mode 100644 index fffc12a1c..000000000 --- a/designer_base/src/com/fr/start/server/JettyServerListener.java +++ /dev/null @@ -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); - -} \ No newline at end of file diff --git a/designer_base/src/com/fr/start/server/ServerManageFrame.java b/designer_base/src/com/fr/start/server/ServerManageFrame.java index 5f0f5578c..48f7ec3c3 100644 --- a/designer_base/src/com/fr/start/server/ServerManageFrame.java +++ b/designer_base/src/com/fr/start/server/ServerManageFrame.java @@ -24,16 +24,16 @@ import com.fr.design.utils.DesignUtils; import com.fr.design.utils.gui.GUICoreUtils; /** - * 内置Jetty服务器管理界面 + * 内置Tomcat服务器管理界面 */ public class ServerManageFrame extends JFrame { // 由于实际情况,只需要一个当前对象的Instance. private static ServerManageFrame serverManageFrame = null; - private JettyHost hostJettyServer; + private TomcatHost hostTomcatServer; - public static ServerManageFrame getServerManageFrame(JettyHost hostJettyServer) { + public static ServerManageFrame getServerManageFrame(TomcatHost hostTomcatServer) { if(serverManageFrame == null) { - serverManageFrame = new ServerManageFrame(hostJettyServer); + serverManageFrame = new ServerManageFrame(hostTomcatServer); } //p:每次启动之前都需要检查按钮的Enabled属性. @@ -49,8 +49,8 @@ public class ServerManageFrame extends JFrame { private JPanel startPane; private JPanel stopPane; - private ServerManageFrame(JettyHost hostJettyServer) { - this.hostJettyServer = hostJettyServer; + private ServerManageFrame(TomcatHost hostTomcatServer) { + this.hostTomcatServer = hostTomcatServer; DesignUtils.initLookAndFeel(); this.setIconImage(BaseUtils.readImage("/com/fr/base/images/oem/trayStarted.png")); @@ -82,11 +82,11 @@ public class ServerManageFrame extends JFrame { startPane.add(new UILabel(Inter.getLocText("Server-Start"))); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - JettyHost jettyServer = StartServer.getInstance(); + TomcatHost tomcatServer = StartServer.getInstance(); try { - if(!jettyServer.isStarted()) { - jettyServer.start(); - jettyServer.addAndStartLocalEnvHomeWebApp(); + if(!tomcatServer.isStarted()) { + tomcatServer.start(); + tomcatServer.addAndStartLocalEnvHomeWebApp(); } checkButtonEnabled(); } catch(Exception exp) { @@ -103,10 +103,10 @@ public class ServerManageFrame extends JFrame { stopPane.add(new UILabel(Inter.getLocText("Server-Stop"))); stopButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - JettyHost jettyServer = StartServer.getInstance(); + TomcatHost tomcatServer = StartServer.getInstance(); try { - if(jettyServer.isStarted()) { - jettyServer.stop(); + if(tomcatServer.isStarted()) { + tomcatServer.stop(); } checkButtonEnabled(); } catch(Exception exp) { @@ -126,7 +126,7 @@ public class ServerManageFrame extends JFrame { logPathTextField.setEditable(false); // logfile - logPathTextField.setText(hostJettyServer.getOutLogFile().getPath()); + logPathTextField.setText(hostTomcatServer.getOutLogFile().getPath()); UIButton openButton = new UIButton(); infoPane.add(openButton, BorderLayout.EAST); @@ -137,7 +137,7 @@ public class ServerManageFrame extends JFrame { Desktop desktop = Desktop.getDesktop(); try { - desktop.open(ServerManageFrame.this.hostJettyServer.getOutLogFile()); + desktop.open(ServerManageFrame.this.hostTomcatServer.getOutLogFile()); } catch(Exception exp) { FRContext.getLogger().error(exp.getMessage()); } @@ -154,8 +154,8 @@ public class ServerManageFrame extends JFrame { * @throws Exception 异常 */ public void checkButtonEnabled() throws Exception { - JettyHost jettyServer = StartServer.getInstance(); - if(jettyServer.isStarted()) { + TomcatHost tomcatServer = StartServer.getInstance(); + if(tomcatServer.isStarted()) { GUICoreUtils.setEnabled(startPane, false); GUICoreUtils.setEnabled(stopPane, true); } else { diff --git a/designer_base/src/com/fr/start/server/ServerTray.java b/designer_base/src/com/fr/start/server/ServerTray.java index e04c79495..d7029085a 100644 --- a/designer_base/src/com/fr/start/server/ServerTray.java +++ b/designer_base/src/com/fr/start/server/ServerTray.java @@ -32,19 +32,19 @@ public class ServerTray { private TrayIcon trayIcon; - private JettyHost hostJettyServer; + private TomcatHost hostTomcatServer; - public ServerTray(JettyHost hostJettyServer) { + public ServerTray(TomcatHost hostTomcatServer) { - this.hostJettyServer = hostJettyServer; + this.hostTomcatServer = hostTomcatServer; //p:首先构建右键菜单 PopupMenu popup = new PopupMenu(); manangeMenu = new MenuItem(Inter.getLocText("Server-Open_Service_Manager")); manangeMenu.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - serverManageFrame = ServerManageFrame.getServerManageFrame(ServerTray.this.hostJettyServer); + serverManageFrame = ServerManageFrame.getServerManageFrame(ServerTray.this.hostTomcatServer); if(!serverManageFrame.isVisible()) { serverManageFrame.setVisible(true); } @@ -57,11 +57,11 @@ public class ServerTray { //创建打开监听器 ActionListener startListener = new ActionListener() { public void actionPerformed(ActionEvent e) { - JettyHost jettyServer = StartServer.getInstance(); + TomcatHost tomcatServer = StartServer.getInstance(); try { - if(!jettyServer.isStarted()) { - jettyServer.start(); - jettyServer.addAndStartLocalEnvHomeWebApp();//暂停后再打开jetty,需要addApp + if(!tomcatServer.isStarted()) { + tomcatServer.start(); + tomcatServer.addAndStartLocalEnvHomeWebApp();//暂停后再打开Tomcat,需要addApp } } catch(Exception exp) { FRContext.getLogger().error(exp.getMessage(), exp); @@ -70,10 +70,10 @@ public class ServerTray { }; ActionListener stopListener = new ActionListener() { public void actionPerformed(ActionEvent e) { - JettyHost jettyServer = StartServer.getInstance(); + TomcatHost tomcatServer = StartServer.getInstance(); try { - if(jettyServer.isStarted()) { - jettyServer.stop(); + if(tomcatServer.isStarted()) { + tomcatServer.stop(); } } catch(Exception exp) { FRContext.getLogger().error(exp.getMessage(), exp); @@ -106,7 +106,7 @@ public class ServerTray { return; } - ServerManageFrame serverManageFrame = ServerManageFrame.getServerManageFrame(ServerTray.this.hostJettyServer); + ServerManageFrame serverManageFrame = ServerManageFrame.getServerManageFrame(ServerTray.this.hostTomcatServer); if(!serverManageFrame.isVisible()) { serverManageFrame.setVisible(true); } @@ -126,13 +126,13 @@ public class ServerTray { } //p:先check - checkPopupMenuItemEnabled(this.hostJettyServer); + checkPopupMenuItemEnabled(this.hostTomcatServer); // TODOJ - this.hostJettyServer.addListener(new MyJettyListner()); + this.hostTomcatServer.addListener(new MyTomcatListner()); try { - if (!this.hostJettyServer.isStarted()) { - this.hostJettyServer.start(); + if (!this.hostTomcatServer.isStarted()) { + this.hostTomcatServer.start(); } } catch (Exception e){ FRContext.getLogger().error(e.getMessage(), e); @@ -140,16 +140,16 @@ public class ServerTray { } private void exit() { - if (hostJettyServer != null) { + if (hostTomcatServer != null) { try { - if(hostJettyServer.isStarted()) { - hostJettyServer.exit(); + if(hostTomcatServer.isStarted()) { + hostTomcatServer.exit(); } } catch(Exception exp) { FRContext.getLogger().error(exp.getMessage(), exp); } - hostJettyServer = null; + hostTomcatServer = null; } @@ -160,30 +160,30 @@ public class ServerTray { } } - class MyJettyListner implements JettyServerListener { + class MyTomcatListner implements TomcatServerListener { /** * Started */ - public void started(JettyHost jettyServer) { - checkPopupMenuItemEnabled(jettyServer); + public void started(TomcatHost tomcatServer) { + checkPopupMenuItemEnabled(tomcatServer); } /** * Stopped */ - public void stopped(JettyHost jettyServer) { - checkPopupMenuItemEnabled(jettyServer); + public void stopped(TomcatHost tomcatServer) { + checkPopupMenuItemEnabled(tomcatServer); } @Override - public void exited(JettyHost jettyServer) { + public void exited(TomcatHost tomcatServer) { exit(); } } - private void checkPopupMenuItemEnabled(JettyHost jettyServer) { + private void checkPopupMenuItemEnabled(TomcatHost tomcatServer) { try { - if(jettyServer.isStarted()) { + if(tomcatServer.isStarted()) { startMenu.setEnabled(false); stopMenu.setEnabled(true); diff --git a/designer_base/src/com/fr/start/server/TomcatHost.java b/designer_base/src/com/fr/start/server/TomcatHost.java new file mode 100644 index 000000000..533a5f0d2 --- /dev/null +++ b/designer_base/src/com/fr/start/server/TomcatHost.java @@ -0,0 +1,295 @@ +package com.fr.start.server; + +import com.fr.module.ModuleContext; +import java.awt.SystemTray; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.lang.reflect.Field; +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 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 StandardServer server; + private AprLifecycleListener listener; + +// private Server server; + private MultiOutputStream multiOutputStream = null; + private File outLogFile = null; + private int currentPort = -1; + // 内置服务器一个端口下面可以有多个应用,但是content不能重名 + private Map webAppsMap = new HashMap(); + private List listenerList = new ArrayList(); + 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(StableUtils.getInstallHome()); + this.server = (StandardServer) tomcat.getServer(); + this.listener = new AprLifecycleListener(); + this.server.addLifecycleListener(listener); + this.tomcat.getHost().setAppBase(StableUtils.getInstallHome() + 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()) { + start(); + } + Context webapp = tomcat.addWebapp(context, webappsPath); + webAppsMap.put(context, webapp); + } catch (Exception e) { + FRContext.getLogger().error(e.getMessage(), e); + } + } + + 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; + } + + //MoMeak:调试用,等ju那边联调好了删 + private void setRootNull(){ + Class clazz = ModuleContext.class; + try { + Field field = clazz.getDeclaredField("root"); + field.setAccessible(true); + field.set(null,null); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + + /** + * Start + * + * @throws Exception + */ + public void start() throws Exception { + //MoMeak:调试用 + setRootNull(); + tomcat.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(); + + 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; + } + +} \ No newline at end of file diff --git a/designer_base/src/com/fr/start/server/TomcatServerListener.java b/designer_base/src/com/fr/start/server/TomcatServerListener.java new file mode 100644 index 000000000..30702d340 --- /dev/null +++ b/designer_base/src/com/fr/start/server/TomcatServerListener.java @@ -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); + +} \ No newline at end of file