You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2001/09/28 04:32:23 UTC

cvs commit: jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources ProxyDirContext.java

remm        01/09/27 19:32:23

  Modified:    catalina/src/share/org/apache/naming/resources
                        ProxyDirContext.java
  Log:
  - Add content caching.
  - Objects which don't have their content cached are now revalidated on every
    access. That should solve some problems when updating them (where it was
    found out that there could be some cache inconcistencies problems.
  - Overall, all resource accesses should be faster (esp the repetitive ones),
    which should impact everything in Tomcat (from CL to static file serving).
  
  Revision  Changes    Path
  1.7       +49 -14    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.7
  diff -u -r1.6 -r1.7
  --- ProxyDirContext.java	2001/07/26 00:16:08	1.6
  +++ ProxyDirContext.java	2001/09/28 02:32:22	1.7
  @@ -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.7 2001/09/28 02:32:22 remm Exp $
  + * $Revision: 1.7 $
  + * $Date: 2001/09/28 02:32:22 $
    *
    * ====================================================================
    *
  @@ -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.7 $ $Date: 2001/09/28 02:32:22 $
    */
   
   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;
  @@ -238,6 +249,33 @@
           CacheEntry entry = cacheLookup(name);
           if (entry != null) {
               if (entry.resource != null) {
  +                if ((entry.resource.getContent() == null) 
  +                    && (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
  +                        }
  +                    }
  +                }
                   return entry.resource;
               } else {
                   return entry.context;
  @@ -1350,8 +1388,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 +1405,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 +1416,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;
           }