You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by mc...@apache.org on 2003/03/06 10:11:35 UTC

cvs commit: avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/engine DefaultRepositoryManager.java

mcconnell    2003/03/06 01:11:35

  Modified:    assembly/src/java/org/apache/avalon/assembly/engine
                        DefaultRepositoryManager.java
  Log:
  Addition of extended protocol support for file scanning.
  
  Revision  Changes    Path
  1.14      +231 -121  avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/engine/DefaultRepositoryManager.java
  
  Index: DefaultRepositoryManager.java
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/engine/DefaultRepositoryManager.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- DefaultRepositoryManager.java	27 Feb 2003 23:07:37 -0000	1.13
  +++ DefaultRepositoryManager.java	6 Mar 2003 09:11:34 -0000	1.14
  @@ -58,7 +58,9 @@
   import java.io.File;
   import java.io.IOException;
   import java.io.FileFilter;
  +import java.io.InputStream;
   import java.net.JarURLConnection;
  +import java.util.jar.JarInputStream;
   import java.net.URL;
   import java.net.URLClassLoader;
   import java.net.MalformedURLException;
  @@ -172,7 +174,8 @@
       *        profile managers created by this repository
       * @param parent the parent repository
       */
  -    DefaultRepositoryManager( ClassLoader classloader, RepositoryManager parent, File home )
  +    DefaultRepositoryManager( 
  +      ClassLoader classloader, RepositoryManager parent, File home )
       {
           if( classloader == null )
           {
  @@ -271,60 +274,73 @@
               return;
           }
   
  -        if( getLogger().isDebugEnabled() )
  +        if( isFile( url ) )
           {
  -            getLogger().debug("scanning: " + url );
  -        }
  +            if( getLogger().isDebugEnabled() )
  +            {
  +                getLogger().debug("scanning: " + url );
  +            }
   
  -        try
  -        {
               //
               // we are dealing with a jar file so we need to scan the classes
               // for service and type defintions
               //
   
  -            URL resource = getJarURL( url );
  -            if( resource.toString().endsWith( "!/" ) )
  +            try
               {
  -
  -                final JarURLConnection jar = (JarURLConnection)resource.openConnection();
  -                final JarFile base = jar.getJarFile();
  -                m_scanned.add( url );
  -                load( base );
  -            }
  -            else if( url.toString().indexOf( "!/" ) > 0 )
  -            {
  -                //
  -                // this is a jar file reference container in a jar URL therefor
  -                // we need to fall back toi the filesystem home and check for the 
  -                // existance of the file in question
  -                //
  -   
  -                String path = url.toString();
  -                String filename = path.substring( path.indexOf( "!/" ) + 2 );
  -                getLogger().debug( "### " + filename );
  -                final String error =
  -                  "Not ready yet with URL: " + url;
  -                throw new EngineRuntimeException( error );
  +                URL resource = getJarURL( url );
  +                if( resource.toString().endsWith( "!/" ) )
  +                {
  +                    final JarURLConnection jar = (JarURLConnection)resource.openConnection();
  +                    final JarFile base = jar.getJarFile();
  +                    m_scanned.add( url );
  +                    load( base );
  +                    return;
  +                }
  +                else
  +                {
  +                    final String error =
  +                      "Embeded jar file loading not supported at this time: " + url;
  +                    throw new EngineRuntimeException( error );
  +                }
               }
  -            else
  +            catch( EngineRuntimeException e )
               {
  -                final String error =
  -                  "Cannot resolve the supplied URL: " + url;
  +                throw e; 
  +            }
  +            catch( Throwable e )
  +            {
  +                final String error = 
  +                  "Unexpected error while handling a file url: " + url;
                   throw new EngineRuntimeException( error );
               }
           }
  -        catch( IOException e )
  +
  +        if( getLogger().isDebugEnabled() )
           {
  -            final String error =
  -                "IO exception while attempt to read url: "
  -                + url;
  -            throw new EngineRuntimeException( error, e );
  +            getLogger().debug("scanning: " + url );
  +        }
  +
  +        try
  +        {
  +            Object object = url.getContent();
  +            if( object instanceof InputStream )
  +            {
  +                JarInputStream stream = new JarInputStream( (InputStream) object );
  +                loadJarInputStream( stream );
  +                return;
  +            }
  +            else
  +            {
  +                getLogger().warn("unrecognized content type [" 
  +                 + object.getClass() + "] in url: " + url );
  +                return;
  +            }
           }
           catch( Throwable e )
           {
  -            final String error = "Unexpected exception while resolving URL: ";
  -            throw new EngineRuntimeException( error + url, e );
  +            getLogger().warn("content error related to url: " + url, e );
  +            return;
           }
       }
   
  @@ -342,47 +358,170 @@
   
       private void load( File base, File dir ) throws IOException
       {
  +        List types = new ArrayList();
  +        load( types, base, dir );
  +
  +        Type[] created = (Type[]) types.toArray( new Type[0] );
  +        for( int i=0; i<created.length; i++ )
  +        {
  +            Type type = created[i];
  +            try
  +            {
  +                Profile[] profiles = m_profiles.loadProfiles( type );
  +                m_profiles.addProfiles( profiles );
  +            }
  +            catch( Throwable e )
  +            {
  +                final String warning = 
  +                  "Profile registration error from type: " + type.getInfo().getClassname();
  +                getLogger().warn( warning, e );
  +            }
  +        }
  +    }
  +
  +    private void load( List types, File base, File dir ) throws IOException
  +    {
           File[] files = dir.listFiles();
           String path = base.toString();
           int j = path.length();
  -        getLogger().debug( "scanning: " + path );
           for( int i=0; i<files.length; i++ )
           {
               File file = files[i];
               if( file.isDirectory() )
               {
  -                load( dir, file );
  +               getLogger().debug( "scanning dir: " + file );
  +               load( types, base, file );
               }
  -            String filename = file.toString();
  -            String name = filename.substring( j, filename.length() );
  -            if( name.endsWith( X_TYPE ) )
  +            else
               {
  -                getLogger().debug( "parsing: " + name );
  -                try
  +                String filename = file.toString();
  +                String name = filename.substring( j, filename.length() );
  +                if( name.endsWith( X_TYPE ) )
                   {
  -                    Type type = processType( name );
  -
  -                    //
  -                    // ## code incomplete - need to add type to m_types
  -                    // and process the type - this needs some refactoring of the 
  -                    // load( jar ) method so we do not duplicate code.
  -                    // 
  +                    addType( types, name );
  +                }
  +                else if( name.endsWith( X_INFO ) )
  +                {
  +                    addType( types, name );
                   }
  -                catch( Throwable e )
  +                else if( name.endsWith( X_SERVICE ) )
                   {
  -                    final String warning = 
  -                      "Removing resource in path " + path + " due to a parse error: " + name;
  -                    getLogger().warn( warning, e );
  +                    addService( name );
                   }
               }
           }
       }
   
  -    private void load( JarFile base ) throws IOException
  +    private void addType( List types, String name )
       {
  +        String classname = parseResourceName( name );
  +        try
  +        {
  +            Type type = m_types.createType( classname );
  +            m_types.addType( type );
  +            types.add( type );
  +        }
  +        catch( Throwable e )
  +        {
  +            final String error = 
  +            "Could not create type from classname: " + classname;
  +            if( getLogger().isWarnEnabled() )
  +            {
  +                final String warning = 
  +                  ExceptionHelper.packException( error, e, false );
  +                getLogger().warn( warning );
  +            }
  +        }
  +    }
  +
  +    private void addService( String name )
  +    {
  +        String classname = parseResourceName( name );
  +        try
  +        {
  +            Service service = m_services.createService( classname );
  +            m_services.addService( service );
  +        }
  +        catch( Throwable e )
  +        {
  +            final String error = 
  +            "Could not create service defintion from classname: " + classname;
  +            if( getLogger().isWarnEnabled() )
  +            {
  +                final String warning = 
  +                  ExceptionHelper.packException( error, e, false );
  +                getLogger().warn( warning );
  +            }
  +        }
  +    }
  +
  +    private void loadJarInputStream( JarInputStream base ) throws IOException
  +    {
  + 
  +        ZipEntry entry = null;
  +        try
  +        {
  +            entry = base.getNextEntry();
  +        }
  +        catch( Throwable e )
  +        {
  +            entry = null;
  +        }
   
           List list = new ArrayList();
           List types = new ArrayList();
  +        while( entry != null )
  +        {
  +            String name = entry.getName();
  +
  +            if( name.endsWith( X_TYPE ) )
  +            {
  +                String classname = parseResourceName( name );
  +                addType( types, name );
  +            }
  +            else if( name.endsWith( X_INFO ) )
  +            {
  +                addType( types, name );
  +            }
  +
  +            if( name.endsWith( X_SERVICE ) )
  +            {
  +                addService( name );
  +            }
  +
  +            try
  +            {
  +                entry = base.getNextEntry();
  +            }
  +            catch( Throwable e )
  +            {
  +                entry = null;
  +            }
  +        }
  +
  +        Type[] created = (Type[]) types.toArray( new Type[0] );
  +        for( int i=0; i<created.length; i++ )
  +        {
  +            Type type = created[i];
  +            try
  +            {
  +                Profile[] profiles = m_profiles.loadProfiles( type );
  +                m_profiles.addProfiles( profiles );
  +            }
  +            catch( Throwable e )
  +            {
  +                final String warning = 
  +                  "Profile registration error from type: " + type.getInfo().getClassname();
  +                getLogger().warn( warning, e );
  +            }
  +        }
  +    }
  +
  +
  +    private void load( JarFile base ) throws IOException
  +    {
  +        List list = new ArrayList();
  +        List types = new ArrayList();
           Enumeration entries = base.entries();
           while( entries.hasMoreElements() )
           {
  @@ -392,63 +531,16 @@
               if( name.endsWith( X_TYPE ) )
               {
                   String classname = parseResourceName( name );
  -                try
  -                {
  -                    Type type = m_types.createType( classname );
  -                    m_types.addType( type );
  -                    types.add( type );
  -                }
  -                catch( Throwable e )
  -                {
  -                    final String error = 
  -                      "Could not create type from classname: " + classname 
  -                      + " in the jar: " + base.getName();
  -                    if( getLogger().isWarnEnabled() )
  -                    {
  -                        final String warning = 
  -                          ExceptionHelper.packException( error, e, false );
  -                        getLogger().warn( warning );
  -                    }
  -                }
  +                addType( types, name );
               }
               else if( name.endsWith( X_INFO ) )
               {
  -                String classname = parseResourceName( name );
  -                try
  -                {
  -                    Type type = m_types.createType( classname );
  -                    m_types.addType( type );
  -                    types.add( type );
  -                }
  -                catch( Throwable e )
  -                {
  -                    final String error = 
  -                      "Could not create type from classname: " + classname 
  -                      + " in the jar: " + base.getName();
  -                    if( getLogger().isWarnEnabled() )
  -                    {
  -                        final String warning = 
  -                          ExceptionHelper.packException( error, e, false );
  -                        getLogger().warn( warning );
  -                    }
  -                }
  +                addType( types, name );
               }
   
               if( name.endsWith( X_SERVICE ) )
               {
  -                String classname = parseResourceName( name );
  -                try
  -                {
  -                    Service service = m_services.createService( classname );
  -                    m_services.addService( service );
  -                }
  -                catch( Throwable e )
  -                {
  -                    final String error = 
  -                      "Could not create service from classname: " + classname 
  -                      + " in the jar: " + base.getName();
  -                    throw new EngineRuntimeException( error, e );
  -                }
  +                addService( name );
               }
           }
   
  @@ -482,35 +574,53 @@
   
       private boolean isDirectory( URL url )
       {
  -        if( url.getProtocol().equals( "file" ) )
  +        if( isFile( url ) )
           {
  -            File file = new File( url.toString().substring( 5 ) );
  -            return file.isDirectory();
  +            return getFile( url ).isDirectory();
           }
           return false;
       }
   
  +    private boolean isFile( URL url )
  +    {
  +        return ( url.getProtocol().equals( "file" ) );
  +    }
  +
       private File getDirectory( URL url ) throws IllegalArgumentException
       {
  +        File file = getFile( url );
  +        if( file.isDirectory() )
  +        {
  +            return file;
  +        }
  +        throw new IllegalArgumentException( 
  +          "URL does not refer to a directory: " + url );
  +    }
  +
  +    private File getFile( URL url ) throws IllegalArgumentException
  +    {
           if( url.getProtocol().equals( "file" ) )
           {
  -            File file = new File( url.toString().substring( 5 ) );
  -            if( file.isDirectory() )
  -            {
  -                return file;
  -            }
  -            throw new IllegalArgumentException( 
  -              "URL does not refer to a directory: " + url );
  +            return new File( url.toString().substring( 5 ) );
           }
           throw new IllegalArgumentException( 
  -          "URL protocol does not match the required file protocol: " + url );
  +          "URL protocol does not match the required file: protocol: " + url );
       }
   
  +
       private String parseResourceName( String resource )
       {
           int i = resource.lastIndexOf(".");
           String name = resource.substring( 0, i );
  -        return name.replace( '/', '.' );
  +        String name2 = name.replace( '/', '.' );
  +        String name3 = name2.replace( '\\', '.' );
  +        if( name3.startsWith( "." ) )
  +        {
  +            return name3.substring( 1, name3.length() );
  +        }
  +        else
  +        {
  +            return name3;
  +        }
       }
  -
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org