tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject cvs commit: jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources BaseDirContext.java FileDirContext.java ProxyDirContext.java Resource.java ResourceAttributes.java
Date Thu, 04 Oct 2001 19:29:13 GMT
remm        01/10/04 12:29:13

  Modified:    catalina/src/share/org/apache/naming/resources Tag:
                        tomcat_40_branch BaseDirContext.java
                        FileDirContext.java ProxyDirContext.java
                        Resource.java ResourceAttributes.java
  Log:
  - Merge a variety of fixes and small feature additions which have been made
    and debugged in the HEAD branch, including:
    - Removal of Jasper loader (merged with the shared loader).
    - Fixes a lot of spec complaince issue regarding the commit state of the
      response when using forwards, sendError or sendRedirect.
    - Fixes cache consistency issues for static resource serving.
    - Adds content caching for static resources (that's linked to the bugfix
      mentioned just above).
    - Merge enhanced error reporting and error page dispatching (which will be
      able to display error pages for most errors returned from the pipeline -
      like the 401 and 403 returned by the authenticator).
    - Merge the fixes for all the other more recent bugfixes that have been
      fixed in the HEAD branch.
    - Passes all tester and Watchdog tests.
    - Merge a variety of small enhancements to the build scripts which have been
      made in the HEAD branch.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.1   +25 -3     jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/BaseDirContext.java
  
  Index: BaseDirContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/BaseDirContext.java,v
  retrieving revision 1.1
  retrieving revision 1.1.2.1
  diff -u -r1.1 -r1.1.2.1
  --- BaseDirContext.java	2001/01/23 03:41:28	1.1
  +++ BaseDirContext.java	2001/10/04 19:29:12	1.1.2.1
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/BaseDirContext.java,v 1.1 2001/01/23 03:41:28 remm Exp $
  - * $Revision: 1.1 $
  - * $Date: 2001/01/23 03:41:28 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/BaseDirContext.java,v 1.1.2.1 2001/10/04 19:29:12 remm Exp $
  + * $Revision: 1.1.2.1 $
  + * $Date: 2001/10/04 19:29:12 $
    *
    * ====================================================================
    *
  @@ -85,7 +85,7 @@
    * Directory Context implementation helper class.
    *
    * @author Remy Maucherat
  - * @version $Revision: 1.1 $ $Date: 2001/01/23 03:41:28 $
  + * @version $Revision: 1.1.2.1 $ $Date: 2001/10/04 19:29:12 $
    */
   
   public abstract class BaseDirContext implements DirContext {
  @@ -158,6 +158,12 @@
       protected int cacheTTL = 5000; // 5s
   
   
  +    /**
  +     * Max size of resources which will have their content cached.
  +     */
  +    protected int cacheObjectMaxSize = 32768; // 32 KB
  +
  +
       // ------------------------------------------------------------- Properties
   
   
  @@ -239,6 +245,22 @@
        */
       public int getCacheTTL() {
           return cacheTTL;
  +    }
  +
  +
  +    /**
  +     * Set cacheObjectMaxSize.
  +     */
  +    public void setCacheObjectMaxSize(int cacheObjectMaxSize) {
  +        this.cacheObjectMaxSize = cacheObjectMaxSize;
  +    }
  +
  +
  +    /**
  +     * Get cacheObjectMaxSize.
  +     */
  +    public int getCacheObjectMaxSize() {
  +        return cacheObjectMaxSize;
       }
   
   
  
  
  
  1.7.2.1   +155 -15   jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/FileDirContext.java
  
  Index: FileDirContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/FileDirContext.java,v
  retrieving revision 1.7
  retrieving revision 1.7.2.1
  diff -u -r1.7 -r1.7.2.1
  --- FileDirContext.java	2001/08/16 00:57:18	1.7
  +++ FileDirContext.java	2001/10/04 19:29:12	1.7.2.1
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/FileDirContext.java,v 1.7 2001/08/16 00:57:18 craigmcc Exp $
  - * $Revision: 1.7 $
  - * $Date: 2001/08/16 00:57:18 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/FileDirContext.java,v 1.7.2.1 2001/10/04 19:29:12 remm Exp $
  + * $Revision: 1.7.2.1 $
  + * $Date: 2001/10/04 19:29:12 $
    *
    * ====================================================================
    *
  @@ -99,7 +99,7 @@
    * Filesystem Directory Context implementation helper class.
    *
    * @author Remy Maucherat
  - * @version $Revision: 1.7 $ $Date: 2001/08/16 00:57:18 $
  + * @version $Revision: 1.7.2.1 $ $Date: 2001/10/04 19:29:12 $
    */
   
   public class FileDirContext extends BaseDirContext {
  @@ -450,16 +450,7 @@
               throw new NamingException
                   (sm.getString("resources.notFound", name));
   
  -        // Return all the attributes all the time
  -        // FIXME
  -        ResourceAttributes attrs = new ResourceAttributes();
  -        attrs.setCreationDate(new Date(file.lastModified()));
  -        attrs.setName(file.getName());
  -        if (!file.isDirectory())
  -            attrs.setResourceType("");
  -        attrs.setContentLength(file.length());
  -        attrs.setLastModified(new Date(file.lastModified()));
  -        return attrs;
  +        return new FileResourceAttributes(file);
   
       }
   
  @@ -944,19 +935,168 @@
           // --------------------------------------------------- Member Variables
           
           
  +        /**
  +         * Associated file object.
  +         */
           protected File file;
           
           
           /**
  +         * File length.
  +         */
  +        protected long length = -1L;
  +        
  +        
  +        // --------------------------------------------------- Resource Methods
  +        
  +        
  +        /**
            * Content accessor.
            * 
            * @return InputStream
            */
           public InputStream streamContent()
               throws IOException {
  -            if (binaryContent == null)
  +            if (binaryContent == null) {
                   inputStream = new FileInputStream(file);
  +            }
               return super.streamContent();
  +        }
  +        
  +        
  +    }
  +
  +
  +    // ------------------------------------- FileResourceAttributes Inner Class
  +
  +
  +    /**
  +     * This specialized resource attribute implementation does some lazy 
  +     * reading (to speed up simple checks, like checking the last modified 
  +     * date).
  +     */
  +    protected class FileResourceAttributes extends ResourceAttributes {
  +
  +
  +        // -------------------------------------------------------- Constructor
  +
  +
  +        public FileResourceAttributes(File file) {
  +            this.file = file;
  +        }
  +        
  +        // --------------------------------------------------- Member Variables
  +        
  +        
  +        protected File file;
  +        
  +        
  +        protected boolean accessed = false;
  +        
  +        
  +        // ----------------------------------------- ResourceAttributes Methods
  +        
  +        
  +        /**
  +         * Is collection.
  +         */
  +        public boolean isCollection() {
  +            if (!accessed) {
  +                collection = file.isDirectory();
  +                accessed = true;
  +            }
  +            return super.isCollection();
  +        }
  +        
  +        
  +        /**
  +         * Get content length.
  +         * 
  +         * @return content length value
  +         */
  +        public long getContentLength() {
  +            if (contentLength != -1L)
  +                return contentLength;
  +            contentLength = file.length();
  +            return contentLength;
  +        }
  +        
  +        
  +        /**
  +         * Get creation time.
  +         * 
  +         * @return creation time value
  +         */
  +        public long getCreation() {
  +            if (creation != -1L)
  +                return creation;
  +            creation = file.lastModified();
  +            return creation;
  +        }
  +        
  +        
  +        /**
  +         * Get creation date.
  +         * 
  +         * @return Creation date value
  +         */
  +        public Date getCreationDate() {
  +            if (creation == -1L) {
  +                creation = file.lastModified();
  +            }
  +            return super.getCreationDate();
  +        }
  +        
  +        
  +        /**
  +         * Get last modified time.
  +         * 
  +         * @return lastModified time value
  +         */
  +        public long getLastModified() {
  +            if (lastModified != -1L)
  +                return lastModified;
  +            lastModified = file.lastModified();
  +            return lastModified;
  +        }
  +        
  +        
  +        /**
  +         * Get lastModified date.
  +         * 
  +         * @return LastModified date value
  +         */
  +        public Date getLastModifiedDate() {
  +            if (lastModified == -1L) {
  +                lastModified = file.lastModified();
  +            }
  +            return super.getLastModifiedDate();
  +        }
  +        
  +        
  +        /**
  +         * Get name.
  +         * 
  +         * @return Name value
  +         */
  +        public String getName() {
  +            if (name == null)
  +                name = file.getName();
  +            return name;
  +        }
  +        
  +        
  +        /**
  +         * Get resource type.
  +         * 
  +         * @return String resource type
  +         */
  +        public String getResourceType() {
  +            if (!accessed) {
  +                collection = file.isDirectory();
  +                accessed = true;
  +            }
  +            return super.getResourceType();
           }
           
           
  
  
  
  1.6.2.1   +59 -16    jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/ProxyDirContext.java
  
  Index: ProxyDirContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/ProxyDirContext.java,v
  retrieving revision 1.6
  retrieving revision 1.6.2.1
  diff -u -r1.6 -r1.6.2.1
  --- ProxyDirContext.java	2001/07/26 00:16:08	1.6
  +++ ProxyDirContext.java	2001/10/04 19:29:12	1.6.2.1
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/ProxyDirContext.java,v 1.6 2001/07/26 00:16:08 remm Exp $
  - * $Revision: 1.6 $
  - * $Date: 2001/07/26 00:16:08 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/ProxyDirContext.java,v 1.6.2.1 2001/10/04 19:29:12 remm Exp $
  + * $Revision: 1.6.2.1 $
  + * $Date: 2001/10/04 19:29:12 $
    *
    * ====================================================================
    *
  @@ -67,6 +67,7 @@
   import java.util.Hashtable;
   import java.util.Date;
   import java.io.InputStream;
  +import java.io.IOException;
   import java.io.ByteArrayInputStream;
   import javax.naming.Context;
   import javax.naming.Name;
  @@ -84,7 +85,7 @@
    * Proxy Directory Context implementation.
    *
    * @author Remy Maucherat
  - * @version $Revision: 1.6 $ $Date: 2001/07/26 00:16:08 $
  + * @version $Revision: 1.6.2.1 $ $Date: 2001/10/04 19:29:12 $
    */
   
   public class ProxyDirContext implements DirContext {
  @@ -112,6 +113,8 @@
               if (((BaseDirContext) dirContext).isCached()) {
                   cache = new Hashtable();
                   cacheTTL = ((BaseDirContext) dirContext).getCacheTTL();
  +                cacheObjectMaxSize = 
  +                    ((BaseDirContext) dirContext).getCacheObjectMaxSize();
               }
           }
           hostName = (String) env.get(HOST);
  @@ -165,6 +168,12 @@
       protected int cacheTTL = 5000; // 5s
   
   
  +    /**
  +     * Max size of resources which will have their content cached.
  +     */
  +    protected int cacheObjectMaxSize = 32768; // 32 KB
  +
  +
       // --------------------------------------------------------- Public Methods
   
   
  @@ -213,6 +222,8 @@
           CacheEntry entry = cacheLookup(name.toString());
           if (entry != null) {
               if (entry.resource != null) {
  +                // Check content caching.
  +                
                   return entry.resource;
               } else {
                   return entry.context;
  @@ -1350,8 +1361,9 @@
        * Validate entry.
        */
       protected boolean validate(CacheEntry entry) {
  -        long currentTime = System.currentTimeMillis();
  -        if (currentTime < entry.timestamp) {
  +        if ((entry.resource != null) 
  +            && (entry.resource.getContent() != null) 
  +            && (System.currentTimeMillis() < entry.timestamp)) {
               return true;
           }
           return false;
  @@ -1366,11 +1378,9 @@
           // modification date
           if (entry.attributes == null)
               return false;
  -        Date lastModificationDate = entry.attributes.getLastModified();
  -        if ((lastModificationDate == null) 
  -            || (lastModificationDate.getTime() <= 0))
  +        long lastModified = entry.attributes.getLastModified();
  +        if (lastModified <= 0)
               return false;
  -        long lastModified = lastModificationDate.getTime();
           try {
               Attributes tempAttributes = dirContext.getAttributes(entry.name);
               ResourceAttributes attributes = null;
  @@ -1379,10 +1389,8 @@
               } else {
                   attributes = (ResourceAttributes) tempAttributes;
               }
  -            lastModificationDate = attributes.getLastModified();
  -            if (lastModificationDate == null)
  -                return false;
  -            return (lastModified == lastModificationDate.getTime());
  +            long lastModified2 = attributes.getLastModified();
  +            return (lastModified == lastModified2);
           } catch (NamingException e) {
               return false;
           }
  @@ -1393,10 +1401,14 @@
        * Load entry into cache.
        */
       protected boolean cacheLoad(CacheEntry entry) {
  +
           if (cache == null)
               return false;
  +
           String name = entry.name;
  +
           // Retrieve missing info
  +
           // Retrieving attributes
           if (entry.attributes == null) {
               try {
  @@ -1411,6 +1423,7 @@
                   return false;
               }
           }
  +
           // Retriving object
           if ((entry.resource == null) && (entry.context == null)) {
               try {
  @@ -1429,14 +1442,44 @@
                   return false;
               }
           }
  -        // Load content
  -        // TODO
  +
  +        // Load object content
  +        if ((entry.resource != null) && (entry.resource.getContent() == null) 
  +            && (entry.attributes.getContentLength() >= 0)
  +            && (entry.attributes.getContentLength() < cacheObjectMaxSize)) {
  +            int length = (int) entry.attributes.getContentLength();
  +            InputStream is = null;
  +            try {
  +                is = entry.resource.streamContent();
  +                int pos = 0;
  +                byte[] b = new byte[length];
  +                while (pos < length) {
  +                    int n = is.read(b, pos, length - pos);
  +                    if (n < 0)
  +                        break;
  +                    pos = pos + n;
  +                }
  +                entry.resource.setContent(b);
  +            } catch (IOException e) {
  +                ; // Ignore
  +            } finally {
  +                try {
  +                    if (is != null)
  +                        is.close();
  +                } catch (IOException e) {
  +                    ; // Ignore
  +                }
  +            }
  +        }
           
           // Set timestamp
           entry.timestamp = System.currentTimeMillis() + cacheTTL;
  +
           // Add new entry to cache
           cache.put(name, entry);
  +
           return true;
  +
       }
   
   
  
  
  
  1.1.2.1   +5 -5      jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/Resource.java
  
  Index: Resource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/Resource.java,v
  retrieving revision 1.1
  retrieving revision 1.1.2.1
  diff -u -r1.1 -r1.1.2.1
  --- Resource.java	2001/01/23 03:41:29	1.1
  +++ Resource.java	2001/10/04 19:29:12	1.1.2.1
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/Resource.java,v 1.1 2001/01/23 03:41:29 remm Exp $
  - * $Revision: 1.1 $
  - * $Date: 2001/01/23 03:41:29 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/Resource.java,v 1.1.2.1 2001/10/04 19:29:12 remm Exp $
  + * $Revision: 1.1.2.1 $
  + * $Date: 2001/10/04 19:29:12 $
    *
    * ====================================================================
    *
  @@ -71,7 +71,7 @@
    * Encapsultes the contents of a resource.
    * 
    * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
  - * @version $Revision: 1.1 $
  + * @version $Revision: 1.1.2.1 $
    */
   public class Resource {
       
  @@ -118,8 +118,9 @@
        */
       public InputStream streamContent()
           throws IOException {
  -        if (binaryContent != null)
  +        if (binaryContent != null) {
               return new ByteArrayInputStream(binaryContent);
  +        }
           return inputStream;
       }
       
  @@ -129,8 +130,7 @@
        * 
        * @return binary content
        */
  -    public byte[] getContent()
  -        throws IOException {
  +    public byte[] getContent() {
           return binaryContent;
       }
       
  
  
  
  1.3.2.1   +481 -250  jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/ResourceAttributes.java
  
  Index: ResourceAttributes.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/ResourceAttributes.java,v
  retrieving revision 1.3
  retrieving revision 1.3.2.1
  diff -u -r1.3 -r1.3.2.1
  --- ResourceAttributes.java	2001/09/13 16:31:42	1.3
  +++ ResourceAttributes.java	2001/10/04 19:29:12	1.3.2.1
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/ResourceAttributes.java,v 1.3 2001/09/13 16:31:42 remm Exp $
  - * $Revision: 1.3 $
  - * $Date: 2001/09/13 16:31:42 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/ResourceAttributes.java,v 1.3.2.1 2001/10/04 19:29:12 remm Exp $
  + * $Revision: 1.3.2.1 $
  + * $Date: 2001/10/04 19:29:12 $
    *
    * ====================================================================
    *
  @@ -83,9 +83,9 @@
    * Attributes implementation.
    * 
    * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
  - * @version $Revision: 1.3 $
  + * @version $Revision: 1.3.2.1 $
    */
  -public class ResourceAttributes extends BasicAttributes {
  +public class ResourceAttributes implements Attributes {
       
       
       // -------------------------------------------------------------- Constants
  @@ -102,7 +102,7 @@
       /**
        * Creation date.
        */
  -    public static final String ALTERNATE_CREATION_DATE = "last-modified";
  +    public static final String ALTERNATE_CREATION_DATE = "creation-date";
       
       
       /**
  @@ -202,7 +202,6 @@
        * Default constructor.
        */
       public ResourceAttributes() {
  -        super(false);
       }
       
       
  @@ -210,300 +209,448 @@
        * Merges with another attribute set.
        */
       public ResourceAttributes(Attributes attributes) {
  -        super(false);
  -        // Merging attributes
  -        try {
  -            Enumeration enum = attributes.getAll();
  -            while (enum.hasMoreElements()) {
  -                Attribute attribute = (Attribute) enum.nextElement();
  -                // FIXME: Check if it's a protected attribute ?
  -                put(attribute);
  -            }
  -        } catch (Throwable t) {
  -        }
  +        this.attributes = attributes;
       }
       
       
  +    // ----------------------------------------------------- Instance Variables
  +
  +
       /**
  -     * Package private constructor (used for caching).
  +     * Collection flag.
        */
  -    ResourceAttributes(Hashtable attributes) {
  -        super(false);
  -        this.protectedAttributes = attributes;
  -    }
  -    
  -    
  -    // ----------------------------------------------------- Instance Variables
  +    protected boolean collection = false;
   
   
       /**
  -     * Protected attributes.
  +     * Content length.
        */
  -    protected Hashtable protectedAttributes = new Hashtable();
  +    protected long contentLength = -1;
   
   
  -    // ------------------------------------------------------------- Properties
  +    /**
  +     * Creation time.
  +     */
  +    protected long creation = -1;
   
   
  -    // --------------------------------------------------------- Public Methods
  +    /**
  +     * Creation date.
  +     */
  +    protected Date creationDate = null;
   
   
       /**
  -     * Creation date accessor.
  -     * 
  -     * @return Creation date
  +     * Last modified time.
        */
  -    public Date getCreationDate() {
  -        Attribute creationDate = getProtectedAttribute(CREATION_DATE);
  -        if (creationDate == null)
  -            creationDate = getProtectedAttribute(ALTERNATE_CREATION_DATE);
  -        if (creationDate == null)
  -            return null;
  -        try {
  -            if (creationDate.get() instanceof Date) {
  -                return (Date) creationDate.get();
  -            } else {
  -                String creationDateValue = creationDate.get().toString();
  -                Date result = null;
  -                // Parsing the HTTP Date
  -                for (int i = 0; (result == null) && 
  -                         (i < formats.length); i++) {
  -                    try {
  -                        result = formats[i].parse(creationDateValue);
  -                    } catch (ParseException e) {
  -                        ;
  -                    }
  -                }
  -                return result;
  -            }
  -        } catch (NamingException e) {
  -            // No value for the attribute (shouldn't happen)
  -            return null;
  +    protected long lastModified = -1;
  +
  +
  +    /**
  +     * Last modified date.
  +     */
  +    protected Date lastModifiedDate = null;
  +
  +
  +    /**
  +     * Name.
  +     */
  +    protected String name = null;
  +
  +
  +    /**
  +     * External attributes.
  +     */
  +    protected Attributes attributes = null;
  +
  +
  +    // ------------------------------------------------------------- Properties
  +
  +
  +    /**
  +     * Is collection.
  +     */
  +    public boolean isCollection() {
  +        if (attributes != null) {
  +            return (getResourceType().equals(COLLECTION_TYPE));
  +        } else {
  +            return (collection);
           }
       }
       
       
       /**
  -     * Last modified date accessor.
  -     * 
  -     * @return Last modification date
  -     */
  -    public Date getLastModified() {
  -        Attribute lastModified = getProtectedAttribute(LAST_MODIFIED);
  -        if (lastModified == null)
  -            lastModified = getProtectedAttribute(ALTERNATE_LAST_MODIFIED);
  -        if (lastModified == null)
  -            return null;
  -        try {
  -            if (lastModified.get() instanceof Date) {
  -                return (Date) lastModified.get();
  -            } else {
  -                String lastModifiedValue = lastModified.get().toString();
  -                Date result = null;
  -                // Parsing the HTTP Date
  -                for (int i = 0; (result == null) && 
  -                         (i < formats.length); i++) {
  -                    try {
  -                        result = formats[i].parse(lastModifiedValue);
  -                    } catch (ParseException e) {
  -                        ;
  -                    }
  -                }
  -                return result;
  -            }
  -        } catch (NamingException e) {
  -            // No value for the attribute (shouldn't happen)
  -            return null;
  +     * Set collection flag.
  +     * 
  +     * @return value of the collection flag
  +     */
  +    public void setCollection(boolean collection) {
  +        this.collection = collection;
  +        if (attributes != null) {
  +            String value = "";
  +            if (collection)
  +                value = COLLECTION_TYPE;
  +            attributes.put(TYPE, value);
           }
       }
       
       
       /**
  -     * Content length accessor.
  +     * Get content length.
        * 
  -     * @return String
  +     * @return content length value
        */
       public long getContentLength() {
  -        Attribute contentLength = getProtectedAttribute(CONTENT_LENGTH);
  -        if (contentLength == null)
  -            contentLength = getProtectedAttribute(ALTERNATE_CONTENT_LENGTH);
  -        if (contentLength == null)
  -            return -1L;
  -        try {
  -            if (contentLength.get() instanceof Long) {
  -                return ((Long) contentLength.get()).longValue();
  -            }
  -            if (contentLength.get() instanceof String) {
  -                return (new Long((String) contentLength.get())).longValue();
  +        if (contentLength != -1L)
  +            return contentLength;
  +        if (attributes != null) {
  +            Attribute attribute = attributes.get(CONTENT_LENGTH);
  +            if (attribute != null) {
  +                try {
  +                    Object value = attribute.get();
  +                    if (value instanceof Long) {
  +                        contentLength = ((Long) value).longValue();
  +                    } else {
  +                        try {
  +                            contentLength = Long.parseLong(value.toString());
  +                        } catch (NumberFormatException e) {
  +                            ; // Ignore
  +                        }
  +                    }
  +                } catch (NamingException e) {
  +                    ; // No value for the attribute
  +                }
               }
  -        } catch (NamingException e) {
           }
  -        return -1L;
  -        
  +        return contentLength;
       }
       
       
       /**
  -     * Is collection.
  +     * Set content length.
  +     * 
  +     * @param contentLength New content length value
        */
  -    public boolean isCollection() {
  -        return (getResourceType().equals(COLLECTION_TYPE));
  +    public void setContentLength(long contentLength) {
  +        this.contentLength = contentLength;
  +        if (attributes != null)
  +            attributes.put(CONTENT_LENGTH, new Long(contentLength));
       }
       
       
       /**
  -     * Name accessor.
  -     * 
  -     * @return String name 
  -     */
  -    public String getName() {
  -        Attribute name = getProtectedAttribute(NAME);
  -        try {
  -            if (name == null) {
  -                return "";
  -            } else {
  -                return (String) name.get();
  +     * Get creation time.
  +     * 
  +     * @return creation time value
  +     */
  +    public long getCreation() {
  +        if (creation != -1L)
  +            return creation;
  +        if (creationDate != null)
  +            return creationDate.getTime();
  +        if (attributes != null) {
  +            Attribute attribute = attributes.get(CREATION_DATE);
  +            if (attribute != null) {
  +                try {
  +                    Object value = attribute.get();
  +                    if (value instanceof Long) {
  +                        creation = ((Long) value).longValue();
  +                    } else if (value instanceof Date) {
  +                        creation = ((Date) value).getTime();
  +                        creationDate = (Date) value;
  +                    } else {
  +                        String creationDateValue = value.toString();
  +                        Date result = null;
  +                        // Parsing the HTTP Date
  +                        for (int i = 0; (result == null) && 
  +                                 (i < formats.length); i++) {
  +                            try {
  +                                result = formats[i].parse(creationDateValue);
  +                            } catch (ParseException e) {
  +                                ;
  +                            }
  +                        }
  +                        if (result != null) {
  +                            creation = result.getTime();
  +                            creationDate = result;
  +                        }
  +                    }
  +                } catch (NamingException e) {
  +                    ; // No value for the attribute
  +                }
               }
  -        } catch (NamingException e) {
  -            return "";
           }
  +        return creation;
       }
  -
  -
  +    
  +    
       /**
  -     * Resource type accessor.
  +     * Set creation.
        * 
  -     * @return String resource type
  +     * @param creation New creation value
        */
  -    public String getResourceType() {
  -        Attribute resourceType = getProtectedAttribute(TYPE);
  -        if (resourceType == null)
  -            resourceType = getProtectedAttribute(ALTERNATE_TYPE);
  -        if (resourceType == null)
  -            return "";
  -        try {
  -            return (String) resourceType.get();
  -        } catch (NamingException e) {
  -            return "";
  -        }
  +    public void setCreation(long creation) {
  +        this.creation = creation;
  +        this.creationDate = null;
  +        if (attributes != null)
  +            attributes.put(CREATION_DATE, new Date(creation));
       }
  -
  -
  +    
  +    
       /**
  -     * Get attribute.
  +     * Get creation date.
  +     * 
  +     * @return Creation date value
        */
  -    public Attribute get(String attrID) {
  -        // Try in the protected list first
  -        Attribute result = getProtectedAttribute(attrID);
  -        if (result != null)
  -            return result;
  -        else
  -            return super.get(attrID);
  +    public Date getCreationDate() {
  +        if (creationDate != null)
  +            return creationDate;
  +        if (creation != -1L) {
  +            creationDate = new Date(creation);
  +            return creationDate;
  +        }
  +        if (attributes != null) {
  +            Attribute attribute = attributes.get(CREATION_DATE);
  +            if (attribute != null) {
  +                try {
  +                    Object value = attribute.get();
  +                    if (value instanceof Long) {
  +                        creation = ((Long) value).longValue();
  +                        creationDate = new Date(creation);
  +                    } else if (value instanceof Date) {
  +                        creation = ((Date) value).getTime();
  +                        creationDate = (Date) value;
  +                    } else {
  +                        String creationDateValue = value.toString();
  +                        Date result = null;
  +                        // Parsing the HTTP Date
  +                        for (int i = 0; (result == null) && 
  +                                 (i < formats.length); i++) {
  +                            try {
  +                                result = formats[i].parse(creationDateValue);
  +                            } catch (ParseException e) {
  +                                ;
  +                            }
  +                        }
  +                        if (result != null) {
  +                            creation = result.getTime();
  +                            creationDate = result;
  +                        }
  +                    }
  +                } catch (NamingException e) {
  +                    ; // No value for the attribute
  +                }
  +            }
  +        }
  +        return creationDate;
       }
       
       
       /**
  -     * Get all attributes.
  +     * Creation date mutator.
  +     * 
  +     * @param creationDate New creation date
        */
  -    public NamingEnumeration getAll() {
  -        Enumeration enum = super.getAll();
  -        Vector attributes = new Vector();
  -        while (enum.hasMoreElements()) {
  -            Object obj = enum.nextElement();
  -            attributes.addElement(obj);
  -        }
  -        enum = protectedAttributes.elements();
  -        while (enum.hasMoreElements()) {
  -            Object obj = enum.nextElement();
  -            attributes.addElement(obj);
  -        }
  -        return new RecyclableNamingEnumeration(attributes);
  +    public void setCreationDate(Date creationDate) {
  +        this.creation = creationDate.getTime();
  +        this.creationDate = creationDate;
  +        if (attributes != null)
  +            attributes.put(CREATION_DATE, creationDate);
       }
       
       
       /**
  -     * Get all attribute IDs.
  -     */
  -    public NamingEnumeration getIDs() {
  -        Enumeration enum = super.getIDs();
  -        Vector attributeIDs = new Vector();
  -        while (enum.hasMoreElements()) {
  -            Object obj = enum.nextElement();
  -            attributeIDs.addElement(obj);
  -        }
  -        enum = protectedAttributes.elements();
  -        while (enum.hasMoreElements()) {
  -            Object obj = ((Attribute) enum.nextElement()).getID();
  -            attributeIDs.addElement(obj);
  +     * Get last modified time.
  +     * 
  +     * @return lastModified time value
  +     */
  +    public long getLastModified() {
  +        if (lastModified != -1L)
  +            return lastModified;
  +        if (lastModifiedDate != null)
  +            return lastModifiedDate.getTime();
  +        if (attributes != null) {
  +            Attribute attribute = attributes.get(LAST_MODIFIED);
  +            if (attribute != null) {
  +                try {
  +                    Object value = attribute.get();
  +                    if (value instanceof Long) {
  +                        lastModified = ((Long) value).longValue();
  +                    } else if (value instanceof Date) {
  +                        lastModified = ((Date) value).getTime();
  +                        lastModifiedDate = (Date) value;
  +                    } else {
  +                        String lastModifiedDateValue = value.toString();
  +                        Date result = null;
  +                        // Parsing the HTTP Date
  +                        for (int i = 0; (result == null) && 
  +                                 (i < formats.length); i++) {
  +                            try {
  +                                result = 
  +                                    formats[i].parse(lastModifiedDateValue);
  +                            } catch (ParseException e) {
  +                                ;
  +                            }
  +                        }
  +                        if (result != null) {
  +                            lastModified = result.getTime();
  +                            lastModifiedDate = result;
  +                        }
  +                    }
  +                } catch (NamingException e) {
  +                    ; // No value for the attribute
  +                }
  +            }
           }
  -        return new RecyclableNamingEnumeration(attributeIDs);
  +        return lastModified;
       }
       
       
       /**
  -     * Retrieves the number of attributes in the attribute set.
  +     * Set last modified.
  +     * 
  +     * @param lastModified New last modified value
        */
  -    public int size() {
  -        return (protectedAttributes.size() + super.size());
  +    public void setLastModified(long lastModified) {
  +        this.lastModified = lastModified;
  +        this.lastModifiedDate = null;
  +        if (attributes != null)
  +            attributes.put(LAST_MODIFIED, new Date(lastModified));
       }
       
       
       /**
  -     * Generates the string representation of this attribute set.
  -     * The string consists of each attribute identifier and the contents
  -     * of each attribute. The contents of this string is useful
  -     * for debugging and is not meant to be interpreted programmatically.
  +     * Set last modified date.
        * 
  -     * @return A non-null string listing the contents of this attribute set.
  +     * @param lastModified New last modified date value
  +     * @deprecated
        */
  -    public String toString() {
  -        String result = protectedAttributes.toString();
  -        if (super.size() > 0)
  -            result += super.toString();
  -        return result;
  +    public void setLastModified(Date lastModified) {
  +        setLastModifiedDate(lastModified);
       }
  -    
  -    
  -    // -------------------------------------------------------- Package Methods
   
   
       /**
  -     * Creation date mutator.
  +     * Get lastModified date.
        * 
  -     * @param creationDate New creation date
  +     * @return LastModified date value
        */
  -    public void setCreationDate(Date creationDate) {
  -        setProtectedAttribute(CREATION_DATE, creationDate);
  +    public Date getLastModifiedDate() {
  +        if (lastModifiedDate != null)
  +            return lastModifiedDate;
  +        if (lastModified != -1L) {
  +            lastModifiedDate = new Date(lastModified);
  +            return lastModifiedDate;
  +        }
  +        if (attributes != null) {
  +            Attribute attribute = attributes.get(LAST_MODIFIED);
  +            if (attribute != null) {
  +                try {
  +                    Object value = attribute.get();
  +                    if (value instanceof Long) {
  +                        lastModified = ((Long) value).longValue();
  +                        lastModifiedDate = new Date(lastModified);
  +                    } else if (value instanceof Date) {
  +                        lastModified = ((Date) value).getTime();
  +                        lastModifiedDate = (Date) value;
  +                    } else {
  +                        String lastModifiedDateValue = value.toString();
  +                        Date result = null;
  +                        // Parsing the HTTP Date
  +                        for (int i = 0; (result == null) && 
  +                                 (i < formats.length); i++) {
  +                            try {
  +                                result = 
  +                                    formats[i].parse(lastModifiedDateValue);
  +                            } catch (ParseException e) {
  +                                ;
  +                            }
  +                        }
  +                        if (result != null) {
  +                            lastModified = result.getTime();
  +                            lastModifiedDate = result;
  +                        }
  +                    }
  +                } catch (NamingException e) {
  +                    ; // No value for the attribute
  +                }
  +            }
  +        }
  +        return lastModifiedDate;
       }
       
       
       /**
  -     * Last modified mutator.
  +     * Last modified date mutator.
        * 
  -     * @param lastModified New last modified date
  +     * @param lastModifiedDate New last modified date
        */
  -    public void setLastModified(Date lastModified) {
  -        setProtectedAttribute(LAST_MODIFIED, lastModified);
  +    public void setLastModifiedDate(Date lastModifiedDate) {
  +        this.lastModified = lastModifiedDate.getTime();
  +        this.lastModifiedDate = lastModifiedDate;
  +        if (attributes != null)
  +            attributes.put(LAST_MODIFIED, lastModifiedDate);
       }
       
       
       /**
  -     * Content length mutator.
  +     * Get name.
        * 
  -     * @param contentLength New content length
  +     * @return Name value
        */
  -    public void setContentLength(long contentLength) {
  -        setProtectedAttribute(CONTENT_LENGTH, new Long(contentLength));
  +    public String getName() {
  +        if (name != null)
  +            return name;
  +        if (attributes != null) {
  +            Attribute attribute = attributes.get(NAME);
  +            if (attribute != null) {
  +                try {
  +                    name = attribute.get().toString();
  +                } catch (NamingException e) {
  +                    ; // No value for the attribute
  +                }
  +            }
  +        }
  +        return name;
  +    }
  +
  +
  +    /**
  +     * Set name.
  +     * 
  +     * @param name New name value
  +     */
  +    public void setName(String name) {
  +        this.name = name;
  +        if (attributes != null)
  +            attributes.put(NAME, name);
       }
       
       
       /**
  -     * Name mutator.
  +     * Get resource type.
        * 
  -     * @param name New name
  +     * @return String resource type
        */
  -    public void setName(String name) {
  -        setProtectedAttribute(NAME, name);
  +    public String getResourceType() {
  +        String result = null;
  +        if (attributes != null) {
  +            Attribute attribute = attributes.get(TYPE);
  +            if (attribute != null) {
  +                try {
  +                    result = attribute.get().toString();
  +                } catch (NamingException e) {
  +                    ; // No value for the attribute
  +                }
  +            }
  +        }
  +        if (result == null) {
  +            if (collection)
  +                result = COLLECTION_TYPE;
  +            else
  +                result = "";
  +        }
  +        return result;
       }
       
       
  @@ -513,73 +660,157 @@
        * @param resourceType New resource type
        */
       public void setResourceType(String resourceType) {
  -        setProtectedAttribute(TYPE, resourceType);
  +        collection = resourceType.equals(COLLECTION_TYPE);
  +        if (attributes != null)
  +            attributes.put(TYPE, resourceType);
       }
       
       
  +    // ----------------------------------------------------- Attributes Methods
  +
  +
       /**
  -     * Get all the attributes in a Hashtable. Will mutate the 
  -     * protectedAttributes hashtable.
  +     * Get attribute.
        */
  -    Hashtable getAttributes() {
  -        Enumeration enum = super.getAll();
  -        while (enum.hasMoreElements()) {
  -            Attribute attribute = (Attribute) enum.nextElement();
  -            String id = attribute.getID();
  -            if (!protectedAttributes.containsKey(id)) {
  -                protectedAttributes.put(id, attribute);
  +    public Attribute get(String attrID) {
  +        if (attributes == null) {
  +            if (attrID.equals(CREATION_DATE)) {
  +                return new BasicAttribute(CREATION_DATE, getCreationDate());
  +            } else if (attrID.equals(ALTERNATE_CREATION_DATE)) {
  +                return new BasicAttribute(ALTERNATE_CREATION_DATE, 
  +                                          getCreationDate());
  +            } else if (attrID.equals(LAST_MODIFIED)) {
  +                return new BasicAttribute(LAST_MODIFIED, 
  +                                          getLastModifiedDate());
  +            } else if (attrID.equals(ALTERNATE_LAST_MODIFIED)) {
  +                return new BasicAttribute(ALTERNATE_LAST_MODIFIED,
  +                                          getLastModifiedDate());
  +            } else if (attrID.equals(NAME)) {
  +                return new BasicAttribute(NAME, getName());
  +            } else if (attrID.equals(TYPE)) {
  +                return new BasicAttribute(TYPE, getResourceType());
  +            } else if (attrID.equals(ALTERNATE_TYPE)) {
  +                return new BasicAttribute(ALTERNATE_TYPE, getResourceType());
  +            } else if (attrID.equals(CONTENT_LENGTH)) {
  +                return new BasicAttribute(CONTENT_LENGTH, 
  +                                          new Long(getContentLength()));
  +            } else if (attrID.equals(ALTERNATE_CONTENT_LENGTH)) {
  +                return new BasicAttribute(ALTERNATE_CONTENT_LENGTH, 
  +                                          new Long(getContentLength()));
               }
  +        } else {
  +            return attributes.get(attrID);
           }
  -        return protectedAttributes;
  +        return null;
       }
       
       
  -    // ------------------------------------------------------ Protected Methods
  -
  -
       /**
  -     * Protected attribute mutator.
  -     * 
  -     * @param name Attribute name
  -     * @param value Attribute value
  +     * Put attribute.
        */
  -    protected void setProtectedAttribute(String name, Object value) {
  -        Attribute attribute = new BasicAttribute(name, value);
  -        protectedAttributes.put(name, attribute);
  +    public Attribute put(Attribute attribute) {
  +        if (attributes == null) {
  +            try {
  +                return put(attribute.getID(), attribute.get());
  +            } catch (NamingException e) {
  +                return null;
  +            }
  +        } else {
  +            return attributes.put(attribute);
  +        }
       }
       
       
       /**
  -     * Protected attribute accessor.
  -     * 
  -     * @param name Attribute name
  +     * Put attribute.
        */
  -    protected Attribute getProtectedAttribute(String name) {
  -        Attribute result = null;
  -        if (name.startsWith(Constants.DEFAULT_NAMESPACE)) {
  -            result = (Attribute) protectedAttributes.get
  -                (name.substring(Constants.DEFAULT_NAMESPACE.length()));
  +    public Attribute put(String attrID, Object val) {
  +        if (attributes == null) {
  +            return null; // No reason to implement this
           } else {
  -            result = (Attribute) protectedAttributes.get(name);
  +            return attributes.put(attrID, val);
           }
  -        if (result != null)
  -            return result;
  -        return super.get(name);
       }
       
       
       /**
  -     * Initialize the set of protected attributes.
  +     * Remove attribute.
        */
  -    protected void intializeAttributes() {
  -        setCreationDate(new Date());
  -        setProtectedAttribute(NAME, "");
  -        // By default, a resource is a collection
  -        setProtectedAttribute(TYPE, "<collection/>");
  -        setProtectedAttribute(SOURCE, "");
  -        setContentLength(-1);
  -        setLastModified(new Date());
  +    public Attribute remove(String attrID) {
  +        if (attributes == null) {
  +            return null; // No reason to implement this
  +        } else {
  +            return attributes.remove(attrID);
  +        }
       }
  -
  -
  +    
  +    
  +    /**
  +     * Get all attributes.
  +     */
  +    public NamingEnumeration getAll() {
  +        if (attributes == null) {
  +            Vector attributes = new Vector();
  +            attributes.addElement(new BasicAttribute
  +                                  (CREATION_DATE, getCreationDate()));
  +            attributes.addElement(new BasicAttribute
  +                                  (LAST_MODIFIED, getLastModifiedDate()));
  +            attributes.addElement(new BasicAttribute(NAME, getName()));
  +            attributes.addElement(new BasicAttribute(TYPE, getResourceType()));
  +            attributes.addElement
  +                (new BasicAttribute(CONTENT_LENGTH, 
  +                                    new Long(getContentLength())));
  +            return new RecyclableNamingEnumeration(attributes);
  +        } else {
  +            return attributes.getAll();
  +        }
  +    }
  +    
  +    
  +    /**
  +     * Get all attribute IDs.
  +     */
  +    public NamingEnumeration getIDs() {
  +        if (attributes == null) {
  +            Vector attributeIDs = new Vector();
  +            attributeIDs.addElement(CREATION_DATE);
  +            attributeIDs.addElement(LAST_MODIFIED);
  +            attributeIDs.addElement(NAME);
  +            attributeIDs.addElement(TYPE);
  +            attributeIDs.addElement(CONTENT_LENGTH);
  +            return new RecyclableNamingEnumeration(attributeIDs);
  +        } else {
  +            return attributes.getIDs();
  +        }
  +    }
  +    
  +    
  +    /**
  +     * Retrieves the number of attributes in the attribute set.
  +     */
  +    public int size() {
  +        if (attributes == null) {
  +            return 5;
  +        } else {
  +            return attributes.size();
  +        }
  +    }
  +    
  +    
  +    /**
  +     * Clone the attributes object (WARNING: fake cloning).
  +     */
  +    public Object clone() {
  +        return this;
  +    }
  +    
  +    
  +    /**
  +     * Case sensitivity.
  +     */
  +    public boolean isCaseIgnored() {
  +        return false;
  +    }
  +    
  +    
   }
  
  
  

Mime
View raw message