001: /*
002: * Copyright 2004-2007 Joe Walker and Geert Bevin
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: *
005: * $Id: DwrServiceDeployer.java 3819 2007-06-28 13:13:23Z gbevin $
006: */
007: package com.uwyn.rife.engine.elements;
008:
009: import com.uwyn.rife.engine.ElementDeployer;
010: import com.uwyn.rife.engine.exceptions.EngineException;
011: import com.uwyn.rife.ioc.HierarchicalProperties;
012: import com.uwyn.rife.resources.ResourceFinderClasspath;
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.net.MalformedURLException;
016: import java.net.URL;
017: import java.util.Collections;
018: import java.util.Enumeration;
019: import java.util.Set;
020: import javax.servlet.RequestDispatcher;
021: import javax.servlet.Servlet;
022: import javax.servlet.ServletConfig;
023: import javax.servlet.ServletContext;
024: import javax.servlet.ServletException;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027: import javax.xml.parsers.ParserConfigurationException;
028: import org.directwebremoting.Container;
029: import org.directwebremoting.WebContextFactory;
030: import org.directwebremoting.WebContextFactory.WebContextBuilder;
031: import org.directwebremoting.extend.DwrConstants;
032: import org.directwebremoting.extend.PageNormalizer;
033: import org.directwebremoting.impl.ContainerUtil;
034: import org.directwebremoting.impl.DefaultContainer;
035: import org.directwebremoting.impl.DwrXmlConfigurator;
036: import org.directwebremoting.servlet.UrlProcessor;
037: import org.directwebremoting.util.ServletLoggingOutput;
038: import org.xml.sax.SAXException;
039:
040: public class DwrServiceDeployer extends ElementDeployer {
041: /**
042: * The processor will actually handle the http requests
043: */
044: protected UrlProcessor mProcessor;
045:
046: /**
047: * The WebContext that keeps http objects local to a thread
048: */
049: protected WebContextBuilder mWebContextBuilder;
050:
051: /**
052: * The IoC container
053: */
054: protected DefaultContainer mContainer;
055:
056: public UrlProcessor getProcessor() {
057: return mProcessor;
058: }
059:
060: public Container getContainer() {
061: return mContainer;
062: }
063:
064: /**
065: * Proxy to <code>WebContextBuilder.set()</code>.
066: * @param request The HTTP request
067: * @param response The HTTP response
068: */
069: public void initWebContextBuilder(HttpServletRequest request,
070: HttpServletResponse response) {
071: // And we lace it with the context so far to help init go smoothly
072: mWebContextBuilder.set(request, response, null, null,
073: mContainer);
074: }
075:
076: /**
077: * Proxy to <code>WebContextBuilder.unset()</code>.
078: */
079: public void deinitWebContextBuilder() {
080: if (mWebContextBuilder != null) {
081: mWebContextBuilder.unset();
082: }
083: }
084:
085: /**
086: * Add the element level configurator to the end of the list of
087: * configurators.
088: * @throws SAXException If the config file parse fails
089: * @throws ParserConfigurationException If the config file parse fails
090: * @throws IOException If the config file read fails
091: */
092: public void addElementDwrXmlConfigurator() throws IOException,
093: ParserConfigurationException, SAXException {
094: if (!getElementInfo().containsProperty(
095: DwrService.PROPERTY_XML_CONFIGURATOR_PATH)) {
096: return;
097: }
098:
099: DwrXmlConfigurator system = new DwrXmlConfigurator();
100: if (getElementInfo().isPropertyEmpty(
101: DwrService.PROPERTY_XML_CONFIGURATOR_PATH)) {
102: throw new EngineException(
103: "The DWR configuration file should be specified in the property '"
104: + DwrService.PROPERTY_XML_CONFIGURATOR_PATH
105: + "' of element "
106: + getElementInfo().getDeclarationName()
107: + ".");
108: }
109: String config_path = getElementInfo().getPropertyString(
110: DwrService.PROPERTY_XML_CONFIGURATOR_PATH);
111: URL resource = ResourceFinderClasspath.getInstance()
112: .getResource(config_path);
113: if (null == resource) {
114: throw new EngineException(
115: "The DWR configuration file '"
116: + config_path
117: + "' could not be found. It was specified in the property '"
118: + DwrService.PROPERTY_XML_CONFIGURATOR_PATH
119: + "' of element "
120: + getElementInfo().getDeclarationName()
121: + ".");
122: }
123: system.setInputStream(resource.openStream());
124: system.configure(mContainer);
125: }
126:
127: public void deploy() throws EngineException {
128: mContainer = new DefaultContainer();
129: try {
130: ContainerUtil.setupDefaults(mContainer,
131: new ServletConfig() {
132: public String getServletName() {
133: return "";
134: }
135:
136: public ServletContext getServletContext() {
137: return new ServletContext() {
138: public ServletContext getContext(
139: String p1) {
140: return null;
141: }
142:
143: public int getMajorVersion() {
144: return 0;
145: }
146:
147: public int getMinorVersion() {
148: return 0;
149: }
150:
151: public String getMimeType(String p1) {
152: return null;
153: }
154:
155: public Set getResourcePaths(String p1) {
156: return Collections.emptySet();
157: }
158:
159: public URL getResource(String p1)
160: throws MalformedURLException {
161: return null;
162: }
163:
164: public InputStream getResourceAsStream(
165: String p1) {
166: return null;
167: }
168:
169: public RequestDispatcher getRequestDispatcher(
170: String p1) {
171: return null;
172: }
173:
174: public RequestDispatcher getNamedDispatcher(
175: String p1) {
176: return null;
177: }
178:
179: public Servlet getServlet(String p1)
180: throws ServletException {
181: return null;
182: }
183:
184: public Enumeration getServlets() {
185: return Collections
186: .enumeration(Collections
187: .emptyList());
188: }
189:
190: public Enumeration getServletNames() {
191: return Collections
192: .enumeration(Collections
193: .emptyList());
194: }
195:
196: public void log(String p1) {
197: }
198:
199: public void log(Exception p1, String p2) {
200: }
201:
202: public void log(String p1, Throwable p2) {
203: }
204:
205: public String getRealPath(String p1) {
206: return "";
207: }
208:
209: public String getServerInfo() {
210: return "";
211: }
212:
213: public String getInitParameter(String p1) {
214: return null;
215: }
216:
217: public Enumeration getInitParameterNames() {
218: return Collections
219: .enumeration(Collections
220: .emptyList());
221: }
222:
223: public Object getAttribute(String p1) {
224: return null;
225: }
226:
227: public Enumeration getAttributeNames() {
228: return Collections
229: .enumeration(Collections
230: .emptyList());
231: }
232:
233: public void setAttribute(String p1,
234: Object p2) {
235: }
236:
237: public void removeAttribute(String p1) {
238: }
239:
240: public String getServletContextName() {
241: return "";
242: }
243:
244: };
245: }
246:
247: public String getInitParameter(String p1) {
248: return null;
249: }
250:
251: public Enumeration getInitParameterNames() {
252: return Collections.enumeration(Collections
253: .emptyList());
254: }
255: });
256:
257: // override the default page normalizer with RIFE's version
258: mContainer.addParameter(PageNormalizer.class.getName(),
259: new DwrRifePageNormalizer(getElementInfo()
260: .getSite()));
261:
262: // create a shadow copy of the element properties to exclude the root
263: // properties (these are the system properties), this to prevent DWR to be
264: // overpopulated with unrelated properties
265: HierarchicalProperties properties = getElementInfo()
266: .getProperties();
267: HierarchicalProperties shadow = properties
268: .createShadow(properties.getRoot());
269:
270: // add all element properties that are not RIFE-specific to the DWR container
271: for (String property_name : shadow.getInjectableNames()) {
272: if (!DwrService.DWR_ELEMENT_PROPERTIES
273: .contains(property_name)) {
274: mContainer.addParameter(property_name, shadow
275: .getValue(property_name));
276: }
277: }
278:
279: mContainer.setupFinished();
280:
281: // Cached to save looking them up
282: mWebContextBuilder = (WebContextBuilder) mContainer
283: .getBean(WebContextBuilder.class.getName());
284: mProcessor = (UrlProcessor) mContainer
285: .getBean(UrlProcessor.class.getName());
286:
287: // Now we have set the implementations we can set the WebContext up
288: WebContextFactory.setWebContextBuilder(mWebContextBuilder);
289:
290: initWebContextBuilder(null, null);
291:
292: // The dwr.xml from within the JAR file.
293: DwrXmlConfigurator system = new DwrXmlConfigurator();
294: system.setClassResourceName(DwrConstants.FILE_DWR_XML);
295: system.configure(mContainer);
296:
297: addElementDwrXmlConfigurator();
298: } catch (EngineException e) {
299: throw e;
300: } catch (Exception e) {
301: throw new EngineException(e);
302: } finally {
303: deinitWebContextBuilder();
304: ServletLoggingOutput.unsetExecutionContext();
305: }
306: }
307: }
|