ws-wsif-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ow...@apache.org
Subject cvs commit: xml-axis-wsif/java/src/org/apache/wsif WSIFConstants.java
Date Fri, 09 May 2003 13:01:06 GMT
owenb       2003/05/09 06:01:06

  Modified:    java/src/org/apache/wsif/base WSIFServiceFactoryImpl.java
               java/src/org/apache/wsif WSIFConstants.java
  Added:       java/src/org/apache/wsif/base WSIFServiceCache.java
  Log:
  Improvement to service caching. Set a limit on the cache size. When the number of entries
in the cache nears the limit, the oldest entries in the cache will be purged.
  The size of the cache can be set via a feature called WSIF_FEATURE_SERVICE_CACHE_SIZE on
the WSIFServiceFactory. The default size is 100.
  
  Revision  Changes    Path
  1.12      +32 -2     xml-axis-wsif/java/src/org/apache/wsif/base/WSIFServiceFactoryImpl.java
  
  Index: WSIFServiceFactoryImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-axis-wsif/java/src/org/apache/wsif/base/WSIFServiceFactoryImpl.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- WSIFServiceFactoryImpl.java	6 Mar 2003 15:54:40 -0000	1.11
  +++ WSIFServiceFactoryImpl.java	9 May 2003 13:01:06 -0000	1.12
  @@ -82,7 +82,7 @@
   public class WSIFServiceFactoryImpl extends WSIFServiceFactory {
   
       private boolean useCache = false;
  -    private Map cache = new Hashtable();
  +	private WSIFServiceCache cache = null;
       private Map features = new Hashtable();
   
       /**
  @@ -398,11 +398,25 @@
           	if (value != null && value instanceof Boolean) {
           		if (((Boolean) value).booleanValue()) {
           			useCache = true;
  +					if (cache == null) {
  +						int size = 100;
  +						Object tempInt = getFeature(WSIFConstants.WSIF_FEATURE_SERVICE_CACHE_SIZE);
  +						if (tempInt != null && tempInt instanceof Integer) {
  +							size = ((Integer) tempInt).intValue();
  +						}
  +						cache = new WSIFServiceCache(size);
  +					}
           		} else {
           		    useCache = false;
  +        		    cache = null;
           		}
           	}
  -        }
  +        } else if (WSIFConstants.WSIF_FEATURE_SERVICE_CACHE_SIZE.equals(name)) {      
 	
  +        	if (value != null && value instanceof Integer && cache != null)
{
  +        		int size = ((Integer) value).intValue();
  +        		cache.setCacheSize(size);
  +        	}
  +        } 
           features.put(name, value);
           Trc.exit();
       }    
  @@ -419,9 +433,25 @@
           	if (value != null && value instanceof Boolean) {
           		if (((Boolean) value).booleanValue()) {
           			useCache = true;
  +        			if (cache == null) {
  +        				int size = 100;
  +						Object tempInt = getFeature(WSIFConstants.WSIF_FEATURE_SERVICE_CACHE_SIZE);
  +						if (tempInt != null && tempInt instanceof Integer) {
  +							size = ((Integer) tempInt).intValue();
  +						}
  +						cache = new WSIFServiceCache(size);
  +        			}
           		} else {
           		    useCache = false;
  +        		    cache = null;
           		}
  +        	}
  +        }
  +        if (map.containsKey(WSIFConstants.WSIF_FEATURE_SERVICE_CACHE_SIZE)) {
  +        	Object value = map.get(WSIFConstants.WSIF_FEATURE_SERVICE_CACHE_SIZE);       
	
  +        	if (value != null && value instanceof Integer && cache != null)
{
  +        		int size = ((Integer) value).intValue();
  +        		cache.setCacheSize(size);
           	}
           }        
           Trc.exit();
  
  
  
  1.1                  xml-axis-wsif/java/src/org/apache/wsif/base/WSIFServiceCache.java
  
  Index: WSIFServiceCache.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:  
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "WSIF" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 2001, 2002, International
   * Business Machines, Inc., http://www.ibm.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.wsif.base;
  
  import java.util.Hashtable;
  import java.util.Vector;
  
  import org.apache.wsif.logging.Trc;
  
  /**
   * Class to act as a cache of WSIFServices.
   * 
   * @author Paul Harris
   * @author Owen Burroughs
   */
  
  public class WSIFServiceCache extends Hashtable {
      static final int CUSHION_PERCENT = 10;
      // How much of the cache will be cleaned out when it's limit has been reached
  
      LRUWrapper oldestObject = null; //The oldest (used) object in the cache
      LRUWrapper newestObject = null; //The newest (used) object in the cache
  
      int cushionSize = 1;
      // Calculated from CUSHION_PERCENT & cacheLimit - this is the number of objects
      // we'll clean out of the cache when we need to shrink it.
      
      int cacheLimit = 0; // The size (in objects) of the cache  
      int currentSize = 0;
  
      WSIFServiceCache(int size) {
      	super(size);
          cacheLimit = size;
          cushionSize = ((cacheLimit * CUSHION_PERCENT) / 100); // 10%
          if (cushionSize == 0) {
              cushionSize = 1;
          }
          Trc.event(this,
                  "WSIFServiceCache created - cache limit:"
                      + cacheLimit
                      + " Cushion:"
                      + cushionSize);
      }
      
      public void setCacheSize(int newSize) {
      	synchronized (this) {
      	    cacheLimit = newSize;
              cushionSize = ((cacheLimit * CUSHION_PERCENT) / 100);
      	}
      }
  
      public Object put(Object key, Object value) {
          LRUWrapper newObject = new LRUWrapper(key, value);
  
          if (currentSize >= cacheLimit)
              shrinkCache();
          if (oldestObject == null) {
              oldestObject = newObject;
              newestObject = newObject;
          } else {
              addToTopOfLRUList(newObject);
          }
  
          newObject = (LRUWrapper) super.put(key, newObject);
          currentSize++;
          Trc.event(this,
                  "WSIFServiceCache (put). Current cache size: " + currentSize);
          if (newObject == null)
              return null;
          else
              return newObject.value; //to make this consistent with Hashtable
      }
  
      public Object get(Object key) {
          LRUWrapper tempObject = (LRUWrapper) super.get(key);
          //Adjust the LRU list
  
          if (tempObject == null) {
              Trc.event(this,
                      "WSIFServiceCache (get). No hit. Cache size: "
                          + currentSize);
              return null;
          } else {
              Trc.event(this,
                      "WSIFServiceCache (get). MATCH FOUND. Cache size: "
                          + currentSize);
              if (newestObject != tempObject) {
                  removeFromLRUList(tempObject);
                  addToTopOfLRUList(tempObject);
              }
              return tempObject.value;
          }
      }
  
      private void addToTopOfLRUList(LRUWrapper newObject) {
          newestObject.nextObject = newObject;
          newObject.prevObject = newestObject;
          newestObject = newObject;
      }
  
      private void removeFromLRUList(LRUWrapper oldObject) {
          LRUWrapper nextObject, prevObject;
  
          if (oldObject != oldestObject) {
              oldObject.prevObject.nextObject = oldObject.nextObject;
              oldObject.nextObject.prevObject = oldObject.prevObject;
          } else {
              oldObject.nextObject.prevObject = null;
              oldestObject = oldObject.nextObject;
          }
  
      }
  
      private void shrinkCache() {
          LRUWrapper currentLRUWrapper = oldestObject;
  
          for (int i = 1; i <= cushionSize; i++) {
              super.remove(currentLRUWrapper.key);
              currentLRUWrapper = currentLRUWrapper.nextObject;
          }
          oldestObject = currentLRUWrapper;
          currentSize -= cushionSize;
          Trc.event(this,
                  "WSIFServiceCache (put). Cache size after shrinkage: "
                      + currentSize);
      }
  
      /**
       * This class wraps whatever we're caching with some references used in a double linked
list. 
       * This means we can traverse the objects referenced by the Hashtable in the order they
were
       * last used.
       **/
      class LRUWrapper {
          LRUWrapper nextObject = null;
          LRUWrapper prevObject = null;
          Object value = null;
          Object key = null;
  
          LRUWrapper(Object inKey, Object inValue) {
              value = inValue;
              key = inKey;
          }
      }    
  }
  
  
  
  1.26      +9 -0      xml-axis-wsif/java/src/org/apache/wsif/WSIFConstants.java
  
  Index: WSIFConstants.java
  ===================================================================
  RCS file: /home/cvs/xml-axis-wsif/java/src/org/apache/wsif/WSIFConstants.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- WSIFConstants.java	3 Apr 2003 16:16:32 -0000	1.25
  +++ WSIFConstants.java	9 May 2003 13:01:06 -0000	1.26
  @@ -333,6 +333,15 @@
   	public static final String WSIF_FEATURE_SERVICE_CACHING = "org.apache.wsif.servicecaching";
   
       /**
  +     * Feature name for service cache size. The value of this feature should be a 
  +     * <code>java.lang.Integer</code> object<br><br>
  +     * This feature is used in conjunction with the service caching feature. It sets the
number of
  +     * instances of WSIFService to be cached by the WSIFServiceFactory. When this number
is reached
  +     * the oldest entries in the cache will be purged.
  +     */	
  +	public static final String WSIF_FEATURE_SERVICE_CACHE_SIZE = "org.apache.wsif.servicecachesize";
  +
  +    /**
        * Feature name for automatic mapping of types. The value of this feature should be
a 
        * <code>java.lang.Boolean</code> object<br><br>
        * Setting this feature as <code>true</code> will cause instances of WSIFService
created by this factory,
  
  
  

Mime
View raw message