com.github.yoojia.next.ModelManager Maven / Gradle / Ivy
package com.github.yoojia.next;
import com.github.yoojia.next.actions.NextAction;
import com.github.yoojia.next.interceptors.NextPostInterceptor;
import com.github.yoojia.next.interceptors.NextPreInterceptor;
import com.github.yoojia.next.lang.ClassLoader;
import com.github.yoojia.next.lang.Sorts;
import com.github.yoojia.next.supports.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* @author YOOJIA.CHEN ([email protected])
* @version 2015-10-14
*/
class ModelManager implements KernelManager {
private final Logger mLogger = LoggerFactory.getLogger(ModelManager.class);
private final ArrayList mKernelModels = new ArrayList<>();
private final ImmutableObject mLoader = new ImmutableObject<>();
private final ImmutableObject mContext = new ImmutableObject<>();
private final ImmutableObject mChain = new ImmutableObject<>();
ModelManager() {
}
public void init(ClassLoader loader, Context context, DispatchChain chain){
mLoader.setOnce(loader);
mContext.setOnce(context);
mChain.setOnce(chain);
}
@Override
public void onPrepares() {
// 1. Kernel modules
final Config rootConfig = mContext.get().getRootConfig();
// 1.1 pre interceptors
final List> tempKernel = new ArrayList<>();
final NextPreInterceptor pre = new NextPreInterceptor();
tempKernel.add(new Config.Wrap(pre.getPriority(), pre, rootConfig.getSection("preInterceptor")));
// 1.2 Post interceptors
final NextPostInterceptor post = new NextPostInterceptor();
tempKernel.add(new Config.Wrap(post.getPriority(), post, rootConfig.getSection("postInterceptor")));
// 1.3 Http actions
final NextAction action = new NextAction();
tempKernel.add(new Config.Wrap(action.getPriority(), action,
rootConfig.getSection("httpAction")));
final Comparator ordering = new Comparator() {
@Override
public int compare(Config.Wrap w1, Config.Wrap w2) {
return w1.priority - w2.priority;
}
};
final List configs = mContext.get().getRootConfig().getSections("appModules");
for (Config c : configs) {
final String className = c.getValue("class", null);
final AbstractModule module = mLoader.get().newClassByName(className);
final int priority = c.getValue("priority", module.getPriority());
final Config args = c.getSection("args");
mLogger.trace("Prepare APP module, Class={}, Priority={}, Args={}", className, priority, args);
tempKernel.add(new Config.Wrap<>(priority, module, args));
}
for(Config.Wrap c : new Sorts>(ordering).sortedCopy(tempKernel)) {
final AbstractModule module = c.target;
module.init(mContext.get(), c.args == null ? rootConfig : c.args);
mKernelModels.add(module);
mChain.get().addService(module);
}
}
public void onStarts(){
for (AbstractModule p : mKernelModels) {
p.onStartup(mContext.get());
}
}
public void onStops(){
for (AbstractModule p : mKernelModels) {
p.onShutdown(mContext.get());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy