KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > PropagatorAction


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.cocoon.acting;
17
18 import org.apache.avalon.framework.configuration.Configurable;
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.apache.avalon.framework.service.ServiceSelector;
23 import org.apache.avalon.framework.thread.ThreadSafe;
24
25 import org.apache.cocoon.components.modules.output.OutputModule;
26 import org.apache.cocoon.environment.Redirector;
27 import org.apache.cocoon.environment.SourceResolver;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 /**
37  * This is the action used to propagate parameters into a store using an
38  * {@link org.apache.cocoon.components.modules.output.OutputModule}. It
39  * simply propagates given expression. Additionaly it will make all propagated values
40  * available via returned Map.
41  *
42  * <p>Example configuration:</p>
43  * <pre>
44  * &lt;map:action type="...." name="...." logger="..."&gt;
45  * &lt;output-module name="session-attr"&gt;
46  * &lt;!-- optional configuration for output module --&gt;
47  * &lt;/output-module&gt;
48  * &lt;store-empty-parameters&gt;true&lt;/store-empty-parameters&gt;
49  * &lt;defaults&gt;
50  * &lt;default name="..." value="...."/&gt;
51  * &lt;default name="..." value="..."/&gt;
52  * &lt;/defaults&gt;
53  * &lt;/map:action&gt;
54  * </pre>
55  *
56  * <p>Example use:</p>
57  * <pre>
58  * &lt;map:act type="session-propagator"&gt;
59  * &lt;paramater name="example" value="{example}"/&gt;
60  * &lt;paramater name="example1" value="xxx"/&gt;
61  * &lt;parameter name="PropagatorAction:store-empty-parameters" value="true"/&gt;
62  * &lt;parameter name="PropagatorAction:output-module" value="session-attr"/&gt;
63  * &lt;/map:act&gt;
64  * </pre>
65  *
66  * <h3>Configuration</h3>
67  * <table><tbody>
68  * <tr>
69  * <th>output-module</th>
70  * <td>Nested element configuring output to use. Name attribute holds
71  * output module hint.</td>
72  * <td></td><td>XML</td><td><code>request-attr</code></td>
73  * </tr>
74  * <tr>
75  * <th>store-empty-parameters</th>
76  * <td>Propagate parameters with empty values.</td>
77  * <td></td><td>boolean</td><td><code>true</code></td>
78  * </tr>
79  * <tr>
80  * <th>defaults</th>
81  * <td>Parent for default parameters to propagate.</td>
82  * <td></td><td>XML</td><td></td>
83  * </tr>
84  * <tr>
85  * <th>defaults/default</th>
86  * <td>Name attribute holds parameter name, value attribute holds
87  * parameter value. Will be used when not set on use.</td>
88  * <td></td><td>parameter</td><td></td>
89  * </tr>
90  * </tbody></table>
91  *
92  *<h3>Parameters</h3>
93  * <table><tbody>
94  * <tr>
95  * <th>PropagatorAction:output-module</th>
96  * <td>Alternative output module hint to use. A <code>null</code> configuration
97  * will be passed to a module selected this way.</td>
98  * <td></td><td>String</td><td>as determined by configuration</td>
99  * </tr>
100  * <tr>
101  * <th>PropagatorAction:store-empty-parameters</th>
102  * <td>Propagate parameters with empty values.</td>
103  * <td></td><td>boolean</td><td>as determined by configuration</td>
104  * </tr>
105  * <tr>
106  * <th>any other</th>
107  * <td>Any other parameter will be propagated.</td>
108  * <td></td><td>String</td><td></td>
109  * </tr>
110  * </tbody></table>
111  *
112  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
113  * @author <a HREF="mailto:Martin.Man@seznam.cz">Martin Man</a>
114  * @version CVS $Id: PropagatorAction.java 126301 2005-01-24 16:24:28Z vgritsenko $
115  */

