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. 15
      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. 33
      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());
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(); return ModuleContext.getModule(FineEmbedServerActivator.class).isRunning();
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 {

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

@ -59,16 +59,18 @@ public class ServerTray {
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) { public void actionPerformed(ActionEvent e) {
try { try {
FineEmbedServer.getInstance().stop(); FineEmbedServer.stop();
} catch (Throwable exp) { } catch (Throwable exp) {
FRContext.getLogger().error(exp.getMessage(), exp); FRContext.getLogger().error(exp.getMessage(), exp);
} }
@ -78,7 +80,9 @@ public class ServerTray {
stopMenu.addActionListener(stopListener); stopMenu.addActionListener(stopListener);
//创建退出菜单监听器 //创建退出菜单监听器
ActionListener exitListener = new ActionListener() { ActionListener exitListener = new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
exit(); exit();
} }
}; };
@ -95,7 +99,9 @@ public class ServerTray {
trayIcon = new TrayIcon(trayStartedImage, Inter.getLocText("Server-Embedded_Server"), popup); trayIcon = new TrayIcon(trayStartedImage, Inter.getLocText("Server-Embedded_Server"), popup);
trayIcon.setImageAutoSize(true); trayIcon.setImageAutoSize(true);
trayIcon.addMouseListener(new MouseAdapter() { trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
if (e.getClickCount() < 2) { if (e.getClickCount() < 2) {
return; return;
} }
@ -138,13 +144,14 @@ 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 { try {
if (FineEmbedServer.getInstance().isRunning()) { if (FineEmbedServer.isRunning()) {
startMenu.setEnabled(false); startMenu.setEnabled(false);
stopMenu.setEnabled(true); stopMenu.setEnabled(true);

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

33
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,6 +34,29 @@ 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