1 16 package org.apache.cocoon.components.modules.input; 17 18 import org.apache.avalon.framework.component.ComponentException; 19 import org.apache.avalon.framework.component.ComponentManager; 20 import org.apache.avalon.framework.component.Composable; 21 import org.apache.avalon.framework.configuration.Configuration; 22 import org.apache.avalon.framework.configuration.ConfigurationException; 23 import org.apache.avalon.framework.thread.ThreadSafe; 24 25 import org.apache.excalibur.source.Source; 26 import org.apache.excalibur.source.SourceResolver; 27 28 import java.util.Collection ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.TreeSet ; 32 import java.util.Vector ; 33 34 59 public class LocateResource extends AbstractInputModule implements Composable, ThreadSafe { 60 61 protected static final Collection col = new TreeSet (); 62 63 protected ComponentManager manager = null; 64 65 71 protected int calculateMinLen(String name) { 72 73 int minLen = name.indexOf(':'); 74 minLen = (minLen == -1 ? 0 : minLen); 75 76 return minLen; 77 } 78 79 87 protected String shortenURI(String urlstring, int minLen) { 88 89 int idx = urlstring.lastIndexOf('/'); 90 idx = (idx <= minLen + 1) ? minLen : idx; 91 urlstring = urlstring.substring(0, idx); 92 93 return urlstring; 94 } 95 96 100 protected String extractFilename(String urlstring) { 101 102 String filename = ""; 103 if (!urlstring.endsWith("/")) { 104 int idx = urlstring.lastIndexOf('/'); 105 filename = urlstring.substring(idx); 106 } 107 108 return filename; 109 } 110 111 121 protected String locateResource(String urlstring, String filename, int minLen) { 122 String sourcename = null; 123 Source src = null; 124 SourceResolver resolver = null; 125 boolean found = false; 126 try { 127 resolver = 128 (SourceResolver) this.manager.lookup( 129 org.apache.excalibur.source.SourceResolver.ROLE); 130 while (!found && urlstring.length() > minLen) { 131 sourcename = urlstring + filename; 132 try { 133 src = resolver.resolveURI(sourcename); 134 if (src.exists()) 135 found = true; 136 } catch (Exception e) { 137 if (this.getLogger().isWarnEnabled()) 138 this.getLogger().warn("Exception resolving URL " + sourcename, e); 139 } finally { 140 resolver.release(src); 141 } 142 if (!found) { 143 urlstring = shortenURI(urlstring, minLen); 144 } 145 } 146 } catch (ComponentException e1) { 147 if (this.getLogger().isErrorEnabled()) 148 this.getLogger().error("Exception obtaining source resolver ", e1); 149 } finally { 150 if (resolver != null) { 151 this.manager.release(resolver); 152 } 153 } 154 return (found ? urlstring : null); 155 } 156 157 161 public void compose(ComponentManager manager) throws ComponentException { 162 this.manager = manager; 163 } 164 165 168 public Object getAttribute(String name, Configuration modeConf, Map objectModel) 169 throws ConfigurationException { 170 171 String urlstring = name; 172 String filename = extractFilename(urlstring); 173 int minLen = calculateMinLen(name); 174 if (!filename.equals("")) { 175 urlstring = shortenURI(urlstring, minLen); 176 } 177 178 String result = locateResource(urlstring, filename, minLen); 179 result = (result == null? result : result + filename); 180 if (this.getLogger().isDebugEnabled()) 181 this.getLogger().debug( 182 "located " + name + " @ " + result); 183 return result; 184 } 185 186 189 public Iterator getAttributeNames(Configuration modeConf, Map objectModel) 190 throws ConfigurationException { 191 return LocateResource.col.iterator(); 193 } 194 195 198 public Object [] getAttributeValues(String name, Configuration modeConf, Map objectModel) 199 throws ConfigurationException { 200 201 Vector uris = null; 202 String urlstring = name; 203 String filename = extractFilename(urlstring); 204 int minLen = calculateMinLen(name); 205 if (!filename.equals("")) { 206 urlstring = shortenURI(urlstring, minLen); 207 } 208 209 while (urlstring != null && urlstring.length() > minLen) { 210 urlstring = this.locateResource(urlstring, filename, minLen); 211 if (urlstring != null) { 212 if (uris == null) 213 uris = new Vector (); 214 if (this.getLogger().isDebugEnabled()) 215 this.getLogger().debug("-> located " + name + " @ " + urlstring + filename); 216 uris.add(urlstring + filename); 217 urlstring = shortenURI(urlstring, minLen); 218 } 219 } 220 return (uris == null ? null : uris.toArray()); 221 } 222 223 } 224 | Popular Tags |