001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.chain.web.servlet;
017:
018: import java.io.IOException;
019: import javax.servlet.ServletException;
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022: import org.apache.commons.chain.Catalog;
023: import org.apache.commons.chain.CatalogFactory;
024: import org.apache.commons.chain.Command;
025: import org.apache.commons.chain.web.ChainServlet;
026:
027: /**
028: * <p>Custom subclass of {@link ChainServlet} that also dispatches incoming
029: * requests to a configurable {@link Command} loaded from the specified
030: * {@link Catalog}.</p>
031: *
032: * <p>In addition to the <em>servlet</em> init parameters supported by
033: * {@link ChainServlet}, this class supports the following additional
034: * parameters:</p>
035: * <ul>
036: * <li><strong>org.apache.commons.chain.CATALOG</strong> - Name of the
037: * catalog from which to acquire commands to be executed. If not
038: * specified, the default catalog for this application will be used.</li>
039: * <li><strong>org.apache.commons.chain.COMMAND</strong> - Name of the
040: * {@link Command} (looked up in our configured {@link Catalog} used
041: * to process all incoming servlet requests. If not specified,
042: * defaults to <code>command</code>.</li>
043: * </ul>
044: *
045: * <p>Also, the <code>org.apache.commons.chain.CONFIG_ATTR</code>
046: * init parameter is also used to identify the
047: * {@link org.apache.commons.chain.Context} attribute under
048: * which our configured {@link Catalog} will be made available to
049: * {@link Command}s processing our requests, in addition to its definition
050: * of the <code>ServletContext</code> attribute key under which the
051: * {@link Catalog} is available.</p>
052: */
053:
054: public class ChainProcessor extends ChainServlet {
055:
056: // ------------------------------------------------------ Manifest Constants
057:
058: /**
059: * <p>The name of the servlet init parameter containing the name of the
060: * {@link Catalog} to use for processing incoming requests.</p>
061: */
062: public static final String CATALOG = "org.apache.commons.chain.CATALOG";
063:
064: /**
065: * <p>The default request attribute under which we expose the
066: * {@link Catalog} being used to subordinate {@link Command}s.</p>
067: */
068: public static final String CATALOG_DEFAULT = "org.apache.commons.chain.CATALOG";
069:
070: /**
071: * <p>The name of the servlet init parameter containing the name of the
072: * {@link Command} (loaded from our configured {@link Catalog} to use
073: * for processing each incoming request.</p>
074: */
075: public static final String COMMAND = "org.apache.commons.chain.COMMAND";
076:
077: /**
078: * <p>The default command name.</p>
079: */
080: private static final String COMMAND_DEFAULT = "command";
081:
082: // ------------------------------------------------------ Instance Variables
083:
084: /**
085: * <p>The name of the context attribute under which our {@link Catalog}
086: * is stored. This value is also used as the name of the
087: * context attribute under which the catalog is exposed to commands.
088: * If not specified, we will look up commands in the appropriate
089: * {@link Catalog} retrieved from our {@link CatalogFactory}.</p>
090: */
091: private String attribute = null;
092:
093: /**
094: * <p>The name of the {@link Catalog} to retrieve from the
095: * {@link CatalogFactory} for this application, or <code>null</code>
096: * to select the default {@link Catalog}.</p>
097: */
098: private String catalog = null;
099:
100: /**
101: * <p>The name of the {@link Command} to be executed for each incoming
102: * request.</p>
103: */
104: private String command = null;
105:
106: /**
107: * <p>The {@link CatalogFactory} for this application.</p>
108: */
109: private CatalogFactory factory = null;
110:
111: // --------------------------------------------------------- Servlet Methods
112:
113: /**
114: * <p>Clean up as this application is shut down.</p>
115: */
116: public void destroy() {
117:
118: super .destroy();
119: attribute = null;
120: catalog = null;
121: command = null;
122: factory = null;
123:
124: }
125:
126: /**
127: * <p>Cache the name of the command we should execute for each request.</p>
128: *
129: * @exception ServletException if an initialization error occurs
130: */
131: public void init() throws ServletException {
132:
133: super .init();
134: attribute = getServletConfig().getInitParameter(CONFIG_ATTR);
135: catalog = getServletConfig().getInitParameter(CATALOG);
136: command = getServletConfig().getInitParameter(COMMAND);
137: if (command == null) {
138: command = COMMAND_DEFAULT;
139: }
140: factory = CatalogFactory.getInstance();
141:
142: }
143:
144: /**
145: * <p>Configure a {@link ServletWebContext} for the current request, and
146: * pass it to the <code>execute()</code> method of the specified
147: * {@link Command}, loaded from our configured {@link Catalog}.</p>
148: *
149: * @param request The request we are processing
150: * @param response The response we are creating
151: *
152: * @exception IOException if an input/output error occurs
153: * @exception ServletException if a servlet exception occurs
154: */
155: public void service(HttpServletRequest request,
156: HttpServletResponse response) throws IOException,
157: ServletException {
158:
159: ServletWebContext context = new ServletWebContext(
160: getServletContext(), request, response);
161: Catalog theCatalog = null;
162: if (attribute != null) {
163: theCatalog = (Catalog) getServletContext().getAttribute(
164: this .attribute);
165: } else if (catalog != null) {
166: theCatalog = factory.getCatalog(catalog);
167: } else {
168: theCatalog = factory.getCatalog();
169: }
170: if (attribute == null) {
171: request.setAttribute(CATALOG_DEFAULT, theCatalog);
172: }
173: Command command = theCatalog.getCommand(this .command);
174: try {
175: command.execute(context);
176: } catch (Exception e) {
177: throw new ServletException(e);
178: }
179:
180: }
181:
182: }
|