1 8 package org.apache.avalon.excalibur.source; 9 10 import org.apache.avalon.framework.activity.Disposable; 11 import org.apache.avalon.framework.component.Component; 12 import org.apache.avalon.framework.component.ComponentException; 13 import org.apache.avalon.framework.component.ComponentManager; 14 import org.apache.avalon.framework.component.ComponentSelector; 15 import org.apache.avalon.framework.component.Composable; 16 import org.apache.avalon.framework.configuration.Configurable; 17 import org.apache.avalon.framework.configuration.Configuration; 18 import org.apache.avalon.framework.configuration.ConfigurationException; 19 import org.apache.avalon.framework.context.Context; 20 import org.apache.avalon.framework.context.ContextException; 21 import org.apache.avalon.framework.context.Contextualizable; 22 import org.apache.avalon.framework.logger.AbstractLogEnabled; 23 import org.apache.avalon.framework.logger.LogEnabled; 24 import org.apache.avalon.framework.thread.ThreadSafe; 25 import org.apache.avalon.excalibur.pool.Recyclable; 26 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 32 33 56 public class SourceResolverImpl 57 extends AbstractLogEnabled 58 implements Composable, 59 Contextualizable, 60 Disposable, 61 SourceResolver, 62 ThreadSafe 63 { 64 65 66 protected ComponentManager m_manager; 67 68 69 protected ComponentSelector m_factorySelector; 70 71 72 protected Context m_context; 73 74 77 protected URL m_baseURL; 78 79 82 public void contextualize(Context context) 83 throws ContextException 84 { 85 m_context = context; 86 87 try 88 { 89 m_baseURL = ((File ) m_context.get("container.rootDir")).toURL(); 90 } 91 catch (ContextException ce) 92 { 93 try 95 { 96 m_baseURL = new File (System.getProperty("user.dir")).toURL(); 97 if ( this.getLogger().isDebugEnabled() ) 98 { 99 this.getLogger().debug("SourceResolver: Using base URL: " + m_baseURL); 100 } 101 } 102 catch (MalformedURLException mue) 103 { 104 throw new ContextException("Malformed URL for user.dir, and no container.rootDir exists", mue); 105 } 106 } 107 catch (MalformedURLException mue) 108 { 109 throw new ContextException("Malformed URL for container.rootDir", mue); 110 } 111 } 112 113 117 public void compose(ComponentManager manager) 118 throws ComponentException 119 { 120 m_manager = manager; 121 m_factorySelector = (ComponentSelector)m_manager.lookup(SourceFactory.ROLE + "Selector"); 122 } 123 124 127 public void dispose() 128 { 129 if (m_manager != null) 130 { 131 m_manager.release(m_factorySelector); 132 m_factorySelector = null; 133 } 134 } 135 136 139 public Source resolve(String location) 140 throws MalformedURLException , IOException , ComponentException 141 { 142 return this.resolve(m_baseURL, location, null); 143 } 144 145 148 public Source resolve(String location, 149 SourceParameters parameters) 150 throws MalformedURLException , IOException , ComponentException 151 { 152 return this.resolve( m_baseURL, location, parameters ); 153 } 154 155 158 public Source resolve(URL base, String location) 159 throws MalformedURLException , IOException , ComponentException 160 { 161 return this.resolve(base, location, null); 162 } 163 164 165 168 public Source resolve(URL base, 169 String location, 170 SourceParameters parameters) 171 throws MalformedURLException , IOException , ComponentException 172 { 173 if ( this.getLogger().isDebugEnabled() ) { 174 this.getLogger().debug("Resolving '"+location+"' in context '" + base + "'"); 175 } 176 if (location == null) throw new MalformedURLException ("Invalid System ID"); 177 178 String systemID; 180 if (base == null) base = m_baseURL; 181 182 if (location.length() == 0) { 183 systemID = base.toExternalForm(); 184 } else if (location.indexOf(":") > 1) 185 { 186 systemID = location; 187 } 188 else if (location.charAt(0) == '/') 189 { 190 systemID = new StringBuffer (base.getProtocol()) 191 .append(":").append(location).toString(); 192 } 194 else if (location.length() > 1 && location.charAt(1) == ':') 195 { 196 systemID = new StringBuffer (base.getProtocol()) 197 .append(":/").append(location).toString(); 198 } 199 else 200 { 201 if (base.getProtocol().equals("file") == true) 202 { 203 File temp = new File (base.toExternalForm().substring("file:".length()), location); 204 String path = temp.getAbsolutePath(); 205 if (path.charAt(0) != File.separator.charAt(0)) 207 { 208 systemID = "file:/" + path; 209 } 210 else 211 { 212 systemID = "file:" + path; 213 } 214 } 215 else 216 { 217 systemID = new URL (base, location).toExternalForm(); 218 } 219 } 220 if ( this.getLogger().isDebugEnabled() ) 221 { 222 this.getLogger().debug("Resolved to systemID '"+systemID+"'"); 223 } 224 225 Source source = null; 226 final int protocolPos = systemID.indexOf(':'); 228 if ( protocolPos != -1 ) 229 { 230 final String protocol = systemID.substring(0, protocolPos); 231 if ( m_factorySelector.hasComponent(protocol) ) 232 { 233 SourceFactory factory = null; 234 try 235 { 236 factory = ( SourceFactory )m_factorySelector.select( protocol ); 237 source = factory.getSource( systemID, parameters ); 238 } 239 finally 240 { 241 m_factorySelector.release( factory ); 242 } 243 } 244 } 245 246 if ( null == source ) 247 { 248 try 250 { 251 if (this.getLogger().isDebugEnabled() == true) 252 { 253 getLogger().debug("Making URL from " + systemID); 254 } 255 source = new URLSource(new URL (systemID), parameters); 256 } 257 catch (MalformedURLException mue) 258 { 259 if ( this.getLogger().isDebugEnabled() ) 260 { 261 getLogger().debug("Making URL - MalformedURLException in getURL:" , mue); 262 getLogger().debug("Making URL a File (assuming that it is full path):" + systemID); 263 } 264 source = new URLSource((new File (systemID)).toURL(), parameters); 265 } 266 } 267 if (source instanceof LogEnabled) 268 { 269 ((LogEnabled) source).enableLogging(getLogger()); 270 } 271 try 272 { 273 if (source instanceof Contextualizable) 274 { 275 ((Contextualizable) source).contextualize (m_context); 276 } 277 } 278 catch (ContextException ce) 279 { 280 throw new ComponentException("ContextException occured during source resolving.", ce); 281 } 282 283 if (source instanceof Composable) 284 { 285 ((Composable) source).compose(m_manager); 286 } 287 return source; 288 } 289 290 293 public void release( Source source ) 294 { 295 if ( source == null) return; 296 if ( source instanceof Recyclable ) 297 { 298 ((Recyclable)source).recycle(); 299 } 300 if ( source instanceof Disposable ) 301 { 302 ((Disposable) source).dispose(); 303 } 304 } 305 306 } 307 | Popular Tags |