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.selection.SwitchSelector;
033: import org.apache.cocoon.sitemap.PatternException;
034:
035: import java.util.Map;
036:
037: /**
038: *
039: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
040: * @version CVS $Id: SwitchSelectNode.java 433543 2006-08-22 06:22:54Z crossley $
041: */
042: public class SwitchSelectNode extends SimpleSelectorProcessingNode
043: implements ParameterizableProcessingNode, Composable,
044: Disposable {
045:
046: /** The parameters of this node */
047: private Map parameters;
048:
049: /** Pre-selected selector, if it's ThreadSafe */
050: protected SwitchSelector threadSafeSelector;
051:
052: private ProcessingNode[][] whenNodes;
053:
054: private VariableResolver[] whenTests;
055:
056: private ProcessingNode[] otherwhiseNodes;
057:
058: private ComponentManager manager;
059:
060: public SwitchSelectNode(String name) throws PatternException {
061: super (name);
062: }
063:
064: public void setParameters(Map parameterMap) {
065: this .parameters = parameterMap;
066: }
067:
068: public void setCases(ProcessingNode[][] whenNodes,
069: VariableResolver[] whenTests,
070: ProcessingNode[] otherwhiseNodes) {
071: this .whenNodes = whenNodes;
072: this .whenTests = whenTests;
073: this .otherwhiseNodes = otherwhiseNodes;
074: }
075:
076: public void compose(ComponentManager manager)
077: throws ComponentException {
078: this .manager = manager;
079:
080: setSelector((ComponentSelector) manager.lookup(Selector.ROLE
081: + "Selector"));
082:
083: // Get the selector, if it's ThreadSafe
084: this .threadSafeSelector = (SwitchSelector) this
085: .getThreadSafeComponent();
086: }
087:
088: public final boolean invoke(Environment env, InvokeContext context)
089: throws Exception {
090:
091: // Perform any common invoke functionality
092: super .invoke(env, context);
093:
094: // Prepare data needed by the action
095: Map objectModel = env.getObjectModel();
096: Parameters resolvedParams = VariableResolver.buildParameters(
097: this .parameters, context, objectModel);
098:
099: // If selector is ThreadSafe, avoid select() and try/catch block (faster !)
100: if (this .threadSafeSelector != null) {
101:
102: Object ctx = this .threadSafeSelector.getSelectorContext(
103: objectModel, resolvedParams);
104:
105: for (int i = 0; i < this .whenTests.length; i++) {
106: if (this .threadSafeSelector.select(whenTests[i]
107: .resolve(context, objectModel), ctx)) {
108: return invokeNodes(this .whenNodes[i], env, context);
109: }
110: }
111:
112: if (this .otherwhiseNodes != null) {
113: return invokeNodes(this .otherwhiseNodes, env, context);
114: }
115:
116: return false;
117:
118: } else {
119: SwitchSelector selector = (SwitchSelector) this .selector
120: .select(this .componentName);
121:
122: Object ctx = selector.getSelectorContext(objectModel,
123: resolvedParams);
124:
125: try {
126: for (int i = 0; i < this .whenTests.length; i++) {
127: if (selector.select(whenTests[i].resolve(context,
128: objectModel), ctx)) {
129: return invokeNodes(this .whenNodes[i], env,
130: context);
131: }
132: }
133:
134: if (this .otherwhiseNodes != null) {
135: return invokeNodes(this .otherwhiseNodes, env,
136: context);
137: }
138:
139: return false;
140: } finally {
141: this .selector.release(selector);
142: }
143: }
144: }
145:
146: public void dispose() {
147: if (this.threadSafeSelector != null) {
148: this.selector.release(this.threadSafeSelector);
149: this.threadSafeSelector = null;
150: }
151: if (this.selector == null) {
152: this.manager.release(this.selector);
153: this.selector = null;
154: }
155: this.manager = null;
156: }
157: }
|