View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.chain.web.servlet;
17  
18  
19  import java.io.IOException;
20  import javax.servlet.ServletException;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  import org.apache.commons.chain.Catalog;
24  import org.apache.commons.chain.CatalogFactory;
25  import org.apache.commons.chain.Command;
26  import org.apache.commons.chain.web.ChainServlet;
27  
28  
29  /***
30   * <p>Custom subclass of {@link ChainServlet} that also dispatches incoming
31   * requests to a configurable {@link Command} loaded from the specified
32   * {@link Catalog}.</p>
33   *
34   * <p>In addition to the <em>servlet</em> init parameters supported by
35   * {@link ChainServlet}, this class supports the following additional
36   * parameters:</p>
37   * <ul>
38   * <li><strong>org.apache.commons.chain.CATALOG</strong> - Name of the
39   *     catalog from which to acquire commands to be executed.  If not
40   *     specified, the default catalog for this application will be used.</li>
41   * <li><strong>org.apache.commons.chain.COMMAND</strong> - Name of the
42   *     {@link Command} (looked up in our configured {@link Catalog} used
43   *     to process all incoming servlet requests.  If not specified,
44   *     defaults to <code>command</code>.</li>
45   * </ul>
46   *
47   * <p>Also, the <code>org.apache.commons.chain.CONFIG_ATTR</code>
48   * init parameter is also used to identify the
49   * {@link org.apache.commons.chain.Context} attribute under
50   * which our configured {@link Catalog} will be made available to
51   * {@link Command}s processing our requests, in addition to its definition
52   * of the <code>ServletContext</code> attribute key under which the
53   * {@link Catalog} is available.</p>
54   */
55  
56  public class ChainProcessor extends ChainServlet {
57  
58  
59      // ------------------------------------------------------ Manifest Constants
60  
61  
62      /***
63       * <p>The name of the servlet init parameter containing the name of the
64       * {@link Catalog} to use for processing incoming requests.</p>
65       */
66      public static final String CATALOG =
67          "org.apache.commons.chain.CATALOG";
68  
69  
70      /***
71       * <p>The default request attribute under which we expose the
72       * {@link Catalog} being used to subordinate {@link Command}s.</p>
73       */
74      public static final String CATALOG_DEFAULT =
75          "org.apache.commons.chain.CATALOG";
76  
77  
78      /***
79       * <p>The name of the servlet init parameter containing the name of the
80       * {@link Command} (loaded from our configured {@link Catalog} to use
81       * for processing each incoming request.</p>
82       */
83      public static final String COMMAND =
84          "org.apache.commons.chain.COMMAND";
85  
86  
87      /***
88       * <p>The default command name.</p>
89       */
90      private static final String COMMAND_DEFAULT = "command";
91  
92  
93      // ------------------------------------------------------ Instance Variables
94  
95  
96      /***
97       * <p>The name of the context attribute under which our {@link Catalog}
98       * is stored.  This value is also used as the name of the
99       * context attribute under which the catalog is exposed to commands.
100      * If not specified, we will look up commands in the appropriate
101      * {@link Catalog} retrieved from our {@link CatalogFactory}.</p>
102      */
103     private String attribute = null;
104 
105 
106     /***
107      * <p>The name of the {@link Catalog} to retrieve from the
108      * {@link CatalogFactory} for this application, or <code>null</code>
109      * to select the default {@link Catalog}.</p>
110      */
111     private String catalog = null;
112 
113 
114     /***
115      * <p>The name of the {@link Command} to be executed for each incoming
116      * request.</p>
117      */
118     private String command = null;
119 
120 
121     /***
122      * <p>The {@link CatalogFactory} for this application.</p>
123      */
124     private CatalogFactory factory = null;
125 
126 
127     // --------------------------------------------------------- Servlet Methods
128 
129 
130     /***
131      * <p>Clean up as this application is shut down.</p>
132      */
133     public void destroy() {
134 
135         super.destroy();
136         attribute = null;
137         catalog = null;
138         command = null;
139         factory = null;
140 
141     }
142 
143 
144     /***
145      * <p>Cache the name of the command we should execute for each request.</p>
146      *
147      * @exception ServletException if an initialization error occurs
148      */
149     public void init() throws ServletException {
150 
151         super.init();
152         attribute = getServletConfig().getInitParameter(CONFIG_ATTR);
153         catalog = getServletConfig().getInitParameter(CATALOG);
154         command = getServletConfig().getInitParameter(COMMAND);
155         if (command == null) {
156             command = COMMAND_DEFAULT;
157         }
158         factory = CatalogFactory.getInstance();
159 
160     }
161 
162 
163     /***
164      * <p>Configure a {@link ServletWebContext} for the current request, and
165      * pass it to the <code>execute()</code> method of the specified
166      * {@link Command}, loaded from our configured {@link Catalog}.</p>
167      *
168      * @param request The request we are processing
169      * @param response The response we are creating
170      *
171      * @exception IOException if an input/output error occurs
172      * @exception ServletException if a servlet exception occurs
173      */
174     public void service(HttpServletRequest request,
175                         HttpServletResponse response)
176         throws IOException, ServletException {
177 
178         ServletWebContext context =
179             new ServletWebContext(getServletContext(), request, response);
180         Catalog theCatalog = null;
181         if (attribute != null) {
182             theCatalog = (Catalog) getServletContext().getAttribute
183                 (this.attribute);
184         } else if (catalog != null) {
185             theCatalog = factory.getCatalog(catalog);
186         } else {
187             theCatalog = factory.getCatalog();
188         }
189         if (attribute == null) {
190             request.setAttribute(CATALOG_DEFAULT, theCatalog);
191         }
192         Command command = theCatalog.getCommand(this.command);
193         try {
194             command.execute(context);
195         } catch (Exception e) {
196             throw new ServletException(e);
197         }
198 
199     }
200 
201 
202 }