com.huawei.cmclient.common.sys.InitStruts2Filter
==================================================================================================
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.ng.ExecuteOperations;
import org.apache.struts2.dispatcher.ng.PrepareOperations;
import org.apache.struts2.dispatcher.ng.filter.FilterHostConfig;
import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;
import com.huawei.cmclient.common.log.IDebugLog;
import com.huawei.cmclient.common.log.LogFactory;
/**重载Struts2核心控制器
* @version Jul 14, 2011
* @see InitStruts2Filter
* @since
*/
public class InitStruts2Filter
extends StrutsPrepareAndExecuteFilter
{
/**
* 日志对象
*/
private static IDebugLog LOG = LogFactory.getLog(InitStruts2Filter.class);
/**初始化
* @param filterConfig
* @see
*/
public void init(FilterConfig filterConfig)
throws ServletException
{
LOG.debug("Enter Func [ init(FilterConfig filterConfig) ]");
InitStruts2Operations init = new InitStruts2Operations();
try
{
FilterHostConfig config = new FilterHostConfig(filterConfig);
init.initLogging(config);
Dispatcher dispatcher = init.initDispatcher(config);
init.initStaticContentLoader(config, dispatcher);
this.prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
this.execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
postInit(dispatcher, filterConfig);
}
finally
{
init.cleanup();
LOG.info("initialized struts2 integration successfully !");
}
}
}
=====================================================================import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.ng.HostConfig;
import org.apache.struts2.dispatcher.ng.InitOperations;
import com.huawei.cmclient.common.log.IDebugLog;
import com.huawei.cmclient.common.log.LogFactory;
/**
* Struts2核心控制器初始化类
* @version Jul 15, 2011
* @see InitStruts2Operations
* @since
*/
public class InitStruts2Operations
extends InitOperations
{
/**
* 日志对象
*/
private static IDebugLog LOG = LogFactory.getLog(InitStruts2Operations.class);
/**创建核心控制器
* @param filterConfig
* @return
* @see
*/
private Dispatcher createDispatcher(HostConfig hostConfig)
{
LOG.debug("Enter Func [ createDispatcher(HostConfig hostConfig) ]");
Map
String name;
String value;
for (Iterator< ? > e = hostConfig.getInitParameterNames(); e.hasNext(); params.put(name,
value))
{
name = (String) e.next();
value = hostConfig.getInitParameter(name);
}
parseStrutsConfig(hostConfig, params);
return new Dispatcher(hostConfig.getServletContext(), params);
}
/**初始化核心控制器
* @param filterConfig
* @return
* @see
*/
public Dispatcher initDispatcher(HostConfig hostConfig)
{
LOG.debug("Enter Func [ initDispatcher(HostConfig hostConfig) ]");
Dispatcher dispatcher = createDispatcher(hostConfig);
dispatcher.init();
return dispatcher;
}
/**获取所有配置文件的路径
* @param m
* @see
*/
private void parseStrutsConfig(HostConfig hostConfig, Map
{
LOG.debug("Enter Func [ parseStrutsConfig(HostConfig hostConfig, Map
String home = hostConfig.getServletContext().getRealPath("/");
home.replace("//", "/");
if (home.startsWith("/"))
{
home = home + "/";
}
StringBuffer strutsPath = new StringBuffer("struts-default.xml,struts-plugin.xml");
getStrutsPath(new File(home), strutsPath);
m.put("config", strutsPath.toString());
}
/**获取工程下所有的struts配置文件
* @param file
* @param pathBuffer
* @see
*/
private void getStrutsPath(File file, StringBuffer pathBuffer)
{
LOG.debug("Enter Func [ getStrutsPath(File file, StringBuffer pathBuffer) ]");
if (null == file)
{
LOG.warn("the struts config files not exist !");
return;
}
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++)
{
File f = files[i];
if (f.isDirectory())
{
getStrutsPath(f, pathBuffer);
continue;
}
String fName = files[i].getName();
if (fName.endsWith("struts.xml"))
{
pathBuffer.append(",");
pathBuffer.append(files[i].getAbsolutePath());
}
}
}
}