1 16 package org.apache.cocoon.components.source.impl; 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.thread.ThreadSafe; 31 import org.apache.cocoon.components.url.URLFactory; 32 import org.apache.cocoon.util.ClassUtils; 33 import org.apache.excalibur.source.Source; 34 import org.apache.excalibur.source.SourceFactory; 35 import org.apache.excalibur.source.impl.URLSource; 36 37 import java.io.IOException ; 38 import java.net.MalformedURLException ; 39 import java.net.URL ; 40 import java.util.Map ; 41 42 50 51 public final class URLFactoryWrapper 52 extends AbstractLogEnabled 53 implements SourceFactory, 54 ThreadSafe, 55 Configurable, 56 Disposable, 57 Composable, 58 Contextualizable 59 { 60 61 private ComponentManager manager; 62 63 64 private URLFactory urlFactory; 65 66 67 private Context context; 68 69 72 public void configure(final Configuration conf) 73 throws ConfigurationException { 74 75 try { 76 final Configuration factoryConf = conf.getChild("url-factory"); 77 final String className = factoryConf.getAttribute("class"); 78 if (this.getLogger().isDebugEnabled()) { 79 this.getLogger().debug("Getting the URLFactory " + className); 80 } 81 this.urlFactory = (URLFactory)ClassUtils.newInstance(className); 82 this.init(this.urlFactory, factoryConf); 83 } catch (ConfigurationException e) { 84 throw e; 85 } catch (Exception e) { 86 throw new ConfigurationException("Could not get parameters because: " + 87 e.getMessage(), e); 88 } 89 } 90 91 94 public void contextualize(Context context) 95 throws ContextException { 96 this.context = context; 97 } 98 99 103 public void compose(ComponentManager manager) 104 throws ComponentException { 105 this.manager = manager; 106 } 107 108 111 public void dispose() { 112 if (this.urlFactory != null) { 113 this.deinit(this.urlFactory); 114 } 115 this.urlFactory = null; 116 } 117 118 119 123 public Source getSource( String location, Map parameters ) 124 throws MalformedURLException , IOException 125 { 126 if( this.getLogger().isDebugEnabled() ) { 127 this.getLogger().debug( "Creating source object for " + location ); 128 } 129 130 final int protocolPos = location.indexOf("://"); 131 final URL url = this.urlFactory.getURL(location.substring(protocolPos+3)); 132 final URLSource source = new org.apache.excalibur.source.impl.URLSource(); 133 source.init(url, parameters); 134 return source; 135 } 136 137 140 private void init(URLFactory factory, 141 Configuration config) 142 throws ContextException, ComponentException, ConfigurationException { 143 if (factory instanceof LogEnabled) { 144 ((LogEnabled) factory).enableLogging(getLogger()); 145 } 146 if (factory instanceof Contextualizable) { 147 ((Contextualizable) factory).contextualize (this.context); 148 } 149 if (factory instanceof Composable) { 150 ((Composable) factory).compose(this.manager); 151 } 152 if (config != null && factory instanceof Configurable) { 153 ((Configurable) factory).configure(config); 154 } 155 } 156 157 160 private void deinit(URLFactory factory) { 161 if (factory instanceof Disposable) { 162 ((Disposable) factory).dispose(); 163 } 164 } 165 166 169 public void release( Source source ) { 170 if ( null != source ) { 171 if ( this.getLogger().isDebugEnabled() ) { 172 this.getLogger().debug("Releasing source " + source.getURI()); 173 } 174 } 176 } 177 178 } 179 | Popular Tags |