1 17 18 19 20 package org.apache.lenya.cms.cocoon.components.modules.input; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import org.apache.avalon.framework.configuration.Configuration; 30 import org.apache.avalon.framework.configuration.ConfigurationException; 31 import org.apache.avalon.framework.service.ServiceException; 32 import org.apache.avalon.framework.service.ServiceManager; 33 import org.apache.avalon.framework.service.Serviceable; 34 import org.apache.excalibur.source.Source; 35 import org.apache.excalibur.source.SourceResolver; 36 import org.apache.excalibur.source.SourceUtil; 37 38 48 public class FallbackModule extends AbstractPageEnvelopeModule implements Serviceable { 49 50 private ServiceManager manager; 51 52 private String [] baseUris; 53 54 public static final String PATH_PREFIX = "lenya/"; 55 56 protected static final String ELEMENT_PATH = "directory"; 57 58 protected static final String ATTRIBUTE_SRC = "src"; 59 60 63 public void configure(Configuration conf) throws ConfigurationException { 64 super.configure(conf); 65 66 Configuration[] pathConfigs = conf.getChildren(ELEMENT_PATH); 67 List baseUriList = new ArrayList (); 68 69 SourceResolver resolver = null; 70 try { 71 resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 72 Source source = null; 73 for (int i = 0; i < pathConfigs.length; i++) { 74 String uri = pathConfigs[i].getAttribute(ATTRIBUTE_SRC); 75 try { 76 source = resolver.resolveURI(uri); 77 if (source.exists()) { 78 File file = SourceUtil.getFile(source); 79 if (file.isDirectory()) { 80 baseUriList.add(uri); 81 } else { 82 getLogger().warn("Omitting path [" + uri + "] (not a directory)."); 83 } 84 } else { 85 getLogger().warn("Omitting path [" + uri + "] (does not exist)."); 86 } 87 } catch (Exception e) { 88 getLogger().error("Could not resolve path [" + uri + "]: ", e); 89 throw e; 90 } finally { 91 if (source != null) { 92 resolver.release(source); 93 } 94 } 95 } 96 } catch (Exception e) { 97 throw new ConfigurationException("Configuring failed: ", e); 98 } finally { 99 if (resolver != null) { 100 manager.release(resolver); 101 } 102 } 103 104 this.baseUris = (String []) baseUriList.toArray(new String [baseUriList.size()]); 105 } 106 107 111 public Object getAttribute(String name, Configuration modeConf, Map objectModel) 112 throws ConfigurationException { 113 114 if (getLogger().isDebugEnabled()) { 115 getLogger().debug("Resolving file for path [" + name + "]"); 116 } 117 118 String resolvedUri = resolveURI(name, objectModel); 119 return resolvedUri; 120 } 121 122 129 protected String resolveURI(String path, Map objectModel) throws ConfigurationException { 130 String resolvedUri = null; 131 String checkedUris = "\n"; 132 133 SourceResolver resolver = null; 134 try { 135 resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 136 137 String [] baseUris = getBaseURIs(objectModel); 138 Source source = null; 139 int i = 0; 140 while (resolvedUri == null && i < baseUris.length) { 141 String uri = baseUris[i] + "/" + path; 142 143 checkedUris += uri + "\n"; 144 145 if (getLogger().isDebugEnabled()) { 146 getLogger().debug("Trying to resolve URI [" + uri + "]"); 147 } 148 149 try { 150 source = resolver.resolveURI(uri); 151 if (source.exists()) { 152 resolvedUri = uri; 153 } else { 154 if (getLogger().isDebugEnabled()) { 155 getLogger().debug("Skipping URI [" + uri + "] (does not exist)."); 156 } 157 } 158 } catch (Exception e) { 159 getLogger().error("Could not resolve URI [" + uri + "]: ", e); 160 throw e; 161 } finally { 162 if (source != null) { 163 resolver.release(source); 164 } 165 } 166 i++; 167 } 168 169 } catch (Exception e) { 170 throw new ConfigurationException("Resolving attribute [" + path + "] failed: ", e); 171 } finally { 172 if (resolver != null) { 173 manager.release(resolver); 174 } 175 } 176 177 if (resolvedUri == null) { 178 throw new ConfigurationException("Could not resolve file for path [" + path + "]." 179 + "\nChecked URIs:" + checkedUris); 180 } 181 else { 182 if (getLogger().isDebugEnabled()) { 183 getLogger().debug("Resolved URI: [" + resolvedUri + "]"); 184 } 185 } 186 return resolvedUri; 187 } 188 189 195 protected String [] getBaseURIs(Map objectModel) throws ConfigurationException { 196 return this.baseUris; 197 } 198 199 203 public Iterator getAttributeNames(Configuration modeConf, Map objectModel) 204 throws ConfigurationException { 205 return Collections.EMPTY_SET.iterator(); 206 } 207 208 212 public Object [] getAttributeValues(String name, Configuration modeConf, Map objectModel) 213 throws ConfigurationException { 214 Object [] objects = { getAttribute(name, modeConf, objectModel) }; 215 216 return objects; 217 } 218 219 222 public void service(ServiceManager manager) throws ServiceException { 223 this.manager = manager; 224 } 225 226 } | Popular Tags |