velocity-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From da...@apache.org
Subject cvs commit: jakarta-velocity/src/java/org/apache/velocity/runtime/resource/loader JarResourceLoader.java
Date Sun, 04 Mar 2001 21:27:08 GMT
daveb       01/03/04 13:27:08

  Modified:    src/java/org/apache/velocity/runtime/resource/loader
                        JarResourceLoader.java
  Log:
  New multi loader.
  
  Revision  Changes    Path
  1.4       +83 -90    jakarta-velocity/src/java/org/apache/velocity/runtime/resource/loader/JarResourceLoader.java
  
  Index: JarResourceLoader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/resource/loader/JarResourceLoader.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- JarResourceLoader.java	2001/03/03 20:33:22	1.3
  +++ JarResourceLoader.java	2001/03/04 21:27:08	1.4
  @@ -54,17 +54,16 @@
    * <http://www.apache.org/>.
    */
   
  -import java.io.File;
   import java.io.InputStream;
  -import java.io.FileInputStream;
  -import java.io.BufferedInputStream;
  -import java.io.FileNotFoundException;
  -import java.io.ByteArrayInputStream;
  +//import java.io.FileInputStream;
  +//import java.io.BufferedInputStream;
  +//import java.io.FileNotFoundException;
  +//import java.io.ByteArrayInputStream;
   import java.io.IOException;
   import java.net.JarURLConnection;
   import java.net.MalformedURLException;
   import java.net.URL;
  -import java.util.ArrayList;
  +//import java.util.ArrayList;
   import java.util.Enumeration;
   import java.util.Iterator;
   import java.util.jar.JarEntry;
  @@ -80,91 +79,95 @@
   
   import org.apache.velocity.exception.ResourceNotFoundException;
   
  -
   /**
  - * Loader to grab templates from a Jar file.
  + * ResourceLoader to load templates from multiple Jar files.
    * 
  - * Adopted from Craig R. McClanahan JarResources.java in Catalina and
  - * Jason Van Zyl's FileResourceLoader.java
    * 
    * @author <a href="mailto:daveb@miceda-data.com">Dave Bryson</a>
  - * $Revision: 1.3 $
  + * $Revision: 1.4 $
    */
   public class JarResourceLoader extends ResourceLoader
   {
       /**
  -     * The URLConnection to our JAR file.
  +     * Maps entries to the parent JAR File
  +     * Key = the entry *excluding* plain directories
  +     * Value = the JAR URL
        */
  -    protected JarURLConnection conn = null;
  -
  +    private Hashtable entryDirectory = new Hashtable(559);
  +    
       /**
  -     * The JarFile object associated with our document base.
  +     * Maps JAR URLs to the actual JAR
  +     * Key = the JAR URL
  +     * Value = the JAR
        */
  -    protected JarFile jarFile = null;
  -    
  -    public void init(Configuration configuration)
  -    {
  -        String path = configuration.getString("resource.path");
  -        setJarUrl( path );
  -        
  -        Runtime.info("Resources Loaded From: " +  path);
  -        Runtime.info("JarResourceLoader Initialized.");
  -    }
  -
  +    private Hashtable jarfiles = new Hashtable(89);
  +   
       /**
  -     * Setup Jar URL 
  +     * Called by Velocity to initialize the loader
        */
  -    private void setJarUrl( String jarPath )
  +    public void init(Configuration configuration)
       {
  -        // Validate the format of the proposed document root
  -        if ( jarPath == null )
  +        Vector paths = configuration.getVector("resource.path");
  +        
  +        for ( int i=0; i<paths.size(); i++ )
           {
  -            throw new IllegalArgumentException( "JarPath is NULL" );
  +            loadJar( (String)paths.elementAt(i) );
           }
  -        if ( !jarPath.startsWith("jar:") )
  +        
  +        Runtime.info("JarResourceLoader initialized...");
  +    }
  +    
  +    private void loadJar( String path )
  +    {
  +        // Check path information
  +        if ( path == null )
           {
  -            throw new IllegalArgumentException( "JarPath must start with - jar: - see java.net.JarURLConnection"
);
  +            Runtime.error("Can not load a JAR - JAR path is null");
           }
  -        if ( !jarPath.endsWith("!/") )
  +        if ( !path.startsWith("jar:") )
           {
  -            jarPath += "!/";
  +            Runtime.error("JAR path must start with jar: -> see java.net.JarURLConnection
for information");
           }
  -        // Close any previous JAR that we have opened
  -        if ( jarFile != null ) 
  +        if ( !path.endsWith("!/") )
           {
  -            try
  -            {
  -                jarFile.close();
  -            }
  -            catch (IOException e)
  -            {
  -                Runtime.error("Error Closing JAR file " +  e);
  -            }
  -            
  -            jarFile = null;
  -            conn = null;
  +            path += "!/";
           }
           
  -        // Open a URLConnection to the specified JAR file
  -        try
  -        {
  -            URL url = new URL( jarPath );
  -            conn = (JarURLConnection) url.openConnection();
  -            conn.setAllowUserInteraction(false);
  -            conn.setDoInput(true);
  -            conn.setDoOutput(false);
  -            conn.connect();
  -            jarFile = conn.getJarFile();
  +        // Close the jar if it's already open
  +        // this is useful for a reload
  +        closeJar( path );
  +        
  +        // Create a new JarHolder
  +        JarHolder temp = new JarHolder( path );
  +        // Add it's entries to the entryCollection
  +        addEntries( temp.getEntries() );
  +        // Add it to the Jar table
  +        jarfiles.put( temp.getUrlPath(), temp );
  +    }
   
  -            //loop through Jar file and cache entries here
  -        } 
  -        catch (Exception e)
  +    /**
  +     * Closes a Jar file and set its URLConnection 
  +     * to null.
  +     */
  +    private void closeJar( String path )
  +    {
  +        if ( jarfiles.containsKey(path) )
           {
  -            Runtime.error("Error establishing connection to JAR "+ e);
  +            JarHolder theJar = (JarHolder)jarfiles.get(path);
  +            theJar.close();
           }
       }
       
       /**
  +     * Copy all the entries into the entryDirectory
  +     * It will overwrite any duplicate keys.
  +     */
  +    private synchronized void addEntries( Hashtable entries )
  +    {
  +        entryDirectory.putAll( entries );
  +    }
  +    
  +    /**
        * Get an InputStream so that the Runtime can build a
        * template with it.
        *
  @@ -173,17 +176,18 @@
        * @throws ResourceNotFoundException if template not found
        *         in the file template path.
        */
  -    public synchronized InputStream getResourceStream( String name )
  +    public synchronized InputStream getResourceStream( String source )
           throws ResourceNotFoundException
       {
  -        InputStream result = null;
  -        
  -        if (name == null || name.length() == 0)
  +        InputStream results = null;
  +
  +        if ( source == null || source.length() == 0)
           {
               throw new ResourceNotFoundException ("Need to a resource!");
           }
           
  -        String normalizedPath = StringUtils.normalizePath(name);
  +        String normalizedPath = StringUtils.normalizePath( source );
  +        
           if ( normalizedPath == null || normalizedPath.length() == 0 )
           {
               String msg = "File resource error : argument " + normalizedPath + 
  @@ -202,35 +206,24 @@
           {
               normalizedPath = normalizedPath.substring(1);
           }
  -        
  -        try 
  +    
  +        if ( entryDirectory.containsKey( normalizedPath ) )
           {
  -            JarEntry entry = jarFile.getJarEntry( normalizedPath );
  -            if (entry == null)
  -            {
  -                Runtime.error("Resource Not Found" );
  -            }
  -            else
  +            String jarurl  = (String)entryDirectory.get( normalizedPath );
  +            
  +            if ( jarfiles.containsKey( jarurl ) )
               {
  -                
  -                result =  jarFile.getInputStream(entry);
  +                JarHolder holder = (JarHolder)jarfiles.get( jarurl );
  +                results =  holder.getResource( normalizedPath );
               }
           }
  -        catch( Exception fnfe )
  -        {
  -            /*
  -             *  log and convert to a general Velocity ResourceNotFoundException
  -             */
  -            
  -            Runtime.error("FileResourceLoader Error : exception : " + fnfe );
  -            throw new ResourceNotFoundException( fnfe.getMessage() );
  -        }
           
  -        return result;
  +        return results;
       }
  -    
  -    
  -    
  +        
  +        
  +    // TO DO BELOW 
  +    // SHOULD BE DELEGATED TO THE JARHOLDER
       public boolean isSourceModified(Resource resource)
       {
           return true;
  
  
  

Mime
View raw message