KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > matching > modular > CachingRegexpMatcher


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.matching.modular;
17
18 import org.apache.avalon.framework.configuration.Configurable;
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.apache.avalon.framework.component.ComponentSelector;
23 import org.apache.avalon.framework.component.ComponentException;
24 import org.apache.avalon.framework.component.ComponentManager;
25 import org.apache.avalon.framework.component.Composable;
26 import org.apache.avalon.framework.activity.Initializable;
27 import org.apache.avalon.framework.activity.Disposable;
28 import org.apache.avalon.framework.thread.ThreadSafe;
29
30 import org.apache.cocoon.components.modules.input.InputModule;
31
32 import org.apache.cocoon.matching.AbstractRegexpMatcher;
33
34 import java.util.Map JavaDoc;
35
36 /**
37  * Matches against a regular expression. Needs an input module to
38  * obtain value to match against.
39  *
40  * <p><b>Global and local configuration</b></p>
41  * <table border="1">
42  * <tr><td><code>input-module</code></td><td>Name of the input module used to obtain the value</td></tr>
43  * <tr><td><code>parameter-name</code></td><td>Name of the parameter to match * against</td></tr>
44  * </table>
45  *
46  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
47  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
48  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
49  * @version CVS $Id: CachingRegexpMatcher.java 30932 2004-07-29 17:35:38Z vgritsenko $
50  */

51 public class CachingRegexpMatcher extends AbstractRegexpMatcher
52     implements Configurable, Initializable, Composable, Disposable
53 {
54
55     /** The component manager instance */
56     protected ComponentManager manager;
57
58     private String JavaDoc defaultParam;
59     private String JavaDoc defaultInput = "request-param"; // default to request parameters
60
private Configuration inputConf = null; // will become an empty configuration object
61
// during configure() so why bother here...
62
String JavaDoc INPUT_MODULE_ROLE = InputModule.ROLE;
63     String JavaDoc INPUT_MODULE_SELECTOR = INPUT_MODULE_ROLE+"Selector";
64
65     private boolean initialized = false;
66     private InputModule input = null;
67     private ComponentSelector inputSelector = null;
68
69     /**
70      * Set the current <code>ComponentManager</code> instance used by this
71      * <code>Composable</code>.
72      */

73     public void compose(ComponentManager manager) throws ComponentException {
74
75         this.manager=manager;
76     }
77
78
79
80     public void configure(Configuration config) throws ConfigurationException {
81
82         this.defaultParam = config.getChild("parameter-name").getValue(null);
83         this.inputConf = config.getChild("input-module");
84         this.defaultInput = this.inputConf.getAttribute("name",this.defaultInput);
85     }
86
87
88
89     public void initialize() {
90
91         try {
92             // obtain input module
93
this.inputSelector=(ComponentSelector) this.manager.lookup(INPUT_MODULE_SELECTOR);
94             if (this.defaultInput != null &&
95                 this.inputSelector != null &&
96                 this.inputSelector.hasComponent(this.defaultInput)
97                 ){
98                 this.input = (InputModule) this.inputSelector.select(this.defaultInput);
99                 if (!(this.input instanceof ThreadSafe && this.inputSelector instanceof ThreadSafe) ) {
100                     this.inputSelector.release(this.input);
101                     this.manager.release(this.inputSelector);
102                     this.input = null;
103                     this.inputSelector = null;
104                 }
105                 this.initialized = true;
106             } else {
107                 if (getLogger().isErrorEnabled())
108                     getLogger().error("A problem occurred setting up '" + this.defaultInput
109                                       + "': Selector is "+(this.inputSelector!=null?"not ":"")
110                                       +"null, Component is "
111                                       +(this.inputSelector!=null&&this.inputSelector.hasComponent(this.defaultInput)?"known":"unknown"));
112             }
113         } catch (Exception JavaDoc e) {
114             if (getLogger().isWarnEnabled())
115                 getLogger().warn("A problem occurred setting up '" + this.defaultInput + "': " + e.getMessage());
116         }
117     }
118
119
120
121     public void dispose() {
122
123         if (!this.initialized)
124             if (getLogger().isErrorEnabled())
125                 getLogger().error("Uninitialized Component! FAILING");
126         else
127             if (this.inputSelector != null) {
128                 if (this.input != null)
129                     this.inputSelector.release(this.input);
130                 this.manager.release(this.inputSelector);
131             }
132     }
133
134
135
136     protected String JavaDoc getMatchString(Map JavaDoc objectModel, Parameters parameters) {
137
138         String JavaDoc paramName = parameters.getParameter("parameter-name", this.defaultParam);
139         String JavaDoc inputName = parameters.getParameter("input-module", this.defaultInput);
140
141         if (!this.initialized) {
142             if (getLogger().isErrorEnabled())
143                 getLogger().error("Uninitialized Component! FAILING");
144             return null;
145         }
146         if (paramName == null) {
147             if (getLogger().isWarnEnabled())
148                 getLogger().warn("No parameter name given. Trying to Continue");
149         }
150         if (inputName == null) {
151             if (getLogger().isWarnEnabled())
152                 getLogger().warn("No input module given. FAILING");
153             return null;
154         }
155
156         Object JavaDoc result = null;
157
158         if (this.input != null && inputName.equals(this.defaultInput)) {
159             // input module is thread safe
160
// thus we still have a reference to it
161
try {
162                 if (this.input != null) {
163                     result = this.input.getAttribute(paramName, this.inputConf, objectModel);
164                 }
165             } catch (Exception JavaDoc e) {
166                 if (getLogger().isWarnEnabled())
167                     getLogger().warn("A problem occurred acquiring Parameter '" + paramName
168                                       + "' from '" + inputName + "': " + e.getMessage());
169             }
170         } else {
171             // input was not thread safe
172
// so acquire it again
173
ComponentSelector iputSelector = null;
174             InputModule iput = null;
175             try {
176                 // obtain input module
177
iputSelector=(ComponentSelector) this.manager.lookup(INPUT_MODULE_SELECTOR);
178                 if (inputName != null && iputSelector != null && iputSelector.hasComponent(inputName)){
179                     iput = (InputModule) iputSelector.select(inputName);
180                 }
181                 if (iput != null) {
182                     result = iput.getAttribute(paramName, this.inputConf, objectModel);
183                 }
184             } catch (Exception JavaDoc e) {
185                 if (getLogger().isWarnEnabled())
186                     getLogger().warn("A problem occurred acquiring Parameter '" + paramName
187                                      + "' from '" + inputName + "': " + e.getMessage());
188             } finally {
189                 // release components
190
if (iputSelector != null) {
191                     if (iput != null)
192                         iputSelector.release(iput);
193                     this.manager.release(iputSelector);
194                 }
195             }
196         }
197
198         if (result instanceof String JavaDoc) {
199             return (String JavaDoc) result;
200         } else {
201             return result.toString();
202         }
203     }
204 }
205
Popular Tags