KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.avalon.framework.activity.Disposable;
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24 import org.apache.avalon.framework.parameters.Parameters;
25 import org.apache.avalon.framework.thread.ThreadSafe;
26
27 import org.apache.avalon.excalibur.component.ComponentHandler;
28
29 import org.apache.cocoon.components.sax.XMLByteStreamCompiler;
30 import org.apache.cocoon.components.sax.XMLByteStreamFragment;
31 import org.apache.cocoon.environment.ObjectModelHelper;
32 import org.apache.cocoon.environment.Redirector;
33 import org.apache.cocoon.environment.Request;
34 import org.apache.cocoon.environment.SourceResolver;
35 import org.apache.cocoon.generation.ServerPagesGenerator;
36 import org.apache.cocoon.xml.AbstractXMLConsumer;
37
38 /**
39  * Allows actions to be written in XSP. This allows to use XSP to produce
40  * XML fragments that are later reused in generators.<br/>
41  *
42  * This action works in concert with the "action" logicheet, that offers
43  * actions-related services such as redirect or result map access, and the
44  * "capture" logicsheet that allows to capture parts of XSP-generated XML
45  * either as an <code>XMLizable</code> containing serialized SAX events,
46  * or as a DOM <code>Node</code>.<br/>
47  *
48  * As for generators, the XSP file name is set using the "src" attribute.<br/>
49  *
50  * This action accepts a single parameter, "output-attribute", which names
51  * the request attribute where the XSP-generated document will be stored
52  * (as an <code>XMLizable</code>). If this parameter is omitted, the
53  * XSP result is discarded (often the case when inner fragments are captured
54  * with the "capture" logicsheet").<br/>
55  *
56  * When "output-attribute" is set, the action status defaults to "success",
57  * meaning child sitemap statements are executed. This allows to use an
58  * existing XSP without modification with this action.<br/>
59  *
60  * When "output-attribute" isn't set, the action status defaults to "failure".
61  * The XSP must then use the "action" logicsheet to set its status.<br/>
62  *
63  * Example :
64  * <pre>
65  * &lt;action type="serverpages" SRC="myAction.xsp"&gt;
66  * &lt;map:param name="output-attribute" value="xsp-action-result"/&gt;
67  * ...
68  * &lt;/action&gt;
69  * </pre>
70  *
71  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
72  * @version CVS $Id: ServerPagesAction.java 125255 2005-01-15 07:56:05Z antonio $
73  */

74 public class ServerPagesAction
75         extends ConfigurableComposerAction
76         implements Disposable, ThreadSafe {
77
78     public static final String JavaDoc REDIRECTOR_OBJECT = "xsp-action:redirector";
79     public static final String JavaDoc ACTION_RESULT_OBJECT = "xsp-action:result";
80     public static final String JavaDoc ACTION_SUCCESS_OBJECT = "xsp-action:success";
81
82     ComponentHandler generatorHandler;
83
84     public void configure(Configuration conf)
85       throws ConfigurationException {
86         try {
87             this.generatorHandler = ComponentHandler.getComponentHandler(
88                 ServerPagesGenerator.class,
89                 conf,
90                 this.manager,
91                 null, // Context
92
null, // RoleManager
93
null, // LogkitLoggerManager
94
null, // InstrumentManager
95
"N/A" // instrumentableName
96
);
97
98             this.generatorHandler.enableLogging(getLogger());
99             this.generatorHandler.initialize();
100
101         } catch(Exception JavaDoc e) {
102             throw new ConfigurationException("Cannot set up component handler", e);
103         }
104     }
105
106     /* (non-Javadoc)
107      * @see org.apache.avalon.framework.activity.Disposable#dispose()
108      */

109     public void dispose() {
110         if (this.generatorHandler != null) {
111             this.generatorHandler.dispose();
112             this.generatorHandler = null;
113         }
114     }
115
116     public Map JavaDoc act(Redirector redirector, SourceResolver resolver, Map JavaDoc objectModel,
117         String JavaDoc source, Parameters parameters)
118       throws Exception JavaDoc {
119
120         if (this.getLogger().isDebugEnabled()) {
121             getLogger().debug("serverpage source: " + source);
122         }
123
124         String JavaDoc outputKey = parameters.getParameter("output-attribute", null);
125         Map JavaDoc resultMap = new HashMap JavaDoc();
126         Object JavaDoc success = null;
127
128         // Get a ServerPagesGenerator
129
ServerPagesGenerator generator = (ServerPagesGenerator)this.generatorHandler.get();
130
131         // Generator output, if output-attribute was given
132
XMLByteStreamCompiler compiler = null;
133
134         try {
135             generator.enableLogging(getLogger());
136             generator.compose(this.manager);
137             generator.setup(resolver, objectModel, source, parameters);
138
139             // Setup generator output
140
if (outputKey == null) {
141                 // discard output to a "black hole"
142
generator.setConsumer(new AbstractXMLConsumer() { } ); // Make the abstract class instanciable
143
} else {
144                 // store output in a byte stream
145
compiler = new XMLByteStreamCompiler();
146                 generator.setConsumer(compiler);
147             }
148
149             // Augment the object model for the "action" logicsheet
150
objectModel.put(REDIRECTOR_OBJECT, redirector);
151             objectModel.put(ACTION_RESULT_OBJECT, resultMap);
152
153             // Let the XSP do it's stuff
154
generator.generate();
155             success = objectModel.get(ACTION_SUCCESS_OBJECT);
156
157         } finally {
158             // Release generator
159
generatorHandler.put(generator);
160
161             // Clean up object model
162
objectModel.remove(REDIRECTOR_OBJECT);
163             objectModel.remove(ACTION_RESULT_OBJECT);
164             objectModel.remove(ACTION_SUCCESS_OBJECT);
165         }
166
167         if (outputKey != null) {
168             // Success defaults to true when the whole output is captured
169
if (success == null) {
170                 success = Boolean.TRUE;
171             }
172
173             if (success == Boolean.TRUE) {
174                 // Store the XSP output in the request
175
Request req = ObjectModelHelper.getRequest(objectModel);
176                 req.setAttribute(outputKey, new XMLByteStreamFragment(compiler.getSAXFragment()));
177             }
178         }
179
180         return (success == Boolean.TRUE) ? resultMap : null;
181     }
182 }
183
Popular Tags