Logo Search packages:      
Sourcecode: libloader version File versions  Download package

OSResourceDataCache.java

/**
 * ================================================
 * LibLoader : a free Java resource loading library
 * ================================================
 *
 * Project Info:  http://reporting.pentaho.org/libloader/
 *
 * (C) Copyright 2006, by Pentaho Corporation and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 *
 * ------------
 * $Id: OSResourceDataCache.java 3520 2007-10-16 10:34:47Z tmorgner $
 * ------------
 * (C) Copyright 2006, by Pentaho Corporation.
 */
package org.jfree.resourceloader.modules.cache.oscache;

import com.opensymphony.oscache.general.GeneralCacheAdministrator;
import com.opensymphony.oscache.base.NeedsRefreshException;
import org.jfree.resourceloader.ResourceData;
import org.jfree.resourceloader.ResourceKey;
import org.jfree.resourceloader.ResourceManager;
import org.jfree.resourceloader.ResourceLoadingException;
import org.jfree.resourceloader.cache.ResourceDataCache;
import org.jfree.resourceloader.cache.ResourceDataCacheEntry;
import org.jfree.resourceloader.cache.CachingResourceData;
import org.jfree.resourceloader.cache.DefaultResourceDataCacheEntry;
import org.jfree.resourceloader.cache.CacheUtility;

/**
 * Creation-Date: 13.04.2006, 16:30:34
 *
 * @author Thomas Morgner
 */
00050 public class OSResourceDataCache implements ResourceDataCache
{
  private GeneralCacheAdministrator dataCache;

  public OSResourceDataCache(final GeneralCacheAdministrator dataCache)
  {
    if (dataCache == null)
    {
      throw new NullPointerException();
    }
    this.dataCache = dataCache;
  }

  /**
   * Retrieves the given data from the cache.
   *
   * @param data
   */
00068   public ResourceDataCacheEntry get(final ResourceKey key)
  {
    final String ext = CacheUtility.externalizeKey(key);
    if (ext == null)
    {
      return null;
    }

    try
    {
      return (ResourceDataCacheEntry) dataCache.getFromCache(ext);
    }
    catch (NeedsRefreshException e)
    {
      dataCache.cancelUpdate(ext);
      dataCache.removeEntry(ext);
      return null;
    }
  }

  /**
   * Stores the given data on the cache. The data is registered by its primary
   * key. The cache has to store the current version of the data.
   *
   * @param data the data to be stored in the cache
   * @return the resource data object, possibly wrapped by a cache-specific
   *         implementation.
   */
00096   public ResourceData put(final ResourceManager caller, final ResourceData data)
          throws ResourceLoadingException
  {
    final ResourceData cdata = CachingResourceData.createCached(data);
    final String ext = CacheUtility.externalizeKey(data.getKey());
    if (ext == null)
    {
      return cdata;
    }
    dataCache.putInCache(ext, new DefaultResourceDataCacheEntry(cdata, caller));
    return cdata;
  }

  public void remove(final ResourceData data)
  {
    final String ext = CacheUtility.externalizeKey(data.getKey());
    if (ext == null)
    {
      return;
    }
    dataCache.removeEntry(ext);
  }

  /**
   * Remove all cached entries. This should be called after the cache has become
   * invalid or after it has been removed from a resource manager.
   */
00123   public void clear()
  {
    dataCache.flushAll();
  }

}

Generated by  Doxygen 1.6.0   Back to index