116 public class PropagatorAction extends ServiceableAction
117                               implements Configurable, ThreadSafe {
118
119     /** Prefix for sitemap parameters targeted at this action. */
120     private static final String JavaDoc ACTION_PREFIX = "PropagatorAction:";
121
122     /** Configuration parameter name. */
123     private static final String JavaDoc CONFIG_STORE_EMPTY = "store-empty-parameters";
124
125     /** Configuration parameter name. */
126     private static final String JavaDoc CONFIG_OUTPUT_MODULE = "output-module";
127
128     /** Default output module name. */
129     private static final String JavaDoc OUTPUT_HINT = "request-attr"; // defaults to request attributes
130

131
132     /** Should empty parameter values be propagated? */
133     private boolean storeEmpty = true;
134
135     /** Configuration object for output module. */
136     private Configuration outputConf;
137
138     /** Name of output module to use. */
139     private String JavaDoc outputName;
140
141     /** List of {@link Entry}s holding default values. */
142     private List JavaDoc defaults;
143
144     /**
145      * A private helper holding default parameter entries.
146      *
147      */

148     private static class Entry {
149         public String JavaDoc key;
150         public String JavaDoc value;
151
152         public Entry(String JavaDoc key, String JavaDoc value) {
153             this.key = key;
154             this.value = value;
155         }
156     }
157
158     /* (non-Javadoc)
159      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
160      */

161     public void configure(Configuration config) throws ConfigurationException {
162         this.outputConf = config.getChild(CONFIG_OUTPUT_MODULE);
163         this.outputName = this.outputConf.getAttribute("name", OUTPUT_HINT);
164         this.storeEmpty =
165             config.getChild(CONFIG_STORE_EMPTY).getValueAsBoolean(this.storeEmpty);
166
167         Configuration[] dflts = config.getChild("defaults").getChildren("default");
168         if (dflts != null) {
169             this.defaults = new ArrayList JavaDoc(dflts.length);
170             for (int i = 0; i < dflts.length; i++) {
171                 this.defaults.add(
172                         new Entry(dflts[i].getAttribute("name"),
173                                   dflts[i].getAttribute("value")));
174             }
175         } else {
176             this.defaults = new ArrayList JavaDoc(0);
177         }
178     }
179
180     /* (non-Javadoc)
181      * @see org.apache.cocoon.acting.Action#act(Redirector, SourceResolver, Map, String, Parameters)
182      */

183     public Map JavaDoc act(Redirector redirector,
184                    SourceResolver resolver,
185                    Map JavaDoc objectModel,
186                    String JavaDoc source,
187                    Parameters parameters)
188     throws Exception JavaDoc {
189         // Read action parameters
190
String JavaDoc outputName = parameters.getParameter(ACTION_PREFIX + CONFIG_OUTPUT_MODULE,
191                                                     null);
192         boolean storeEmpty = parameters.getParameterAsBoolean(ACTION_PREFIX + CONFIG_STORE_EMPTY,
193                                                               this.storeEmpty);
194         parameters.removeParameter(ACTION_PREFIX + CONFIG_OUTPUT_MODULE);
195         parameters.removeParameter(ACTION_PREFIX + CONFIG_STORE_EMPTY);
196
197         Configuration outputConf = null;
198         if (outputName == null) {
199             outputName = this.outputName;
200             outputConf = this.outputConf;
201         }
202
203         // Action results map
204
final Map JavaDoc results = new HashMap JavaDoc();
205
206         OutputModule output = null;
207         ServiceSelector selector = null;
208         try {
209             selector = (ServiceSelector) this.manager.lookup(OutputModule.ROLE + "Selector");
210             if (outputName != null
211                 && selector != null
212                 && selector.isSelectable(outputName)) {
213
214                 output = (OutputModule) selector.select(outputName);
215
216                 String JavaDoc[] names = parameters.getNames();
217                 for (int i = 0; i < names.length; i++) {
218                     String JavaDoc name = names[i];
219                     String JavaDoc value = parameters.getParameter(name);
220                     if (storeEmpty || (value != null && !value.equals(""))) {
221                         if (getLogger().isDebugEnabled()) {
222                             getLogger().debug("Propagating <" + name + "> value <" + value + ">");
223                         }
224                         output.setAttribute(outputConf,
225                                             objectModel,
226                                             name,
227                                             value);
228                         results.put(name, value);
229                     }
230                 }
231
232                 // Defaults, that are not overridden
233
for (Iterator JavaDoc i = defaults.iterator(); i.hasNext();) {
234                     Entry entry = (Entry) i.next();
235                     if (!results.containsKey(entry.key)) {
236                         if (getLogger().isDebugEnabled()) {
237                             getLogger().debug("Propagating default <" + entry.key + "> value <" + entry.value + ">");
238                         }
239                         output.setAttribute(outputConf,
240                                             objectModel,
241                                             entry.key,
242                                             entry.value);
243                         results.put(entry.key, entry.value);
244                     }
245                 }
246
247                 output.commit(outputConf, objectModel);
248             }
249         } catch (Exception JavaDoc e) {
250             if (output != null) {
251                 output.rollback(outputConf, objectModel, e);
252             }
253             throw e;
254         } finally {
255             if (selector != null) {
256                 if (output != null) {
257                     selector.release(output);
258                 }
259                 this.manager.release(selector);
260             }
261         }
262
263         return Collections.unmodifiableMap(results);
264     }
265 }
266
Popular Tags