1 16 package web.struts; 17 18 import java.io.File ; 19 import java.util.*; 20 import javax.servlet.*; 21 import javax.sql.DataSource ; 22 import org.apache.commons.beanutils.BeanUtils; 23 import org.apache.commons.lang.StringUtils; 24 import org.apache.struts.action.ActionServlet; 25 import org.apache.struts.config.DataSourceConfig; 26 import org.apache.struts.config.ModuleConfig; 27 import org.apache.struts.util.*; 28 29 33 public class ActionServletExtend extends ActionServlet { 34 35 38 protected void initModuleDataSources(ModuleConfig config) throws ServletException 39 { 40 ServletContextWriter scw = new ServletContextWriter(getServletContext()); 41 DataSourceConfig dscs[] = config.findDataSourceConfigs(); 42 if (dscs == null) 43 dscs = new DataSourceConfig[0]; 44 dataSources.setFast(false); 45 for (int i = 0; i < dscs.length; i++) { 46 DataSource ds = null; 47 boolean encoding = isEncodingEnabled(dscs[i].getProperties()); 48 try { 49 ds = (DataSource ) RequestUtils.applicationInstance(dscs[i].getType()); 50 BeanUtils.populate(ds, rebuildProperties(dscs[i].getProperties())); 51 ds.setLogWriter(scw); 52 } catch (Exception e) { 53 ActionServlet.log.error(internal.getMessage("dataSource.init",dscs[i].getKey()),e); 54 throw new UnavailableException(internal.getMessage("dataSource.init", dscs[i].getKey())); 55 } 56 if (dscs[i].getProperties().get(ENCODING_KEY) != null) { 58 DataSource ds_proxy = (new _DataSource(ds, encoding)).getDataSource(); 59 getServletContext().setAttribute(dscs[i].getKey()+config.getPrefix(),ds_proxy); 60 dataSources.put(dscs[i].getKey(), ds_proxy); 61 } else { 62 getServletContext().setAttribute(dscs[i].getKey()+config.getPrefix(),ds); 63 dataSources.put(dscs[i].getKey(), ds); 64 } 65 } 66 67 dataSources.setFast(true); 68 } 69 70 75 private boolean isEncodingEnabled(Map props) { 76 String value = (String ) props.get(ENCODING_KEY); 77 return TRUE.equalsIgnoreCase(value); 78 } 79 80 private Map rebuildProperties(Map props) { 81 String webapp_path = getServletContext().getRealPath(ROOT_PATH); 82 if (webapp_path.endsWith(File.separator)) 83 webapp_path = webapp_path.substring(0, webapp_path.length() - 1); 84 Properties p = new Properties(); 85 String key; 86 String value; 87 for (Iterator keys = props.keySet().iterator(); 88 keys.hasNext(); 89 p.setProperty(key, StringUtils.replace(value, WEBAPP_PATH_KEY,webapp_path))) 90 { 91 key = (String ) keys.next(); 92 value = (String ) props.get(key); 93 } 94 95 return p; 96 } 97 98 public static final String WEBAPP_PATH_KEY = "${webapp}"; 99 public static final String ROOT_PATH = "/"; 100 public static final String ENCODING_KEY = "encoding"; 101 public static final String TRUE = "true"; 102 } | Popular Tags |