KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > web > struts > ActionServletExtend


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package web.struts;
17
18 import java.io.File JavaDoc;
19 import java.util.*;
20 import javax.servlet.*;
21 import javax.sql.DataSource JavaDoc;
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 /**
30  * 扩展Struts的数据源部分
31  * @author Winter Lau
32  */

33 public class ActionServletExtend extends ActionServlet {
34
35     /**
36      * 重新初始化数据源
37      */

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 JavaDoc ds = null;
47             boolean encoding = isEncodingEnabled(dscs[i].getProperties());
48             try {
49                 ds = (DataSource JavaDoc) RequestUtils.applicationInstance(dscs[i].getType());
50                 BeanUtils.populate(ds, rebuildProperties(dscs[i].getProperties()));
51                 ds.setLogWriter(scw);
52             } catch (Exception JavaDoc 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             //判断是否需要进行连接池对象的接管
57
if (dscs[i].getProperties().get(ENCODING_KEY) != null) {
58                 DataSource JavaDoc 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     /**
71      * 返回启用自动编码处理功能
72      * @param props
73      * @return
74      */

75     private boolean isEncodingEnabled(Map props) {
76         String JavaDoc value = (String JavaDoc) props.get(ENCODING_KEY);
77         return TRUE.equalsIgnoreCase(value);
78     }
79
80     private Map rebuildProperties(Map props) {
81         String JavaDoc 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 JavaDoc key;
86         String JavaDoc 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 JavaDoc) keys.next();
92             value = (String JavaDoc) props.get(key);
93         }
94
95         return p;
96     }
97
98     public static final String JavaDoc WEBAPP_PATH_KEY = "${webapp}";
99     public static final String JavaDoc ROOT_PATH = "/";
100     public static final String JavaDoc ENCODING_KEY = "encoding";
101     public static final String JavaDoc TRUE = "true";
102 }
Popular Tags