package com.syaccp.common.config;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet;
import com.syaccp.common.util.LocationConfigUtil;
/**
* 加载Struts配置文件
* @author 张 柏 2009-1-10 TODO
*
*/
public class LoadStrutsConfig extends ActionServlet {
private static final long serialVersionUID = 1L;
private String baseFile="\\config";
private LocationConfigUtil configLocationUtil=new LocationConfigUtil();
public void init() throws ServletException {
ServletContext sc=this.getServletContext();
String parentPath=sc.getRealPath(baseFile);
String filter="struts-config.xml";
List<String> list=configLocationUtil.getConfigPath(parentPath,filter);
String config=null;
for (Iterator iterator =list.iterator();iterator.hasNext();){
String configPath=(String)iterator.next();
configPath=configPath.replace(parentPath,"");
configPath=baseFile+configPath;
configPath=configPath.replace('\\', '/');
if(config==null||"".equals(config)){
/* 说明还是第一次循环 */
config=configPath;
}else{
config=config+","+configPath;
System.out.println("struts--->"+config);
}
}
this.config=config;
super.init();
}
}
package com.syaccp.common.config;
import java.io.File;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
import com.syaccp.common.util.LocationConfigUtil;
/**
* 加载Hibernate配置文件<br>
* ApplicationContextAware <br>
* 获取任何目录下的hibernate.hbm.xml配置文件<br>
* 原理:spring技术手册P78---BeanPostProcessor<br>
* 就是通过ApplicationContextAware 的实例获得的;<br>
* <li> 1.先获得该配置文件的路径<br>
* 问题:如果配置了configLoaction属性就会将路径固定的配置在spring.xml文件中classpath:hibernate.hbm.xml<br>
* 所以要实现afterPropertieesSet()方法:<br>
* <li> 2.重写该接口中的方法获得上下文<br>
* <li> 3.根据上下文获得Resource:context.getResource(path);<br>
* @author 张 柏 2009-1-10 TODO
* @copyright
*
*/
public class LoadHibernateConfig extends LocalSessionFactoryBean implements
ApplicationContextAware {
public LoadHibernateConfig() {
}
private ApplicationContext applicationContext;
/**
* 装饰器设计模式<br>
* setApplicationContext()和afterPropertiesSet()是系统自带的回调方法:<br>
* 即使你没有在配置文件中设置该属性也会给该属性赋值
*
*/
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
/**
* 实现抽象类中的方法
*/
private LocationConfigUtil locationConfigUtil=new LocationConfigUtil();
public void afterPropertiesSet() throws Exception {
/*
* e.g: String baseFileName="\\config";
*
*/
if (applicationContext instanceof LoadSpringConfig){
String path = ((LoadSpringConfig) applicationContext).getContextBasePath();
File currentFile=new File(path);
String baseFileName=currentFile.getName();
String filter="hibernate.xml";//过滤条件
List<String> list=locationConfigUtil.getConfigPath(path,filter);
String[] hibernateStr =new String[list.size()];
for(int i=0;i<list.size();i++){
String configPath=list.get(i);
configPath=configPath.replace(path,"");
configPath=baseFileName+configPath;
configPath=configPath.replace('\\', '/');
hibernateStr =list.get(i);
}
Resource[] resources = new Resource[hibernateStr.length];
PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
for (int i = 0; i < hibernateStr.length; i++) {
hibernateStr=hibernateStr.replace('\\', '/');
System.out.println("hibernate:--->file:/"+hibernateStr);
resources = resourcePatternResolver.getResource("file:/"+hibernateStr);
}
this.setApplicationContext(applicationContext);
setConfigLocations(resources);
}
super.afterPropertiesSet();
}
}
package com.syaccp.common.config;
import java.util.List;
import org.springframework.web.context.support.XmlWebApplicationContext;
import com.syaccp.common.util.LocationConfigUtil;
/**
* 重写父类中的getDefaultConfigLocations()
* 加载Spring配置文件
* @author 张 柏 2009-1-10 TODO
*
*/
public class LoadSpringConfig extends XmlWebApplicationContext{
private String contextBasePath=null;
private String baseFile="\\config";
private LocationConfigUtil configLocationUtil=new LocationConfigUtil();
protected String[] getDefaultConfigLocations() {
contextBasePath=this.getServletContext().getRealPath(baseFile);
String filter="spring.xml";//过滤条件
List<String> configList=configLocationUtil.getConfigPath(contextBasePath,filter);
String[] strConfig=new String[configList.size()];
for(int i=0;i<configList.size();i++){
String configPath=configList.get(i);
configPath=configPath.replace(contextBasePath,"");
configPath=baseFile+configPath;
configPath=configPath.replace('\\', '/');
strConfig=configPath;
System.out.println("spring----->"+configPath);
}
return strConfig;
}
public String getContextBasePath() {
return contextBasePath;
}
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet;
import com.syaccp.common.util.LocationConfigUtil;
/**
* 加载Struts配置文件
* @author 张 柏 2009-1-10 TODO
*
*/
public class LoadStrutsConfig extends ActionServlet {
private static final long serialVersionUID = 1L;
private String baseFile="\\config";
private LocationConfigUtil configLocationUtil=new LocationConfigUtil();
public void init() throws ServletException {
ServletContext sc=this.getServletContext();
String parentPath=sc.getRealPath(baseFile);
String filter="struts-config.xml";
List<String> list=configLocationUtil.getConfigPath(parentPath,filter);
String config=null;
for (Iterator iterator =list.iterator();iterator.hasNext();){
String configPath=(String)iterator.next();
configPath=configPath.replace(parentPath,"");
configPath=baseFile+configPath;
configPath=configPath.replace('\\', '/');
if(config==null||"".equals(config)){
/* 说明还是第一次循环 */
config=configPath;
}else{
config=config+","+configPath;
System.out.println("struts--->"+config);
}
}
this.config=config;
super.init();
}
}
package com.syaccp.common.config;
import java.io.File;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
import com.syaccp.common.util.LocationConfigUtil;
/**
* 加载Hibernate配置文件<br>
* ApplicationContextAware <br>
* 获取任何目录下的hibernate.hbm.xml配置文件<br>
* 原理:spring技术手册P78---BeanPostProcessor<br>
* 就是通过ApplicationContextAware 的实例获得的;<br>
* <li> 1.先获得该配置文件的路径<br>
* 问题:如果配置了configLoaction属性就会将路径固定的配置在spring.xml文件中classpath:hibernate.hbm.xml<br>
* 所以要实现afterPropertieesSet()方法:<br>
* <li> 2.重写该接口中的方法获得上下文<br>
* <li> 3.根据上下文获得Resource:context.getResource(path);<br>
* @author 张 柏 2009-1-10 TODO
* @copyright
*
*/
public class LoadHibernateConfig extends LocalSessionFactoryBean implements
ApplicationContextAware {
public LoadHibernateConfig() {
}
private ApplicationContext applicationContext;
/**
* 装饰器设计模式<br>
* setApplicationContext()和afterPropertiesSet()是系统自带的回调方法:<br>
* 即使你没有在配置文件中设置该属性也会给该属性赋值
*
*/
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
/**
* 实现抽象类中的方法
*/
private LocationConfigUtil locationConfigUtil=new LocationConfigUtil();
public void afterPropertiesSet() throws Exception {
/*
* e.g: String baseFileName="\\config";
*
*/
if (applicationContext instanceof LoadSpringConfig){
String path = ((LoadSpringConfig) applicationContext).getContextBasePath();
File currentFile=new File(path);
String baseFileName=currentFile.getName();
String filter="hibernate.xml";//过滤条件
List<String> list=locationConfigUtil.getConfigPath(path,filter);
String[] hibernateStr =new String[list.size()];
for(int i=0;i<list.size();i++){
String configPath=list.get(i);
configPath=configPath.replace(path,"");
configPath=baseFileName+configPath;
configPath=configPath.replace('\\', '/');
hibernateStr =list.get(i);
}
Resource[] resources = new Resource[hibernateStr.length];
PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
for (int i = 0; i < hibernateStr.length; i++) {
hibernateStr=hibernateStr.replace('\\', '/');
System.out.println("hibernate:--->file:/"+hibernateStr);
resources = resourcePatternResolver.getResource("file:/"+hibernateStr);
}
this.setApplicationContext(applicationContext);
setConfigLocations(resources);
}
super.afterPropertiesSet();
}
}
package com.syaccp.common.config;
import java.util.List;
import org.springframework.web.context.support.XmlWebApplicationContext;
import com.syaccp.common.util.LocationConfigUtil;
/**
* 重写父类中的getDefaultConfigLocations()
* 加载Spring配置文件
* @author 张 柏 2009-1-10 TODO
*
*/
public class LoadSpringConfig extends XmlWebApplicationContext{
private String contextBasePath=null;
private String baseFile="\\config";
private LocationConfigUtil configLocationUtil=new LocationConfigUtil();
protected String[] getDefaultConfigLocations() {
contextBasePath=this.getServletContext().getRealPath(baseFile);
String filter="spring.xml";//过滤条件
List<String> configList=configLocationUtil.getConfigPath(contextBasePath,filter);
String[] strConfig=new String[configList.size()];
for(int i=0;i<configList.size();i++){
String configPath=configList.get(i);
configPath=configPath.replace(contextBasePath,"");
configPath=baseFile+configPath;
configPath=configPath.replace('\\', '/');
strConfig=configPath;
System.out.println("spring----->"+configPath);
}
return strConfig;
}
public String getContextBasePath() {
return contextBasePath;
}