Browse Source

CORE-76 Activator重复启动内置服务器、切换环境的支持

切换环境重启部分activator
master
ju 7 years ago
parent
commit
69eaddf7e5
  1. 9
      designer-base/src/com/fr/start/ServerStarter.java
  2. 123
      designer-base/src/com/fr/start/server/FineEmbedServer.java
  3. 107
      designer-base/src/com/fr/start/server/FineEmbedServerActivator.java
  4. 7
      designer-base/src/com/fr/start/server/ServerManageFrame.java
  5. 257
      designer-base/src/com/fr/start/server/ServerTray.java
  6. 6
      designer-realize/src/com/fr/start/Designer.java
  7. 2
      designer-realize/src/com/fr/start/module/DesignerModuleActivator.java
  8. 35
      designer-realize/src/com/fr/start/module/DesignerStartup.java
  9. 29
      designer-realize/src/com/fr/start/module/EnvBasedModule.java

9
designer-base/src/com/fr/start/ServerStarter.java

@ -72,7 +72,7 @@ public class ServerStarter {
private static void initDemoServerAndBrowser() { private static void initDemoServerAndBrowser() {
try { try {
FineEmbedServer.getInstance().start(); FineEmbedServer.start();
} finally { } finally {
//先访问Demo, 后访问报表, 不需要重置服务器. //先访问Demo, 后访问报表, 不需要重置服务器.
browser("http://localhost:" + DesignerEnvManager.getEnvManager().getEmbedServerPort() + "/" + GeneralContext.getCurrentAppNameOfEnv() + "/" + ServerConfig.getInstance().getServletName()); browser("http://localhost:" + DesignerEnvManager.getEnvManager().getEmbedServerPort() + "/" + GeneralContext.getCurrentAppNameOfEnv() + "/" + ServerConfig.getInstance().getServletName());
@ -86,7 +86,7 @@ public class ServerStarter {
*/ */
public static void browserURLWithLocalEnv(String url) { public static void browserURLWithLocalEnv(String url) {
FineEmbedServer.getInstance().start(); FineEmbedServer.start();
browser(url); browser(url);
} }
@ -124,11 +124,6 @@ public class ServerStarter {
} }
} }
public static boolean isStarted() {
return FineEmbedServer.getInstance().isRunning();
}
private static class InformationPane extends BasicPane { private static class InformationPane extends BasicPane {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

123
designer-base/src/com/fr/start/server/FineEmbedServer.java

@ -1,134 +1,29 @@
package com.fr.start.server; package com.fr.start.server;
import com.fr.base.FRContext;
import com.fr.design.DesignerEnvManager;
import com.fr.event.EventDispatcher; import com.fr.event.EventDispatcher;
import com.fr.log.FineLoggerFactory; import com.fr.module.ModuleContext;
import com.fr.module.ModuleRole;
import com.fr.startup.FineWebApplicationInitializer;
import com.fr.third.springframework.web.SpringServletContainerInitializer;
import com.fr.third.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.loader.VirtualWebappLoader;
import org.apache.catalina.startup.Tomcat;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
/** /**
* Created by juhaoyu on 2018/6/5. * Created by juhaoyu on 2018/6/6.
*/ */
public class FineEmbedServer { public abstract class FineEmbedServer {
private static final FineEmbedServer INSTANCE = new FineEmbedServer();
private Tomcat tomcat;
private volatile boolean isRunning = false;
public static FineEmbedServer getInstance() {
return INSTANCE;
}
private FineEmbedServer() {}
public synchronized static void start() {
public synchronized void start() {
if (isRunning) {
return;
}
EventDispatcher.fire(EmbedServerEvent.BeforeStart); EventDispatcher.fire(EmbedServerEvent.BeforeStart);
try { ModuleContext.getModule(FineEmbedServerActivator.class).start();
//初始化tomcat
initTomcat();
tomcat.start();
} catch (LifecycleException e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
isRunning = true;
EventDispatcher.fire(EmbedServerEvent.AfterStart); EventDispatcher.fire(EmbedServerEvent.AfterStart);
} }
public synchronized void stop() { public synchronized static void stop() {
if (!isRunning) {
return;
}
EventDispatcher.fire(EmbedServerEvent.BeforeStop); EventDispatcher.fire(EmbedServerEvent.BeforeStop);
try { ModuleContext.getModule(FineEmbedServerActivator.class).stop();
stopSpring();
stopServerActivator();
stopTomcat();
} catch (LifecycleException e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
isRunning = false;
EventDispatcher.fire(EmbedServerEvent.AfterStop); EventDispatcher.fire(EmbedServerEvent.AfterStop);
} }
public boolean isRunning() { public static boolean isRunning() {
return isRunning;
}
private void initTomcat() {
tomcat = new Tomcat();
tomcat.setPort(DesignerEnvManager.getEnvManager().getEmbedServerPort()); return ModuleContext.getModule(FineEmbedServerActivator.class).isRunning();
String docBase = new File(FRContext.getCurrentEnv().getPath()).getParent();
String appName = "/" + FRContext.getCurrentEnv().getAppName();
Context context = tomcat.addContext(appName, docBase);
tomcat.addServlet(appName, "default", "org.apache.catalina.servlets.DefaultServlet");
//覆盖tomcat的WebAppClassLoader
context.setLoader(new FRTomcatLoader());
//直接指定initializer,tomcat就不用再扫描一遍了
SpringServletContainerInitializer initializer = new SpringServletContainerInitializer();
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(FineWebApplicationInitializer.class);
context.addServletContainerInitializer(initializer, classes);
} }
private void stopServerActivator() {
ModuleRole.ServerRoot.stop();
}
private void stopSpring() {
AnnotationConfigWebApplicationContext context = ModuleRole.ServerRoot.getSingleton(AnnotationConfigWebApplicationContext.class);
if (context != null) {
context.stop();
context.destroy();
}
}
private void stopTomcat() throws LifecycleException {
tomcat.stop();
tomcat.destroy();
}
/**
* Created by juhaoyu on 2018/6/5.
* 自定义的tomcat loader主要用于防止内置服务器再加载一遍class
*/
private static class FRTomcatLoader extends VirtualWebappLoader {
@Override
public ClassLoader getClassLoader() {
return this.getClass().getClassLoader();
}
}
} }

107
designer-base/src/com/fr/start/server/FineEmbedServerActivator.java

@ -0,0 +1,107 @@
package com.fr.start.server;
import com.fr.base.FRContext;
import com.fr.design.DesignerEnvManager;
import com.fr.event.EventDispatcher;
import com.fr.log.FineLoggerFactory;
import com.fr.module.Activator;
import com.fr.module.ModuleRole;
import com.fr.startup.FineWebApplicationInitializer;
import com.fr.third.springframework.web.SpringServletContainerInitializer;
import com.fr.third.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.loader.VirtualWebappLoader;
import org.apache.catalina.startup.Tomcat;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
/**
* Created by juhaoyu on 2018/6/5.
*/
public class FineEmbedServerActivator extends Activator {
private Tomcat tomcat;
@Override
public synchronized void start() {
try {
//初始化tomcat
initTomcat();
tomcat.start();
} catch (LifecycleException e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
}
@Override
public synchronized void stop() {
try {
stopSpring();
stopServerActivator();
stopTomcat();
} catch (LifecycleException e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
}
private void initTomcat() {
tomcat = new Tomcat();
tomcat.setPort(DesignerEnvManager.getEnvManager().getEmbedServerPort());
String docBase = new File(FRContext.getCurrentEnv().getPath()).getParent();
String appName = "/" + FRContext.getCurrentEnv().getAppName();
Context context = tomcat.addContext(appName, docBase);
tomcat.addServlet(appName, "default", "org.apache.catalina.servlets.DefaultServlet");
//覆盖tomcat的WebAppClassLoader
context.setLoader(new FRTomcatLoader());
//直接指定initializer,tomcat就不用再扫描一遍了
SpringServletContainerInitializer initializer = new SpringServletContainerInitializer();
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(FineWebApplicationInitializer.class);
context.addServletContainerInitializer(initializer, classes);
}
private void stopServerActivator() {
ModuleRole.ServerRoot.stop();
}
private void stopSpring() {
AnnotationConfigWebApplicationContext context = ModuleRole.ServerRoot.getSingleton(AnnotationConfigWebApplicationContext.class);
if (context != null) {
context.stop();
context.destroy();
}
}
private void stopTomcat() throws LifecycleException {
tomcat.stop();
tomcat.destroy();
}
/**
* Created by juhaoyu on 2018/6/5.
* 自定义的tomcat loader主要用于防止内置服务器再加载一遍class
*/
private static class FRTomcatLoader extends VirtualWebappLoader {
@Override
public ClassLoader getClassLoader() {
return this.getClass().getClassLoader();
}
}
}

7
designer-base/src/com/fr/start/server/ServerManageFrame.java

@ -9,7 +9,6 @@ import com.fr.design.layout.FRGUIPaneFactory;
import com.fr.design.utils.DesignUtils; import com.fr.design.utils.DesignUtils;
import com.fr.design.utils.gui.GUICoreUtils; import com.fr.design.utils.gui.GUICoreUtils;
import com.fr.general.Inter; import com.fr.general.Inter;
import com.fr.start.ServerStarter;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -75,7 +74,7 @@ public class ServerManageFrame extends JFrame {
startButton.addActionListener(new ActionListener() { startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try { try {
FineEmbedServer.getInstance().start(); FineEmbedServer.start();
checkButtonEnabled(); checkButtonEnabled();
} catch(Exception exp) { } catch(Exception exp) {
FRContext.getLogger().error(exp.getMessage()); FRContext.getLogger().error(exp.getMessage());
@ -92,7 +91,7 @@ public class ServerManageFrame extends JFrame {
stopButton.addActionListener(new ActionListener() { stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try { try {
FineEmbedServer.getInstance().stop(); FineEmbedServer.stop();
checkButtonEnabled(); checkButtonEnabled();
} catch(Exception exp) { } catch(Exception exp) {
FRContext.getLogger().error(exp.getMessage()); FRContext.getLogger().error(exp.getMessage());
@ -137,7 +136,7 @@ public class ServerManageFrame extends JFrame {
*/ */
public void checkButtonEnabled() throws Exception { public void checkButtonEnabled() throws Exception {
if (ServerStarter.isStarted()) { if (FineEmbedServer.isRunning()) {
GUICoreUtils.setEnabled(startPane, false); GUICoreUtils.setEnabled(startPane, false);
GUICoreUtils.setEnabled(stopPane, true); GUICoreUtils.setEnabled(stopPane, true);
} else { } else {

257
designer-base/src/com/fr/start/server/ServerTray.java

@ -17,20 +17,20 @@ import java.awt.event.MouseEvent;
* Create server tray. * Create server tray.
*/ */
public class ServerTray { public class ServerTray {
private static ServerTray INSTANCE; private static ServerTray INSTANCE;
private MenuItem startMenu; private MenuItem startMenu;
private MenuItem stopMenu; private MenuItem stopMenu;
private Image trayStartedImage = BaseUtils.readImage("/com/fr/base/images/oem/trayStarted.png"); private Image trayStartedImage = BaseUtils.readImage("/com/fr/base/images/oem/trayStarted.png");
private Image trayStoppedImage = BaseUtils.readImage("/com/fr/base/images/oem/trayStopped.png"); private Image trayStoppedImage = BaseUtils.readImage("/com/fr/base/images/oem/trayStopped.png");
private ServerManageFrame serverManageFrame; private ServerManageFrame serverManageFrame;
private TrayIcon trayIcon; private TrayIcon trayIcon;
private ServerTray() { private ServerTray() {
@ -38,89 +38,95 @@ public class ServerTray {
listen(); listen();
//p:首先构建右键菜单 //p:首先构建右键菜单
PopupMenu popup = new PopupMenu(); PopupMenu popup = new PopupMenu();
MenuItem manangeMenu = new MenuItem(Inter.getLocText("Server-Open_Service_Manager")); MenuItem manangeMenu = new MenuItem(Inter.getLocText("Server-Open_Service_Manager"));
manangeMenu.addActionListener(new ActionListener() { manangeMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
serverManageFrame = ServerManageFrame.getServerManageFrame(); serverManageFrame = ServerManageFrame.getServerManageFrame();
if (!serverManageFrame.isVisible()) { if (!serverManageFrame.isVisible()) {
serverManageFrame.setVisible(true); serverManageFrame.setVisible(true);
} }
serverManageFrame.toFront();//p:到第一个. serverManageFrame.toFront();//p:到第一个.
} }
}); });
startMenu = new MenuItem(Inter.getLocText("FR-Server_Embedded_Server_Start")); startMenu = new MenuItem(Inter.getLocText("FR-Server_Embedded_Server_Start"));
stopMenu = new MenuItem(Inter.getLocText("FR-Server_Embedded_Server_Stop")); stopMenu = new MenuItem(Inter.getLocText("FR-Server_Embedded_Server_Stop"));
MenuItem exitMenu = new MenuItem(Inter.getLocText("Exit")); MenuItem exitMenu = new MenuItem(Inter.getLocText("Exit"));
//创建打开监听器 //创建打开监听器
ActionListener startListener = new ActionListener() { ActionListener startListener = new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try { try {
FineEmbedServer.getInstance().start(); FineEmbedServer.start();
} catch (Exception exp) { } catch (Exception exp) {
FRContext.getLogger().error(exp.getMessage(), exp); FRContext.getLogger().error(exp.getMessage(), exp);
} }
} }
}; };
ActionListener stopListener = new ActionListener() { ActionListener stopListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try { public void actionPerformed(ActionEvent e) {
FineEmbedServer.getInstance().stop();
} catch (Throwable exp) { try {
FRContext.getLogger().error(exp.getMessage(), exp); FineEmbedServer.stop();
} } catch (Throwable exp) {
} FRContext.getLogger().error(exp.getMessage(), exp);
}; }
startMenu.addActionListener(startListener); }
stopMenu.addActionListener(stopListener); };
//创建退出菜单监听器 startMenu.addActionListener(startListener);
ActionListener exitListener = new ActionListener() { stopMenu.addActionListener(stopListener);
public void actionPerformed(ActionEvent e) { //创建退出菜单监听器
exit(); ActionListener exitListener = new ActionListener() {
}
}; public void actionPerformed(ActionEvent e) {
exitMenu.addActionListener(exitListener); exit();
popup.add(manangeMenu); }
popup.addSeparator(); };
popup.add(startMenu);
popup.add(stopMenu); exitMenu.addActionListener(exitListener);
popup.addSeparator(); popup.add(manangeMenu);
popup.add(exitMenu); popup.addSeparator();
popup.add(startMenu);
//p:开始创建托盘. popup.add(stopMenu);
trayIcon = new TrayIcon(trayStartedImage, Inter.getLocText("Server-Embedded_Server"), popup); popup.addSeparator();
trayIcon.setImageAutoSize(true); popup.add(exitMenu);
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) { //p:开始创建托盘.
if(e.getClickCount() < 2) { trayIcon = new TrayIcon(trayStartedImage, Inter.getLocText("Server-Embedded_Server"), popup);
return; trayIcon.setImageAutoSize(true);
} trayIcon.addMouseListener(new MouseAdapter() {
ServerManageFrame serverManageFrame = ServerManageFrame.getServerManageFrame(); public void mouseClicked(MouseEvent e) {
if (!serverManageFrame.isVisible()) {
serverManageFrame.setVisible(true); if (e.getClickCount() < 2) {
} return;
serverManageFrame.toFront();//p:到第一个. }
}
}); ServerManageFrame serverManageFrame = ServerManageFrame.getServerManageFrame();
if (!serverManageFrame.isVisible()) {
TrayIcon[] icons = SystemTray.getSystemTray().getTrayIcons(); serverManageFrame.setVisible(true);
for (TrayIcon icon : icons) { }
SystemTray.getSystemTray().remove(icon); serverManageFrame.toFront();//p:到第一个.
} }
});
try {
SystemTray.getSystemTray().add(trayIcon); TrayIcon[] icons = SystemTray.getSystemTray().getTrayIcons();
} catch (AWTException e) { for (TrayIcon icon : icons) {
System.err.println("Can not create the System Tray:" + e); SystemTray.getSystemTray().remove(icon);
} }
checkPopupMenuItemEnabled(); try {
} SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException e) {
System.err.println("Can not create the System Tray:" + e);
}
checkPopupMenuItemEnabled();
}
private void listen() { private void listen() {
@ -137,37 +143,38 @@ public class ServerTray {
} }
private void exit() { private void exit() {
FineEmbedServer.getInstance().stop(); FineEmbedServer.stop();
SystemTray.getSystemTray().remove(trayIcon); SystemTray.getSystemTray().remove(trayIcon);
} }
private void checkPopupMenuItemEnabled() { private void checkPopupMenuItemEnabled() {
try {
if (FineEmbedServer.getInstance().isRunning()) { try {
startMenu.setEnabled(false); if (FineEmbedServer.isRunning()) {
stopMenu.setEnabled(true); startMenu.setEnabled(false);
stopMenu.setEnabled(true);
trayIcon.setImage(trayStartedImage);
} else { trayIcon.setImage(trayStartedImage);
startMenu.setEnabled(true); } else {
stopMenu.setEnabled(false); startMenu.setEnabled(true);
stopMenu.setEnabled(false);
trayIcon.setImage(trayStoppedImage);
} trayIcon.setImage(trayStoppedImage);
}
if (serverManageFrame != null) {
serverManageFrame.checkButtonEnabled(); if (serverManageFrame != null) {
serverManageFrame.repaint(); serverManageFrame.checkButtonEnabled();
} serverManageFrame.repaint();
} catch(Exception exp) { }
} catch (Exception exp) {
FRContext.getLogger().error(exp.getMessage(), exp); FRContext.getLogger().error(exp.getMessage(), exp);
} }
} }
public static void init() {
INSTANCE = new ServerTray();
public static void init() {
INSTANCE = new ServerTray();
} }
} }

6
designer-realize/src/com/fr/start/Designer.java

@ -39,13 +39,10 @@ import com.fr.design.module.DesignModuleFactory;
import com.fr.design.module.DesignerModule; import com.fr.design.module.DesignerModule;
import com.fr.design.utils.concurrent.ThreadFactoryBuilder; import com.fr.design.utils.concurrent.ThreadFactoryBuilder;
import com.fr.design.utils.gui.GUICoreUtils; import com.fr.design.utils.gui.GUICoreUtils;
import com.fr.event.EventDispatcher;
import com.fr.general.ComparatorUtils; import com.fr.general.ComparatorUtils;
import com.fr.general.Inter; import com.fr.general.Inter;
import com.fr.locale.InterProviderFactory;
import com.fr.module.Module; import com.fr.module.Module;
import com.fr.module.ModuleContext; import com.fr.module.ModuleContext;
import com.fr.module.ModuleEvent;
import com.fr.stable.BuildContext; import com.fr.stable.BuildContext;
import com.fr.stable.OperatingSystem; import com.fr.stable.OperatingSystem;
import com.fr.stable.ProductConstants; import com.fr.stable.ProductConstants;
@ -99,8 +96,7 @@ public class Designer extends BaseDesigner {
designerRoot.setSingleton(StartupArgs.class, new StartupArgs(args)); designerRoot.setSingleton(StartupArgs.class, new StartupArgs(args));
designerRoot.start(); designerRoot.start();
if (FRContext.getCurrentEnv() instanceof LocalEnv) { if (FRContext.getCurrentEnv() instanceof LocalEnv) {
// 预启动一下 //初始化一下serverTray
FineEmbedServer.getInstance().start();
ServerTray.init(); ServerTray.init();
} }

2
designer-realize/src/com/fr/start/module/DesignerModuleActivator.java

@ -23,7 +23,7 @@ public class DesignerModuleActivator extends Activator implements Prepare {
@Override @Override
public void stop() { public void stop() {
ModuleContext.stopModule(DesignerModule.class.getName());
} }
@Override @Override

35
designer-realize/src/com/fr/start/module/DesignerStartup.java

@ -1,7 +1,10 @@
package com.fr.start.module; package com.fr.start.module;
import com.fr.core.env.EnvConfig;
import com.fr.core.env.EnvEvent;
import com.fr.event.Event;
import com.fr.event.Listener;
import com.fr.module.Activator; import com.fr.module.Activator;
import com.fr.stable.CoreActivator;
import com.fr.start.Designer; import com.fr.start.Designer;
import com.fr.start.EnvSwitcher; import com.fr.start.EnvSwitcher;
import com.fr.start.SplashContext; import com.fr.start.SplashContext;
@ -21,10 +24,9 @@ public class DesignerStartup extends Activator {
Designer designer = new Designer(args); Designer designer = new Designer(args);
//启动env //启动env
startSub(DesignerEnvProvider.class); startSub(DesignerEnvProvider.class);
//启动各个模块 getSub(EnvBasedModule.class).start();
getSub(CoreActivator.class).start();
getSub("designer").start();
getRoot().getSingleton(EnvSwitcher.class).switch2LastEnv(); getRoot().getSingleton(EnvSwitcher.class).switch2LastEnv();
registerEnvListener();
//启动设计器界面 //启动设计器界面
designer.show(args); designer.show(args);
//启动画面结束 //启动画面结束
@ -32,7 +34,30 @@ public class DesignerStartup extends Activator {
startSub(StartFinishActivator.class); startSub(StartFinishActivator.class);
} }
/**
* 切换环境时重新启动所有相关模块
*/
private void registerEnvListener() {
listenEvent(EnvEvent.BEFORE_SIGN_OUT, new Listener<EnvConfig>() {
@Override
public void on(Event event, EnvConfig param) {
getSub(EnvBasedModule.class).stop();
}
});
listenEvent(EnvEvent.AFTER_SIGN_IN, new Listener<EnvConfig>() {
@Override
public void on(Event event, EnvConfig param) {
getSub(EnvBasedModule.class).start();
}
});
}
@Override @Override
public void stop() { public void stop() {

29
designer-realize/src/com/fr/start/module/EnvBasedModule.java

@ -0,0 +1,29 @@
package com.fr.start.module;
import com.fr.module.Activator;
import com.fr.stable.CoreActivator;
import com.fr.start.server.FineEmbedServerActivator;
/**
* Created by juhaoyu on 2018/6/6.
* 基于env的模块启动关闭
*/
public class EnvBasedModule extends Activator {
@Override
public void start() {
//core和设计器启动
getSub(CoreActivator.class).start();
getSub("designer").start();
//这里不启动tomcat,由客户手动触发
}
@Override
public void stop() {
//先关闭tomcat(如果已经启动了的话)
getSub(FineEmbedServerActivator.class).stop();
//倒叙关闭其他模块
getSub("designer").stop();
getSub(CoreActivator.class).stop();
}
}
Loading…
Cancel
Save