1 16 package org.apache.cocoon.components.source.impl; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import java.util.Map ; 23 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.Constants; 32 import org.apache.cocoon.environment.Context; 33 import org.apache.excalibur.source.Source; 34 import org.apache.excalibur.source.SourceException; 35 import org.apache.excalibur.source.SourceFactory; 36 import org.apache.excalibur.source.SourceResolver; 37 import org.apache.excalibur.source.SourceUtil; 38 import org.apache.excalibur.source.URIAbsolutizer; 39 40 50 public class ContextSourceFactory 51 extends AbstractLogEnabled 52 implements SourceFactory, 53 Serviceable, 54 Contextualizable, 55 ThreadSafe, 56 URIAbsolutizer { 57 58 59 protected Context envContext; 60 61 62 protected ServiceManager manager; 63 64 67 public void service(ServiceManager manager) throws ServiceException { 68 this.manager = manager; 69 } 70 71 74 public void contextualize(org.apache.avalon.framework.context.Context context) 75 throws ContextException { 76 this.envContext = (Context)context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT); 77 } 78 79 82 public Source getSource( String location, Map parameters ) 83 throws SourceException, MalformedURLException , IOException { 84 if( this.getLogger().isDebugEnabled() ) { 85 this.getLogger().debug( "Creating source object for " + location ); 86 } 87 88 SourceResolver resolver = null; 90 try { 91 resolver = (SourceResolver)this.manager.lookup( SourceResolver.ROLE ); 92 93 final int pos = location.indexOf(":/"); 95 final String path = location.substring(pos+1); 96 97 if ( path.indexOf("../") != -1 ) { 99 throw new MalformedURLException ("Invalid path ('../' is not allowed) : " + path); 100 } 101 102 URL u; 103 104 String actualPath = envContext.getRealPath(path); 106 if (actualPath != null) { 107 u = new File (actualPath).toURL(); 108 } else { 109 u = envContext.getResource(path); 110 } 111 112 if (u != null) { 113 return resolver.resolveURI(u.toExternalForm()); 114 115 } else { 116 String message = location + " could not be found. (possible context problem)"; 117 getLogger().info(message); 118 throw new MalformedURLException (message); 119 } 120 } catch (ServiceException se) { 121 throw new SourceException("Unable to lookup source resolver.", se); 122 } finally { 123 this.manager.release( resolver ); 124 } 125 126 } 127 128 131 public void release( Source source ) { 132 if ( null != source ) { 136 if ( this.getLogger().isDebugEnabled() ) { 137 this.getLogger().debug("Releasing source " + source.getURI()); 138 } 139 SourceResolver resolver = null; 140 try { 141 resolver = (SourceResolver)this.manager.lookup( SourceResolver.ROLE ); 142 resolver.release( source ); 143 } catch (ServiceException ingore) { 144 } finally { 145 this.manager.release( resolver ); 146 } 147 } 148 } 149 150 153 public String absolutize(String baseURI, String location) { 154 return SourceUtil.absolutize(baseURI, location, true); 155 } 156 } 157 | Popular Tags |