mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Merged enterprise features
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2746 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
140
source/java/org/alfresco/repo/cache/TreeCacheAdapter.java
vendored
Normal file
140
source/java/org/alfresco/repo/cache/TreeCacheAdapter.java
vendored
Normal file
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Alfresco Network License. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfrescosoftware.com/legal/
|
||||
*
|
||||
* Please view the license relevant to your network subscription.
|
||||
*
|
||||
* BY CLICKING THE "I UNDERSTAND AND ACCEPT" BOX, OR INSTALLING,
|
||||
* READING OR USING ALFRESCO'S Network SOFTWARE (THE "SOFTWARE"),
|
||||
* YOU ARE AGREEING ON BEHALF OF THE ENTITY LICENSING THE SOFTWARE
|
||||
* ("COMPANY") THAT COMPANY WILL BE BOUND BY AND IS BECOMING A PARTY TO
|
||||
* THIS ALFRESCO NETWORK AGREEMENT ("AGREEMENT") AND THAT YOU HAVE THE
|
||||
* AUTHORITY TO BIND COMPANY. IF COMPANY DOES NOT AGREE TO ALL OF THE
|
||||
* TERMS OF THIS AGREEMENT, DO NOT SELECT THE "I UNDERSTAND AND AGREE"
|
||||
* BOX AND DO NOT INSTALL THE SOFTWARE OR VIEW THE SOURCE CODE. COMPANY
|
||||
* HAS NOT BECOME A LICENSEE OF, AND IS NOT AUTHORIZED TO USE THE
|
||||
* SOFTWARE UNLESS AND UNTIL IT HAS AGREED TO BE BOUND BY THESE LICENSE
|
||||
* TERMS. THE "EFFECTIVE DATE" FOR THIS AGREEMENT SHALL BE THE DAY YOU
|
||||
* CHECK THE "I UNDERSTAND AND ACCEPT" BOX.
|
||||
*/
|
||||
package org.alfresco.repo.cache;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.alfresco.error.AlfrescoRuntimeException;
|
||||
import org.jboss.cache.Fqn;
|
||||
import org.jboss.cache.TreeCache;
|
||||
|
||||
/**
|
||||
* A thin adapter for <b>TreeCache</b> support.
|
||||
*
|
||||
* @author Derek Hulley
|
||||
*/
|
||||
public class TreeCacheAdapter<K extends Serializable, V extends Serializable>
|
||||
implements SimpleCache<K, V>
|
||||
{
|
||||
private TreeCache cache;
|
||||
private Fqn regionFqn;
|
||||
|
||||
public TreeCacheAdapter()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cache the backing Ehcache instance
|
||||
*/
|
||||
public void setCache(TreeCache cache)
|
||||
{
|
||||
this.cache = cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the uniquely named region of the cache within which all object must be cached
|
||||
*
|
||||
* @param regionName the cache region
|
||||
*/
|
||||
public void setRegionName(String regionName)
|
||||
{
|
||||
this.regionFqn = new Fqn(regionName);
|
||||
}
|
||||
|
||||
public boolean contains(K key)
|
||||
{
|
||||
try
|
||||
{
|
||||
return cache.exists(regionFqn, key);
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("contains failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public V get(K key)
|
||||
{
|
||||
try
|
||||
{
|
||||
Object element = cache.get(regionFqn, key);
|
||||
if (element != null)
|
||||
{
|
||||
return (V) element;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Failed to get from TreeCache: \n" +
|
||||
" key: " + key,
|
||||
e);
|
||||
}
|
||||
}
|
||||
|
||||
public void put(K key, V value)
|
||||
{
|
||||
try
|
||||
{
|
||||
cache.put(regionFqn, key, value);
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Failed to put into TreeCache: \n" +
|
||||
" key: " + key + "\n" +
|
||||
" value: " + value,
|
||||
e);
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(K key)
|
||||
{
|
||||
try
|
||||
{
|
||||
cache.remove(regionFqn, key);
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Failed to remove from TreeCache: \n" +
|
||||
" key: " + key,
|
||||
e);
|
||||
}
|
||||
}
|
||||
|
||||
public void clear()
|
||||
{
|
||||
try
|
||||
{
|
||||
cache.remove(regionFqn);
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Failed to clear cache", e);
|
||||
}
|
||||
}
|
||||
}
|
75
source/java/org/alfresco/repo/cache/TreeCacheAdapterTest.java
vendored
Normal file
75
source/java/org/alfresco/repo/cache/TreeCacheAdapterTest.java
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Alfresco Network License. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfrescosoftware.com/legal/
|
||||
*
|
||||
* Please view the license relevant to your network subscription.
|
||||
*
|
||||
* BY CLICKING THE "I UNDERSTAND AND ACCEPT" BOX, OR INSTALLING,
|
||||
* READING OR USING ALFRESCO'S Network SOFTWARE (THE "SOFTWARE"),
|
||||
* YOU ARE AGREEING ON BEHALF OF THE ENTITY LICENSING THE SOFTWARE
|
||||
* ("COMPANY") THAT COMPANY WILL BE BOUND BY AND IS BECOMING A PARTY TO
|
||||
* THIS ALFRESCO NETWORK AGREEMENT ("AGREEMENT") AND THAT YOU HAVE THE
|
||||
* AUTHORITY TO BIND COMPANY. IF COMPANY DOES NOT AGREE TO ALL OF THE
|
||||
* TERMS OF THIS AGREEMENT, DO NOT SELECT THE "I UNDERSTAND AND AGREE"
|
||||
* BOX AND DO NOT INSTALL THE SOFTWARE OR VIEW THE SOURCE CODE. COMPANY
|
||||
* HAS NOT BECOME A LICENSEE OF, AND IS NOT AUTHORIZED TO USE THE
|
||||
* SOFTWARE UNLESS AND UNTIL IT HAS AGREED TO BE BOUND BY THESE LICENSE
|
||||
* TERMS. THE "EFFECTIVE DATE" FOR THIS AGREEMENT SHALL BE THE DAY YOU
|
||||
* CHECK THE "I UNDERSTAND AND ACCEPT" BOX.
|
||||
*/
|
||||
package org.alfresco.repo.cache;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.jboss.cache.DummyTransactionManagerLookup;
|
||||
import org.jboss.cache.Fqn;
|
||||
import org.jboss.cache.TreeCache;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.cache.TreeCacheAdapter
|
||||
*
|
||||
* @author Derek Hulley
|
||||
*/
|
||||
public class TreeCacheAdapterTest extends TestCase
|
||||
{
|
||||
private static final String KEY_A = "A";
|
||||
private static final String VALUE_A = "AAA";
|
||||
private static final String KEY_B = "B";
|
||||
private static final String VALUE_B = "BBB";
|
||||
|
||||
private TreeCache treeCache;
|
||||
private TreeCacheAdapter<Serializable, Serializable> cache;
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
treeCache = new TreeCache();
|
||||
treeCache.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
|
||||
treeCache.start();
|
||||
|
||||
cache = new TreeCacheAdapter<Serializable, Serializable>();
|
||||
cache.setCache(treeCache);
|
||||
cache.setRegionName(getName());
|
||||
}
|
||||
|
||||
public void testSimplePutGet() throws Exception
|
||||
{
|
||||
cache.put(KEY_A, VALUE_A);
|
||||
cache.put(KEY_B, VALUE_B);
|
||||
|
||||
// check that this is present in the underlying cache
|
||||
Serializable checkValueA = (Serializable) treeCache.get(new Fqn(getName()), KEY_A);
|
||||
assertNotNull("Value A is not present in underlying cache", checkValueA);
|
||||
assertEquals("Value A is incorrect in underlying cache", VALUE_A, checkValueA);
|
||||
|
||||
Serializable checkValueB = cache.get(KEY_B);
|
||||
assertNotNull("Value B is not present in cache", checkValueB);
|
||||
assertEquals("Value B is incorrect in cache", VALUE_B, checkValueB);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user