001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.treeprocessor.sitemap;
018:
019: import org.apache.avalon.framework.activity.Disposable;
020: import org.apache.avalon.framework.component.ComponentException;
021: import org.apache.avalon.framework.component.ComponentManager;
022: import org.apache.avalon.framework.component.ComponentSelector;
023: import org.apache.avalon.framework.component.Composable;
024: import org.apache.avalon.framework.parameters.Parameters;
025: import org.apache.cocoon.components.treeprocessor.InvokeContext;
026: import org.apache.cocoon.components.treeprocessor.ParameterizableProcessingNode;
027: import org.apache.cocoon.components.treeprocessor.ProcessingNode;
028: import org.apache.cocoon.components.treeprocessor.SimpleSelectorProcessingNode;
029: import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
030: import org.apache.cocoon.environment.Environment;
031: import org.apache.cocoon.selection.Selector;
032: import org.apache.cocoon.sitemap.PatternException;
033:
034: import java.util.Map;
035:
036: /**
037: *
038: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
039: * @version CVS $Id: SelectNode.java 433543 2006-08-22 06:22:54Z crossley $
040: */
041: public class SelectNode extends SimpleSelectorProcessingNode implements
042: ParameterizableProcessingNode, Composable, Disposable {
043:
044: /** The parameters of this node */
045: private Map parameters;
046:
047: /** Pre-selected selector, if it's ThreadSafe */
048: protected Selector threadSafeSelector;
049:
050: private ProcessingNode[][] whenNodes;
051:
052: private VariableResolver[] whenTests;
053:
054: private ProcessingNode[] otherwhiseNodes;
055:
056: private ComponentManager manager;
057:
058: public SelectNode(String name) throws PatternException {
059: super (name);
060: }
061:
062: public void setParameters(Map parameterMap) {
063: this .parameters = parameterMap;
064: }
065:
066: public void setCases(ProcessingNode[][] whenNodes,
067: VariableResolver[] whenTests,
068: ProcessingNode[] otherwhiseNodes) {
069: this .whenNodes = whenNodes;
070: this .whenTests = whenTests;
071: this .otherwhiseNodes = otherwhiseNodes;
072: }
073:
074: public void compose(ComponentManager manager)
075: throws ComponentException {
076: this .manager = manager;
077: setSelector((ComponentSelector) manager.lookup(Selector.ROLE
078: + "Selector"));
079:
080: // Get the selector, if it's ThreadSafe
081: this .threadSafeSelector = (Selector) this
082: .getThreadSafeComponent();
083: }
084:
085: public final boolean invoke(Environment env, InvokeContext context)
086: throws Exception {
087:
088: // Perform any common invoke functionality
089: super .invoke(env, context);
090:
091: // Prepare data needed by the action
092: Map objectModel = env.getObjectModel();
093: Parameters resolvedParams = VariableResolver.buildParameters(
094: this .parameters, context, objectModel);
095:
096: // If selector is ThreadSafe, avoid select() and try/catch block (faster !)
097: if (this .threadSafeSelector != null) {
098:
099: for (int i = 0; i < this .whenTests.length; i++) {
100: if (this .threadSafeSelector.select(whenTests[i]
101: .resolve(context, objectModel), objectModel,
102: resolvedParams)) {
103: return invokeNodes(this .whenNodes[i], env, context);
104: }
105: }
106:
107: if (this .otherwhiseNodes != null) {
108: return invokeNodes(this .otherwhiseNodes, env, context);
109: }
110:
111: return false;
112:
113: } else {
114: Selector selector = (Selector) this .selector
115: .select(this .componentName);
116: try {
117:
118: for (int i = 0; i < this .whenTests.length; i++) {
119: if (selector.select(whenTests[i].resolve(context,
120: objectModel), objectModel, resolvedParams)) {
121: return invokeNodes(this .whenNodes[i], env,
122: context);
123: }
124: }
125:
126: if (this .otherwhiseNodes != null) {
127: return invokeNodes(this .otherwhiseNodes, env,
128: context);
129: }
130:
131: return false;
132: } finally {
133: this .selector.release(selector);
134: }
135: }
136: }
137:
138: public void dispose() {
139: if (this.threadSafeSelector != null) {
140: this.selector.release(this.threadSafeSelector);
141: this.threadSafeSelector = null;
142: }
143: if (this.selector != null) {
144: this.manager.release(this.selector);
145: this.selector = null;
146: }
147: this.manager = null;
148: }
149: }
|