1 16 package org.apache.cocoon.components.modules.input; 17 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.TreeMap ; 21 22 import org.apache.avalon.framework.component.ComponentException; 23 import org.apache.avalon.framework.component.ComponentManager; 24 import org.apache.avalon.framework.configuration.Configuration; 25 import org.apache.avalon.framework.configuration.ConfigurationException; 26 import org.apache.avalon.framework.thread.ThreadSafe; 27 28 52 public class SelectMetaInputModule extends AbstractMetaModule implements ThreadSafe { 53 54 private Map whenTest = null; 55 private ModuleHolder expression = null; 56 private ModuleHolder otherwise = null; 57 private String parameter = null; 58 59 public SelectMetaInputModule() { 60 super(); 61 this.defaultInput = null; } 63 64 67 public void configure(Configuration config) throws ConfigurationException { 68 69 Configuration[] expr = config.getChildren("input-module"); 70 if (expr == null || expr.length != 1) { 71 throw new ConfigurationException("Need to have exactly one input-module element."); 72 } 73 this.parameter = config.getChild("parameter").getValue(); 74 Configuration[] whens = config.getChildren("when"); 75 Configuration[] others = config.getChildren("otherwise"); 76 if ((whens == null && others == null) 77 || ((whens == null || whens.length == 0) && (others == null || others.length == 0))) { 78 throw new ConfigurationException("Need to have at least one when or otherwise element."); 79 } 80 if (others != null && others.length > 1) { 81 throw new ConfigurationException("Need to have at most one otherwise element."); 82 } 83 this.whenTest = new TreeMap (); 84 for (int i = 0; i < expr.length; i++) { 85 String name = expr[i].getAttribute("name"); 86 this.expression = new ModuleHolder(name, expr[i], null); 87 } 88 89 if (others != null) { 90 for (int i = 0; i < others.length; i++) { 91 String name = others[i].getAttribute("name"); 92 this.otherwise = new ModuleHolder(name, others[i], null); 93 } 94 } 95 96 if (whens != null) { 97 for (int i = 0; i < whens.length; i++) { 98 String name = whens[i].getAttribute("name"); 99 this.whenTest.put( 100 whens[i].getAttribute("test"), 101 new ModuleHolder(name, whens[i], null)); 102 } 103 } 104 } 105 106 109 public Object getAttribute(String name, Configuration modeConf, Map objectModel) 110 throws ConfigurationException { 111 Object result = this.getAttribute(name, modeConf, objectModel, false); 112 return result; 113 } 114 115 public Object [] getAttributeValues(String name, Configuration modeConf, Map objectModel) 116 throws ConfigurationException { 117 Object result = this.getAttribute(name, modeConf, objectModel, true); 118 return (result != null ? (Object []) result : null ); 119 } 120 121 private Object getAttribute(String name, Configuration modeConf, Map objectModel, boolean getValues) 122 throws ConfigurationException { 123 if (!this.initialized) { 124 this.lazy_initialize(); 125 } 126 ModuleHolder expression = this.expression; 127 ModuleHolder otherwise = this.otherwise; 128 ModuleHolder module = null; 129 String parameter = this.parameter; 130 boolean needRelease = false; 131 boolean dynamicConfig = (modeConf != null && modeConf.getChildren().length > 0); 132 133 if (dynamicConfig) { 134 expression = null; 137 otherwise = null; 138 needRelease = true; 139 140 Configuration[] expr = modeConf.getChildren("input-module"); 141 Configuration[] other = modeConf.getChildren("otherwise"); 142 if (expr != null && expr.length == 1) { 143 expression = new ModuleHolder(expr[0].getAttribute("name"), expr[0]); 144 } 145 if (other != null && other.length == 1) { 146 otherwise = new ModuleHolder(other[0].getAttribute("name"), other[0]); 147 } 148 parameter = modeConf.getChild("parameter").getValue(); 149 } 150 151 String value = 152 (String ) this.getValue(parameter, objectModel, expression.input, expression.name, expression.config); 153 if (needRelease) { 154 this.releaseModule(expression.input); 155 } 156 if (this.getLogger().isDebugEnabled()) { 157 this.getLogger().debug( 158 (dynamicConfig ? "(dyn)" : "(static)") 159 + " select (" 160 + value 161 + ") from " 162 + expression.name 163 + ":" 164 + parameter); 165 } 166 167 if (dynamicConfig && value != null) { 168 Configuration[] whens = modeConf.getChildren("when"); 169 if (whens != null && whens.length > 0) { 170 int i = 0; 171 boolean found = false; 172 while (!found && i < whens.length) { 173 if (whens[i].getAttribute("test").equals(value)) { 174 found = true; 175 break; 176 } 177 i++; 178 } 179 if (found) { 180 module = new ModuleHolder(whens[i].getAttribute("name"), whens[i]); 181 } 182 } 183 } else if (value != null) { 184 module = (ModuleHolder) this.whenTest.get(value); 185 } 186 if (module != null) { 187 if (this.getLogger().isDebugEnabled()) { 188 this.getLogger().debug("found matching when : "+module.name); 189 } 190 } else { 191 module = otherwise; 192 if (this.getLogger().isDebugEnabled()) { 193 this.getLogger().debug("using otherwise : "+module.name); 194 } 195 } 196 197 Object result; 198 if (getValues){ 199 result = (module == null ? null : this.getValues(name, objectModel, module)); 200 } else { 201 result = (module == null ? null : this.getValue(name, objectModel, module)); 202 } 203 204 if (needRelease && module != null) { 205 this.releaseModule(module.input); 206 } 207 if (this.getLogger().isDebugEnabled()) { 208 this.getLogger().debug("Obtained value : "+result); 209 } 210 return result; 211 } 212 213 216 public void compose(ComponentManager manager) throws ComponentException { 217 super.compose(manager); 218 } 219 220 223 public void dispose() { 224 this.releaseModule(this.expression.input); 225 this.expression = null; 226 227 if (this.otherwise != null) { 228 this.releaseModule(this.otherwise.input); 229 this.otherwise = null; 230 } 231 232 for (Iterator i = this.whenTest.values().iterator(); i.hasNext();) { 233 ModuleHolder holder = (ModuleHolder) i.next(); 234 this.releaseModule(holder.input); 235 } 236 this.whenTest = null; 237 238 super.dispose(); 239 } 240 241 244 public synchronized void lazy_initialize() { 245 if (this.initialized) { 246 return; 247 } 248 249 super.lazy_initialize(); 250 251 if (this.expression != null) { 252 this.expression.input = this.obtainModule(this.expression.name); 253 } 254 if (this.otherwise != null){ 255 this.otherwise.input = this.obtainModule(this.otherwise.name); 256 } 257 if (this.whenTest != null){ 258 for (Iterator i = this.whenTest.values().iterator(); i.hasNext(); ){ 259 ModuleHolder moduleHolder = (ModuleHolder) i.next(); 260 moduleHolder.input = this.obtainModule(moduleHolder.name); 261 } 262 } 263 } 264 } 265 | Popular Tags |