1 16 17 package org.apache.cocoon.components.source.impl; 18 19 import java.io.IOException ; 20 import java.net.MalformedURLException ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.context.Context; 24 import org.apache.avalon.framework.context.ContextException; 25 import org.apache.avalon.framework.context.Contextualizable; 26 import org.apache.avalon.framework.logger.AbstractLogEnabled; 27 import org.apache.avalon.framework.service.ServiceException; 28 import org.apache.avalon.framework.service.ServiceManager; 29 import org.apache.avalon.framework.service.Serviceable; 30 import org.apache.avalon.framework.thread.ThreadSafe; 31 import org.apache.cocoon.components.slide.SlideRepository; 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.SourceParameters; 36 import org.apache.excalibur.source.SourceUtil; 37 import org.apache.slide.common.NamespaceAccessToken; 38 39 44 public class SlideSourceFactory extends AbstractLogEnabled 45 implements SourceFactory, Contextualizable, Serviceable, ThreadSafe { 46 47 private ServiceManager m_manager; 48 private SlideRepository m_repository; 49 private Context m_context; 50 51 public SlideSourceFactory() { 52 } 53 54 59 public void contextualize(Context context) throws ContextException { 60 m_context = context; 61 } 62 63 68 public void service(ServiceManager manager) throws ServiceException { 69 m_manager = manager; 70 } 71 72 73 81 public Source getSource(String location, Map parameters) 82 throws MalformedURLException , IOException , SourceException { 83 84 if ( m_repository == null ) { 85 try { 86 m_repository = (SlideRepository) m_manager.lookup(SlideRepository.ROLE); 87 } catch (ServiceException se) { 88 throw new SourceException("Unable to lookup repository.", se); 89 } 90 } 91 92 if (getLogger().isDebugEnabled()) { 93 getLogger().debug("Creating source object for " + location); 94 } 95 96 final String [] parts = SourceUtil.parseUrl(location); 97 final String scheme = parts[SourceUtil.SCHEME]; 98 final String authority = parts[SourceUtil.AUTHORITY]; 99 final String query = parts[SourceUtil.QUERY]; 100 String path = parts[SourceUtil.PATH]; 101 102 String principal; 103 String namespace; 104 105 int index = authority.indexOf('@'); 107 if (index == -1) { 108 principal = "guest"; 109 namespace = authority; 110 } 111 else { 112 principal = authority.substring(0,index); 113 namespace = authority.substring(index+1); 114 } 115 116 if (path == null || path.length() == 0) { 117 path = "/"; 118 } 119 120 NamespaceAccessToken nat = m_repository.getNamespaceToken(namespace); 121 if (nat == null) { 122 throw new SourceException("No such namespace: " + namespace); 123 } 124 125 SourceParameters queryParameters = null; 126 127 if (query == null || query.length() == 0) { 128 queryParameters = new SourceParameters(); 129 } else { 130 queryParameters = new SourceParameters(query); 131 } 132 133 String version = queryParameters.getParameter("version",null); 134 String scope = queryParameters.getParameter("scope", 135 nat.getNamespaceConfig().getFilesPath()); 136 137 if (getLogger().isDebugEnabled()) { 138 getLogger().debug("scheme: " + scheme); 139 getLogger().debug("principal: " + principal); 140 getLogger().debug("namespace: " + namespace); 141 getLogger().debug("path: " + path); 142 getLogger().debug("version: " + version); 143 getLogger().debug("scope: " + scope); 144 } 145 146 SlideSource source = new SlideSource(nat,scheme,scope,path,principal,version); 147 148 source.enableLogging(getLogger()); 149 source.contextualize(m_context); 150 source.service(m_manager); 151 source.initialize(); 152 153 return source; 154 } 155 156 161 public void release(Source source) { 162 if (null!=source) { 163 if (getLogger().isDebugEnabled()) { 164 getLogger().debug("Releasing source "+source.getURI()); 165 } 166 } 167 } 168 169 } 170 171 | Popular Tags |