1 17 package org.apache.excalibur.source.impl; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.util.Map ; 24 25 import org.apache.avalon.framework.CascadingRuntimeException; 26 import org.apache.avalon.framework.activity.Disposable; 27 import org.apache.avalon.framework.context.Context; 28 import org.apache.avalon.framework.context.ContextException; 29 import org.apache.avalon.framework.context.Contextualizable; 30 import org.apache.avalon.framework.logger.AbstractLogEnabled; 31 import org.apache.avalon.framework.service.ServiceException; 32 import org.apache.avalon.framework.service.ServiceManager; 33 import org.apache.avalon.framework.service.ServiceSelector; 34 import org.apache.avalon.framework.service.Serviceable; 35 import org.apache.avalon.framework.thread.ThreadSafe; 36 import org.apache.excalibur.source.*; 37 38 59 public class SourceResolverImpl 60 extends AbstractLogEnabled 61 implements Serviceable, 62 Contextualizable, 63 Disposable, 64 SourceResolver, 65 ThreadSafe 66 { 67 68 protected ServiceManager m_manager; 69 70 71 protected ServiceSelector m_factorySelector; 72 73 76 protected URL m_baseURL; 77 78 81 public void contextualize( Context context ) 82 throws ContextException 83 { 84 try 85 { 86 if( context.get( "context-root" ) instanceof URL ) 87 { 88 m_baseURL = (URL )context.get( "context-root" ); 89 } 90 else 91 { 92 m_baseURL = ( (File )context.get( "context-root" ) ).toURL(); 93 } 94 } 95 catch( ContextException ce ) 96 { 97 try 99 { 100 m_baseURL = new File ( System.getProperty( "user.dir" ) ).toURL(); 101 if( getLogger().isDebugEnabled() ) 102 { 103 getLogger().debug( "SourceResolver: Using base URL: " + m_baseURL ); 104 } 105 } 106 catch( MalformedURLException mue ) 107 { 108 getLogger().warn( "Malformed URL for user.dir, and no container.rootDir exists", mue ); 109 throw new ContextException( "Malformed URL for user.dir, and no container.rootDir exists", mue ); 110 } 111 } 112 catch( MalformedURLException mue ) 113 { 114 getLogger().warn( "Malformed URL for container.rootDir", mue ); 115 throw new ContextException( "Malformed URL for container.rootDir", mue ); 116 } 117 } 118 119 125 public void service( final ServiceManager manager ) 126 throws ServiceException 127 { 128 m_manager = manager; 129 130 if ( m_manager.hasService( SourceFactory.ROLE + "Selector" ) ) 131 { 132 m_factorySelector = (ServiceSelector) m_manager.lookup( SourceFactory.ROLE + "Selector" ); 133 } 134 } 135 136 public void dispose() 137 { 138 if( null != m_manager ) 139 { 140 m_manager.release( m_factorySelector ); 141 m_factorySelector = null; 142 } 143 } 144 145 149 public Source resolveURI( String location ) 150 throws MalformedURLException , IOException , SourceException 151 { 152 return this.resolveURI( location, null, null ); 153 } 154 155 159 public Source resolveURI( String location, 160 String baseURI, 161 Map parameters ) 162 throws MalformedURLException , IOException , SourceException 163 { 164 if( getLogger().isDebugEnabled() ) 165 { 166 getLogger().debug( "Resolving '" + location + "' with base '" + baseURI + "' in context '" + m_baseURL + "'" ); 167 } 168 if( location == null ) throw new MalformedURLException ( "Invalid System ID" ); 169 if( null != baseURI && SourceUtil.indexOfSchemeColon(baseURI) == -1 ) 170 { 171 throw new MalformedURLException ( "BaseURI is not valid, it must contain a protocol: " + baseURI ); 172 } 173 174 if( baseURI == null ) baseURI = m_baseURL.toExternalForm(); 175 176 String systemID = location; 177 if( location.length() > 1 && location.charAt( 1 ) == ':' ) 179 systemID = "file:/" + location; 180 else if( location.length() > 2 && location.charAt(0) == '/' && location.charAt(2) == ':' ) 181 systemID = "file:" + location; 182 183 String protocol; 185 int protocolPos = SourceUtil.indexOfSchemeColon(systemID); 186 if( protocolPos != -1 ) 187 { 188 protocol = systemID.substring( 0, protocolPos ); 189 } 190 else 191 { 192 protocolPos = SourceUtil.indexOfSchemeColon(baseURI); 193 if( protocolPos != -1 ) 194 protocol = baseURI.substring( 0, protocolPos ); 195 else 196 protocol = "*"; 197 } 198 199 Source source = null; 200 SourceFactory factory = null; 202 try 203 { 204 factory = (SourceFactory)m_factorySelector.select( protocol ); 205 systemID = absolutize( factory, baseURI, systemID ); 206 if( getLogger().isDebugEnabled() ) 207 getLogger().debug( "Resolved to systemID : " + systemID ); 208 source = factory.getSource( systemID, parameters ); 209 } 210 catch( final ServiceException ce ) 211 { 212 } 214 finally 215 { 216 m_factorySelector.release( factory ); 217 } 218 219 if( null == source ) 220 { 221 try 222 { 223 factory = (SourceFactory) m_factorySelector.select("*"); 224 systemID = absolutize( factory, baseURI, systemID ); 225 if( getLogger().isDebugEnabled() ) 226 getLogger().debug( "Resolved to systemID : " + systemID ); 227 source = factory.getSource( systemID, parameters ); 228 } 229 catch (ServiceException se ) 230 { 231 throw new SourceException( "Unable to select source factory for " + systemID, se ); 232 } 233 finally 234 { 235 m_factorySelector.release(factory); 236 } 237 } 238 239 return source; 240 } 241 242 245 private String absolutize( SourceFactory factory, String baseURI, String systemID ) 246 { 247 if( factory instanceof URIAbsolutizer ) 248 systemID = ((URIAbsolutizer)factory).absolutize(baseURI, systemID); 249 else 250 systemID = SourceUtil.absolutize(baseURI, systemID); 251 return systemID; 252 } 253 254 258 public void release( final Source source ) 259 { 260 if( source == null ) return; 261 262 final String scheme = source.getScheme(); 264 SourceFactory factory = null; 265 266 try 267 { 268 factory = (SourceFactory) m_factorySelector.select(scheme); 269 factory.release(source); 270 } 271 catch (ServiceException se ) 272 { 273 try 274 { 275 factory = (SourceFactory) m_factorySelector.select("*"); 276 factory.release(source); 277 } 278 catch (ServiceException sse ) 279 { 280 throw new CascadingRuntimeException( "Unable to select source factory for " + source.getURI(), se ); 281 } 282 } 283 finally 284 { 285 m_factorySelector.release( factory ); 286 } 287 } 288 } 289 | Popular Tags |