1 16 package org.apache.cocoon.components.source.impl; 17 18 import java.io.IOException ; 19 import java.net.MalformedURLException ; 20 import java.util.Map ; 21 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.logger.AbstractLogEnabled; 26 import org.apache.avalon.framework.service.ServiceException; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.avalon.framework.service.Serviceable; 29 import org.apache.avalon.framework.thread.ThreadSafe; 30 import org.apache.cocoon.components.source.SourceDescriptor; 31 import org.apache.excalibur.source.ModifiableTraversableSource; 32 import org.apache.excalibur.source.Source; 33 import org.apache.excalibur.source.SourceException; 34 import org.apache.excalibur.source.SourceFactory; 35 import org.apache.excalibur.source.SourceResolver; 36 37 40 public class RepositorySourceFactory extends AbstractLogEnabled 41 implements SourceFactory, Serviceable, Configurable, ThreadSafe { 42 43 private ServiceManager m_manager; 44 private SourceResolver m_resolver; 45 private SourceDescriptor m_descriptor; 46 private String m_name; 47 private boolean m_isInitialized; 48 49 public RepositorySourceFactory() { 50 } 51 52 private synchronized void lazyInitialize() throws IOException { 53 if (m_isInitialized) { 54 return; 55 } 56 if (m_resolver == null) { 57 try { 58 m_resolver = (SourceResolver) m_manager.lookup(SourceResolver.ROLE); 59 } 60 catch (ServiceException e) { 61 throw new IOException ("Resolver service is not available: " + e.toString()); 62 } 63 } 64 if (m_manager.hasService(SourceDescriptor.ROLE)) { 65 try { 66 m_descriptor = (SourceDescriptor) m_manager.lookup(SourceDescriptor.ROLE); 67 } 68 catch (ServiceException e) { 69 } 71 } 72 else { 73 m_descriptor = null; 74 if (getLogger().isInfoEnabled()) { 75 final String message = 76 "SourceDescriptor is not available. " + 77 "RepositorySource will not support " + 78 "source properties."; 79 getLogger().info(message); 80 } 81 } 82 } 83 84 87 public void configure(final Configuration configuration) throws ConfigurationException { 88 m_name = configuration.getAttribute("name"); 89 } 90 91 94 public void service(final ServiceManager manager) { 95 m_manager = manager; 96 } 97 98 public Source getSource(String location, Map parameters) 99 throws IOException , MalformedURLException { 100 101 if (getLogger().isDebugEnabled()) { 102 getLogger().debug("Creating RepositorySource for " + location); 103 } 104 105 if (!m_isInitialized) { 107 lazyInitialize(); 108 } 109 110 location = location.substring(m_name.length()+1); 112 Source source = m_resolver.resolveURI(location); 113 if (!(source instanceof ModifiableTraversableSource)) { 114 final String message = "Delegate should be a ModifiableTraversableSource"; 115 throw new SourceException(message); 116 } 117 118 return new RepositorySource( 119 m_name, 120 (ModifiableTraversableSource) source, 121 m_descriptor, 122 getLogger() 123 ); 124 } 125 126 public void release(final Source source) { 127 if (source instanceof RepositorySource) { 128 m_resolver.release(((RepositorySource) source).m_delegate); 129 } 130 } 131 132 } 133 | Popular Tags |