1 16 package org.apache.cocoon.matching; 17 18 import org.apache.avalon.framework.configuration.Configuration; 19 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 20 import org.apache.avalon.framework.logger.AbstractLogEnabled; 21 import org.apache.avalon.framework.parameters.ParameterException; 22 import org.apache.avalon.framework.parameters.Parameterizable; 23 import org.apache.avalon.framework.parameters.Parameters; 24 import org.apache.avalon.framework.service.ServiceException; 25 import org.apache.avalon.framework.service.ServiceManager; 26 import org.apache.avalon.framework.service.Serviceable; 27 import org.apache.avalon.framework.thread.ThreadSafe; 28 29 import org.apache.cocoon.components.source.SourceUtil; 30 import org.apache.cocoon.environment.ObjectModelHelper; 31 import org.apache.cocoon.environment.Request; 32 import org.apache.cocoon.sitemap.PatternException; 33 34 import org.apache.excalibur.source.Source; 35 import org.apache.excalibur.source.SourceResolver; 36 import org.apache.excalibur.source.SourceValidity; 37 38 import java.util.Collections ; 39 import java.util.HashMap ; 40 import java.util.Iterator ; 41 import java.util.Map ; 42 43 79 public class MountTableMatcher extends AbstractLogEnabled 80 implements Matcher, ThreadSafe, Serviceable, Parameterizable { 81 82 private ServiceManager manager; 83 private SourceResolver resolver; 84 private Map mountTables = Collections.synchronizedMap(new HashMap ()); 85 private boolean ignoreMissingTables; 86 87 public void service(ServiceManager manager) throws ServiceException { 88 this.manager = manager; 89 this.resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE); 90 } 91 92 public void parameterize(Parameters params) throws ParameterException { 93 this.ignoreMissingTables = params.getParameterAsBoolean("ignore-missing-tables", false); 94 } 95 96 private Map getMountTable(String src) throws Exception { 97 Source source = null; 98 try { 99 source = this.resolver.resolveURI(src); 100 final String uri = source.getURI(); 101 102 if (!source.exists()) { 104 if (this.ignoreMissingTables) { 105 return Collections.EMPTY_MAP; 106 } else { 107 throw new PatternException("Mount table does not exist: '" + uri + "'"); 108 } 109 } 110 111 Object [] values = (Object []) this.mountTables.get(uri); 113 if (values != null) { 114 SourceValidity oldValidity = (SourceValidity) values[1]; 116 117 int valid = oldValidity != null ? oldValidity.isValid() : SourceValidity.INVALID; 118 if (valid == SourceValidity.VALID) { 119 return (Map ) values[0]; 121 } 122 123 if (valid == SourceValidity.UNKNOWN && 124 oldValidity.isValid(source.getValidity()) == SourceValidity.VALID) { 125 return (Map ) values[0]; 127 } 128 129 } else { 131 values = new Object [2]; 132 } 133 134 Map mounts = new HashMap (); 136 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 137 Configuration config = builder.build(SourceUtil.getInputSource(source)); 138 139 Configuration[] children = config.getChildren(); 140 for (int i = 0; i < children.length; i++) { 141 Configuration child = children[i]; 142 if ("mount".equals(child.getName())) { 143 String prefix = children[i].getAttribute("uri-prefix"); 144 if (!prefix.endsWith("/") && prefix.length() != 0) { 148 prefix = prefix + '/'; 149 } 150 mounts.put(prefix, children[i].getAttribute("src")); 151 } else { 152 throw new PatternException( 153 "Unexpected element '" + child.getName() + "' (awaiting 'mount'), at " + child.getLocation()); 154 } 155 } 156 values[0] = mounts; 157 values[1] = source.getValidity(); 158 159 this.mountTables.put(uri, values); 161 162 return mounts; 163 164 } catch (SecurityException e) { 165 if (this.ignoreMissingTables) { 166 return Collections.EMPTY_MAP; 167 } else { 168 throw new PatternException("Mount table is not accessible: '" + src + "' (" + e + ")"); 169 } 170 171 } finally { 172 if (source != null) { 173 this.resolver.release(source); 174 } 175 } 176 } 177 178 public Map match(String pattern, Map objectModel, Parameters parameters) throws PatternException { 179 Map mounts; 180 try { 181 mounts = getMountTable(pattern); 182 } catch (PatternException pe) { 183 throw pe; 184 } catch (Exception e) { 185 throw new PatternException(e); 186 } 187 188 Request request = ObjectModelHelper.getRequest(objectModel); 190 String uri = request.getSitemapURI(); 191 192 Iterator iter = mounts.entrySet().iterator(); 194 while (iter.hasNext()) { 195 Map.Entry entry = (Map.Entry ) iter.next(); 196 String prefix = (String ) entry.getKey(); 197 if (uri.startsWith(prefix)) { 198 Map result = new HashMap (2); 200 result.put("uri-prefix", prefix); 201 result.put("src", entry.getValue()); 202 203 return result; 205 } 206 } 207 208 return null; 210 } 211 } 212 | Popular Tags |