db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattba...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/cache AttributeBasedObjectCacheImpl.java
Date Tue, 05 Aug 2003 21:13:09 GMT
mattbaird    2003/08/05 14:13:09

  Added:       src/java/org/apache/ojb/broker/cache
                        AttributeBasedObjectCacheImpl.java
  Log:
  new attribute based cache per class. Just add an attribute cacheClass="some value"
  
  Revision  Changes    Path
  1.1                  db-ojb/src/java/org/apache/ojb/broker/cache/AttributeBasedObjectCacheImpl.java
  
  Index: AttributeBasedObjectCacheImpl.java
  ===================================================================
  package org.apache.ojb.broker.cache;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 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 "Apache" and "Apache Software Foundation" and
   *    "Apache ObjectRelationalBridge" 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",
   *    "Apache ObjectRelationalBridge", 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.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.ojb.broker.Identity;
  import org.apache.ojb.broker.PersistenceBroker;
  import org.apache.ojb.broker.metadata.ClassDescriptor;
  import org.apache.ojb.broker.metadata.MetadataManager;
  import org.apache.ojb.broker.util.logging.Logger;
  import org.apache.ojb.broker.util.logging.LoggerFactory;
  
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  
  /**
   * reads the attribute from the class descriptor to find the appropriate cache to use.
   *
   * @author  Matthew Baird  (mattbaird@yahoo.com)
   */
  public class AttributeBasedObjectCacheImpl implements ObjectCache
  {
  	private Logger logger = LoggerFactory.getLogger(AttributeBasedObjectCacheImpl.class);
  	private Map m_caches = new HashMap();
  	private ObjectCache m_defaultCache;
  	private PersistenceBroker m_broker;
  	private static final String CACHE_CLASS_ATTRIBUTE_NAME = "cacheClass";
  
  	public AttributeBasedObjectCacheImpl()
  	{
  
  	}
  
  	/**
  	 * public Default Constructor
  	 */
  	public AttributeBasedObjectCacheImpl(PersistenceBroker broker)
  	{
  		m_broker = broker;
  		m_defaultCache = new ObjectCacheDefaultImpl(broker);
  	}
  
  	public void cache(Identity oid, Object obj)
  	{
  		ObjectCache cache = getCacheForClass(oid.getObjectsRealClass());
  		cache.cache(oid, obj);
  	}
  
  	public Object lookup(Identity oid)
  	{
  		ObjectCache cache = getCacheForClass(oid.getObjectsRealClass());
  		return cache.lookup(oid);
  	}
  
  	public void remove(Identity oid)
  	{
  		ObjectCache cache = getCacheForClass(oid.getObjectsRealClass());
  		cache.remove(oid);
  	}
  
  	public void clear()
  	{
  		/**
  		 * first be nice to the GC and clear each internal cache.
  		 */
  		Iterator it = m_caches.values().iterator();
  		ObjectCache oc = null;
  		while (it.hasNext())
  		{
  			oc = (ObjectCache) it.next();
  			oc.clear();
  		}
  		/**
  		 * then call clear on the caches hashmap.
  		 */
  		m_caches.clear();
  	}
  
  	private ObjectCache getCacheForClass(Class clazz)
  	{
  		ObjectCache retval = null;
  		retval = (ObjectCache) m_caches.get(clazz);
  		if (retval == null)
  		{
  			// lookup type and create
  			ClassDescriptor cd = MetadataManager.getInstance().getGlobalRepository().getDescriptorFor(clazz);
  			String cacheClass = cd.getAttribute(CACHE_CLASS_ATTRIBUTE_NAME);
  			if (cacheClass != null && cacheClass.trim().length() > 0)
  			{
  				try
  				{
  					retval = (ObjectCache) Class.forName(cacheClass).newInstance();
  				}
  				catch (InstantiationException e)
  				{
  					logger.error("AttributeBasedObjectCacheImpl caught an InstantiationException for class
type: " + cacheClass);
  				}
  				catch (IllegalAccessException e)
  				{
  					logger.error("AttributeBasedObjectCacheImpl caught an IllegalAccessException for class
type: " + cacheClass);
  				}
  				catch (ClassNotFoundException e)
  				{
  					logger.error("AttributeBasedObjectCacheImpl caught an ClassNotFoundException for class
type: " + cacheClass);
  				}
  			}
  			/**
  			 * handle all cases of failure
  			 */
  			if (retval == null)
  			{
  				retval = m_defaultCache;
  			}
  			m_caches.put(clazz, retval);
  		}
  		return retval;
  	}
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message