1 16 package org.apache.cocoon.components.url; 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.Composable; 22 import org.apache.avalon.framework.configuration.Configurable; 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 import org.apache.avalon.framework.context.Context; 26 import org.apache.avalon.framework.context.ContextException; 27 import org.apache.avalon.framework.context.Contextualizable; 28 import org.apache.avalon.framework.logger.AbstractLogEnabled; 29 import org.apache.avalon.framework.logger.LogEnabled; 30 import org.apache.avalon.framework.parameters.ParameterException; 31 import org.apache.avalon.framework.parameters.Parameterizable; 32 import org.apache.avalon.framework.parameters.Parameters; 33 import org.apache.avalon.framework.thread.ThreadSafe; 34 import org.apache.cocoon.Constants; 35 import org.apache.cocoon.util.ClassUtils; 36 37 import java.io.File ; 38 import java.net.MalformedURLException ; 39 import java.net.URL ; 40 import java.util.HashMap ; 41 import java.util.Iterator ; 42 import java.util.Map ; 43 44 45 51 public class URLFactoryImpl extends AbstractLogEnabled 52 implements ThreadSafe, Configurable, Disposable, Composable, Contextualizable, URLFactory { 53 54 57 protected Context context; 58 59 62 protected Map factories; 63 64 65 private ComponentManager manager; 66 67 75 public URL getURL(String location) throws MalformedURLException { 76 Iterator iter = factories.keySet().iterator(); 77 String protocol = null; 78 while (iter.hasNext()) { 79 protocol = (String )iter.next(); 80 if (location.startsWith(protocol + "://")) { 81 return ((URLFactory)factories.get(protocol)).getURL(location.substring(protocol.length() + 3)); 82 } 83 } 84 try { 85 if (getLogger().isDebugEnabled()) { 86 getLogger().debug("Making URL from " + location); 87 } 88 return new URL (location); 89 } catch (MalformedURLException mue) { 90 if (getLogger().isDebugEnabled()) { 91 getLogger().debug("Making URL - MalformedURLException in getURL:" , mue); 92 } 93 94 org.apache.cocoon.environment.Context envContext = null; 95 try { 96 envContext = (org.apache.cocoon.environment.Context) 97 context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT); 98 } catch (ContextException e){ 99 getLogger().error("Making URL - ContextException in getURL",e); 100 } 101 102 final String path = envContext.getRealPath(location); 103 if (path != null) 104 return (new File (path)).toURL(); 105 106 if (getLogger().isDebugEnabled()) { 107 getLogger().debug("Making URL a Resource:" + location); 108 } 109 URL url = envContext.getResource(location); 110 if(url != null) 111 return url; 112 113 if (getLogger().isDebugEnabled()) { 114 getLogger().debug("Making URL a File (assuming that it is full path):" + location); 115 } 116 return (new File (location)).toURL(); 117 } 118 } 119 120 public URL getURL(URL base, String location) throws MalformedURLException { 121 if ( base != null ) { 122 if (base.getProtocol().equals("file")) { 123 File temp = new File (base.toExternalForm().substring("file:".length()), location); 124 String path = temp.getAbsolutePath(); 125 if (path.charAt(0) != File.separator.charAt(0)) { 127 return getURL("file:/" + path); 128 } else { 129 return getURL("file:" + path); 130 } 131 } 132 133 return getURL(new URL (base, location).toExternalForm()); 134 } else { 135 return getURL(location); 136 } 137 } 138 139 142 public void contextualize(Context context) throws ContextException { 143 if (this.context == null) { 144 this.context = context; 145 } 146 } 147 148 151 public void configure(final Configuration conf) 152 throws ConfigurationException { 153 try { 154 getLogger().debug("Getting the URLFactories"); 155 factories = new HashMap (); 156 Configuration[] configs = conf.getChildren("protocol"); 157 URLFactory urlFactory = null; 158 String protocol = null; 159 for (int i = 0; i < configs.length; i++) { 160 protocol = configs[i].getAttribute("name"); 161 if (factories.containsKey(protocol)) { 162 throw new ConfigurationException("URLFactory defined twice for protocol: " + protocol); 163 } 164 if (getLogger().isDebugEnabled()) { 165 getLogger().debug("\tfor protocol: " + protocol + " " + configs[i].getAttribute("class")); 166 } 167 urlFactory = (URLFactory) ClassUtils.newInstance(configs[i].getAttribute("class")); 168 this.init(urlFactory, configs[i]); 169 factories.put(protocol, urlFactory); 170 } 171 } catch (Exception e) { 172 getLogger().error("Could not get URLFactories", e); 173 throw new ConfigurationException("Could not get parameters because: " + 174 e.getMessage()); 175 } 176 } 177 178 182 public void compose(ComponentManager manager) 183 throws ComponentException { 184 this.manager = manager; 185 } 186 187 190 public void dispose() { 191 Iterator iter = this.factories.values().iterator(); 192 URLFactory current; 193 while (iter.hasNext()) { 194 current = (URLFactory) iter.next(); 195 this.deinit(current); 196 } 197 this.factories = null; 198 } 199 200 203 private void init(URLFactory factory, Configuration config) 204 throws ContextException, ComponentException, ConfigurationException, ParameterException { 205 if (factory instanceof LogEnabled) { 206 ((LogEnabled) factory).enableLogging(getLogger()); 207 } 208 if (factory instanceof Contextualizable) { 209 ((Contextualizable) factory).contextualize (this.context); 210 } 211 if (factory instanceof Composable) { 212 ((Composable) factory).compose(this.manager); 213 } 214 if (config != null && factory instanceof Configurable) { 215 ((Configurable) factory).configure(config); 216 } 217 if (config != null && factory instanceof Parameterizable) { 218 ((Parameterizable) factory).parameterize(Parameters.fromConfiguration(config)); 219 } 220 } 221 222 225 private void deinit(URLFactory factory) { 226 if (factory instanceof Disposable) { 227 ((Disposable) factory).dispose(); 228 } 229 } 230 } 231 | Popular Tags |