1 16 package org.apache.cocoon.components.treeprocessor.sitemap; 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.activity.Disposable; 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.cocoon.ProcessingException; 27 import org.apache.cocoon.components.pipeline.ProcessingPipeline; 28 import org.apache.cocoon.components.treeprocessor.AbstractProcessingNode; 29 import org.apache.cocoon.components.treeprocessor.InvokeContext; 30 import org.apache.cocoon.components.treeprocessor.TreeProcessor; 31 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver; 32 import org.apache.cocoon.environment.Environment; 33 import org.apache.commons.lang.BooleanUtils; 34 35 41 public class MountNode extends AbstractProcessingNode 42 implements Composable, Disposable { 43 44 45 public final static String COCOON_PASS_THROUGH = "COCOON_PASS_THROUGH"; 46 47 48 private final VariableResolver prefix; 49 50 51 private final VariableResolver source; 52 53 54 private Map processors = new HashMap (); 55 56 57 private final TreeProcessor parentProcessor; 58 59 60 private final boolean checkReload; 61 62 63 private ComponentManager manager; 64 65 66 private final Boolean passThrough; 67 68 public MountNode(VariableResolver prefix, 69 VariableResolver source, 70 TreeProcessor parentProcessor, 71 boolean checkReload, 72 boolean passThrough) { 73 this.prefix = prefix; 74 this.source = source; 75 this.parentProcessor = parentProcessor; 76 this.checkReload = checkReload; 77 this.passThrough = BooleanUtils.toBooleanObject(passThrough); 78 } 79 80 public void compose(ComponentManager manager) throws ComponentException { 81 this.manager = manager; 82 } 83 84 public final boolean invoke(Environment env, InvokeContext context) 85 throws Exception { 86 final Map objectModel = env.getObjectModel(); 87 88 String resolvedSource = this.source.resolve(context, objectModel); 89 String resolvedPrefix = this.prefix.resolve(context, objectModel); 90 91 if (resolvedSource.length() == 0) { 92 throw new ProcessingException("Source of mount statement is empty"); 93 } 94 TreeProcessor processor = getProcessor(resolvedSource); 95 96 String oldPrefix = env.getURIPrefix(); 98 String oldURI = env.getURI(); 99 String oldContext = env.getContext(); 100 Object oldPassThrough = env.getAttribute(COCOON_PASS_THROUGH); 101 env.setAttribute(COCOON_PASS_THROUGH, this.passThrough); 102 103 boolean pipelineWasBuilt = false; 104 105 try { 106 env.changeContext(resolvedPrefix, resolvedSource); 107 108 if (context.isBuildingPipelineOnly()) { 109 ProcessingPipeline pp = processor.buildPipeline(env); 111 if (pp != null) { 112 context.setProcessingPipeline( pp ); 113 pipelineWasBuilt = true; 114 } 115 } else { 116 pipelineWasBuilt = processor.process(env); 118 } 119 } catch(Exception e) { 120 throw ProcessingException.throwLocated("Sitemap: error when calling sub-sitemap", e, getLocation()); 122 123 } finally { 124 if (!pipelineWasBuilt) { 129 env.setContext(oldPrefix, oldURI, oldContext); 130 } 131 132 if (oldPassThrough != null) { 133 env.setAttribute(COCOON_PASS_THROUGH, oldPassThrough); 134 } else { 135 env.removeAttribute(COCOON_PASS_THROUGH); 136 } 137 138 } 143 144 return pipelineWasBuilt; 145 } 146 147 private synchronized TreeProcessor getProcessor(String source) 148 throws Exception { 149 150 TreeProcessor processor = (TreeProcessor) processors.get(source); 151 if (processor == null) { 152 String actualSource; 154 if (source.charAt(source.length() - 1) == '/') { 155 actualSource = source + "sitemap.xmap"; 156 } else { 157 actualSource = source; 158 } 159 160 processor = this.parentProcessor.createChildProcessor(this.manager, actualSource, this.checkReload); 161 162 processors.put(source, processor); 164 } 165 166 return processor; 167 } 168 169 172 public void dispose() { 173 Iterator iter = this.processors.values().iterator(); 174 while(iter.hasNext()) { 175 ((TreeProcessor)iter.next()).dispose(); 176 } 177 } 178 } 179 | Popular Tags |