You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by cz...@apache.org on 2002/01/07 09:57:56 UTC

cvs commit: jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source SourceResolverImpl.java

cziegeler    02/01/07 00:57:56

  Modified:    src/scratchpad/org/apache/avalon/excalibur/source
                        SourceResolverImpl.java
  Log:
  Make SourceResolve recyclable instead of threadsafe
  
  Revision  Changes    Path
  1.11      +107 -47   jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source/SourceResolverImpl.java
  
  Index: SourceResolverImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source/SourceResolverImpl.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- SourceResolverImpl.java	27 Dec 2001 15:05:47 -0000	1.10
  +++ SourceResolverImpl.java	7 Jan 2002 08:57:56 -0000	1.11
  @@ -22,7 +22,6 @@
   import org.apache.avalon.framework.context.Contextualizable;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
   import org.apache.avalon.framework.logger.LogEnabled;
  -import org.apache.avalon.framework.thread.ThreadSafe;
   
   import java.io.File;
   import java.io.IOException;
  @@ -46,7 +45,7 @@
    * and releasing them.
    *
    * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
  - * @version $Id: SourceResolverImpl.java,v 1.10 2001/12/27 15:05:47 cziegeler Exp $
  + * @version $Id: SourceResolverImpl.java,v 1.11 2002/01/07 08:57:56 cziegeler Exp $
    */
   public class SourceResolverImpl
   extends AbstractLogEnabled
  @@ -55,7 +54,7 @@
              Contextualizable,
              Disposable,
              SourceResolver,
  -           ThreadSafe
  +           Recyclable
   {
   
       /** The component manager */
  @@ -73,27 +72,37 @@
       protected URL baseURL;
   
       /**
  +     * The user directory
  +     */
  +    protected URL userDirectory;
  +
  +    /**
        * Configure the SourceFactories
        */
       public void configure(final Configuration conf)
  -    throws ConfigurationException {
  +    throws ConfigurationException
  +    {
           // set the base URL to the current directory
  -        try {
  -            this.baseURL = new File(System.getProperty("user.dir")).toURL();
  +        try
  +        {
  +            this.userDirectory = new File(System.getProperty("user.dir")).toURL();
               if ( this.getLogger().isDebugEnabled() )
               {
  -                this.getLogger().debug("SourceResolver: Using base directory: " + this.baseURL);
  +                this.getLogger().debug("SourceResolver: Using base directory: " + this.userDirectory);
               }
  -        } catch (MalformedURLException mue) {
  +        } catch (MalformedURLException mue)
  +        {
               throw new ConfigurationException("Malformed URL for user.dir");
           }
  +        this.baseURL = this.userDirectory;
       }
   
       /**
        * Get the context
        */
       public void contextualize(Context context)
  -    throws ContextException {
  +    throws ContextException
  +    {
           this.context = context;
       }
   
  @@ -102,7 +111,8 @@
        * <code>Composable</code>.
        */
       public void compose(ComponentManager manager)
  -    throws ComponentException {
  +    throws ComponentException
  +    {
           this.manager = manager;
           this.factorySelector = (ComponentSelector)this.manager.lookup(SourceFactory.ROLE + "Selector");
       }
  @@ -110,19 +120,30 @@
       /**
        * Dispose
        */
  -    public void dispose() {
  -        if (this.manager != null) {
  +    public void dispose()
  +    {
  +        if (this.manager != null)
  +        {
               this.manager.release(this.factorySelector);
               this.factorySelector = null;
           }
       }
   
       /**
  +     * Recycle: reset the base dir
  +     */
  +    public void recycle()
  +    {
  +        this.baseURL = this.userDirectory;
  +    }
  +
  +    /**
        * Set the base URL. All relative references are resolved
        * according to this URL.
        */
  -    public void setBaseURL(URL base) {
  -        if (this.getLogger().isDebugEnabled() == true) {
  +    public void setBaseURL(URL base)
  +    {
  +        if ( this.getLogger().isDebugEnabled() ) {
               this.getLogger().debug("Changing baseURL to: " + base);
           }
           this.baseURL = base;
  @@ -131,7 +152,8 @@
       /**
        * Get the base URL
        */
  -    public URL getBaseURL() {
  +    public URL getBaseURL()
  +    {
           return this.baseURL;
       }
   
  @@ -139,7 +161,8 @@
        * Get a <code>Source</code> object.
        */
       public Source resolve(String location)
  -    throws MalformedURLException, IOException, ComponentException {
  +    throws MalformedURLException, IOException, ComponentException
  +    {
           return this.resolve(this.baseURL, location, null);
       }
   
  @@ -148,15 +171,17 @@
        */
       public Source resolve(String location,
                             SourceParameters parameters)
  -    throws MalformedURLException, IOException, ComponentException {
  -        return this.resolve(this.baseURL, location, parameters);
  +    throws MalformedURLException, IOException, ComponentException
  +    {
  +        return this.resolve( this.baseURL, location, parameters );
       }
   
       /**
        * Get a <code>Source</code> object.
        */
       public Source resolve(URL base, String location)
  -    throws MalformedURLException, IOException, ComponentException {
  +    throws MalformedURLException, IOException, ComponentException
  +    {
           return this.resolve(base, location, null);
       }
   
  @@ -167,8 +192,9 @@
       public Source resolve(URL base,
                             String location,
                             SourceParameters parameters)
  -    throws MalformedURLException, IOException, ComponentException {
  -        if (this.getLogger().isDebugEnabled() == true) {
  +    throws MalformedURLException, IOException, ComponentException
  +    {
  +        if ( this.getLogger().isDebugEnabled() ) {
               this.getLogger().debug("Resolving '"+location+"' in context '" + base + "'");
           }
           if (location == null) throw new MalformedURLException("Invalid System ID");
  @@ -179,76 +205,107 @@
   
           if (location.length() == 0) {
               systemID = base.toExternalForm();
  -        } else if (location.indexOf(":") > 1) {
  +        } else if (location.indexOf(":") > 1)
  +        {
               systemID = location;
  -        } else if (location.charAt(0) == '/') {
  +        }
  +        else if (location.charAt(0) == '/')
  +        {
               systemID = new StringBuffer(base.getProtocol())
                              .append(":").append(location).toString();
           // windows: absolute paths can start with drive letter
  -        } else if (location.length() > 1 && location.charAt(1) == ':') {
  +        }
  +        else if (location.length() > 1 && location.charAt(1) == ':')
  +        {
               systemID = new StringBuffer(base.getProtocol())
                              .append(":/").append(location).toString();
  -        } else {
  -            if (base.getProtocol().equals("file") == true) {
  +        }
  +        else
  +        {
  +            if (base.getProtocol().equals("file") == true)
  +            {
                   File temp = new File(base.toExternalForm().substring("file:".length()), location);
                   String path = temp.getAbsolutePath();
                   // windows paths starts with drive letter
  -                if (path.charAt(0) != File.separator.charAt(0)) {
  +                if (path.charAt(0) != File.separator.charAt(0))
  +                {
                       systemID = "file:/" + path;
  -                } else {
  +                }
  +                else
  +                {
                       systemID = "file:" + path;
                   }
  -            } else {
  +            }
  +            else
  +            {
                   systemID = new URL(base, location).toExternalForm();
               }
           }
  -        if (this.getLogger().isDebugEnabled() == true) {
  +        if ( this.getLogger().isDebugEnabled() )
  +        {
               this.getLogger().debug("Resolved to systemID '"+systemID+"'");
           }
   
           Source source = null;
           // search for a SourceFactory implementing the protocol
           final int protocolPos = systemID.indexOf(':');
  -        if ( protocolPos != -1 ) {
  +        if ( protocolPos != -1 )
  +        {
               final String protocol = systemID.substring(0, protocolPos);
  -            if ( this.factorySelector.hasComponent(protocol) ) {
  +            if ( this.factorySelector.hasComponent(protocol) )
  +            {
                   SourceFactory factory = null;
  -                try {
  +                try
  +                {
                       factory = ( SourceFactory )this.factorySelector.select( protocol );
                       source = factory.getSource( systemID, parameters );
  -                } finally {
  +                }
  +                finally
  +                {
                       this.factorySelector.release( factory );
                   }
               }
           }
   
  -        if ( source == null ) {
  +        if ( null == source )
  +        {
               // no factory found, so usual url handling stuff...
  -            try {
  -                if (this.getLogger().isDebugEnabled() == true) {
  +            try
  +            {
  +                if (this.getLogger().isDebugEnabled() == true)
  +                {
                       getLogger().debug("Making URL from " + systemID);
                   }
                   source = new URLSource(new URL(systemID), parameters);
  -            } catch (MalformedURLException mue) {
  -                if (this.getLogger().isDebugEnabled() == true) {
  +            }
  +            catch (MalformedURLException mue)
  +            {
  +                if ( this.getLogger().isDebugEnabled() )
  +                {
                       getLogger().debug("Making URL - MalformedURLException in getURL:" , mue);
                       getLogger().debug("Making URL a File (assuming that it is full path):" + systemID);
                   }
                   source = new URLSource((new File(systemID)).toURL(), parameters);
               }
           }
  -        if (source instanceof LogEnabled) {
  +        if (source instanceof LogEnabled)
  +        {
               ((LogEnabled) source).enableLogging(getLogger());
           }
  -        try {
  -            if (source instanceof Contextualizable) {
  +        try
  +        {
  +            if (source instanceof Contextualizable)
  +            {
                   ((Contextualizable) source).contextualize (this.context);
               }
  -        } catch (ContextException ce) {
  +        }
  +        catch (ContextException ce)
  +        {
               throw new ComponentException("ContextException occured during source resolving.", ce);
           }
   
  -        if (source instanceof Composable) {
  +        if (source instanceof Composable)
  +        {
               ((Composable) source).compose(this.manager);
           }
           return source;
  @@ -257,12 +314,15 @@
       /**
        * Releases a resolved resource
        */
  -    public void release( Source source ) {
  +    public void release( Source source )
  +    {
           if ( source == null) return;
  -        if ( source instanceof Recyclable ) {
  +        if ( source instanceof Recyclable )
  +        {
               ((Recyclable)source).recycle();
           }
  -        if ( source instanceof Disposable ) {
  +        if ( source instanceof Disposable )
  +        {
               ((Disposable) source).dispose();
           }
       }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>