1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 {@link Context} attribute under
49 * which our configured {@link Catalog} will be made available to
50 * {@link Command}s processing our requests, in addition to its definition
51 * of the <code>ServletContext</code> attribute key under which the
52 * {@link Catalog} is available.</p>
53 */
54
55 public class ChainProcessor extends ChainServlet {
56
57
58
59
60
61 /***
62 * <p>The name of the servlet init parameter containing the name of the
63 * {@link Catalog} to use for processing incoming requests.</p>
64 */
65 public static final String CATALOG =
66 "org.apache.commons.chain.CATALOG";
67
68
69 /***
70 * <p>The default request attribute under which we expose the
71 * {@link Catalog} being used to subordinate {@link Command}s.</p>
72 */
73 public static final String CATALOG_DEFAULT =
74 "org.apache.commons.chain.CATALOG";
75
76
77 /***
78 * <p>The name of the servlet init parameter containing the name of the
79 * {@link Command} (loaded from our configured {@link Catalog} to use
80 * for processing each incoming request.</p>
81 */
82 public static final String COMMAND =
83 "org.apache.commons.chain.COMMAND";
84
85
86 /***
87 * <p>The default command name.</p>
88 */
89 private static final String COMMAND_DEFAULT = "command";
90
91
92
93
94
95 /***
96 * <p>The name of the context attribute under which our {@link Catalog}
97 * is stored. This value is also used as the name of the
98 * context attribute under which the catalog is exposed to commands.
99 * If not specified, we will look up commands in the appropriate
100 * {@link Catalog} retrieved from our {@link CatalogFactory}.</p>
101 */
102 private String attribute = null;
103
104
105 /***
106 * <p>The name of the {@link Catalog} to retrieve from the
107 * {@link CatalogFactory} for this application, or <code>null</code>
108 * to select the default {@link Catalog}.</p>
109 */
110 private String catalog = null;
111
112
113 /***
114 * <p>The name of the {@link Command} to be executed for each incoming
115 * request.</p>
116 */
117 private String command = null;
118
119
120 /***
121 * <p>The {@link CatalogFactory} for this application.</p>
122 */
123 private CatalogFactory factory = null;
124
125
126
127
128
129 /***
130 * <p>Clean up as this application is shut down.</p>
131 */
132 public void destroy() {
133
134 super.destroy();
135 attribute = null;
136 catalog = null;
137 command = null;
138 factory = null;
139
140 }
141
142
143 /***
144 * <p>Cache the name of the command we should execute for each request.</p>
145 *
146 * @exception ServletException if an initialization error occurs
147 */
148 public void init() throws ServletException {
149
150 super.init();
151 attribute = getServletConfig().getInitParameter(CONFIG_ATTR);
152 catalog = getServletConfig().getInitParameter(CATALOG);
153 command = getServletConfig().getInitParameter(COMMAND);
154 if (command == null) {
155 command = COMMAND_DEFAULT;
156 }
157 factory = CatalogFactory.getInstance();
158
159 }
160
161
162 /***
163 * <p>Configure a {@link ServletWebContext} for the current request, and
164 * pass it to the <code>execute()</code> method of the specified
165 * {@link Command}, loaded from our configured {@link Catalog}.</p>
166 *
167 * @param request The request we are processing
168 * @param response The response we are creating
169 *
170 * @exception IOException if an input/output error occurs
171 * @exception ServletException if a servlet exception occurs
172 */
173 public void service(HttpServletRequest request,
174 HttpServletResponse response)
175 throws IOException, ServletException {
176
177 ServletWebContext context =
178 new ServletWebContext(getServletContext(), request, response);
179 Catalog theCatalog = null;
180 if (attribute != null) {
181 theCatalog = (Catalog) getServletContext().getAttribute
182 (this.attribute);
183 } else if (catalog != null) {
184 theCatalog = factory.getCatalog(catalog);
185 } else {
186 theCatalog = factory.getCatalog();
187 }
188 if (attribute == null) {
189 request.setAttribute(CATALOG_DEFAULT, theCatalog);
190 }
191 Command command = theCatalog.getCommand(this.command);
192 try {
193 command.execute(context);
194 } catch (Exception e) {
195 throw new ServletException(e);
196 }
197
198 }
199
200
201 }