All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.ehcache.jcache.JCacheEhcacheDecorator Maven / Gradle / Ivy

There is a newer version: 1.5.0-0.5
Show newest version
/**
 *  Copyright 2003-2010 Terracotta, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package net.sf.ehcache.jcache;


import net.sf.ehcache.CacheException;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.constructs.EhcacheDecoratorAdapter;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Simple decorator that retains a reference to the JCache adpater.
 * 

* This makes is possible to retrieve the JSR107 cache from a reference on just the ehcache * (for instance, to support adapting EHCache events to JSR107 events) * * @param the type of keys used in the JCache that this ehcache decorator wraps * @param the type of values used in the JCache that this ehcache decorator wraps * @author Ryan Gardner */ public class JCacheEhcacheDecorator extends EhcacheDecoratorAdapter { private JCache jcache; public JCacheEhcacheDecorator(Ehcache underlyingCache) { super(underlyingCache); } public void setJcache(JCache jcache) { this.jcache = jcache; } public JCacheEhcacheDecorator(Ehcache underlyingCache, JCache jcache) { super(underlyingCache); this.jcache = jcache; } /** * {@inheritDoc} */ @Override public void put(Element element) throws IllegalArgumentException, IllegalStateException, CacheException { Element newElement = element; if (this.jcache.getConfiguration().isStoreByValue()) { K key = cloneKeyValue((K) element.getKey()); newElement = duplicateElementWithNewKey(element, key); } super.put(newElement); } protected Element duplicateElementWithNewKey(final Element element, final Object newKey) { return new Element(newKey, element.getValue(), element.getVersion(), element.getCreationTime(), element.getLastAccessTime(), element.getHitCount(), element.usesCacheDefaultLifespan(), element.getTimeToLive(), element.getTimeToIdle(), element.getLastUpdateTime()); } private K cloneKeyValue(K key) { ObjectOutputStream oos = null; ObjectInputStream ois = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); oos.writeObject(key); oos.flush(); ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); ois = new ObjectInputStream(bin); return (K) ois.readObject(); } catch (ClassNotFoundException e) { throw new CacheException("Unable to clone Key", e); } catch (IOException e) { throw new CacheException("Unable to clone key", e); } } public JCache getJcache() { return jcache; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy