001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023:
024: package biz.hammurapi.web;
025:
026: import java.io.IOException;
027: import java.sql.SQLException;
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: import javax.naming.InitialContext;
032: import javax.naming.NamingException;
033: import javax.servlet.Filter;
034: import javax.servlet.FilterChain;
035: import javax.servlet.FilterConfig;
036: import javax.servlet.ServletException;
037: import javax.servlet.ServletRequest;
038: import javax.servlet.ServletResponse;
039: import javax.servlet.http.HttpServletRequest;
040: import javax.sql.DataSource;
041:
042: import biz.hammurapi.config.ConfigurationException;
043: import biz.hammurapi.config.Context;
044: import biz.hammurapi.config.MapContext;
045: import biz.hammurapi.config.PathNavigator;
046: import biz.hammurapi.config.Wrapper;
047: import biz.hammurapi.metrics.MeasurementCategoryFactory;
048: import biz.hammurapi.sql.DataAccessObject;
049: import biz.hammurapi.sql.RowsetConfigurableContainer;
050: import biz.hammurapi.web.menu.MenuNavigator;
051: import biz.hammurapi.web.util.DynaSQLProcessor;
052:
053: /**
054: * @author Pavel Vlasov
055: * @version $Revision: 1.1 $
056: */
057:
058: public class InjectorFilter implements Filter {
059: private Context global;
060: Map globalMap = new HashMap();
061: private RowsetConfigurableContainer rcc;
062: private FilterConfig filterConfig;
063: private int counter;
064: private FileNavigator fileNavigator;
065:
066: /**
067: * Init method for this filter
068: *
069: */
070: public void init(final FilterConfig filterConfig)
071: throws ServletException {
072: this .filterConfig = filterConfig;
073: filterConfig.getServletContext().log(
074: "Initializing injector filter");
075:
076: global = PathNavigator.newInstance(new MapContext(globalMap),
077: null);
078: Context configContext = PathNavigator.newInstance(
079: new Context() {
080:
081: public Object get(String name) {
082: return filterConfig.getInitParameter(name);
083: }
084:
085: }, global);
086:
087: globalMap.put("config", configContext);
088: globalMap.put("servlet-context", filterConfig
089: .getServletContext());
090:
091: String dataSourceName = filterConfig
092: .getInitParameter("data-source");
093: if (dataSourceName != null) {
094: try {
095: InitialContext ic = new InitialContext();
096: DataSource ds = (DataSource) ic.lookup(dataSourceName);
097: globalMap.put("data-source", ds);
098: final DynaSQLProcessor processor = new DynaSQLProcessor(
099: ds, null);
100: globalMap.put("sql-processor", processor);
101: fileNavigator = new FileNavigator(processor);
102: String ticName = filterConfig
103: .getInitParameter("time-interval-category");
104: if (ticName != null) {
105: processor
106: .setTimeIntervalCategory(MeasurementCategoryFactory
107: .getTimeIntervalCategory(ticName));
108: }
109: String parametersSql = filterConfig
110: .getInitParameter("db-parameters-sql");
111: if (parametersSql != null) {
112: rcc = new RowsetConfigurableContainer() {
113: public void addComponent(String name,
114: Object component)
115: throws ConfigurationException {
116: super .addComponent(name, component);
117: if (component instanceof DataAccessObject) {
118: try {
119: ((DataAccessObject) component)
120: .setSQLProcessor(processor);
121: } catch (SQLException e) {
122: throw new ConfigurationException(
123: "Could not set SQL processor for "
124: + name + ": " + e,
125: e);
126: }
127: }
128: filterConfig
129: .getServletContext()
130: .log(
131: "Added component '"
132: + name
133: + "' -> "
134: + component
135: .getClass()
136: .getName()
137: + (component instanceof Wrapper ? " (Wrapper)"
138: : ""));
139: }
140: };
141: processor.processSelect(parametersSql, null, rcc);
142: rcc.setOwner(global);
143: globalMap.put("db", rcc);
144: rcc.start();
145: }
146: } catch (NamingException e) {
147: filterConfig.getServletContext().log(
148: "Cannot obtain datasource: " + dataSourceName,
149: e);
150: throw new ServletException("Cannot obtain datasource: "
151: + dataSourceName, e);
152: } catch (SQLException e) {
153: filterConfig.getServletContext().log(
154: "Could not load parameters from database", e);
155: throw new ServletException(
156: "Could not load parameters from database", e);
157: } catch (ConfigurationException e) {
158: filterConfig
159: .getServletContext()
160: .log(
161: "Could not start components loaded from database",
162: e);
163: throw new ServletException(
164: "Could not start components loaded from database",
165: e);
166: }
167: }
168: }
169:
170: /**
171: * @param request The servlet request we are processing
172: * @param result The servlet response we are creating
173: * @param chain The filter chain we are processing
174: *
175: * @exception IOException if an input/output error occurs
176: * @exception ServletException if a servlet error occurs
177: */
178: public void doFilter(final ServletRequest request,
179: final ServletResponse response, FilterChain chain)
180: throws IOException, ServletException {
181: // if (request instanceof HttpServletRequest) {
182: // System.out.println(((HttpServletRequest) request).getRequestURI());
183: // }
184: request.setAttribute("global", global);
185: request.setAttribute("xmenu", new MenuNavigator(
186: (HttpServletRequest) request));
187: request.setAttribute("file", fileNavigator);
188:
189: Integer c;
190: synchronized (this ) {
191: c = new Integer(++counter);
192: }
193: request.setAttribute("counter", c);
194:
195: if (request instanceof HttpServletRequest) {
196: // Retrieves action response
197: Context rr = new Context() {
198:
199: public Object get(final String actionPath) {
200: if (actionPath == null) {
201: return null;
202: }
203: int idx = actionPath.indexOf(":");
204: if (idx <= 0 || idx >= actionPath.length() - 1) {
205: return null;
206: }
207: String servletName = actionPath.substring(0, idx);
208: Object o = filterConfig.getServletContext()
209: .getAttribute("servlet/" + servletName);
210: if (o instanceof DispatchingServlet) {
211: try {
212: return ((DispatchingServlet) o)
213: .executeAction(
214: actionPath
215: .substring(idx + 1),
216: new RequestContext(
217: (HttpServletRequest) request));
218: } catch (Exception e) {
219: filterConfig.getServletContext().log(
220: "Dispatching problem", e);
221: return null;
222: }
223: }
224:
225: return null;
226: }
227:
228: };
229:
230: request.setAttribute("action", rr);
231: }
232:
233: try {
234: chain.doFilter(request, response);
235: } finally {
236: request.removeAttribute("global");
237: }
238: }
239:
240: public void destroy() {
241: if (rcc != null) {
242: try {
243: rcc.stop();
244: } catch (ConfigurationException e) {
245: e.printStackTrace();
246: }
247: }
248: }
249:
250: }
|