1 16 package org.apache.cocoon.components.treeprocessor.sitemap; 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.Composable; 23 import org.apache.avalon.framework.parameters.Parameters; 24 import org.apache.avalon.framework.thread.ThreadSafe; 25 import org.apache.cocoon.components.treeprocessor.InvokeContext; 26 import org.apache.cocoon.components.treeprocessor.ParameterizableProcessingNode; 27 import org.apache.cocoon.components.treeprocessor.SimpleSelectorProcessingNode; 28 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver; 29 import org.apache.cocoon.environment.Environment; 30 import org.apache.cocoon.matching.Matcher; 31 import org.apache.cocoon.matching.PreparableMatcher; 32 import org.apache.cocoon.sitemap.PatternException; 33 34 import java.util.Map ; 35 36 41 public class PreparableMatchNode extends SimpleSelectorProcessingNode 42 implements ParameterizableProcessingNode, Composable, Disposable { 43 44 45 private String pattern; 46 47 48 private String name; 49 50 private Object preparedPattern; 51 52 private Map parameters; 53 54 55 private PreparableMatcher threadSafeMatcher; 56 57 protected ComponentManager manager; 58 59 public PreparableMatchNode(String type, String pattern, String name) throws PatternException { 60 super(type); 61 this.pattern = pattern; 62 this.name = name; 63 } 64 65 public void setParameters(Map parameterMap) { 66 this.parameters = parameterMap; 67 } 68 69 70 public void compose(ComponentManager manager) throws ComponentException { 71 this.manager = manager; 72 setSelector((ComponentSelector)manager.lookup(Matcher.ROLE + "Selector")); 73 74 PreparableMatcher matcher = (PreparableMatcher)selector.select(componentName); 76 77 if (matcher instanceof ThreadSafe) { 78 this.threadSafeMatcher = matcher; 79 } 80 81 try { 82 this.preparedPattern = matcher.preparePattern(this.pattern); 83 84 } catch(PatternException pe) { 85 String msg = "Invalid pattern '" + this.pattern + "' for matcher at " + this.getLocation(); 86 throw new ComponentException(null, msg, pe); 87 88 } finally { 89 if (this.threadSafeMatcher == null) { 90 selector.release(matcher); 91 } 92 } 93 } 94 95 public final boolean invoke(Environment env, InvokeContext context) 96 throws Exception { 97 98 super.invoke(env, context); 100 101 Map objectModel = env.getObjectModel(); 102 Parameters resolvedParams = VariableResolver.buildParameters( 103 this.parameters, context, objectModel 104 ); 105 106 Map result = null; 107 108 if (this.threadSafeMatcher != null) { 109 result = this.threadSafeMatcher.preparedMatch(preparedPattern, objectModel, resolvedParams); 111 112 } else { 113 PreparableMatcher matcher = (PreparableMatcher)this.selector.select(this.componentName); 115 try { 116 result = matcher.preparedMatch(preparedPattern, objectModel, resolvedParams); 117 118 } finally { 119 this.selector.release(matcher); 120 } 121 } 122 123 if (result != null) { 124 if (getLogger().isDebugEnabled()) { 125 getLogger().debug("Matcher '" + this.componentName + "' matched prepared pattern '" + 126 this.pattern + "' at " + this.getLocation()); 127 } 128 129 return this.invokeNodes(children, env, context, name, result); 131 132 } else { 133 return false; 135 } 136 } 137 138 141 public void dispose() { 142 if (this.threadSafeMatcher != null) { 143 selector.release(this.threadSafeMatcher); 144 this.threadSafeMatcher = null; 145 } 146 if (this.selector != null) { 147 this.manager.release(this.selector); 148 this.selector = null; 149 } 150 this.manager = null; 151 } 152 } 153 | Popular Tags |