1 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 ; 35 36 51 public class CachingRegexpMatcher extends AbstractRegexpMatcher 52 implements Configurable, Initializable, Composable, Disposable 53 { 54 55 56 protected ComponentManager manager; 57 58 private String defaultParam; 59 private String defaultInput = "request-param"; private Configuration inputConf = null; String INPUT_MODULE_ROLE = InputModule.ROLE; 63 String 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 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 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 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 getMatchString(Map objectModel, Parameters parameters) { 137 138 String paramName = parameters.getParameter("parameter-name", this.defaultParam); 139 String 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 result = null; 157 158 if (this.input != null && inputName.equals(this.defaultInput)) { 159 try { 162 if (this.input != null) { 163 result = this.input.getAttribute(paramName, this.inputConf, objectModel); 164 } 165 } catch (Exception e) { 166 if (getLogger().isWarnEnabled()) 167 getLogger().warn("A problem occurred acquiring Parameter '" + paramName 168 + "' from '" + inputName + "': " + e.getMessage()); 169 } 170 } else { 171 ComponentSelector iputSelector = null; 174 InputModule iput = null; 175 try { 176 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 e) { 185 if (getLogger().isWarnEnabled()) 186 getLogger().warn("A problem occurred acquiring Parameter '" + paramName 187 + "' from '" + inputName + "': " + e.getMessage()); 188 } finally { 189 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 ) { 199 return (String ) result; 200 } else { 201 return result.toString(); 202 } 203 } 204 } 205 | Popular Tags |