?? module.java
字號:
// Copyright 2005-2007 onetsoft.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.onetsoft.fastjsp;
import com.onetsoft.fastjsp.request.multipart.UploadConfig;
import com.onetsoft.fastjsp.util.Constants;
import com.onetsoft.fastjsp.util.ResourceResolver;
import com.onetsoft.fastjsp.util.StringUtils;
import com.onetsoft.fastjsp.valid.ValidationMessageProvider;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
/**
* FastJsp 模塊
* 模塊保存于{@link ServletContext}
*
* @author <a href="mailto:hgw@onetsoft.com">hgw</a>
* @see AbstractServicer
* @see FastJspServlet
* @since 2.0
*/
public class Module {
private final static String CONFIG_NAME = "config";
final LayoutManagerFactory layoutManagerFactory = new LayoutManagerFactory();
/*當前模塊名:servlet*/
String name = null;
/*缺省訪問jsp文件名,e.g:"index" 嚴重警告:此值必須是 index ,否則會存在首頁定位問題。tomcat的缺省就是"index.html"*/
final String welcomeFileName = "index";
/*url擴展名,一般為 html */
String urlExt = null;
/*Action parameter name,"action" applied if empty or null config*/
String actionName = null;
/*頁面所在包,如:example.pages*/
String pagePackageBase;
/*模塊頁面模板Context相對位置:如:template/skin1 若為根路徑,則為 "" */
String pageTemplateBase;
/*系統缺省Locale*/
Locale locale;
/*系統缺省字符集*/
String encoding;
Charset charset = null;
ServicerFactory servicerFactory = null;
MessagesFactory messagesFactory = null;
PageServiceFactory pageServiceFactory = null;
ValidationMessageProvider validationMessageProvider = null;
LocaleStateManagerFactory localeStateManagerFactory = null;
ValidationMessageDecoratorFactory validationMessageDecoratorFactory = null;
UploadConfig uploadConfig = null;
DataSqueezerFactory dataSqueezerFactory = null;
String moduleServletPath = null; //模塊的servlet path ,"/users" 要么為null,要么是非空值
/**
* 構造當前模塊
*
* @param servletConfig
*/
public Module(ServletConfig servletConfig) {
this.name = servletConfig.getServletName();
init(servletConfig);
}
private final static String EXT = ".html";
void init(ServletConfig servletConfig) {
String cl = StringUtils.trimToEmpty(servletConfig.getInitParameter(CONFIG_NAME));
if (cl.length() == 0)
throw new ApplicationRuntimeException("No FastJsp configuration class found in web.xml!");
Configuration config = null;
try {
config = (Configuration) ResourceResolver.forName(cl).getConstructor(new Class[]{String.class}).newInstance(new Object[]{name});
} catch (Exception e) {
throw new ApplicationRuntimeException("Fail to initialize FastJsp Configuration class \"" + name + "\".", e);
}
parseModuleServletPath(servletConfig);
String ext = config.getUrlExt();
if (ext != null && ext.trim().length() != 0 && ext.indexOf('.') != -1)
urlExt = ext.trim();
else
urlExt = EXT;
actionName = config.getActionName().trim();
if (actionName.length() == 0)
actionName = "action";
pagePackageBase = config.getPagePackageBase();
locale = config.getLocale();
pageTemplateBase = config.getPageTemplateBase();
if (pageTemplateBase.equals(StringUtils.SPLASH))
pageTemplateBase = StringUtils.EMPTY;
if (pageTemplateBase.endsWith(StringUtils.SPLASH))
pageTemplateBase = pageTemplateBase.substring(0, pageTemplateBase.length() - 1);
if (pageTemplateBase.startsWith(StringUtils.SPLASH))
pageTemplateBase = pageTemplateBase.substring(1, pageTemplateBase.length());
encoding = Constants.UTF_8;
try {
charset = Charset.forName(config.getEncoding());
encoding = charset.name();
} catch (Exception e) {
throw new ApplicationRuntimeException(e);
}
servicerFactory = config.getServicerFactory();
messagesFactory = config.getMessagesFactory();
pageServiceFactory = config.getPageServiceFactory();
validationMessageProvider = config.getValidationMessageProvider();
localeStateManagerFactory = config.getLocaleStateManagerFactory();
validationMessageDecoratorFactory = config.getValidationMessageDecoratorFactory();
uploadConfig = config.getUploadConfig();
dataSqueezerFactory = config.getDataSqueezerFactory();
}
private void parseModuleServletPath(ServletConfig servletConfig) {
String s = StringUtils.EMPTY;
try {
SAXReader sax = new SAXReader();
sax.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource() {
public InputStream getByteStream() {
return new InputStream() {
public int read() throws IOException {
return -1;
}
};
}
};
}
});
Document doc = sax.read(new File(servletConfig.getServletContext().getRealPath("/"), "WEB-INF/web.xml"));
for (Iterator iter = doc.getRootElement().elementIterator("servlet-mapping"); iter.hasNext();) {
Element el = (Element) iter.next();
if (el.element("servlet-name").getTextTrim().equals(servletConfig.getServletName())) {
s = el.element("url-pattern").getTextTrim();
break;
}
}
doc.clearContent();
} catch (Exception e) {
throw new ApplicationRuntimeException(e);
}
if (s.length() > 0 && !s.equals(StringUtils.SPLASH)) {
StringBuffer buf = new StringBuffer(s.length());
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '/' && i > 0) break;
buf.append(s.charAt(i));
}
s = buf.toString();
}
if (s.length() > 1)
moduleServletPath = s;
}
/**
* 當前模塊名稱
*
* @return
*/
public String getName() {
return name;
}
/**
* 模式locale
*
* @return
*/
public Locale getLocale() {
return locale;
}
/**
* 默認字符集
*
* @return
*/
public String getEncoding() {
return encoding;
}
/**
* 頁面模板位置
* 此位置相對于Web Context Path, 如:""(表示根路徑) , "/templates"
*
* @return
*/
public String getPageTemplateBase() {
return pageTemplateBase;
}
/**
* 取得18n資源
*
* @return
*/
public MessagesFactory getMessagesFactory() {
return messagesFactory;
}
/**
* 取得自定義page service factory
*
* @return
*/
public PageServiceFactory getPageServiceFactory() {
return pageServiceFactory;
}
/**
* 取得驗證消息提供類
*
* @return
*/
public ValidationMessageProvider getValidationMessageProvider() {
return validationMessageProvider;
}
/**
* 取得locale狀態管理
*
* @return
*/
public LocaleStateManagerFactory getLocaleStateManagerFactory() {
return localeStateManagerFactory;
}
/**
* 取得驗證消息修飾器
*
* @return
*/
public ValidationMessageDecoratorFactory getValidationMessageDecoratorFactory() {
return validationMessageDecoratorFactory;
}
/**
* 取得上載配置
*
* @return
*/
public UploadConfig getUploadConfig() {
return uploadConfig;
}
/**
* 取得Page Data中獲取/保存數據(類)映射工具
*
* @return
*/
public DataSqueezerFactory getDataSqueezerFactory() {
return dataSqueezerFactory;
}
final class LayoutManagerFactory {
private Map pool = new HashMap(1); //key:layout , value:LayoutManager
final LayoutManager getLayoutManager(PageParamsImpl pageParams) {
Object o = pool.get(pageParams.layoutId);
if (o == null) {
synchronized (pool) {
o = pool.get(pageParams.layoutId);
if (o == null) {
o = new LayoutManager();
pool.put(pageParams.layoutId, o);
}
}
}
return (LayoutManager) o;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -