1 16 package org.apache.cocoon.components.source; 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.cocoon.ProcessingException; 31 import org.apache.cocoon.components.url.URLFactory; 32 import org.apache.cocoon.environment.Environment; 33 import org.apache.cocoon.environment.Source; 34 import org.apache.cocoon.util.ClassUtils; 35 36 import java.io.IOException ; 37 import java.net.MalformedURLException ; 38 import java.net.URL ; 39 import java.util.HashMap ; 40 import java.util.Iterator ; 41 import java.util.Map ; 42 43 49 public final class SourceHandlerImpl extends AbstractLogEnabled 50 implements Configurable, Disposable, Composable, Contextualizable, SourceHandler { 51 52 53 private ComponentManager manager; 54 55 56 private URLFactory urlFactory; 57 58 59 private Map sourceFactories; 60 61 62 private Context context; 63 64 67 public void configure(final Configuration conf) 68 throws ConfigurationException { 69 try { 70 if (this.getLogger().isDebugEnabled()) { 71 getLogger().debug("Getting the SourceFactories"); 72 } 73 HashMap factories = new HashMap (); 74 Configuration[] configs = conf.getChildren("protocol"); 75 SourceFactory sourceFactory = null; 76 String protocol = null; 77 for (int i = 0; i < configs.length; i++) { 78 protocol = configs[i].getAttribute("name"); 79 if (factories.containsKey(protocol)) { 80 throw new ConfigurationException("SourceFactory defined twice for protocol: " + protocol); 81 } 82 83 if (this.getLogger().isDebugEnabled()) { 84 getLogger().debug("\tfor protocol: " + protocol + " " + configs[i].getAttribute("class")); 85 } 86 sourceFactory = (SourceFactory) ClassUtils.newInstance(configs[i].getAttribute("class")); 87 this.init(sourceFactory, configs[i]); 88 factories.put(protocol, sourceFactory); 89 } 90 91 this.sourceFactories = java.util.Collections.synchronizedMap(factories); 92 } catch (ConfigurationException e) { 93 throw e; 94 } catch (Exception e) { 95 throw new ConfigurationException("Could not get parameters because: " + 96 e.getMessage()); 97 } 98 } 99 100 103 public void contextualize(Context context) 104 throws ContextException { 105 this.context = context; 106 } 107 108 112 public void compose(ComponentManager manager) 113 throws ComponentException { 114 this.manager = manager; 115 this.urlFactory = (URLFactory)this.manager.lookup(URLFactory.ROLE); 116 } 117 118 121 public void dispose() { 122 this.manager.release(this.urlFactory); 123 124 final Iterator iter = this.sourceFactories.values().iterator(); 125 SourceFactory current; 126 while (iter.hasNext()) { 127 current = (SourceFactory) iter.next(); 128 this.deinit(current); 129 } 130 this.sourceFactories = null; 131 } 132 133 136 public Source getSource(Environment environment, String location) 137 throws ProcessingException, MalformedURLException , IOException { 138 final int protocolEnd = location.indexOf(':'); 139 if (protocolEnd != -1) { 140 final String protocol = location.substring(0, protocolEnd); 141 final SourceFactory sourceFactory = (SourceFactory)this.sourceFactories.get(protocol); 142 if (sourceFactory != null) { 143 return sourceFactory.getSource(environment, location); 144 } 145 } 146 147 Source result = new URLSource(this.urlFactory.getURL(location), this.manager); 149 if (result instanceof LogEnabled) { 150 ((LogEnabled)result).enableLogging(getLogger()); 151 } 152 return result; 153 } 154 155 158 public Source getSource(Environment environment, URL base, String location) 159 throws ProcessingException, MalformedURLException , IOException { 160 final String protocol = base.getProtocol(); 161 final SourceFactory sourceFactory = (SourceFactory)this.sourceFactories.get(protocol); 162 if (sourceFactory != null) { 163 return sourceFactory.getSource(environment, base, location); 164 } 165 166 return new URLSource(this.urlFactory.getURL(base, location), this.manager); 168 } 169 170 173 public void addFactory(String protocol, SourceFactory factory) 174 throws ProcessingException { 175 try { 176 this.init(factory, null); 177 SourceFactory oldFactory = (SourceFactory)this.sourceFactories.put(protocol, factory); 178 if (oldFactory != null) { 179 deinit(oldFactory); 180 } 181 } catch (ComponentException e) { 182 throw new ProcessingException("cannot initialize factory: " + factory, e); 183 } catch (ContextException e) { 184 throw new ProcessingException("cannot initialize factory: " + factory, e); 185 } catch (ConfigurationException e) { 186 throw new ProcessingException("cannot configure factory: " + factory, e); 187 } 188 } 189 190 193 private void init(SourceFactory factory, Configuration config) 194 throws ContextException, ComponentException, ConfigurationException { 195 if (factory instanceof LogEnabled) { 196 ((LogEnabled) factory).enableLogging(getLogger()); 197 } 198 if (factory instanceof Contextualizable) { 199 ((Contextualizable) factory).contextualize (this.context); 200 } 201 if (factory instanceof Composable) { 202 ((Composable) factory).compose(this.manager); 203 } 204 if (config != null && factory instanceof Configurable) { 205 ((Configurable) factory).configure(config); 206 } 207 } 208 209 212 private void deinit(SourceFactory factory) { 213 if (factory instanceof Disposable) { 214 ((Disposable) factory).dispose(); 215 } 216 } 217 218 } 219 | Popular Tags |