[jboss-cvs] JBossCache/src/org/jboss/cache/loader ...
Manik Surtani
msurtani at jboss.com
Wed Jul 19 04:29:18 EDT 2006
User: msurtani
Date: 06/07/19 04:29:18
Modified: src/org/jboss/cache/loader
AsyncCacheLoader.java CacheLoader.java
CacheLoaderManager.java ChainingCacheLoader.java
ClusteredCacheLoader.java
DelegatingCacheLoader.java FileCacheLoader.java
JDBCCacheLoader.java
LocalDelegatingCacheLoader.java
RmiDelegatingCacheLoader.java
RpcDelegatingCacheLoader.java
SharedStoreCacheLoader.java
Added: src/org/jboss/cache/loader
AbstractCacheLoader.java
TcpDelegatingCacheLoader.java
Removed: src/org/jboss/cache/loader
AsyncExtendedCacheLoader.java
ExtendedCacheLoader.java
FileExtendedCacheLoader.java
JDBCExtendedCacheLoader.java
Log:
Migration to new CacheLoader interface
Revision Changes Path
1.14 +45 -41 JBossCache/src/org/jboss/cache/loader/AsyncCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: AsyncCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/AsyncCacheLoader.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -b -r1.13 -r1.14
--- AsyncCacheLoader.java 26 Apr 2006 11:18:42 -0000 1.13
+++ AsyncCacheLoader.java 19 Jul 2006 08:29:18 -0000 1.14
@@ -9,9 +9,10 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.CacheException;
+import org.jboss.cache.CacheSPI;
import org.jboss.cache.Fqn;
import org.jboss.cache.Modification;
-import org.jboss.cache.TreeCache;
+import org.jboss.cache.marshall.RegionManager;
import java.io.IOException;
import java.util.ArrayList;
@@ -76,7 +77,7 @@
*
* @author Manik Surtani (manik.surtani at jboss.com)
*/
-public class AsyncCacheLoader implements CacheLoader
+public class AsyncCacheLoader extends AbstractCacheLoader
{
private static final Log log = LogFactory.getLog(AsyncCacheLoader.class);
@@ -147,23 +148,17 @@
delegateTo.setConfig(props);
}
- public void setCache(TreeCache c)
+ public void setCache(CacheSPI c)
{
- delegateTo.setCache( c );
+ super.setCache(c);
+ delegateTo.setCache(c);
}
- public Set getChildrenNames(Fqn fqn) throws Exception
+ public Set<String> getChildrenNames(Fqn fqn) throws Exception
{
return delegateTo.getChildrenNames( fqn );
}
- // See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
-
-// public Object get(Fqn name, Object key) throws Exception
-// {
-// return delegateTo.get( name, key );
-// }
-
public Map get(Fqn name) throws Exception
{
try
@@ -223,7 +218,7 @@
else delegateTo.put(name, attributes);
}
- public void put(List modifications) throws Exception
+ public void put(List<Modification> modifications) throws Exception
{
if (asyncPut)
{
@@ -275,12 +270,17 @@
return delegateTo.loadEntireState();
}
- void storeState(byte[] state, Fqn subtree) throws Exception
+ public void storeState(byte[] state, Fqn subtree) throws Exception
{
Modification mod = new Modification(Modification.STORE_STATE, subtree, null, state);
enqueue(mod);
}
+ public void setRegionManager(RegionManager manager)
+ {
+ delegateTo.setRegionManager(manager);
+ }
+
/**
* Stores the entire state.
* This is processed asynchronously.
@@ -291,6 +291,11 @@
enqueue(mod);
}
+ public byte[] loadState(Fqn subtree) throws Exception
+ {
+ return new byte[0];
+ }
+
public void create() throws Exception
{
delegateTo.create();
@@ -405,7 +410,6 @@
}
private void addTaken(Object o)
- throws InterruptedException
{
if (o instanceof List)
{
@@ -432,7 +436,7 @@
if (fqn == null)
delegateTo.storeEntireState(b);
else
- ((ExtendedCacheLoader)delegateTo).storeState(b, fqn);
+ delegateTo.storeState(b, fqn);
}
catch (Exception e)
{
1.6 +243 -165 JBossCache/src/org/jboss/cache/loader/CacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/CacheLoader.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -b -r1.5 -r1.6
--- CacheLoader.java 2 Apr 2006 07:33:15 -0000 1.5
+++ CacheLoader.java 19 Jul 2006 08:29:18 -0000 1.6
@@ -1,13 +1,20 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
package org.jboss.cache.loader;
+import org.jboss.cache.marshall.RegionManager;
import org.jboss.cache.Fqn;
-import org.jboss.cache.TreeCache;
-import org.jboss.system.Service;
+import org.jboss.cache.CacheSPI;
+import org.jboss.cache.Modification;
-import java.util.List;
-import java.util.Map;
import java.util.Properties;
import java.util.Set;
+import java.util.Map;
+import java.util.List;
/**
* A <code>CacheLoader</code> implementation persists and load keys to and from
@@ -18,17 +25,18 @@
* <p/>
* Lifecycle: First an instance of the loader is created, then the
* configuration ({@link #setConfig(java.util.Properties)} ) and cache ({@link
- * #setCache(TreeCache)}) are set. After this, {@link #create()} is called.
+ * #setCache(CacheSPI)}) are set. After this, {@link #create()} is called.
* Then {@link #start()} is called. When re-deployed, {@link #stop()} will be
* called, followed by another {@link #start()}. Finally, when shut down,
* {@link #destroy()} is called, after which the loader is unusable.
*
- * @author Bela Ban Oct 31, 2003
- * @version $Id: CacheLoader.java,v 1.5 2006/04/02 07:33:15 genman Exp $
+ * @see CacheSPI
+ * @since 2.0.0
+ * @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
*/
-public interface CacheLoader extends Service {
-
+public interface CacheLoader
+{
/**
* Sets the configuration. This is called before {@link #create()} and {@link #start()}.
* @param properties a collection of configuration properties
@@ -37,13 +45,13 @@
/**
- * Sets the {@link TreeCache} that is maintaining this CacheLoader.
- * This method allows this CacheLoader to invoke methods on TreeCache,
+ * Sets the {@link CacheSPI} that is maintaining this CacheLoader.
+ * This method allows this CacheLoader to invoke methods on cache,
* including fetching additional configuration information. This method is
* called be called after the CacheLoader instance has been constructed.
* @param c The cache on which this loader works
*/
- void setCache(TreeCache c);
+ void setCache(CacheSPI c);
/**
@@ -51,22 +59,14 @@
* All names are <em>relative</em> to this parent {@link Fqn}.
* Returns null if the named node is not found or there are no children.
* The returned set must not be modifiable. Implementors can use
- * {@link java.util.Collections#unmodifiableSet(Set)} to make the set unmodifiable.
+ * {@link java.util.Collections#unmodifiableSet(java.util.Set)} to make the set unmodifiable.
*
- * @param fqn The FQN of the parent
- * @return Set<String> a set of children. Returns null if no children nodes are
+ * @param fqn The {@link Fqn} of the parent
+ * @return Set a set of children. Returns null if no children nodes are
* present, or the parent is not present
*/
Set getChildrenNames(Fqn fqn) throws Exception;
-
- /**
- * Returns the value for a given key. Returns null if the node doesn't
- * exist, or the value is null itself.
- */
-// Object get(Fqn name, Object key) throws Exception;
-
-
/**
* Returns all keys and values from the persistent store, given a fully qualified name.
*
@@ -78,7 +78,6 @@
* @return Map<Object,Object> keys and values for the given node. Returns
* null if the node is not found. If the node is found but has no
* attributes, this method returns an empty Map.
- * @throws Exception
*/
Map get(Fqn name) throws Exception;
@@ -108,36 +107,36 @@
*/
void put(Fqn name, Map attributes) throws Exception;
-
-
/**
* Applies all modifications to the backend store.
* Changes may be applied in a single operation.
*
* @param modifications A List<Modification> of modifications
- * @throws Exception
*/
- void put(List modifications) throws Exception;
-
+ void put(List<Modification> modifications) throws Exception;
/**
* Removes the given key and value from the attributes of the given node.
* Does nothing if the node doesn't exist
* Returns the removed value.
*/
- Object remove(Fqn name, Object key) throws Exception;
+ Object remove(Fqn fqn, Object key) throws Exception;
/**
* Removes the given node and all its subnodes.
+ *
+ * @param fqn the {@link Fqn} of the node
*/
- void remove(Fqn name) throws Exception;
+ void remove(Fqn fqn) throws Exception;
/**
* Removes all attributes from a given node, but doesn't delete the node
* itself or any subnodes.
+ *
+ * @param fqn the {@link Fqn} of the node
*/
- void removeData(Fqn name) throws Exception;
+ void removeData(Fqn fqn) throws Exception;
/**
@@ -155,7 +154,7 @@
* we won't get a {@link #commit(Object)} or {@link #rollback(Object)} method call later
* @throws Exception
*/
- void prepare(Object tx, List modifications, boolean one_phase) throws Exception;
+ void prepare(Object tx, List<Modification> modifications, boolean one_phase) throws Exception;
/**
* Commits the transaction. A DB-based CacheLoader would look up the local
@@ -180,17 +179,96 @@
/**
- * Fetches the entire state for this cache from secondary storage (disk, DB)
+ * Fetches the entire state for this cache from secondary storage (disk, database)
* and returns it as a byte buffer. This is for initialization of a new
* cache from a remote cache. The new cache would then call
* {@link #storeEntireState}.
+ *
+ * @return a byte[] containing the state of the cache loader
*/
byte[] loadEntireState() throws Exception;
/**
* Stores the given state in secondary storage. Overwrites whatever is
* currently in storage.
+ *
+ * @param state state to store
*/
void storeEntireState(byte[] state) throws Exception;
+ /**
+ * Fetch a portion of the state for this cache from secondary storage
+ * (disk, database) and return it as a byte buffer.
+ * This is for activation of a portion of new cache from a remote cache.
+ * The new cache would then call {@link #storeState(byte[], Fqn)}.
+ *
+ * @param subtree Fqn naming the root (i.e. highest level parent) node of
+ * the subtree for which state is requested.
+ *
+ * @see org.jboss.cache.Region#activate()
+ */
+ byte[] loadState(Fqn subtree) throws Exception;
+
+ /**
+ * Store the given portion of the cache tree's state in secondary storage.
+ * Overwrite whatever is currently in secondary storage. If the transferred
+ * state has Fqns equal to or children of parameter <code>subtree</code>,
+ * then no special behavior is required. Otherwise, ensure that
+ * the state is integrated under the given <code>subtree</code>. Typically
+ * in the latter case <code>subtree</code> would be the Fqn of the buddy
+ * backup region for
+ * a buddy group; e.g.
+ * <p>
+ * If the the transferred state had Fqns starting with "/a" and
+ * <code>subtree</code> was "/_BUDDY_BACKUP_/192.168.1.2:5555" then the
+ * state should be stored in the local persistent store under
+ * "/_BUDDY_BACKUP_/192.168.1.2:5555/a"
+ * </p>
+ *
+ * @param state the state to store
+ * @param subtree Fqn naming the root (i.e. highest level parent) node of
+ * the subtree included in <code>state</code>. If the Fqns
+ * of the data included in <code>state</code> are not
+ * already children of <code>subtree</code>, then their
+ * Fqns should be altered to make them children of
+ * <code>subtree</code> before they are persisted.
+ */
+ void storeState(byte[] state, Fqn subtree) throws Exception;
+
+ /**
+ * Sets the {@link org.jboss.cache.marshall.RegionManager} this object should use to manage
+ * marshalling/unmarshalling of different regions using different
+ * classloaders.
+ * <p>
+ * <strong>NOTE:</strong> This method is only intended to be used
+ * by the <code>TreeCache</code> instance this cache loader is
+ * associated with.
+ * </p>
+ *
+ * @param manager the region manager to use, or <code>null</code>.
+ */
+ void setRegionManager(RegionManager manager);
+
+ /**
+ * Lifecycle method, called when the cache loader is created.
+ * @throws java.lang.Exception
+ */
+ void create() throws java.lang.Exception;
+
+ /**
+ * Lifecycle method, called when the cache loader is started.
+ * @throws java.lang.Exception
+ */
+ void start() throws java.lang.Exception;
+
+ /**
+ * Lifecycle method, called when the cache loader is stopped.
+ */
+ void stop();
+
+ /**
+ * Lifecycle method, called when the cache loader is destroyed.
+ */
+ void destroy();
+
}
1.20 +4 -31 JBossCache/src/org/jboss/cache/loader/CacheLoaderManager.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CacheLoaderManager.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/CacheLoaderManager.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -b -r1.19 -r1.20
--- CacheLoaderManager.java 1 Jun 2006 11:50:44 -0000 1.19
+++ CacheLoaderManager.java 19 Jul 2006 08:29:18 -0000 1.20
@@ -62,7 +62,6 @@
private TreeCache cache;
private CacheLoader loader;
private boolean fetchPersistentState;
- private boolean extendedCacheLoader = true;
/**
* Creates a cache loader based on an XML element passed in.
@@ -144,10 +143,6 @@
CacheLoader l = createCacheLoader(cfg, cache);
// Only loaders that deal w/ state transfer factor into
// whether the overall chain supports ExtendedCacheLoader
- if (cfg.isFetchPersistentState())
- {
- extendedCacheLoader = extendedCacheLoader && (l instanceof ExtendedCacheLoader);
- }
ccl.addCacheLoader(l, cfg);
}
}
@@ -156,7 +151,6 @@
CacheLoaderConfig.IndividualCacheLoaderConfig cfg = (CacheLoaderConfig.IndividualCacheLoaderConfig) config.getIndividualCacheLoaderConfigs().get(0);
tmpLoader = createCacheLoader(cfg, cache);
fetchPersistentState = cfg.isFetchPersistentState();
- extendedCacheLoader = (tmpLoader instanceof ExtendedCacheLoader);
}
return tmpLoader;
@@ -180,29 +174,21 @@
if (cfg.isAsync())
{
CacheLoader asyncDecorator;
- if (tmpLoader instanceof ExtendedCacheLoader)
- {
-
- asyncDecorator = new AsyncExtendedCacheLoader((ExtendedCacheLoader) tmpLoader);
- }
- else
- {
asyncDecorator = new AsyncCacheLoader(tmpLoader);
- }
tmpLoader = asyncDecorator;
}
// load props
tmpLoader.setConfig(cfg.getProperties());
- tmpLoader.setCache(cache);
+ tmpLoader.setCache(cache.getSPI());
// we should not be creating/starting the cache loader here - this should be done in the separate
// startCacheLoader() method.
// tmpLoader.create();
// tmpLoader.start();
- if (cache != null && cache.getUseMarshalling() && tmpLoader instanceof ExtendedCacheLoader)
+ if (cache != null && cache.getUseMarshalling())
{
- ((ExtendedCacheLoader) tmpLoader).setRegionManager(cache.getRegionManager());
+ tmpLoader.setRegionManager(cache.getRegionManager());
}
}
return tmpLoader;
@@ -322,19 +308,6 @@
return fetchPersistentState;
}
- /**
- * Gets whether the cache loader supports the {@link ExtendedCacheLoader} API.
- *
- * @return <code>true</code> if the cache loader implements ExtendedCacheLoader,
- * or, if the cache loader is a {@link ChainingCacheLoader}, all
- * cache loaders in the chain whose <code>FetchPersistentState</code>
- * property is <code>true</code> implement ExtendedCacheLoader.
- */
- public boolean isExtendedCacheLoader()
- {
- return extendedCacheLoader;
- }
-
public void stopCacheLoader()
{
if (loader == null) throw new RuntimeException("Problem with configured cache loader - it has been set to null!");
1.8 +76 -124 JBossCache/src/org/jboss/cache/loader/ChainingCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ChainingCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/ChainingCacheLoader.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- ChainingCacheLoader.java 30 May 2006 18:40:25 -0000 1.7
+++ ChainingCacheLoader.java 19 Jul 2006 08:29:18 -0000 1.8
@@ -6,12 +6,18 @@
*/
package org.jboss.cache.loader;
-import org.jboss.cache.TreeCache;
import org.jboss.cache.Fqn;
+import org.jboss.cache.Modification;
import org.jboss.cache.config.CacheLoaderConfig;
import org.jboss.cache.marshall.RegionManager;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
/**
* This decorator is used whenever more than one cache loader is configured. READ operations are directed to
@@ -22,11 +28,11 @@
*
* @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
*/
-public class ChainingCacheLoader implements ExtendedCacheLoader
+public class ChainingCacheLoader extends AbstractCacheLoader
{
- private final List cacheLoaders = new ArrayList(2);
- private final List writeCacheLoaders = new ArrayList(2);
- private final List cacheLoaderConfigs = new ArrayList(2);
+ private final List<CacheLoader> cacheLoaders = new ArrayList<CacheLoader>(2);
+ private final List<CacheLoader> writeCacheLoaders = new ArrayList<CacheLoader>(2);
+ private final List<CacheLoaderConfig.IndividualCacheLoaderConfig> cacheLoaderConfigs = new ArrayList<CacheLoaderConfig.IndividualCacheLoaderConfig>(2);
/**
* Sets the configuration. Will be called before {@link #create()} and {@link #start()}
@@ -39,19 +45,6 @@
}
/**
- * This method allows the CacheLoader to set the TreeCache, therefore allowing the CacheLoader to invoke
- * methods of the TreeCache. It can also use the TreeCache to fetch configuration information. Alternatively,
- * the CacheLoader could maintain its own configuration<br/>
- * This method will be called directly after the CacheLoader instance has been created
- *
- * @param c The cache on which this loader works
- */
- public void setCache(TreeCache c)
- {
- // not much to do here?
- }
-
- /**
* Returns a list of children names, all names are <em>relative</em>. Returns null if the parent node is not found.
* The returned set must not be modified, e.g. use Collections.unmodifiableSet(s) to return the result
*
@@ -59,41 +52,19 @@
* @return Set<String>. A list of children. Returns null if no children nodes are present, or the parent is
* not present
*/
- public Set getChildrenNames(Fqn fqn) throws Exception
+ public Set<String> getChildrenNames(Fqn fqn) throws Exception
{
- Set answer = null;
- Iterator i = cacheLoaders.iterator();
+ Set<String> answer = null;
+ Iterator<CacheLoader> i = cacheLoaders.iterator();
while (i.hasNext())
{
- CacheLoader l = (CacheLoader) i.next();
+ CacheLoader l = i.next();
answer = l.getChildrenNames(fqn);
if (answer != null && answer.size() > 0) break;
}
return answer;
}
- // See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
-
- /**
- * Returns the value for a given key. Returns null if the node doesn't exist, or the value is not bound
- *
- * @param name
- * @return
- * @throws Exception
- */
-// public Object get(Fqn name, Object key) throws Exception
-// {
-// Object answer = null;
-// Iterator i = cacheLoaders.iterator();
-// while (i.hasNext())
-// {
-// CacheLoader l = (CacheLoader) i.next();
-// answer = l.get(name, key);
-// if (answer != null) break;
-// }
-// return answer;
-// }
-
/**
* Returns all keys and values from the persistent store, given a fully qualified name
*
@@ -105,10 +76,10 @@
public Map get(Fqn name) throws Exception
{
Map answer = null;
- Iterator i = cacheLoaders.iterator();
+ Iterator<CacheLoader> i = cacheLoaders.iterator();
while (i.hasNext())
{
- CacheLoader l = (CacheLoader) i.next();
+ CacheLoader l = i.next();
answer = l.get(name);
if (answer != null && answer.size() > 0) break;
}
@@ -124,10 +95,10 @@
public boolean exists(Fqn name) throws Exception
{
boolean answer = false;
- Iterator i = cacheLoaders.iterator();
+ Iterator<CacheLoader> i = cacheLoaders.iterator();
while (i.hasNext())
{
- CacheLoader l = (CacheLoader) i.next();
+ CacheLoader l = i.next();
answer = l.exists(name);
if (answer) break;
}
@@ -141,11 +112,11 @@
public Object put(Fqn name, Object key, Object value) throws Exception
{
Object answer = null;
- Iterator i = writeCacheLoaders.iterator();
+ Iterator<CacheLoader> i = writeCacheLoaders.iterator();
boolean isFirst = true;
while (i.hasNext())
{
- CacheLoader l = (CacheLoader) i.next();
+ CacheLoader l = i.next();
Object tAnswer = l.put(name, key, value);
if (isFirst)
{
@@ -168,10 +139,10 @@
*/
public void put(Fqn name, Map attributes) throws Exception
{
- Iterator i = writeCacheLoaders.iterator();
+ Iterator<CacheLoader> i = writeCacheLoaders.iterator();
while (i.hasNext())
{
- CacheLoader l = (CacheLoader) i.next();
+ CacheLoader l = i.next();
l.put(name, attributes);
}
}
@@ -183,12 +154,12 @@
* @param modifications A List<Modification> of modifications
* @throws Exception
*/
- public void put(List modifications) throws Exception
+ public void put(List<Modification> modifications) throws Exception
{
- Iterator i = writeCacheLoaders.iterator();
+ Iterator<CacheLoader> i = writeCacheLoaders.iterator();
while (i.hasNext())
{
- CacheLoader l = (CacheLoader) i.next();
+ CacheLoader l = i.next();
l.put(modifications);
}
}
@@ -200,11 +171,11 @@
public Object remove(Fqn name, Object key) throws Exception
{
Object answer = null;
- Iterator i = writeCacheLoaders.iterator();
+ Iterator<CacheLoader> i = writeCacheLoaders.iterator();
boolean isFirst = true;
while (i.hasNext())
{
- CacheLoader l = (CacheLoader) i.next();
+ CacheLoader l = i.next();
Object tAnswer = l.remove(name, key);
if (isFirst)
{
@@ -221,10 +192,10 @@
*/
public void remove(Fqn name) throws Exception
{
- Iterator i = writeCacheLoaders.iterator();
+ Iterator<CacheLoader> i = writeCacheLoaders.iterator();
while (i.hasNext())
{
- CacheLoader l = (CacheLoader) i.next();
+ CacheLoader l = i.next();
l.remove(name);
}
}
@@ -237,10 +208,10 @@
*/
public void removeData(Fqn name) throws Exception
{
- Iterator i = writeCacheLoaders.iterator();
+ Iterator<CacheLoader> i = writeCacheLoaders.iterator();
while (i.hasNext())
{
- CacheLoader l = (CacheLoader) i.next();
+ CacheLoader l = i.next();
l.removeData(name);
}
}
@@ -262,10 +233,10 @@
*/
public void prepare(Object tx, List modifications, boolean one_phase) throws Exception
{
- Iterator i = writeCacheLoaders.iterator();
+ Iterator<CacheLoader> i = writeCacheLoaders.iterator();
while (i.hasNext())
{
- CacheLoader l = (CacheLoader) i.next();
+ CacheLoader l = i.next();
l.prepare(tx, modifications, one_phase);
}
}
@@ -281,10 +252,10 @@
*/
public void commit(Object tx) throws Exception
{
- Iterator i = writeCacheLoaders.iterator();
+ Iterator<CacheLoader> i = writeCacheLoaders.iterator();
while (i.hasNext())
{
- CacheLoader l = (CacheLoader) i.next();
+ CacheLoader l = i.next();
l.commit(tx);
}
}
@@ -297,10 +268,10 @@
*/
public void rollback(Object tx)
{
- Iterator i = writeCacheLoaders.iterator();
+ Iterator<CacheLoader> i = writeCacheLoaders.iterator();
while (i.hasNext())
{
- CacheLoader l = (CacheLoader) i.next();
+ CacheLoader l = i.next();
l.rollback(tx);
}
}
@@ -317,12 +288,12 @@
public byte[] loadEntireState() throws Exception
{
byte[] answer = null;
- Iterator i = cacheLoaders.iterator();
- Iterator cfgs = cacheLoaderConfigs.iterator();
+ Iterator<CacheLoader> i = cacheLoaders.iterator();
+ Iterator<CacheLoaderConfig.IndividualCacheLoaderConfig> cfgs = cacheLoaderConfigs.iterator();
while (i.hasNext() && cfgs.hasNext())
{
- CacheLoader l = (CacheLoader) i.next();
- CacheLoaderConfig.IndividualCacheLoaderConfig cfg = (CacheLoaderConfig.IndividualCacheLoaderConfig) cfgs.next();
+ CacheLoader l = i.next();
+ CacheLoaderConfig.IndividualCacheLoaderConfig cfg = cfgs.next();
if (cfg.isFetchPersistentState())
{
answer = l.loadEntireState();
@@ -339,12 +310,12 @@
*/
public void storeEntireState(byte[] state) throws Exception
{
- Iterator i = writeCacheLoaders.iterator();
- Iterator cfgs = cacheLoaderConfigs.iterator();
+ Iterator<CacheLoader> i = writeCacheLoaders.iterator();
+ Iterator<CacheLoaderConfig.IndividualCacheLoaderConfig> cfgs = cacheLoaderConfigs.iterator();
while (i.hasNext())
{
- CacheLoader l = (CacheLoader) i.next();
- CacheLoaderConfig.IndividualCacheLoaderConfig cfg = (CacheLoaderConfig.IndividualCacheLoaderConfig) cfgs.next();
+ CacheLoader l = i.next();
+ CacheLoaderConfig.IndividualCacheLoaderConfig cfg = cfgs.next();
if (cfg.isFetchPersistentState())
{
l.storeEntireState(state);
@@ -359,66 +330,55 @@
*/
public void create() throws Exception
{
- Iterator it = cacheLoaders.iterator();
- Iterator cfgIt = cacheLoaderConfigs.iterator();
+ Iterator<CacheLoader> it = cacheLoaders.iterator();
+ Iterator<CacheLoaderConfig.IndividualCacheLoaderConfig> cfgIt = cacheLoaderConfigs.iterator();
while (it.hasNext() && cfgIt.hasNext())
{
- CacheLoader cl = (CacheLoader) it.next();
- CacheLoaderConfig.IndividualCacheLoaderConfig cfg = (CacheLoaderConfig.IndividualCacheLoaderConfig) cfgIt.next();
+ CacheLoader cl = it.next();
+ CacheLoaderConfig.IndividualCacheLoaderConfig cfg = cfgIt.next();
cl.create();
}
}
public void start() throws Exception
{
- Iterator it = cacheLoaders.iterator();
+ Iterator<CacheLoader> it = cacheLoaders.iterator();
while (it.hasNext())
{
- ((CacheLoader) it.next()).start();
+ (it.next()).start();
}
}
public void stop()
{
- Iterator it = cacheLoaders.iterator();
+ Iterator<CacheLoader> it = cacheLoaders.iterator();
while (it.hasNext())
{
- ((CacheLoader) it.next()).stop();
+ (it.next()).stop();
}
}
public void destroy()
{
- Iterator it = cacheLoaders.iterator();
+ Iterator<CacheLoader> it = cacheLoaders.iterator();
while (it.hasNext())
{
- ((CacheLoader) it.next()).destroy();
+ (it.next()).destroy();
}
}
-
- // ---------------------------------------------------- ExtendedCacheLoader
-
public byte[] loadState(Fqn subtree) throws Exception
{
byte[] answer = null;
- Iterator i = cacheLoaders.iterator();
- Iterator cfgs = cacheLoaderConfigs.iterator();
+ Iterator<CacheLoader> i = cacheLoaders.iterator();
+ Iterator<CacheLoaderConfig.IndividualCacheLoaderConfig> cfgs = cacheLoaderConfigs.iterator();
while (i.hasNext() && cfgs.hasNext())
{
- CacheLoader l = (CacheLoader) i.next();
- CacheLoaderConfig.IndividualCacheLoaderConfig cfg = (CacheLoaderConfig.IndividualCacheLoaderConfig) cfgs.next();
+ CacheLoader l = i.next();
+ CacheLoaderConfig.IndividualCacheLoaderConfig cfg = cfgs.next();
if (cfg.isFetchPersistentState())
{
- if (l instanceof ExtendedCacheLoader)
- {
- answer = ((ExtendedCacheLoader)l).loadState(subtree);
- }
- else
- {
- throw new Exception("Cache loader " + l +
- " does not implement ExtendedCacheLoader");
- }
+ answer = l.loadState(subtree);
break;
}
}
@@ -435,23 +395,15 @@
public void storeState(byte[] state, Fqn subtree) throws Exception
{
- Iterator i = writeCacheLoaders.iterator();
- Iterator cfgs = cacheLoaderConfigs.iterator();
+ Iterator<CacheLoader> i = writeCacheLoaders.iterator();
+ Iterator<CacheLoaderConfig.IndividualCacheLoaderConfig> cfgs = cacheLoaderConfigs.iterator();
while (i.hasNext())
{
- CacheLoader l = (CacheLoader) i.next();
- CacheLoaderConfig.IndividualCacheLoaderConfig cfg = (CacheLoaderConfig.IndividualCacheLoaderConfig) cfgs.next();
+ CacheLoader l = i.next();
+ CacheLoaderConfig.IndividualCacheLoaderConfig cfg = cfgs.next();
if (cfg.isFetchPersistentState())
{
- if (l instanceof ExtendedCacheLoader)
- {
- ((ExtendedCacheLoader)l).storeState(state, subtree);
- }
- else
- {
- throw new Exception("Cache loader " + l +
- " does not implement ExtendedCacheLoader");
- }
+ l.storeState(state, subtree);
break;
}
}
@@ -469,7 +421,7 @@
/**
* Returns a List<CacheLoader> of individual cache loaders configured.
*/
- public List getCacheLoaders()
+ public List<CacheLoader> getCacheLoaders()
{
return Collections.unmodifiableList(cacheLoaders);
}
@@ -496,13 +448,13 @@
public String toString()
{
StringBuffer buf = new StringBuffer("ChainingCacheLoader{");
- Iterator i = cacheLoaders.iterator();
- Iterator c = cacheLoaderConfigs.iterator();
+ Iterator<CacheLoader> i = cacheLoaders.iterator();
+ Iterator<CacheLoaderConfig.IndividualCacheLoaderConfig> c = cacheLoaderConfigs.iterator();
int count=0;
while (i.hasNext() && c.hasNext())
{
- CacheLoader loader = (CacheLoader) i.next();
- CacheLoaderConfig.IndividualCacheLoaderConfig cfg = (CacheLoaderConfig.IndividualCacheLoaderConfig) c.next();
+ CacheLoader loader = i.next();
+ CacheLoaderConfig.IndividualCacheLoaderConfig cfg = c.next();
buf.append(++count);
buf.append(": IgnoreMods? ");
@@ -517,13 +469,13 @@
public void purgeIfNecessary() throws Exception
{
- Iterator loaders = cacheLoaders.iterator();
- Iterator configs = cacheLoaderConfigs.iterator();
+ Iterator<CacheLoader> loaders = cacheLoaders.iterator();
+ Iterator<CacheLoaderConfig.IndividualCacheLoaderConfig> configs = cacheLoaderConfigs.iterator();
while (loaders.hasNext() && configs.hasNext())
{
- CacheLoader myLoader = (CacheLoader) loaders.next();
- CacheLoaderConfig.IndividualCacheLoaderConfig myConfig = (CacheLoaderConfig.IndividualCacheLoaderConfig) configs.next();
+ CacheLoader myLoader = loaders.next();
+ CacheLoaderConfig.IndividualCacheLoaderConfig myConfig = configs.next();
if (!myConfig.isIgnoreModifications() && myConfig.isPurgeOnStartup()) myLoader.remove(Fqn.ROOT);
}
1.10 +24 -15 JBossCache/src/org/jboss/cache/loader/ClusteredCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ClusteredCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/ClusteredCacheLoader.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- ClusteredCacheLoader.java 16 May 2006 22:42:28 -0000 1.9
+++ ClusteredCacheLoader.java 19 Jul 2006 08:29:18 -0000 1.10
@@ -10,17 +10,19 @@
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.Fqn;
import org.jboss.cache.TreeCache;
+import org.jboss.cache.Modification;
import org.jboss.cache.marshall.MethodCallFactory;
import org.jboss.cache.marshall.MethodDeclarations;
+import org.jboss.cache.marshall.RegionManager;
import org.jgroups.blocks.GroupRequest;
import org.jgroups.blocks.MethodCall;
+import org.jgroups.Address;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
-import java.util.Vector;
/**
* A cache loader that consults other members in the cluster for values. Does
@@ -31,10 +33,9 @@
*
* @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
*/
-public class ClusteredCacheLoader implements CacheLoader
+public class ClusteredCacheLoader extends AbstractCacheLoader
{
protected long timeout = 10000;
- protected TreeCache cache;
private static Log log = LogFactory.getLog(ClusteredCacheLoader.class);
/**
@@ -55,24 +56,19 @@
}
}
- public void setCache(TreeCache c)
- {
- this.cache = c;
- }
-
- public Set getChildrenNames(Fqn fqn) throws Exception
+ public Set<String> getChildrenNames(Fqn fqn) throws Exception
{
MethodCall call = MethodCallFactory.create(MethodDeclarations.getChildrenNamesMethodLocal, new Object[]{fqn});
Object resp = callRemote(call);
- return (Set) resp;
+ return (Set<String>) resp;
}
private Object callRemote(MethodCall call) throws Exception
{
if (log.isTraceEnabled()) log.trace("cache=" + cache.getLocalAddress() + "; calling with " + call);
- Vector mbrs = cache.getMembers();
+ List<Address> mbrs = cache.getMembers();
MethodCall clusteredGet = MethodCallFactory.create(MethodDeclarations.clusteredGetMethod, new Object[]{call, Boolean.FALSE});
- List resps = cache.callRemoteMethods(mbrs, clusteredGet, GroupRequest.GET_FIRST, true, timeout);
+ List resps = cache.getRPCManager().callRemoteMethods(mbrs, clusteredGet, GroupRequest.GET_FIRST, true, timeout);
if (resps == null)
{
log.error("No replies to call " + call + ". Perhaps we're alone in the cluster?");
@@ -93,9 +89,9 @@
else
{
// keep looping till we find a FOUND answer.
- List clusteredGetResp = (List) o;
+ List<Boolean> clusteredGetResp = (List<Boolean>) o;
// found?
- if (((Boolean) clusteredGetResp.get(0)).booleanValue())
+ if ((clusteredGetResp.get(0)).booleanValue())
{
result = clusteredGetResp.get(1);
break;
@@ -151,7 +147,7 @@
/**
* Does nothing; replication handles put.
*/
- public void put(List modifications) throws Exception
+ public void put(List<Modification> modifications) throws Exception
{
}
@@ -216,6 +212,19 @@
{
}
+ public byte[] loadState(Fqn subtree) throws Exception
+ {
+ return new byte[0];
+ }
+
+ public void storeState(byte[] state, Fqn subtree) throws Exception
+ {
+ }
+
+ public void setRegionManager(RegionManager manager)
+ {
+ }
+
public void create() throws Exception
{
}
1.9 +27 -42 JBossCache/src/org/jboss/cache/loader/DelegatingCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: DelegatingCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/DelegatingCacheLoader.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -b -r1.8 -r1.9
--- DelegatingCacheLoader.java 17 Jan 2006 15:31:21 -0000 1.8
+++ DelegatingCacheLoader.java 19 Jul 2006 08:29:18 -0000 1.9
@@ -10,9 +10,12 @@
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.Fqn;
import org.jboss.cache.Modification;
-import org.jboss.cache.TreeCache;
-import java.util.*;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
/**
* CacheLoader implementation which delegates to another TreeCache. This allows to stack caches on top of each
@@ -20,13 +23,13 @@
* which delegates to a persistent cache.
* @author Bela Ban
* @author Daniel Gredler
- * @version $Id: DelegatingCacheLoader.java,v 1.8 2006/01/17 15:31:21 bela Exp $
+ * @version $Id: DelegatingCacheLoader.java,v 1.9 2006/07/19 08:29:18 msurtani Exp $
*/
-public abstract class DelegatingCacheLoader implements CacheLoader {
+public abstract class DelegatingCacheLoader extends AbstractCacheLoader {
Log log=LogFactory.getLog(getClass());
/** HashMap<Object,List<Modification>>. List of open transactions. Note that this is purely transient, as
* we don't use a log, recovery is not available */
- HashMap transactions=new HashMap();
+ HashMap<Object,List<Modification>> transactions=new HashMap<Object, List<Modification>>();
public static final int delegateGetChildrenNames = 1;
public static final int delegateGetKey = 2;
@@ -45,10 +48,8 @@
public abstract void setConfig(Properties props);
- public abstract void setCache(TreeCache c);
-
- public Set getChildrenNames(Fqn fqn) throws Exception {
- Set retval=delegateGetChildrenNames(fqn);
+ public Set<String> getChildrenNames(Fqn fqn) throws Exception {
+ Set<String> retval=delegateGetChildrenNames(fqn);
return retval == null? null : (retval.size() == 0? null : retval);
}
@@ -83,7 +84,7 @@
put(fqn, attributes);
}
- public void put(List modifications) throws Exception {
+ public void put(List<Modification> modifications) throws Exception {
if(modifications == null || modifications.size() == 0) return;
delegatePut(modifications);
}
@@ -100,7 +101,7 @@
delegateRemoveData(name);
}
- public void prepare(Object tx, List modifications, boolean one_phase) throws Exception {
+ public void prepare(Object tx, List<Modification> modifications, boolean one_phase) throws Exception {
if(one_phase)
put(modifications);
else
@@ -108,7 +109,7 @@
}
public void commit(Object tx) throws Exception {
- List modifications=(List)transactions.get(tx);
+ List modifications=transactions.get(tx);
if(modifications == null)
throw new Exception("transaction " + tx + " not found in transaction table");
put(modifications);
@@ -126,35 +127,15 @@
delegateStoreEntireState(state);
}
-// protected void handleModifications(Modification modifications) {
-// for(Iterator it=modifications.iterator(); it.hasNext();) {
-// Modification m=(Modification)it.next();
-// switch(m.getType()) {
-// case Modification.PUT_DATA:
-// put(m.getFqn(), m.getData());
-// break;
-// case Modification.PUT_DATA_ERASE:
-// put(m.getFqn(), m.getData(), true);
-// break;
-// case Modification.PUT_KEY_VALUE:
-// put(m.getFqn(), m.getKey(), m.getValue());
-// break;
-// case Modification.REMOVE_DATA:
-// removeData(m.getFqn());
-// break;
-// case Modification.REMOVE_KEY_VALUE:
-// remove(m.getFqn(), m.getKey());
-// break;
-// case Modification.REMOVE_NODE:
-// remove(m.getFqn());
-// break;
-// default:
-// log.error("modification type " + m.getType() + " not known");
-// break;
-// }
-// }
-//
-// }
+ public void storeState(byte[] state, Fqn fqn) throws Exception
+ {
+ delegateStoreState(state, fqn);
+ }
+
+ public byte[] loadState(Fqn fqn) throws Exception
+ {
+ return delegateLoadState(fqn);
+ }
public void create() throws Exception {
// Empty.
@@ -197,9 +178,13 @@
protected abstract void delegateRemoveData(Fqn name) throws Exception;
+ protected abstract byte[] delegateLoadState(Fqn fqn) throws Exception;
+
protected abstract byte[] delegateLoadEntireState() throws Exception;
+ protected abstract void delegateStoreState(byte[] state, Fqn fqn) throws Exception;
+
protected abstract void delegateStoreEntireState(byte[] state) throws Exception;
- protected abstract void delegatePut(List modifications) throws Exception;
+ protected abstract void delegatePut(List<Modification> modifications) throws Exception;
}
1.16 +27 -41 JBossCache/src/org/jboss/cache/loader/FileCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: FileCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/FileCacheLoader.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -b -r1.15 -r1.16
--- FileCacheLoader.java 30 May 2006 12:42:31 -0000 1.15
+++ FileCacheLoader.java 19 Jul 2006 08:29:18 -0000 1.16
@@ -5,29 +5,38 @@
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.Fqn;
import org.jboss.cache.Modification;
-import org.jboss.cache.TreeCache;
import org.jboss.cache.buddyreplication.BuddyManager;
-import org.jboss.cache.marshall.RegionManager;
import org.jboss.invocation.MarshalledValueInputStream;
import org.jboss.invocation.MarshalledValueOutputStream;
-import java.io.*;
-import java.util.*;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
/**
* Simple file-based CacheLoader implementation. Nodes are directories, attributes of a node is a file in the directory
* @author Bela Ban
- * @version $Id: FileCacheLoader.java,v 1.15 2006/05/30 12:42:31 msurtani Exp $
+ * @version $Id: FileCacheLoader.java,v 1.16 2006/07/19 08:29:18 msurtani Exp $
*/
-public class FileCacheLoader implements ExtendedCacheLoader {
+public class FileCacheLoader extends AbstractCacheLoader
+{
File root=null;
- TreeCache cache=null;
Log log=LogFactory.getLog(getClass());
- RegionManager manager;
/** HashMap<Object,List<Modification>>. List of open transactions. Note that this is purely transient, as
* we don't use a log, recovery is not available */
- Map transactions = new ConcurrentHashMap();
+ Map<Object, List<Modification>> transactions = new ConcurrentHashMap();
/**
* TreeCache data file.
@@ -48,10 +57,6 @@
root=new File(location);
}
- public void setCache(TreeCache c) {
- cache=c;
- }
-
public void create() throws Exception {
if(root == null) {
String tmpLocation=System.getProperty("java.io.tmpdir", "C:\\tmp");
@@ -78,13 +83,11 @@
public void destroy() {
}
-
-
- public Set getChildrenNames(Fqn fqn) throws Exception {
+ public Set<String> getChildrenNames(Fqn fqn) throws Exception {
File parent=getDirectory(fqn, false);
if(parent == null) return null;
File[] children=parent.listFiles();
- HashSet s=new HashSet();
+ Set<String> s=new HashSet<String>();
for(int i=0; i < children.length; i++) {
File child=children[i];
if(child.isDirectory() && child.getName().endsWith(DIR_SUFFIX)) {
@@ -96,19 +99,8 @@
return s.size() == 0? null : s;
}
- // See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
-
-// public Object get(Fqn fqn, Object key) throws Exception {
-// Map m=loadAttributes(fqn);
-// if(m == null) return null;
-// return m.get(key);
-// }
-
public Map get(Fqn fqn) throws Exception {
return loadAttributes(fqn);
-// Map m=loadAttributes(fqn);
-// if(m == null || m.size() == 0) return null;
-// return m;
}
public boolean exists(Fqn fqn) throws Exception {
@@ -207,7 +199,7 @@
}
}
- public void prepare(Object tx, List modifications, boolean one_phase) throws Exception {
+ public void prepare(Object tx, List<Modification> modifications, boolean one_phase) throws Exception {
if(one_phase)
put(modifications);
else
@@ -215,7 +207,7 @@
}
public void commit(Object tx) throws Exception {
- List modifications=(List)transactions.remove(tx);
+ List modifications=transactions.remove(tx);
if (modifications == null)
throw new Exception("transaction " + tx + " not found in transaction table");
put(modifications);
@@ -308,11 +300,6 @@
}
}
- public void setRegionManager(RegionManager manager)
- {
- this.manager = manager;
- }
-
/* ----------------------- Private methods ------------------------ */
@@ -365,7 +352,6 @@
* Recursively removes this and all subdirectories, plus all DATA files in them. To prevent damage, we only
* remove files that are named DATA (data.dat) and directories which end in ".fdb". If there is a dir or file
* that isn't named this way, the recursive removal will fail
- * @param fqn
* @return <code>true</code> if directory was removed,
* <code>false</code> if not.
*/
@@ -447,9 +433,9 @@
*/
private void setUnmarshallingClassLoader(Fqn subtree)
{
- if (manager != null)
+ if (regionManager != null)
{
- manager.setUnmarshallingClassLoader(subtree);
+ regionManager.setUnmarshallingClassLoader(subtree);
}
}
1.12 +181 -185 JBossCache/src/org/jboss/cache/loader/JDBCCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: JDBCCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/JDBCCacheLoader.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -b -r1.11 -r1.12
--- JDBCCacheLoader.java 16 Feb 2006 10:59:34 -0000 1.11
+++ JDBCCacheLoader.java 19 Jul 2006 08:29:18 -0000 1.12
@@ -10,7 +10,7 @@
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.Fqn;
import org.jboss.cache.Modification;
-import org.jboss.cache.TreeCache;
+import org.jboss.cache.buddyreplication.BuddyManager;
import org.jboss.invocation.MarshalledValue;
import org.jboss.invocation.MarshalledValueInputStream;
import org.jboss.invocation.MarshalledValueOutputStream;
@@ -18,9 +18,21 @@
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
-import java.io.*;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.rmi.MarshalledObject;
-import java.sql.*;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.sql.Types;
import java.util.*;
/**
@@ -63,10 +75,9 @@
*
* @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
* @author <a href="mailto:hmesha at novell.com">Hany Mesha </a>
- * @version <tt>$Revision: 1.11 $</tt>
+ * @version <tt>$Revision: 1.12 $</tt>
*/
-public class JDBCCacheLoader
- implements CacheLoader
+public class JDBCCacheLoader extends AbstractCacheLoader
{
private static final Log log=LogFactory.getLog(JDBCCacheLoader.class);
@@ -92,8 +103,6 @@
private String datasourceName;
private ConnectionFactory cf;
-
-
public void setConfig(Properties props)
{
datasourceName = props.getProperty("cache.jdbc.datasource");
@@ -171,11 +180,6 @@
dropTableDdl = "drop table " + table;
}
- public void setCache(TreeCache c)
- {
- //todo setCache(TreeCache c)
- }
-
/**
* Fetches child node names (not pathes).
*
@@ -183,7 +187,7 @@
* @return a set of child node names or null if there are not children found for the fqn
* @throws Exception
*/
- public Set getChildrenNames(Fqn fqn) throws Exception
+ public Set<String> getChildrenNames(Fqn fqn) throws Exception
{
Set children = null;
Connection con = null;
@@ -341,11 +345,11 @@
put(name, attributes, false);
}
- public void put(List modifications) throws Exception
+ public void put(List<Modification> modifications) throws Exception
{
for(int i = 0; i < modifications.size(); ++i)
{
- Modification m = (Modification) modifications.get(i);
+ Modification m = modifications.get(i);
switch(m.getType())
{
case Modification.PUT_DATA:
@@ -505,7 +509,7 @@
* @param one_phase indicates whether it's one or two phase commit transaction
* @throws Exception
*/
- public void prepare(Object tx, List modifications, boolean one_phase) throws Exception
+ public void prepare(Object tx, List<Modification> modifications, boolean one_phase) throws Exception
{
// start a tx
//JBCACHE-346 fix, we don't need to prepare a DataSource object (Managed connection)
@@ -558,111 +562,103 @@
}
/**
- * WARN: this was copied from other cache loader implementation
+ * Checks the RegionManager for a classloader registered for the
+ * given, and if found sets it as the TCCL
*
- * @return
- * @throws Exception
+ * @param subtree
*/
-/*
- public byte[] loadEntireState() throws Exception
+ private void setUnmarshallingClassLoader(Fqn subtree)
{
- ByteArrayOutputStream out_stream = new ByteArrayOutputStream(1024);
- ObjectOutputStream out = new ObjectOutputStream(out_stream);
- loadState(Fqn.fromString("/"), out);
- out.close();
- return out_stream.toByteArray();
+ if (regionManager != null)
+ {
+ regionManager.setUnmarshallingClassLoader(subtree);
}
-*/
+ }
+
+
/**
- * Loads the entire state from the filesystem and returns it as a byte buffer. The format of the byte buffer
- * must be a list of NodeData elements
- * @return
- * @throws Exception
+ * Overrides the {@link FileCacheLoader#loadEntireState() superclass method}
+ * by taking advantage of any special classloader registered for the
+ * root node.
*/
- public byte[] loadEntireState() throws Exception {
- ByteArrayOutputStream out_stream=new ByteArrayOutputStream(1024);
- ObjectOutputStream out=new MarshalledValueOutputStream(out_stream);
- loadState(Fqn.fromString("/"), out);
- out.close();
- return out_stream.toByteArray();
+ public byte[] loadEntireState() throws Exception
+ {
+ return loadState(Fqn.ROOT);
}
-
/**
- * WARN: this was copied from other cache loader implementation
- *
- * @param state
- * @throws Exception
+ * Overrides the {@link FileCacheLoader#storeEntireState(byte[])} superclass method}
+ * by taking advantage of any special classloader registered for the
+ * root node.
*/
-/* public void storeEntireState(byte[] state) throws Exception
+ public void storeEntireState(byte[] state) throws Exception
{
- Fqn fqn = null;
- Map map;
- int num_attrs = 0;
- ByteArrayInputStream in_stream = new ByteArrayInputStream(state);
- MarshalledValueInputStream in = new MarshalledValueInputStream(in_stream);
-
- // remove previous state
- this.remove(Fqn.fromString("/"));
+ storeState(state, Fqn.ROOT);
+ }
- // store new state
- try
+ public byte[] loadState(Fqn subtree) throws Exception
{
- while(true)
- {
- map = null;
- fqn = (Fqn) in.readObject();
- num_attrs = in.readInt();
- if(num_attrs > -1)
- {
- map = (Map) in.readObject();
- }
- if(map != null)
+ ClassLoader currentCL = Thread.currentThread().getContextClassLoader();
+ try
{
- this.put(fqn, map, true); // creates a node with 0 or more attributes
+ // Set the TCCL to any classloader registered for subtree
+ setUnmarshallingClassLoader(subtree);
+
+ ByteArrayOutputStream out_stream=new ByteArrayOutputStream(1024);
+ ObjectOutputStream out=new MarshalledValueOutputStream(out_stream);
+ loadState(subtree, out);
+ out.close();
+ return out_stream.toByteArray();
}
- else
+ finally
{
- this.put(fqn, null); // creates a node with null attributes
+ Thread.currentThread().setContextClassLoader(currentCL);
}
}
- }
- catch(EOFException eof_ex)
+
+ public void storeState(byte[] state, Fqn subtree) throws Exception
{
- }
- }*/
+ ClassLoader currentCL = Thread.currentThread().getContextClassLoader();
+ try
+ {
+ // Set the TCCL to any classloader registered for subtree
+ setUnmarshallingClassLoader(subtree);
- /** Store the state given as a byte buffer to the database. The byte buffer contains a list
- * of zero or more NodeData elements
- * @param state
- * @throws Exception
- */
- public void storeEntireState(byte[] state) throws Exception {
ByteArrayInputStream in_stream=new ByteArrayInputStream(state);
MarshalledValueInputStream in=new MarshalledValueInputStream(in_stream);
NodeData nd;
// remove entire existing state
- this.remove(Fqn.fromString("/"));
+ this.remove(subtree);
+
+ boolean moveToBuddy =
+ subtree.isChildOf(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN) && subtree.size() > 1;
// store new state
- try {
- while(true) {
- nd=(NodeData) in.readObject();
+ Fqn fqn = null;
+ while(in.available() > 0)
+ {
+ nd=(NodeData)in.readObject();
+
+ if (moveToBuddy)
+ fqn = BuddyManager.getBackupFqn(subtree, nd.fqn);
+ else
+ fqn = nd.fqn;
+
if(nd.attrs != null)
- this.put(nd.fqn, nd.attrs, true); // creates a node with 0 or more attributes
+ this.put(fqn, nd.attrs, true); // creates a node with 0 or more attributes
else
- this.put(nd.fqn, null); // creates a node with null attributes
+ this.put(fqn, null); // creates a node with null attributes
}
}
- catch(EOFException eof_ex) {
+ finally
+ {
+ Thread.currentThread().setContextClassLoader(currentCL);
}
}
-
-
// Service implementation
public void create() throws Exception
1.8 +20 -15 JBossCache/src/org/jboss/cache/loader/LocalDelegatingCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: LocalDelegatingCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/LocalDelegatingCacheLoader.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- LocalDelegatingCacheLoader.java 17 Jan 2006 13:35:36 -0000 1.7
+++ LocalDelegatingCacheLoader.java 19 Jul 2006 08:29:18 -0000 1.8
@@ -26,7 +26,7 @@
* </pre>
* @author Bela Ban
* @author Daniel Gredler
- * @version $Id: LocalDelegatingCacheLoader.java,v 1.7 2006/01/17 13:35:36 bela Exp $
+ * @version $Id: LocalDelegatingCacheLoader.java,v 1.8 2006/07/19 08:29:18 msurtani Exp $
*/
public class LocalDelegatingCacheLoader extends DelegatingCacheLoader {
@@ -43,10 +43,6 @@
public void setConfig(Properties props) {
}
- public void setCache(TreeCache cache) {
- // empty
- }
-
protected Set delegateGetChildrenNames(Fqn fqn) throws Exception {
return delegate.getChildrenNames(fqn);
}
@@ -82,9 +78,8 @@
delegate.put(name, attributes);
}
- protected void delegatePut(List modifications) throws Exception {
- for(Iterator it=modifications.iterator(); it.hasNext();) {
- Modification m=(Modification)it.next();
+ protected void delegatePut(List<Modification> modifications) throws Exception {
+ for(Modification m: modifications) {
switch(m.getType()) {
case Modification.PUT_DATA:
put(m.getFqn(), m.getData());
@@ -123,10 +118,20 @@
delegate.removeData(name);
}
+ protected byte[] delegateLoadState(Fqn fqn) throws Exception
+ {
+ throw new UnsupportedOperationException("setting and loading state for specific Fqns not supported");
+ }
+
protected byte[] delegateLoadEntireState() throws Exception {
return delegate.getStateBytes();
}
+ protected void delegateStoreState(byte[] state, Fqn fqn) throws Exception
+ {
+ throw new UnsupportedOperationException("setting and loading state for specific Fqns not supported");
+ }
+
protected void delegateStoreEntireState(byte[] state) throws Exception {
delegate.setStateBytes(state);
}
1.8 +30 -19 JBossCache/src/org/jboss/cache/loader/RmiDelegatingCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: RmiDelegatingCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/RmiDelegatingCacheLoader.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- RmiDelegatingCacheLoader.java 3 Feb 2006 17:12:21 -0000 1.7
+++ RmiDelegatingCacheLoader.java 19 Jul 2006 08:29:18 -0000 1.8
@@ -6,16 +6,18 @@
*/
package org.jboss.cache.loader;
-import org.jboss.cache.Fqn;
import org.jboss.cache.DataNode;
+import org.jboss.cache.Fqn;
import org.jboss.cache.TreeCache;
+import org.jboss.cache.CacheSPI;
+import org.jboss.cache.Modification;
import org.jboss.cache.loader.rmi.RemoteTreeCache;
import java.rmi.Naming;
+import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
-import java.util.List;
/**
* DelegatingCacheLoader implementation which delegates to a remote (not in the same VM)
@@ -31,7 +33,7 @@
* cacheloader's cache's cluster name.
*
* @author Daniel Gredler
- * @version $Id: RmiDelegatingCacheLoader.java,v 1.7 2006/02/03 17:12:21 msurtani Exp $
+ * @version $Id: RmiDelegatingCacheLoader.java,v 1.8 2006/07/19 08:29:18 msurtani Exp $
*/
public class RmiDelegatingCacheLoader extends DelegatingCacheLoader {
@@ -58,7 +60,6 @@
* @param bindName The name to which the remote object is bound.
*/
public RmiDelegatingCacheLoader(TreeCache cache, String host, int port, String bindName) {
- this.cache = cache;
this.host = host;
this.port = String.valueOf(port);
this.bindName = bindName;
@@ -87,10 +88,10 @@
/**
* Allows configuration via XML config file.
*
- * @see org.jboss.cache.loader.DelegatingCacheLoader#setCache(org.jboss.cache.TreeCache)
+ * @see org.jboss.cache.loader.DelegatingCacheLoader#setCache(org.jboss.cache.CacheSPI)
*/
- public void setCache(TreeCache cache) {
- this.cache = cache;
+ public void setCache(CacheSPI cache) {
+ super.setCache(cache);
this.tryToInitRemoteCache();
}
@@ -140,7 +141,7 @@
if( this.remoteCache != null ) this.remoteCache.put(name, attributes);
}
- protected void delegatePut(List modifications) throws Exception {
+ protected void delegatePut(List<Modification> modifications) throws Exception {
}
@@ -165,12 +166,22 @@
if( this.remoteCache != null ) this.remoteCache.removeData(name);
}
+ protected byte[] delegateLoadState(Fqn fqn) throws Exception
+ {
+ throw new UnsupportedOperationException("setting and loading state for specific Fqns not supported");
+ }
+
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateLoadEntireState()
*/
public byte[] delegateLoadEntireState() throws Exception {
throw new UnsupportedOperationException("operation is not currently supported - need to define semantics first"); }
+ protected void delegateStoreState(byte[] state, Fqn fqn) throws Exception
+ {
+ throw new UnsupportedOperationException("setting and loading state for specific Fqns not supported");
+ }
+
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateStoreEntireState(byte[])
*/
1.8 +45 -40 JBossCache/src/org/jboss/cache/loader/RpcDelegatingCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: RpcDelegatingCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/RpcDelegatingCacheLoader.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- RpcDelegatingCacheLoader.java 16 May 2006 22:42:28 -0000 1.7
+++ RpcDelegatingCacheLoader.java 19 Jul 2006 08:29:18 -0000 1.8
@@ -6,17 +6,22 @@
*/
package org.jboss.cache.loader;
-import org.jboss.cache.Fqn;
+import org.jboss.cache.CacheSPI;
import org.jboss.cache.DataNode;
-import org.jboss.cache.TreeCache;
+import org.jboss.cache.Fqn;
import org.jboss.cache.Modification;
+import org.jboss.cache.TreeCache;
import org.jboss.cache.lock.TimeoutException;
import org.jboss.cache.marshall.MethodCallFactory;
import org.jgroups.Address;
import org.jgroups.blocks.MethodCall;
import java.lang.reflect.Method;
-import java.util.*;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.Vector;
/**
* DelegatingCacheLoader implementation which delegates to a remote (not in the same VM)
@@ -28,12 +33,11 @@
* specified, it defaults to <tt>5000</tt>.
*
* @author Daniel Gredler
- * @version $Id: RpcDelegatingCacheLoader.java,v 1.7 2006/05/16 22:42:28 gzamarreno Exp $
+ * @version $Id: RpcDelegatingCacheLoader.java,v 1.8 2006/07/19 08:29:18 msurtani Exp $
*/
public class RpcDelegatingCacheLoader extends DelegatingCacheLoader {
private int timeout;
- private TreeCache cache;
private Address localAddress;
public static final Method METHOD_GET_STATE;
@@ -65,9 +69,10 @@
METHOD_REMOVE_WITH_2_PARAMS = TreeCache.class.getDeclaredMethod("remove", new Class[] { Fqn.class, Object.class });
METHOD_REMOVE_WITH_1_PARAM = TreeCache.class.getDeclaredMethod("remove", new Class[] { Fqn.class });
METHOD_REMOVE_DATA = TreeCache.class.getDeclaredMethod("removeData", new Class[] { Fqn.class });
- } catch (NoSuchMethodException ex) {
- ex.printStackTrace();
- throw new ExceptionInInitializerError(ex.toString());
+ }
+ catch (NoSuchMethodException ex)
+ {
+ throw new ExceptionInInitializerError(ex);
}
}
@@ -83,8 +88,8 @@
*
* @param timeout The timeout in milliseconds for each RPC call.
*/
- public RpcDelegatingCacheLoader(TreeCache cache, int timeout) {
- this.cache = cache;
+ public RpcDelegatingCacheLoader(CacheSPI cache, int timeout) {
+ setCache(cache);
this.timeout = timeout;
}
@@ -100,15 +105,6 @@
}
/**
- * Allows configuration via XML config file.
- *
- * @see org.jboss.cache.loader.DelegatingCacheLoader#setCache(org.jboss.cache.TreeCache)
- */
- public void setCache(TreeCache cache) {
- this.cache = cache;
- }
-
- /**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGetChildrenNames(org.jboss.cache.Fqn)
*/
protected Set delegateGetChildrenNames(Fqn name) throws Exception {
@@ -155,10 +151,9 @@
this.doMethodCall( METHOD_PUT_WITH_2_PARAMS, new Object[] { name, attributes } );
}
- // todo: we should probably implement put(List) in TreeCache itself, so we don't need to invoke each mod separately
- protected void delegatePut(List modifications) throws Exception {
- for(Iterator it=modifications.iterator(); it.hasNext();) {
- Modification m=(Modification)it.next();
+ protected void delegatePut(List<Modification> modifications) throws Exception {
+ for (Modification m :modifications)
+ {
switch(m.getType()) {
case Modification.PUT_DATA:
put(m.getFqn(), m.getData());
@@ -206,6 +201,11 @@
this.doMethodCall( METHOD_REMOVE_DATA, new Object[] { name } );
}
+ protected byte[] delegateLoadState(Fqn fqn) throws Exception
+ {
+ throw new UnsupportedOperationException("setting and loading state for specific Fqns not supported");
+ }
+
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateLoadEntireState()
*/
@@ -213,6 +213,11 @@
return (byte[]) this.doMethodCall( METHOD_GET_STATE, new Object[0] );
}
+ protected void delegateStoreState(byte[] state, Fqn fqn) throws Exception
+ {
+ throw new UnsupportedOperationException("setting and loading state for specific Fqns not supported");
+ }
+
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateStoreEntireState(byte[])
*/
@@ -230,7 +235,7 @@
* @return The value returned by the remote method call.
*/
private Object doMethodCall( Method method, Object[] args ) throws Exception {
- if( this.cache.isCoordinator() ) {
+ if( this.cache.getRPCManager().isCoordinator() ) {
if( log.isTraceEnabled() ) {
log.trace( "Cannot delegate to the remote coordinator because the cache is itself the coordinator." );
}
@@ -242,16 +247,16 @@
if( this.localAddress == null ) {
throw new Exception( "Cannot delegate to the remote coordinator because the cache has no local address." );
}
- Address coordinator = cache.getCoordinator();
+ Address coordinator = cache.getRPCManager().getCoordinator();
if( coordinator == null ) {
throw new Exception( "Cannot delegate to the remote coordinator because the cache has no coordinator." );
}
- Vector members = new Vector();
+ Vector<Address> members = new Vector<Address>();
members.add( coordinator );
MethodCall methodCall = MethodCallFactory.create( method, args );
boolean synchronous = true;
boolean excludeSelf = true;
- List responses = cache.callRemoteMethods( members, methodCall, synchronous, excludeSelf, this.timeout );
+ List responses = cache.getRPCManager().callRemoteMethods( members, methodCall, synchronous, excludeSelf, this.timeout );
if( responses == null ) {
throw new Exception( "Remote method call [" + cache.getLocalAddress() + "]->[" + coordinator + "]." + methodCall.getMethod().getName() + "() was discarded!" );
}
1.8 +44 -26 JBossCache/src/org/jboss/cache/loader/SharedStoreCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: SharedStoreCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/SharedStoreCacheLoader.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- SharedStoreCacheLoader.java 18 Jul 2006 10:50:45 -0000 1.7
+++ SharedStoreCacheLoader.java 19 Jul 2006 08:29:18 -0000 1.8
@@ -2,13 +2,19 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.jboss.cache.Fqn;
-import org.jboss.cache.TreeCache;
import org.jboss.cache.AbstractCacheListener;
+import org.jboss.cache.CacheSPI;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.Modification;
+import org.jboss.cache.marshall.RegionManager;
import org.jgroups.Address;
import org.jgroups.View;
-import java.util.*;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.Vector;
/**
* CacheLoader proxy used only when multiple CacheLoaders in a cluster access the same underlying store (e.g.
@@ -20,15 +26,15 @@
* Whenever the current coordinator dies (or leaves), the second in line will take over. That SharedStoreCacheLoader
* will then pass writes through to its underlying CacheLoader.
* @author Bela Ban
- * @version $Id: SharedStoreCacheLoader.java,v 1.7 2006/07/18 10:50:45 msurtani Exp $
+ * @version $Id: SharedStoreCacheLoader.java,v 1.8 2006/07/19 08:29:18 msurtani Exp $
*/
-public class SharedStoreCacheLoader extends AbstractCacheListener implements CacheLoader {
+public class SharedStoreCacheLoader extends AbstractCacheListener implements CacheLoader
+{
private Log log=LogFactory.getLog(SharedStoreCacheLoader.class);
private CacheLoader loader=null;
private Address local_addr=null;
private boolean active=true; // only active if coordinator
- private TreeCache cache;
public SharedStoreCacheLoader(CacheLoader loader, Address local_addr, boolean coordinator) {
this.loader=loader;
@@ -53,18 +59,14 @@
loader.setConfig(props);
}
- public void setCache(TreeCache c) {
- this.cache=c;
+ public void setCache(CacheSPI c)
+ {
loader.setCache(c);
}
- public Set getChildrenNames(Fqn fqn) throws Exception {
+ public Set<String> getChildrenNames(Fqn fqn) throws Exception {
return loader.getChildrenNames(fqn);
}
-// See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
-// public Object get(Fqn name, Object key) throws Exception {
-// return loader.get(name, key);
-// }
public Map get(Fqn name) throws Exception {
return loader.get(name);
@@ -86,7 +88,8 @@
loader.put(name, attributes);
}
- public void put(List modifications) throws Exception {
+ public void put(List<Modification> modifications) throws Exception
+ {
if(active)
loader.put(modifications);
}
@@ -107,7 +110,8 @@
loader.removeData(name);
}
- public void prepare(Object tx, List modifications, boolean one_phase) throws Exception {
+ public void prepare(Object tx, List<Modification> modifications, boolean one_phase) throws Exception
+ {
if(active)
loader.prepare(tx, modifications, one_phase);
}
@@ -131,6 +135,20 @@
loader.storeEntireState(state);
}
+ public byte[] loadState(Fqn subtree) throws Exception
+ {
+ return loader.loadState(subtree);
+ }
+
+ public void storeState(byte[] state, Fqn subtree) throws Exception
+ {
+ if (active) loader.storeState(state, subtree);
+ }
+
+ public void setRegionManager(RegionManager manager)
+ {
+ }
+
public void create() throws Exception {
loader.create();
}
1.1 date: 2006/07/19 08:29:18; author: msurtani; state: Exp;JBossCache/src/org/jboss/cache/loader/AbstractCacheLoader.java
Index: AbstractCacheLoader.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.loader;
import org.jboss.cache.CacheSPI;
import org.jboss.cache.marshall.RegionManager;
/**
* A convenience abstract implementation of a {@link org.jboss.cache.CacheLoader}
*
* @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
*/
public abstract class AbstractCacheLoader implements CacheLoader
{
protected CacheSPI cache;
protected RegionManager regionManager;
public void setCache(CacheSPI c)
{
this.cache = c;
}
public void setRegionManager(RegionManager regionManager)
{
this.regionManager = regionManager;
}
}
1.1 date: 2006/07/19 08:29:18; author: msurtani; state: Exp;JBossCache/src/org/jboss/cache/loader/TcpDelegatingCacheLoader.java
Index: TcpDelegatingCacheLoader.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.loader;
import org.jboss.cache.Fqn;
import org.jboss.cache.Modification;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* DelegatingCacheLoader implementation which delegates to a remote (not in the same VM)
* TreeCache using TCP/IP for communication. Example configuration for connecting to a TcpCacheServer
* running at myHost:12345:<pre>
* <attribute name="CacheLoaderClass">org.jboss.cache.loader.TcpDelegatingCacheLoader</attribute>
<attribute name="CacheLoaderConfig">
host=localhost
port=2099
</attribute>
</pre>
*
* @author Bela Ban
* @version $Id: TcpDelegatingCacheLoader.java,v 1.1 2006/07/19 08:29:18 msurtani Exp $
*/
public class TcpDelegatingCacheLoader extends DelegatingCacheLoader {
private Socket sock;
private String host;
private int port;
ObjectInputStream in;
ObjectOutputStream out;
/**
* Default constructor.
*/
public TcpDelegatingCacheLoader() {
// Empty.
}
/**
* Allows programmatic configuration.
*
* @param host The host on which to look up the remote object.
* @param port The port on which to look up the remote object.
*/
public TcpDelegatingCacheLoader(String host, int port) {
this.host = host;
this.port = port;
}
/**
* Allows configuration via XML config file.
*
* @see org.jboss.cache.loader.DelegatingCacheLoader#setConfig(java.util.Properties)
*/
public void setConfig(Properties props) {
this.host = props.getProperty("host");
if(this.host == null || this.host.length() == 0) {
this.host = "localhost";
}
this.port = Integer.parseInt(props.getProperty("port"));
}
public void start() throws Exception {
init();
}
public void stop() {
try {if(in != null) in.close();} catch(IOException e) {}
try {if(out != null) out.close();} catch(IOException e) {}
try {if(sock != null) sock.close();} catch(IOException e) {}
}
private void init() throws IOException {
if(host == null)
host="localhost";
sock=new Socket(host, port);
out=new ObjectOutputStream(sock.getOutputStream());
in=new ObjectInputStream(sock.getInputStream());
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGetChildrenNames(org.jboss.cache.Fqn)
*/
protected Set delegateGetChildrenNames(Fqn fqn) throws Exception {
out.writeInt(DelegatingCacheLoader.delegateGetChildrenNames);
out.writeObject(fqn);
Object retval=in.readObject();
if(retval instanceof Exception)
throw (Exception)retval;
return (Set)retval;
}
// See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGet(org.jboss.cache.Fqn, Object)
*/
// protected Object delegateGet(Fqn name, Object key) throws Exception {
// out.writeInt(DelegatingCacheLoader.delegateGetKey);
// out.writeObject(name);
// out.writeObject(key);
// return in.readObject();
// }
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGet(org.jboss.cache.Fqn)
*/
protected Map delegateGet(Fqn name) throws Exception {
out.writeInt(DelegatingCacheLoader.delegateGet);
out.writeObject(name);
Object retval=in.readObject();
if(retval instanceof Exception)
throw (Exception)retval;
return (Map)retval;
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateExists(org.jboss.cache.Fqn)
*/
protected boolean delegateExists(Fqn name) throws Exception {
out.writeInt(DelegatingCacheLoader.delegateExists);
out.writeObject(name);
Object retval=in.readObject();
if(retval instanceof Exception)
throw (Exception)retval;
return ((Boolean)retval).booleanValue();
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegatePut(org.jboss.cache.Fqn, Object, Object)
*/
protected Object delegatePut(Fqn name, Object key, Object value) throws Exception {
out.writeInt(DelegatingCacheLoader.delegatePutKeyVal);
out.writeObject(name);
out.writeObject(key);
out.writeObject(value);
Object retval=in.readObject();
if(retval instanceof Exception)
throw (Exception)retval;
return retval;
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegatePut(org.jboss.cache.Fqn, java.util.Map)
*/
protected void delegatePut(Fqn name, Map attributes) throws Exception {
out.writeInt(DelegatingCacheLoader.delegatePut);
out.writeObject(name);
out.writeObject(attributes);
out.flush();
Object retval=in.readObject();
if(retval instanceof Exception)
throw (Exception)retval;
}
protected void delegatePut(List<Modification> modifications) throws Exception {
out.writeInt(DelegatingCacheLoader.putList);
int length=modifications != null? modifications.size() : 0;
out.writeInt(length);
if(length > 0) {
for(Modification m : modifications)
{
m.writeExternal(out);
}
}
out.flush();
Object retval=in.readObject();
if(retval instanceof Exception)
throw (Exception)retval;
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemove(org.jboss.cache.Fqn, Object)
*/
protected Object delegateRemove(Fqn name, Object key) throws Exception {
out.writeInt(DelegatingCacheLoader.delegateRemoveKey);
out.writeObject(name);
out.writeObject(key);
Object retval=in.readObject();
if(retval instanceof Exception)
throw (Exception)retval;
return retval;
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemove(org.jboss.cache.Fqn)
*/
protected void delegateRemove(Fqn name) throws Exception {
out.writeInt(DelegatingCacheLoader.delegateRemove);
out.writeObject(name);
out.flush();
Object retval=in.readObject();
if(retval instanceof Exception)
throw (Exception)retval;
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemoveData(org.jboss.cache.Fqn)
*/
protected void delegateRemoveData(Fqn name) throws Exception {
out.writeInt(DelegatingCacheLoader.delegateRemoveData);
out.writeObject(name);
out.flush();
Object retval=in.readObject();
if(retval instanceof Exception)
throw (Exception)retval;
}
protected byte[] delegateLoadState(Fqn fqn) throws Exception
{
throw new UnsupportedOperationException("operation is not currently supported - need to define semantics first");
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateLoadEntireState()
*/
public byte[] delegateLoadEntireState() throws Exception {
throw new UnsupportedOperationException("operation is not currently supported - need to define semantics first");
// out.writeInt(DelegatingCacheLoader.delegateLoadEntireState);
// out.flush();
// Object retval=in.readObject();
// if(retval instanceof Exception)
// throw (Exception)retval;
// return (byte[])retval;
}
protected void delegateStoreState(byte[] state, Fqn fqn) throws Exception
{
throw new UnsupportedOperationException("operation is not currently supported - need to define semantics first");
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateStoreEntireState(byte[])
*/
public void delegateStoreEntireState(byte[] state) throws Exception {
throw new UnsupportedOperationException("operation is not currently supported - need to define semantics first");
// out.writeInt(DelegatingCacheLoader.delegateStoreEntireState);
// out.writeObject(state);
// out.flush();
// Object retval=in.readObject();
// if(retval instanceof Exception)
// throw (Exception)retval;
}
}
More information about the jboss-cvs-commits
mailing list