001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package web.struts;
017:
018: import java.io.File;
019: import java.util.*;
020: import javax.servlet.*;
021: import javax.sql.DataSource;
022: import org.apache.commons.beanutils.BeanUtils;
023: import org.apache.commons.lang.StringUtils;
024: import org.apache.struts.action.ActionServlet;
025: import org.apache.struts.config.DataSourceConfig;
026: import org.apache.struts.config.ModuleConfig;
027: import org.apache.struts.util.*;
028:
029: /**
030: * 扩展Struts的数据源部分
031: * @author Winter Lau
032: */
033: public class ActionServletExtend extends ActionServlet {
034:
035: /**
036: * 重新初始化数据源
037: */
038: protected void initModuleDataSources(ModuleConfig config)
039: throws ServletException {
040: ServletContextWriter scw = new ServletContextWriter(
041: getServletContext());
042: DataSourceConfig dscs[] = config.findDataSourceConfigs();
043: if (dscs == null)
044: dscs = new DataSourceConfig[0];
045: dataSources.setFast(false);
046: for (int i = 0; i < dscs.length; i++) {
047: DataSource ds = null;
048: boolean encoding = isEncodingEnabled(dscs[i]
049: .getProperties());
050: try {
051: ds = (DataSource) RequestUtils
052: .applicationInstance(dscs[i].getType());
053: BeanUtils.populate(ds, rebuildProperties(dscs[i]
054: .getProperties()));
055: ds.setLogWriter(scw);
056: } catch (Exception e) {
057: ActionServlet.log.error(internal.getMessage(
058: "dataSource.init", dscs[i].getKey()), e);
059: throw new UnavailableException(internal.getMessage(
060: "dataSource.init", dscs[i].getKey()));
061: }
062: //判断是否需要进行连接池对象的接管
063: if (dscs[i].getProperties().get(ENCODING_KEY) != null) {
064: DataSource ds_proxy = (new _DataSource(ds, encoding))
065: .getDataSource();
066: getServletContext()
067: .setAttribute(
068: dscs[i].getKey() + config.getPrefix(),
069: ds_proxy);
070: dataSources.put(dscs[i].getKey(), ds_proxy);
071: } else {
072: getServletContext().setAttribute(
073: dscs[i].getKey() + config.getPrefix(), ds);
074: dataSources.put(dscs[i].getKey(), ds);
075: }
076: }
077:
078: dataSources.setFast(true);
079: }
080:
081: /**
082: * 返回启用自动编码处理功能
083: * @param props
084: * @return
085: */
086: private boolean isEncodingEnabled(Map props) {
087: String value = (String) props.get(ENCODING_KEY);
088: return TRUE.equalsIgnoreCase(value);
089: }
090:
091: private Map rebuildProperties(Map props) {
092: String webapp_path = getServletContext().getRealPath(ROOT_PATH);
093: if (webapp_path.endsWith(File.separator))
094: webapp_path = webapp_path.substring(0,
095: webapp_path.length() - 1);
096: Properties p = new Properties();
097: String key;
098: String value;
099: for (Iterator keys = props.keySet().iterator(); keys.hasNext(); p
100: .setProperty(key, StringUtils.replace(value,
101: WEBAPP_PATH_KEY, webapp_path))) {
102: key = (String) keys.next();
103: value = (String) props.get(key);
104: }
105:
106: return p;
107: }
108:
109: public static final String WEBAPP_PATH_KEY = "${webapp}";
110: public static final String ROOT_PATH = "/";
111: public static final String ENCODING_KEY = "encoding";
112: public static final String TRUE = "true";
113: }
|