KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > treeprocessor > sitemap > ActTypeNode


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.components.treeprocessor.sitemap;
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.component.ComponentException;
23 import org.apache.avalon.framework.component.ComponentManager;
24 import org.apache.avalon.framework.component.ComponentSelector;
25 import org.apache.avalon.framework.component.Composable;
26 import org.apache.avalon.framework.parameters.Parameters;
27 import org.apache.cocoon.acting.Action;
28 import org.apache.cocoon.components.treeprocessor.InvokeContext;
29 import org.apache.cocoon.components.treeprocessor.ParameterizableProcessingNode;
30 import org.apache.cocoon.components.treeprocessor.SimpleSelectorProcessingNode;
31 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
32 import org.apache.cocoon.environment.Environment;
33 import org.apache.cocoon.environment.Redirector;
34 import org.apache.cocoon.environment.SourceResolver;
35 import org.apache.cocoon.sitemap.PatternException;
36
37 /**
38  * Handles <map:act type="..."> (action-sets calls are handled by {@link ActSetNode}).
39  *
40  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
41  * @version CVS $Id: ActTypeNode.java 55969 2004-10-29 14:01:54Z cziegeler $
42  */

43
44 public class ActTypeNode extends SimpleSelectorProcessingNode
45   implements ParameterizableProcessingNode, Disposable, Composable {
46
47     /** The parameters of this node */
48     private Map JavaDoc parameters;
49
50     /** The 'src' attribute */
51     protected VariableResolver source;
52
53     /** The 'name' for the variable anchor */
54     protected String JavaDoc name;
55
56     /** Pre-selected action, if it's ThreadSafe */
57     protected Action threadSafeAction;
58
59     protected ComponentManager manager;
60
61     protected boolean inActionSet;
62
63     public ActTypeNode(String JavaDoc type, VariableResolver source, String JavaDoc name,
64             boolean inActionSet) throws PatternException {
65         super(type);
66         this.source = source;
67         this.name = name;
68         this.inActionSet = inActionSet;
69     }
70
71     public void setParameters(Map JavaDoc parameterMap) {
72         this.parameters = parameterMap;
73     }
74
75     public void compose(ComponentManager manager) throws ComponentException {
76         this.manager = manager;
77         setSelector((ComponentSelector)manager.lookup(Action.ROLE + "Selector"));
78
79         // Get the action, if it's thread safe
80
this.threadSafeAction = (Action)this.getThreadSafeComponent();
81     }
82
83     public final boolean invoke(Environment env, InvokeContext context)
84           throws Exception JavaDoc {
85
86         // Perform any common invoke functionality
87
super.invoke(env, context);
88
89         // Prepare data needed by the action
90
Map JavaDoc objectModel = env.getObjectModel();
91         Redirector redirector = context.getRedirector();
92         SourceResolver resolver = getSourceResolver(objectModel);
93         String JavaDoc resolvedSource = source.resolve(context, objectModel);
94         Parameters resolvedParams =
95             VariableResolver.buildParameters(this.parameters,
96                     context, objectModel);
97
98         Map JavaDoc actionResult;
99
100         // If in action set, merge parameters
101
if (inActionSet) {
102             Parameters callerParams =
103                 (Parameters)env.getAttribute(ActionSetNode.CALLER_PARAMETERS);
104             if (resolvedParams == Parameters.EMPTY_PARAMETERS) {
105                 // Just swap
106
resolvedParams = callerParams;
107             } else if (callerParams != Parameters.EMPTY_PARAMETERS) {
108                 // Build new Parameters object, the both we hare are read-only!
109
Parameters newParams = new Parameters();
110                 // And merge both
111
newParams.merge(resolvedParams);
112                 newParams.merge(callerParams);
113                 resolvedParams = newParams;
114             }
115         }
116
117         // If action is ThreadSafe, avoid select() and try/catch block (faster !)
118
if (this.threadSafeAction != null) {
119             actionResult = this.threadSafeAction.act(redirector, resolver,
120                     objectModel, resolvedSource, resolvedParams);
121         } else {
122             Action action = (Action)this.selector.select(this.componentName);
123             try {
124                 actionResult = action.act(redirector, resolver,
125                         objectModel, resolvedSource, resolvedParams);
126             } finally {
127                 this.selector.release(action);
128             }
129         }
130
131         if (redirector.hasRedirected()) {
132             return true;
133         }
134
135         if (actionResult != null) {
136             // Action succeeded : process children if there are some, with the action result
137
if (this.children != null) {
138                 boolean result = this.invokeNodes(this.children, env, context, name, actionResult);
139
140                 if (inActionSet) {
141                     // Merge child action results, if any
142
Map JavaDoc childMap = (Map JavaDoc)env.getAttribute(ActionSetNode.ACTION_RESULTS);
143                     if (childMap != null) {
144                         Map JavaDoc newResults = new HashMap JavaDoc(childMap);
145                         newResults.putAll(actionResult);
146                         env.setAttribute(ActionSetNode.ACTION_RESULTS, newResults);
147                     } else {
148                         // No previous results
149
env.setAttribute(ActionSetNode.ACTION_RESULTS, actionResult);
150                     }
151                 }
152                 return result;
153             }// else {
154
// return false; // Return false to continue sitemap invocation
155
//}
156
}// else {
157
return false; // Action failed
158
//}
159
}
160
161     public void dispose() {
162         if (this.threadSafeAction != null) {
163             this.selector.release(this.threadSafeAction);
164         }
165         this.manager.release(this.selector);
166     }
167 }
168
Popular Tags