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.matching.modular;
018:
019: import org.apache.avalon.framework.configuration.Configurable;
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.ConfigurationException;
022: import org.apache.avalon.framework.parameters.Parameters;
023: import org.apache.avalon.framework.component.ComponentSelector;
024: import org.apache.avalon.framework.component.ComponentException;
025: import org.apache.avalon.framework.component.ComponentManager;
026: import org.apache.avalon.framework.component.Composable;
027:
028: import org.apache.cocoon.components.modules.input.InputModule;
029:
030: import org.apache.cocoon.matching.AbstractWildcardMatcher;
031:
032: import java.util.Map;
033:
034: /**
035: * Matches against a wildcard expression. Needs an input module to
036: * obtain value to match against.
037: *
038: * <p><b>Global and local configuration</b></p>
039: * <table border="1">
040: * <tr><td><code>input-module</code></td><td>Name of the input module used to obtain the value</td></tr>
041: * <tr><td><code>parameter-name</code></td><td>Name of the parameter to match * against</td></tr>
042: * </table>
043: *
044: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
045: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
046: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
047: * @version CVS $Id: WildcardMatcher.java 433543 2006-08-22 06:22:54Z crossley $
048: */
049: public class WildcardMatcher extends AbstractWildcardMatcher implements
050: Configurable, Composable {
051:
052: /** The component manager instance */
053: protected ComponentManager manager;
054:
055: private String defaultParam;
056: private String defaultInput = "request-param"; // default to request parameters
057: private Configuration inputConf = null; // will become an empty configuration object
058: // during configure() so why bother here...
059: String INPUT_MODULE_ROLE = InputModule.ROLE;
060: String INPUT_MODULE_SELECTOR = INPUT_MODULE_ROLE + "Selector";
061:
062: /**
063: * Set the current <code>ComponentManager</code> instance used by this
064: * <code>Composable</code>.
065: */
066: public void compose(ComponentManager manager)
067: throws ComponentException {
068: this .manager = manager;
069: }
070:
071: public void configure(Configuration config)
072: throws ConfigurationException {
073: this .defaultParam = config.getChild("parameter-name").getValue(
074: null);
075: this .inputConf = config.getChild("input-module");
076: this .defaultInput = this .inputConf.getAttribute("name",
077: this .defaultInput);
078: }
079:
080: protected String getMatchString(Map objectModel,
081: Parameters parameters) {
082:
083: String paramName = parameters.getParameter("parameter-name",
084: this .defaultParam);
085: String inputName = parameters.getParameter("input-module",
086: this .defaultInput);
087:
088: if (paramName == null) {
089: if (getLogger().isWarnEnabled())
090: getLogger().warn(
091: "No parameter name given. Trying to continue");
092: }
093: if (inputName == null) {
094: if (getLogger().isWarnEnabled())
095: getLogger().warn("No input module given. FAILING");
096: return null;
097: }
098:
099: InputModule input = null;
100: ComponentSelector inputSelector = null;
101: Object result = null;
102:
103: // one could test whether the input module is ThreadSafe and
104: // keep a reference for that instance. Then one would need
105: // to implement Disposable in order to release it at EOL
106: // That would probably speed up things a lot. Especially, since
107: // matchers are invoked very often.
108: // Perhaps a CachingWildcardMatcher ?
109:
110: try {
111: // obtain input module
112: inputSelector = (ComponentSelector) this .manager
113: .lookup(INPUT_MODULE_SELECTOR);
114: if (inputSelector != null
115: && inputSelector.hasComponent(inputName)) {
116: input = (InputModule) inputSelector.select(inputName);
117: }
118: if (input != null) {
119: result = input.getAttribute(paramName, this .inputConf,
120: objectModel);
121: }
122: } catch (Exception e) {
123: if (getLogger().isWarnEnabled())
124: getLogger().warn(
125: "A problem occurred acquiring Parameter '"
126: + paramName + "' from '" + inputName
127: + "': " + e.getMessage());
128: } finally {
129: // release components
130: if (inputSelector != null) {
131: if (input != null)
132: inputSelector.release(input);
133: this .manager.release(inputSelector);
134: }
135: }
136:
137: if (getLogger().isDebugEnabled())
138: getLogger()
139: .debug(
140: " using " + inputName + " obtained value "
141: + result);
142:
143: if (result instanceof String) {
144: return (String) result;
145: } else {
146: return result.toString();
147: }
148: }
149: }
|