1 16 package org.apache.cocoon.components.source.impl; 17 18 import org.apache.avalon.excalibur.pool.Recyclable; 19 import org.apache.avalon.framework.activity.Disposable; 20 import org.apache.avalon.framework.component.ComponentException; 21 import org.apache.avalon.framework.component.ComponentManager; 22 import org.apache.avalon.framework.component.Composable; 23 import org.apache.avalon.framework.configuration.Configurable; 24 import org.apache.avalon.framework.configuration.Configuration; 25 import org.apache.avalon.framework.configuration.ConfigurationException; 26 import org.apache.avalon.framework.context.Context; 27 import org.apache.avalon.framework.context.ContextException; 28 import org.apache.avalon.framework.context.Contextualizable; 29 import org.apache.avalon.framework.logger.AbstractLogEnabled; 30 import org.apache.avalon.framework.logger.LogEnabled; 31 import org.apache.avalon.framework.thread.ThreadSafe; 32 import org.apache.cocoon.CascadingIOException; 33 import org.apache.cocoon.ProcessingException; 34 import org.apache.cocoon.components.CocoonComponentManager; 35 import org.apache.cocoon.environment.Environment; 36 import org.apache.cocoon.util.ClassUtils; 37 import org.apache.excalibur.source.Source; 38 import org.apache.excalibur.source.SourceFactory; 39 40 import java.io.IOException ; 41 import java.net.MalformedURLException ; 42 import java.util.Map ; 43 44 52 public final class SourceFactoryWrapper 53 extends AbstractLogEnabled 54 implements SourceFactory, 55 ThreadSafe, 56 Configurable, 57 Disposable, 58 Composable, 59 Contextualizable 60 { 61 62 private ComponentManager manager; 63 64 65 private org.apache.cocoon.components.source.SourceFactory sourceFactory; 66 67 68 private Context context; 69 70 73 public void configure(final Configuration conf) 74 throws ConfigurationException { 75 76 try { 77 final Configuration factoryConf = conf.getChild("source-factory"); 78 final String className = factoryConf.getAttribute("class"); 79 if (this.getLogger().isDebugEnabled()) { 80 this.getLogger().debug("Getting the SourceFactory " + className); 81 } 82 this.sourceFactory = (org.apache.cocoon.components.source.SourceFactory) 83 ClassUtils.newInstance(className); 84 this.init(this.sourceFactory, factoryConf); 85 } catch (ConfigurationException e) { 86 throw e; 87 } catch (Exception e) { 88 throw new ConfigurationException("Could not get parameters because: " + 89 e.getMessage(), e); 90 } 91 } 92 93 96 public void contextualize(Context context) 97 throws ContextException { 98 this.context = context; 99 } 100 101 105 public void compose(ComponentManager manager) 106 throws ComponentException { 107 this.manager = manager; 108 } 109 110 113 public void dispose() { 114 if (this.sourceFactory != null) { 115 this.deinit(this.sourceFactory); 116 } 117 this.sourceFactory = null; 118 } 119 120 121 125 public Source getSource( String location, Map parameters ) 126 throws MalformedURLException , IOException 127 { 128 if( getLogger().isDebugEnabled() ) 129 { 130 getLogger().debug( "Creating source object for " + location ); 131 } 132 133 final Environment currentEnv = CocoonComponentManager.getCurrentEnvironment(); 134 org.apache.cocoon.environment.Source source; 135 try { 136 source = this.sourceFactory.getSource(currentEnv, location); 137 } catch (ProcessingException pe) { 138 throw new CascadingIOException("ProcessingException: " + pe.getMessage(), pe); 139 } 140 return new CocoonToAvalonSource( location, source ); 141 } 142 143 146 private void init(org.apache.cocoon.components.source.SourceFactory factory, 147 Configuration config) 148 throws ContextException, ComponentException, ConfigurationException { 149 if (factory instanceof LogEnabled) { 150 ((LogEnabled) factory).enableLogging(getLogger()); 151 } 152 if (factory instanceof Contextualizable) { 153 ((Contextualizable) factory).contextualize (this.context); 154 } 155 if (factory instanceof Composable) { 156 ((Composable) factory).compose(this.manager); 157 } 158 if (config != null && factory instanceof Configurable) { 159 ((Configurable) factory).configure(config); 160 } 161 } 162 163 166 private void deinit(org.apache.cocoon.components.source.SourceFactory factory) { 167 if (factory instanceof Disposable) { 168 ((Disposable) factory).dispose(); 169 } 170 } 171 172 175 public void release( Source source ) { 176 if ( null != source ) { 177 if ( this.getLogger().isDebugEnabled() ) { 178 this.getLogger().debug("Releasing source " + source.getURI()); 179 } 180 ((Recyclable)source).recycle(); 181 } 182 } 183 184 185 } 186 | Popular Tags |