public class MainModuleSetup implements Setup {
private static final Logger logger = Logger.getLogger(MainModuleSetup.class);
private List<SetupListener> setupListeners = new ArrayList<SetupListener>();
private NutConfig config;
@Override
public void init(NutConfig config) {
this.config = config;
ServletContext ctx = config.getServletContext();
//设置工作目录系统参数
//为确保在所有SetupListener之前执行,没有通过SetupListener实现类实现
System.setProperty("context.path", ctx.getContextPath());
String webdir = new File(config.getAppRoot()).toPath().normalize().toString(); //未使用getCanonicalPath,避免需要捕获异常
System.setProperty("web.dir", webdir);//当前应用的根路径
System.setProperty("webinf.dir", webdir + File.separator + "WEB-INF");
System.setProperty("conf.dir", webdir + File.separator + "WEB-INF" + File.separator + "classes" + File.separator+ "config");
System.setProperty("server.ip", LocalAddressUtil.getIp());
System.setProperty("server.hostname", LocalAddressUtil.getHostName());
System.setProperty("server.starttime", String.valueOf(System.currentTimeMillis()));
//启动headless模式,避免Could not initialize class sun.awt.X11GraphicsEnvironment异常问题
System.setProperty("java.awt.headless", "true");
//为确保DaoUtil在所有的伴随启动初始化类之前初始化,避免某些$startup_由于名称原因在DaoUtil之前初始化,从而造成调出出错
DaoUtil.me().setIoc(config.getIoc());
List<SetupListenerSortWrapper> listeners = new ArrayList<SetupListenerSortWrapper>();
Map<String, LinkedHashSet<SetupListenerSortWrapper>> befores = new HashMap<String, LinkedHashSet<SetupListenerSortWrapper>>();
Map<String, LinkedHashSet<SetupListenerSortWrapper>> afters = new HashMap<String, LinkedHashSet<SetupListenerSortWrapper>>();
String[] names = config.getIoc().getNames();
for (String name : names) {
try {
if (name != null && name.trim().toLowerCase().startsWith("$setup_")) {
SetupListener listener = config.getIoc().get(SetupListener.class, name);
if (listener.before() != null && listener.before().trim().length() > 0) {
LinkedHashSet<SetupListenerSortWrapper> set = befores.get(listener.before());
if (set == null) {
set = new LinkedHashSet<SetupListenerSortWrapper>();
befores.put(listener.before().startsWith("$setup_") ? listener.before() : "$setup_" + listener.before(), set);
}
set.add(new SetupListenerSortWrapper(name, listener));
} else if (listener.after() != null && listener.after().trim().length() > 0) {
LinkedHashSet<SetupListenerSortWrapper> set = afters.get(listener.after());
if (set == null) {
set = new LinkedHashSet<SetupListenerSortWrapper>();
afters.put(listener.after().startsWith("$setup_") ? listener.after() : "$setup_" + listener.after(), set);
}
set.add(new SetupListenerSortWrapper(name, listener));
} else {
listeners.add(new SetupListenerSortWrapper(name, listener));
}
}
} catch (Throwable e) {
logger.error(String.format("%s start error: ", name), e);
}
}
Collections.sort(listeners, new Comparator<SetupListenerSortWrapper>() {
@Override
public int compare(SetupListenerSortWrapper o1, SetupListenerSortWrapper o2) {
return o1.ori.priority() - o2.ori.priority();
}
});
for (SetupListenerSortWrapper wrapper : listeners) {
addListener(wrapper, befores, afters);
}
if (!befores.isEmpty())
for (LinkedHashSet<SetupListenerSortWrapper> set : befores.values()) {
for (SetupListenerSortWrapper wrapper : set) {
try {
long start = System.currentTimeMillis();
logger.info(String.format("SetupListener[%s] is initializing...", wrapper.name));
wrapper.ori.init(config);
setupListeners.add(wrapper.ori);
long end = System.currentTimeMillis();
logger.info(String.format("SetupListener[%s] inited in %s ms", wrapper.name, (end - start)));
} catch (Throwable t) {
logger.warn(String.format("SetupListener[%s] init failed!", wrapper.name));
}
}
}
if (!afters.isEmpty()) {
for (LinkedHashSet<SetupListenerSortWrapper> set : afters.values()) {
for (SetupListenerSortWrapper wrapper : set) {
try {
long start = System.currentTimeMillis();
logger.info(String.format("SetupListener[%s] is initializing...", wrapper.name));
wrapper.ori.init(config);
setupListeners.add(wrapper.ori);
long end = System.currentTimeMillis();
logger.info(String.format("SetupListener[%s] inited in %s ms", wrapper.name, (end - start)));
} catch (Throwable t) {
logger.warn(String.format("SetupListener[%s] init failed!", wrapper.name));
}
}
}
}
}
}
3 回复
MainModuleSetup 是不是也配置了@IocBean 或在js中配置了ioc bean?
添加回复
请先登陆