JFinal类

final修饰,说明不能被继承

public final class JFinal {

private Constants constants;
private ActionMapping actionMapping;
private Handler handler;
private ServletContext servletContext;
private static IServer server;
private String contextPath = "";

Handler getHandler() {
	return handler;
}

private static final JFinal me = new JFinal();

构造器私有,说明其是一个单例模式

private JFinal() {
}

通过调用静态me()函数即可获取JFinal类的实例

public static JFinal me() {
	return me;
}

boolean init(JFinalConfig jfinalConfig, ServletContext servletContext) {
	this.servletContext = servletContext;
	this.contextPath = servletContext.getContextPath();
	
	initPathUtil();
	
	Config.configJFinal(jfinalConfig);	// start plugin and init logger factory in this method
	constants = Config.getConstants();
	
	initActionMapping();
	initHandler();
	initRender();
	initOreillyCos();
	initTokenManager();
	
	return true;
}

private void initTokenManager() {
	ITokenCache tokenCache = constants.getTokenCache();
	if (tokenCache != null)
		TokenManager.init(tokenCache);
}

private void initHandler() {
	Handler actionHandler = new ActionHandler(actionMapping, constants);
	handler = HandlerFactory.getHandler(Config.getHandlers().getHandlerList(), actionHandler);
}

private void initOreillyCos() {
	OreillyCos.init(constants.getUploadedFileSaveDirectory(), constants.getMaxPostSize(), constants.getEncoding());
}

private void initPathUtil() {
	String path = servletContext.getRealPath("/");
	PathKit.setWebRootPath(path);
}

private void initRender() {
	RenderFactory renderFactory = RenderFactory.me();
	renderFactory.init(constants, servletContext);
}

private void initActionMapping() {
	actionMapping = new ActionMapping(Config.getRoutes(), Config.getInterceptors());
	actionMapping.buildActionMapping();
}

void stopPlugins() {
	List<IPlugin> plugins = Config.getPlugins().getPluginList();
	if (plugins != null) {
		for (int i=plugins.size()-1; i >= 0; i--) {		// stop plugins
			boolean success = false;
			try {
				success = plugins.get(i).stop();
			} 
			catch (Exception e) {
				success = false;
				e.printStackTrace();
			}
			if (!success) {
				System.err.println("Plugin stop error: " + plugins.get(i).getClass().getName());
			}
		}
	}
}

public ServletContext getServletContext() {
	return this.servletContext;
}

public static void start() {
	server = ServerFactory.getServer();
	server.start();
}

public static void start(String webAppDir, int port, String context, int scanIntervalSeconds) {
	server = ServerFactory.getServer(webAppDir, port, context, scanIntervalSeconds);
	server.start();
}

public static void stop() {
	server.stop();
}

/**
 * Run JFinal Server with Debug Configurations or Run Configurations in Eclipse JavaEE
 * args example: WebRoot 80 / 5
 */
public static void main(String[] args) {
	if (args == null || args.length == 0) {
		server = ServerFactory.getServer();
		server.start();
	}
	else {
		String webAppDir = args[0];
		int port = Integer.parseInt(args[1]);
		String context = args[2];
		int scanIntervalSeconds = Integer.parseInt(args[3]);
		server = ServerFactory.getServer(webAppDir, port, context, scanIntervalSeconds);
		server.start();
	}
}

public List<String> getAllActionKeys() {
	return actionMapping.getAllActionKeys();
}

public Constants getConstants() {
	return Config.getConstants();
}

public Action getAction(String url, String[] urlPara) {
	return actionMapping.getAction(url, urlPara);
}

public String getContextPath() {
	return contextPath;
}

}

转载于:https://my.oschina.net/u/1258399/blog/786749

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值