1 16 package org.apache.cocoon.components.treeprocessor; 17 18 import org.apache.avalon.framework.activity.Disposable; 19 import org.apache.avalon.framework.component.ComponentException; 20 import org.apache.avalon.framework.component.ComponentManager; 21 import org.apache.avalon.framework.component.ComponentSelector; 22 import org.apache.avalon.framework.component.Recomposable; 23 import org.apache.avalon.framework.logger.AbstractLogEnabled; 24 import org.apache.cocoon.components.CocoonComponentManager; 25 import org.apache.cocoon.components.pipeline.ProcessingPipeline; 26 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver; 27 import org.apache.cocoon.environment.Redirector; 28 import org.apache.cocoon.sitemap.SitemapErrorHandler; 29 30 import java.util.ArrayList ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 52 public class InvokeContext extends AbstractLogEnabled 53 implements Recomposable, Disposable { 54 55 private List mapStack = new ArrayList (); 56 private HashMap nameToMap = new HashMap (); 57 private HashMap mapToName = new HashMap (); 58 59 60 private boolean isBuildingPipelineOnly; 61 62 63 protected Redirector redirector; 64 65 66 67 private ComponentManager currentManager; 68 69 70 private ComponentManager pipelinesManager; 71 72 73 protected String processingPipelineName; 74 75 76 protected Map processingPipelineParameters; 77 78 79 protected Map processingPipelineObjectModel; 80 81 82 protected ComponentSelector pipelineSelector; 83 84 85 protected ProcessingPipeline processingPipeline; 86 87 88 protected SitemapErrorHandler errorHandler; 89 90 94 public InvokeContext() { 95 this.isBuildingPipelineOnly = false; 96 } 97 98 101 public boolean pipelineIsSet() { 102 return (this.processingPipeline != null); 103 } 104 105 108 public InvokeContext(boolean isBuildingPipelineOnly) { 109 this.isBuildingPipelineOnly = isBuildingPipelineOnly; 110 } 111 112 115 public void compose(ComponentManager manager) throws ComponentException { 116 this.currentManager = manager; 117 } 118 119 122 public void recompose(ComponentManager manager) throws ComponentException { 123 this.currentManager = manager; 124 if (this.processingPipeline != null) { 125 this.processingPipeline.recompose(manager); 126 } 127 } 128 129 132 public void inform(String pipelineName, 133 Map parameters, 134 Map objectModel) { 135 this.processingPipelineName = pipelineName; 136 this.processingPipelineParameters = parameters; 137 this.processingPipelineObjectModel = objectModel; 138 } 139 140 143 public ProcessingPipeline getProcessingPipeline() 144 throws Exception { 145 if (this.processingPipeline == null) { 146 this.pipelinesManager = this.currentManager; 148 149 this.pipelineSelector = (ComponentSelector)this.pipelinesManager.lookup(ProcessingPipeline.ROLE+"Selector"); 150 this.processingPipeline = (ProcessingPipeline)this.pipelineSelector.select(this.processingPipelineName); 151 this.processingPipeline.recompose( this.pipelinesManager ); 152 this.processingPipeline.setup( 153 VariableResolver.buildParameters(this.processingPipelineParameters, 154 this, 155 this.processingPipelineObjectModel) 156 ); 157 if (this.isBuildingPipelineOnly) { 158 CocoonComponentManager.addComponentForAutomaticRelease(this.pipelineSelector, 159 this.processingPipeline, 160 this.pipelinesManager); 161 } 162 this.processingPipeline.setErrorHandler(this.errorHandler); 163 } 164 return this.processingPipeline; 165 } 166 167 170 public void setProcessingPipeline(ProcessingPipeline pipeline) { 171 this.processingPipeline = pipeline; 172 } 173 174 177 public final boolean isBuildingPipelineOnly() { 178 return this.isBuildingPipelineOnly; 179 } 180 181 184 public final List getMapStack() { 185 return this.mapStack; 186 } 187 188 191 public final Map getMapByAnchor(String anchor) { 192 return (Map ) this.nameToMap.get(anchor); 193 } 194 195 198 public final void pushMap(String anchorName, Map map) { 199 this.mapStack.add(map); 200 201 if (getLogger().isDebugEnabled()) { 202 dumpParameters(); 203 } 204 205 if (anchorName != null) { 206 if (!this.nameToMap.containsKey(anchorName)) { 207 this.nameToMap.put(anchorName,map); 208 this.mapToName.put(map,anchorName); 209 } else { 210 if (getLogger().isErrorEnabled()) { 211 getLogger().error("name [" + anchorName + "] clashes"); 212 } 213 } 214 } 215 } 216 217 220 protected void dumpParameters() { 221 if (!this.mapStack.isEmpty()) { 222 StringBuffer sb = new StringBuffer (); 223 224 sb.append("\nCurrent Sitemap Parameters:\n"); 225 String path = ""; 226 227 for (int i = this.mapStack.size() - 1; i >= 0; i--) { 228 Map map = (Map ) this.mapStack.get(i); 229 sb.append("LEVEL ").append(i+1); 230 if (this.mapToName.containsKey(map)) { 231 sb.append(" is named '").append(String.valueOf(this.mapToName.get(map))).append("'"); 232 } 233 sb.append("\n"); 234 235 for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) { 236 final Map.Entry me = (Map.Entry )iter.next(); 237 final Object key = me.getKey(); 238 sb.append("PARAM: '").append(path).append(key).append("' "); 239 sb.append("VALUE: '").append(me.getValue()).append("'\n"); 240 } 241 path = "../" + path; 242 } 243 244 getLogger().debug(sb.toString()); 245 } 246 } 247 248 251 public final void popMap() { 252 Object map = this.mapStack.remove(this.mapStack.size() - 1); 253 Object name = this.mapToName.get(map); 254 this.mapToName.remove(map); 255 this.nameToMap.remove(name); 256 } 257 258 263 public void setRedirector(Redirector redirector) { 264 this.redirector = redirector; 265 } 266 267 272 public Redirector getRedirector() { 273 return this.redirector; 274 } 275 276 279 public void dispose() { 280 if (!this.isBuildingPipelineOnly && this.pipelinesManager != null) { 281 if (this.pipelineSelector != null) { 282 this.pipelineSelector.release(this.processingPipeline); 283 this.processingPipeline = null; 284 this.pipelinesManager.release(this.pipelineSelector); 285 this.pipelineSelector = null; 286 } 287 this.pipelinesManager = null; 288 289 this.processingPipelineName = null; 290 this.processingPipelineParameters = null; 291 this.processingPipelineObjectModel = null; 292 293 this.errorHandler = null; 294 } 295 } 296 297 300 public void setErrorHandler(SitemapErrorHandler handler) { 301 this.errorHandler = handler; 302 } 303 } 304 | Popular Tags |