[jboss-cvs] JBossCache/src/org/jboss/cache/loader ...
Manik Surtani
msurtani at jboss.com
Fri Sep 22 12:27:56 EDT 2006
User: msurtani
Date: 06/09/22 12:27:56
Modified: src/org/jboss/cache/loader AbstractCacheLoader.java
AsyncCacheLoader.java DelegatingCacheLoader.java
FileCacheLoader.java JDBCCacheLoader.java
LocalDelegatingCacheLoader.java
RmiDelegatingCacheLoader.java
RpcDelegatingCacheLoader.java
TcpDelegatingCacheLoader.java
Log:
- Modification types to Enums.
- Abstracted put(List)
Revision Changes Path
1.6 +9 -8 JBossCache/src/org/jboss/cache/loader/AbstractCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: AbstractCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/AbstractCacheLoader.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -b -r1.5 -r1.6
--- AbstractCacheLoader.java 18 Sep 2006 20:48:44 -0000 1.5
+++ AbstractCacheLoader.java 22 Sep 2006 16:27:56 -0000 1.6
@@ -22,9 +22,10 @@
import java.util.Set;
/**
- * A convenience abstract implementation of a {@link org.jboss.cache.CacheLoader}
+ * A convenience abstract implementation of a {@link CacheLoader}
*
* @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
+ * @since 2.0.0
*/
public abstract class AbstractCacheLoader implements CacheLoader
{
@@ -189,23 +190,23 @@
{
switch (m.getType())
{
- case Modification.PUT_DATA:
+ case PUT_DATA:
put(m.getFqn(), m.getData());
break;
- case Modification.PUT_DATA_ERASE:
+ case PUT_DATA_ERASE:
removeData(m.getFqn());
put(m.getFqn(), m.getData());
break;
- case Modification.PUT_KEY_VALUE:
+ case PUT_KEY_VALUE:
put(m.getFqn(), m.getKey(), m.getValue());
break;
- case Modification.REMOVE_DATA:
+ case REMOVE_DATA:
removeData(m.getFqn());
break;
- case Modification.REMOVE_KEY_VALUE:
+ case REMOVE_KEY_VALUE:
remove(m.getFqn(), m.getKey());
break;
- case Modification.REMOVE_NODE:
+ case REMOVE_NODE:
remove(m.getFqn());
break;
}
1.18 +161 -113 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.17
retrieving revision 1.18
diff -u -b -r1.17 -r1.18
--- AsyncCacheLoader.java 18 Sep 2006 20:48:44 -0000 1.17
+++ AsyncCacheLoader.java 22 Sep 2006 16:27:56 -0000 1.18
@@ -28,50 +28,50 @@
/**
* The AsyncCacheLoader is a delegating cache loader that passes on all
* operations to an underlying CacheLoader.
- *
+ * <p/>
* Read operations are done synchronously, while write (CRUD - Create, Remove,
* Update, Delete) operations are done asynchronously. There is no provision
* for exception handling at the moment for problems encountered with the
* underlying CacheLoader during a CRUD operation, and the exception is just
* logged.
- *
+ * <p/>
* When configuring the CacheLoader, use the following attribute:
- *
+ * <p/>
* <code>
* <attribute name="CacheLoaderAsynchronous">true</attribute>
* </code>
- *
+ * <p/>
* to define whether cache loader operations are to be asynchronous. If not
* specified, a cache loader operation is assumed synchronous.
- * <p>
- *
+ * <p/>
+ * <p/>
* The following additional parameters are available:
- <dl>
- <dt>cache.async.batchSize</dt>
- <dd>Number of modifications to commit in one transaction, default is
- 100. The minimum batch size is 1.</dd>
- <dt>cache.async.pollWait</dt>
- <dd>How long to wait before processing an incomplete batch, in
- milliseconds. Default is 100. Set this to 0 to not wait before processing
- available records.</dd>
- <dt>cache.async.returnOld</dt>
- <dd>If <code>true</code>, this loader returns the old values from {@link
-#put} and {@link #remove} methods. Otherwise, these methods always return
-null. Default is true. <code>false</code> improves the performance of these
-operations.</dd>
- <dt>cache.async.queueSize</dt>
- <dd>Maximum number of entries to enqueue for asynchronous processing.
- Lowering this size may help prevent out-of-memory conditions. It also may
- help to prevent less records lost in the case of JVM failure. Default is
- 10,000 operations.</dd>
- <dt>cache.async.put</dt>
- <dd>If set to false, all {@link #put} operations will be processed
- synchronously, and then only the {@link #remove} operations will be
- processed asynchronously. This mode may be useful for processing
- expiration of messages within a separate thread and keeping other
- operations synchronous for reliability.
- </dd>
- </dl>
+ * <dl>
+ * <dt>cache.async.batchSize</dt>
+ * <dd>Number of modifications to commit in one transaction, default is
+ * 100. The minimum batch size is 1.</dd>
+ * <dt>cache.async.pollWait</dt>
+ * <dd>How long to wait before processing an incomplete batch, in
+ * milliseconds. Default is 100. Set this to 0 to not wait before processing
+ * available records.</dd>
+ * <dt>cache.async.returnOld</dt>
+ * <dd>If <code>true</code>, this loader returns the old values from {@link
+ * #put} and {@link #remove} methods. Otherwise, these methods always return
+ * null. Default is true. <code>false</code> improves the performance of these
+ * operations.</dd>
+ * <dt>cache.async.queueSize</dt>
+ * <dd>Maximum number of entries to enqueue for asynchronous processing.
+ * Lowering this size may help prevent out-of-memory conditions. It also may
+ * help to prevent less records lost in the case of JVM failure. Default is
+ * 10,000 operations.</dd>
+ * <dt>cache.async.put</dt>
+ * <dd>If set to false, all {@link #put} operations will be processed
+ * synchronously, and then only the {@link #remove} operations will be
+ * processed asynchronously. This mode may be useful for processing
+ * expiration of messages within a separate thread and keeping other
+ * operations synchronous for reliability.
+ * </dd>
+ * </dl>
* For increased performance for many smaller transactions, use higher values
* for <code>cache.async.batchSize</code> and
* <code>cache.async.pollWait</code>. For larger sized records, use a smaller
@@ -127,25 +127,37 @@
s = props.getProperty("cache.async.batchSize");
if (s != null)
+ {
batchSize = Integer.parseInt(s);
+ }
if (batchSize <= 0)
+ {
throw new IllegalArgumentException("Invalid size: " + batchSize);
+ }
s = props.getProperty("cache.async.pollWait");
if (s != null)
+ {
pollWait = Integer.parseInt(s);
+ }
s = props.getProperty("cache.async.returnOld");
if (s != null)
+ {
returnOld = Boolean.valueOf(s).booleanValue();
+ }
s = props.getProperty("cache.async.queueSize");
if (s != null)
+ {
queue = new BoundedLinkedQueue(Integer.parseInt(s));
+ }
s = props.getProperty("cache.async.put");
if (s != null)
+ {
asyncPut = Boolean.valueOf(s).booleanValue();
+ }
delegateTo.setConfig(props);
}
@@ -158,14 +170,14 @@
public Set<String> getChildrenNames(Fqn fqn) throws Exception
{
- return delegateTo.getChildrenNames( fqn );
+ return delegateTo.getChildrenNames(fqn);
}
public Map get(Fqn name) throws Exception
{
try
{
- return delegateTo.get( name );
+ return delegateTo.get(name);
}
catch (IOException e)
{
@@ -177,7 +189,7 @@
public boolean exists(Fqn name) throws Exception
{
- return delegateTo.exists( name );
+ return delegateTo.exists(name);
}
Object get(Fqn name, Object key) throws Exception
@@ -186,9 +198,11 @@
{
try
{
- Map map = delegateTo.get( name );
+ Map map = delegateTo.get(name);
if (map != null)
- return map.get( key );
+ {
+ return map.get(key);
+ }
}
catch (IOException e)
{
@@ -204,10 +218,14 @@
if (asyncPut)
{
Object oldValue = get(name, key);
- Modification mod = new Modification(Modification.PUT_KEY_VALUE, name, key, value);
+ Modification mod = new Modification(Modification.ModificationType.PUT_KEY_VALUE, name, key, value);
enqueue(mod);
return oldValue;
- } else return delegateTo.put(name, key, value);
+ }
+ else
+ {
+ return delegateTo.put(name, key, value);
+ }
}
public void put(Fqn name, Map attributes) throws Exception
@@ -216,10 +234,13 @@
{
// JBCACHE-769 -- make a defensive copy
Map attrs = (attributes == null ? null : new HashMap(attributes));
- Modification mod = new Modification(Modification.PUT_DATA, name, attrs);
+ Modification mod = new Modification(Modification.ModificationType.PUT_DATA, name, attrs);
enqueue(mod);
}
- else delegateTo.put(name, attributes); // Let delegate make its own defensive copy
+ else
+ {
+ delegateTo.put(name, attributes); // Let delegate make its own defensive copy
+ }
}
public void put(List<Modification> modifications) throws Exception
@@ -228,45 +249,50 @@
{
Iterator i = modifications.iterator();
while (i.hasNext())
- enqueue((Modification)i.next());
+ {
+ enqueue((Modification) i.next());
+ }
+ }
+ else
+ {
+ delegateTo.put(modifications);
}
- else delegateTo.put(modifications);
}
public Object remove(Fqn name, Object key) throws Exception
{
Object oldValue = get(name, key);
- Modification mod = new Modification(Modification.REMOVE_KEY_VALUE, name, key);
+ Modification mod = new Modification(Modification.ModificationType.REMOVE_KEY_VALUE, name, key);
enqueue(mod);
return oldValue;
}
public void remove(Fqn name) throws Exception
{
- Modification mod = new Modification(Modification.REMOVE_NODE, name);
+ Modification mod = new Modification(Modification.ModificationType.REMOVE_NODE, name);
enqueue(mod);
}
public void removeData(Fqn name) throws Exception
{
- Modification mod = new Modification(Modification.REMOVE_DATA, name);
+ Modification mod = new Modification(Modification.ModificationType.REMOVE_DATA, name);
enqueue(mod);
}
public void prepare(Object tx, List modifications, boolean one_phase)
throws Exception
{
- delegateTo.prepare( tx, modifications, one_phase );
+ delegateTo.prepare(tx, modifications, one_phase);
}
public void commit(Object tx) throws Exception
{
- delegateTo.commit( tx );
+ delegateTo.commit(tx);
}
public void rollback(Object tx)
{
- delegateTo.rollback( tx );
+ delegateTo.rollback(tx);
}
public void setRegionManager(RegionManager manager)
@@ -281,7 +307,7 @@
public void loadState(Fqn subtree, ObjectOutputStream os) throws Exception
{
- delegateTo.loadState(subtree,os);
+ delegateTo.loadState(subtree, os);
}
public void storeEntireState(ObjectInputStream is) throws Exception
@@ -312,7 +338,9 @@
{
stopped.set(true);
if (processor != null)
+ {
processor.stop();
+ }
delegateTo.stop();
}
@@ -325,12 +353,15 @@
throws CacheException, InterruptedException
{
if (stopped.get())
+ {
throw new CacheException("AsyncCacheLoader stopped; no longer accepting more entries.");
+ }
queue.put(mod);
}
/**
* Processes (by batch if possible) a queue of {@link Modification}s.
+ *
* @author manik surtani
*/
private class AsyncProcessor implements Runnable
@@ -340,7 +371,8 @@
// Modifications to process as a single put
private final List mods = new ArrayList(batchSize);
- public void start() {
+ public void start()
+ {
if (t == null || !t.isAlive())
{
t = new Thread(this, "AsyncCacheLoader-" + threadId.increment());
@@ -349,18 +381,24 @@
}
}
- public void stop() {
+ public void stop()
+ {
if (t != null)
{
t.interrupt();
try
{
t.join();
- } catch (InterruptedException e) {}
+ }
+ catch (InterruptedException e)
+ {
+ }
}
if (!queue.isEmpty())
+ {
log.warn("Async queue not yet empty, possibly interrupted");
}
+ }
public void run()
{
@@ -382,15 +420,18 @@
put(mods);
if (log.isTraceEnabled()) log.trace("process remaining queued " + queue.size());
while (!queue.isEmpty())
+ {
run0();
}
+ }
catch (InterruptedException e)
{
log.trace("remaining interrupted");
}
}
- private void run0() throws InterruptedException {
+ private void run0() throws InterruptedException
+ {
log.trace("run0");
Object o = queue.take();
addTaken(o);
@@ -398,11 +439,15 @@
{
o = queue.poll(pollWait);
if (o == null)
+ {
break;
+ }
addTaken(o);
}
if (log.isTraceEnabled())
+ {
log.trace("put " + mods.size());
+ }
put(mods);
mods.clear();
}
@@ -411,16 +456,17 @@
{
if (o instanceof List)
{
- mods.addAll((List)o);
+ mods.addAll((List) o);
}
else
{
- Modification mod = (Modification)o;
+ Modification mod = (Modification) o;
mods.add(mod);
}
}
- private void put(List mods) {
+ private void put(List mods)
+ {
try
{
delegateTo.put(mods);
@@ -432,13 +478,15 @@
}
}
- public String toString() {
+ public String toString()
+ {
return "TQ t=" + t;
}
}
- public String toString() {
+ public String toString()
+ {
return super.toString() +
" delegate=[" + delegateTo + "]" +
" processor=" + processor +
1.11 +83 -66 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.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- DelegatingCacheLoader.java 31 Aug 2006 14:56:45 -0000 1.10
+++ DelegatingCacheLoader.java 22 Sep 2006 16:27:56 -0000 1.11
@@ -23,15 +23,19 @@
* CacheLoader implementation which delegates to another TreeCache. This allows to stack caches on top of each
* other, allowing for hierarchical cache levels. For example, first level cache delegates to a second level cache,
* which delegates to a persistent cache.
+ *
* @author Bela Ban
* @author Daniel Gredler
- * @version $Id: DelegatingCacheLoader.java,v 1.10 2006/08/31 14:56:45 vblagojevic Exp $
+ * @version $Id: DelegatingCacheLoader.java,v 1.11 2006/09/22 16:27:56 msurtani Exp $
*/
-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<Object,List<Modification>> transactions=new HashMap<Object, List<Modification>>();
+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<Object, List<Modification>> transactions = new HashMap<Object, List<Modification>>();
public static final int delegateGetChildrenNames = 1;
public static final int delegateGetKey = 2;
@@ -47,12 +51,12 @@
public static final int putList = 12;
-
public abstract void setConfig(Properties props);
- public Set<String> getChildrenNames(Fqn fqn) throws Exception {
- Set<String> retval=delegateGetChildrenNames(fqn);
- return retval == null? null : (retval.size() == 0? null : retval);
+ public Set<String> getChildrenNames(Fqn fqn) throws Exception
+ {
+ Set<String> retval = delegateGetChildrenNames(fqn);
+ return retval == null ? null : (retval.size() == 0 ? null : retval);
}
// See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
@@ -67,57 +71,70 @@
return delegateGet(name);
}
- public boolean exists(Fqn name) throws Exception {
+ public boolean exists(Fqn name) throws Exception
+ {
return delegateExists(name);
}
- public Object put(Fqn name, Object key, Object value) throws Exception {
+ public Object put(Fqn name, Object key, Object value) throws Exception
+ {
return delegatePut(name, key, value);
}
- public void put(Fqn name, Map attributes) throws Exception {
+ public void put(Fqn name, Map attributes) throws Exception
+ {
delegatePut(name, attributes);
}
- public void put(Fqn fqn, Map attributes, boolean erase) throws Exception {
- if(erase)
+ public void put(Fqn fqn, Map attributes, boolean erase) throws Exception
+ {
+ if (erase)
+ {
removeData(fqn);
- put(fqn, attributes);
}
-
- public void put(List<Modification> modifications) throws Exception {
- if(modifications == null || modifications.size() == 0) return;
- delegatePut(modifications);
+ put(fqn, attributes);
}
- public Object remove(Fqn name, Object key) throws Exception {
+ public Object remove(Fqn name, Object key) throws Exception
+ {
return delegateRemove(name, key);
}
- public void remove(Fqn name) throws Exception {
+ public void remove(Fqn name) throws Exception
+ {
delegateRemove(name);
}
- public void removeData(Fqn name) throws Exception {
+ public void removeData(Fqn name) throws Exception
+ {
delegateRemoveData(name);
}
- public void prepare(Object tx, List<Modification> modifications, boolean one_phase) throws Exception {
- if(one_phase)
+ public void prepare(Object tx, List<Modification> modifications, boolean one_phase) throws Exception
+ {
+ if (one_phase)
+ {
put(modifications);
+ }
else
+ {
transactions.put(tx, modifications);
}
+ }
- public void commit(Object tx) throws Exception {
- List modifications=transactions.get(tx);
- if(modifications == null)
+ public void commit(Object tx) throws Exception
+ {
+ List modifications = transactions.get(tx);
+ if (modifications == null)
+ {
throw new Exception("transaction " + tx + " not found in transaction table");
+ }
put(modifications);
}
- public void rollback(Object tx) {
+ public void rollback(Object tx)
+ {
transactions.remove(tx);
}
@@ -129,7 +146,7 @@
public void loadState(Fqn subtree, ObjectOutputStream os) throws Exception
{
- delegateLoadState(subtree,os);
+ delegateLoadState(subtree, os);
}
public void storeEntireState(ObjectInputStream is) throws Exception
@@ -139,30 +156,32 @@
public void storeState(Fqn subtree, ObjectInputStream is) throws Exception
{
- delegateStoreState(subtree,is);
+ delegateStoreState(subtree, is);
}
- public void create() throws Exception {
+ public void create() throws Exception
+ {
// Empty.
}
- public void start() throws Exception {
+ public void start() throws Exception
+ {
// Empty.
}
- public void stop() {
+ public void stop()
+ {
// Empty.
}
- public void destroy() {
+ public void destroy()
+ {
// Empty.
}
/*------------------------------ Delegating Methods ------------------------------*/
-
-
protected abstract Set delegateGetChildrenNames(Fqn fqn) throws Exception;
// See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
@@ -187,9 +206,7 @@
protected abstract void delegateLoadState(Fqn subtree, ObjectOutputStream os) throws Exception;
- protected abstract void delegateStoreEntireState(ObjectInputStream is)throws Exception;
-
- protected abstract void delegateStoreState(Fqn subtree, ObjectInputStream is)throws Exception;
+ protected abstract void delegateStoreEntireState(ObjectInputStream is) throws Exception;
- protected abstract void delegatePut(List<Modification> modifications) throws Exception;
+ protected abstract void delegateStoreState(Fqn subtree, ObjectInputStream is) throws Exception;
}
1.19 +200 -162 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.18
retrieving revision 1.19
diff -u -b -r1.18 -r1.19
--- FileCacheLoader.java 12 Sep 2006 20:16:55 -0000 1.18
+++ FileCacheLoader.java 22 Sep 2006 16:27:56 -0000 1.19
@@ -5,21 +5,15 @@
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.Fqn;
import org.jboss.cache.Modification;
-import org.jboss.cache.buddyreplication.BuddyManager;
import org.jboss.invocation.MarshalledValueInputStream;
-import org.jboss.invocation.MarshalledValueOutputStream;
-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.ObjectInputStream;
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;
@@ -27,209 +21,227 @@
/**
* 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.18 2006/09/12 20:16:55 vblagojevic Exp $
+ * @version $Id: FileCacheLoader.java,v 1.19 2006/09/22 16:27:56 msurtani Exp $
*/
public class FileCacheLoader extends AbstractCacheLoader
{
- File root=null;
- Log log=LogFactory.getLog(getClass());
+ File root = null;
+ 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<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<Object, List<Modification>> transactions = new ConcurrentHashMap();
/**
* TreeCache data file.
*/
- public static final String DATA="data.dat";
+ public static final String DATA = "data.dat";
/**
* TreeCache directory suffix.
*/
- public static final String DIR_SUFFIX="fdb";
+ public static final String DIR_SUFFIX = "fdb";
- public FileCacheLoader() {
+ public FileCacheLoader()
+ {
}
- public void setConfig(Properties props) {
- String location=props != null? props.getProperty("location") : null;
- if(location != null && location.length() > 0)
- root=new File(location);
+ public void setConfig(Properties props)
+ {
+ String location = props != null ? props.getProperty("location") : null;
+ if (location != null && location.length() > 0)
+ {
+ root = new File(location);
+ }
}
- public void create() throws Exception {
- if(root == null) {
- String tmpLocation=System.getProperty("java.io.tmpdir", "C:\\tmp");
- root=new File(tmpLocation);
+ public void create() throws Exception
+ {
+ if (root == null)
+ {
+ String tmpLocation = System.getProperty("java.io.tmpdir", "C:\\tmp");
+ root = new File(tmpLocation);
}
- if(!root.exists()) {
- if(log.isTraceEnabled())
+ if (!root.exists())
+ {
+ if (log.isTraceEnabled())
+ {
log.trace("Creating cache loader location " + root);
- boolean created=root.mkdirs();
- if(!created)
+ }
+ boolean created = root.mkdirs();
+ if (!created)
+ {
throw new IOException("Unable to create cache loader location " + root);
}
+ }
if (!root.isDirectory())
+ {
throw new IOException("Cache loader location [" + root + "] is not a directory!");
}
+ }
- public void start() throws Exception {
+ public void start() throws Exception
+ {
}
- public void stop() {
+ public void stop()
+ {
}
- public void destroy() {
+ public void destroy()
+ {
}
- public Set<String> getChildrenNames(Fqn fqn) throws Exception {
- File parent=getDirectory(fqn, false);
- if(parent == null) return null;
- File[] children=parent.listFiles();
- 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)) {
- String child_name=child.getName();
- child_name=child_name.substring(0, child_name.lastIndexOf(DIR_SUFFIX)-1);
+ public Set<String> getChildrenNames(Fqn fqn) throws Exception
+ {
+ File parent = getDirectory(fqn, false);
+ if (parent == null) return null;
+ File[] children = parent.listFiles();
+ 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))
+ {
+ String child_name = child.getName();
+ child_name = child_name.substring(0, child_name.lastIndexOf(DIR_SUFFIX) - 1);
s.add(child_name);
}
}
- return s.size() == 0? null : s;
+ return s.size() == 0 ? null : s;
}
- public Map get(Fqn fqn) throws Exception {
+ public Map get(Fqn fqn) throws Exception
+ {
return loadAttributes(fqn);
}
- public boolean exists(Fqn fqn) throws Exception {
- File f=getDirectory(fqn, false);
+ public boolean exists(Fqn fqn) throws Exception
+ {
+ File f = getDirectory(fqn, false);
return f != null;
}
- public Object put(Fqn fqn, Object key, Object value) throws Exception {
+ public Object put(Fqn fqn, Object key, Object value) throws Exception
+ {
Object retval;
- Map m=loadAttributes(fqn);
- if(m == null) m=new HashMap();
- retval=m.put(key, value);
+ Map m = loadAttributes(fqn);
+ if (m == null) m = new HashMap();
+ retval = m.put(key, value);
storeAttributes(fqn, m);
return retval;
}
- public void put(Fqn fqn, Map attributes) throws Exception {
+ public void put(Fqn fqn, Map attributes) throws Exception
+ {
put(fqn, attributes, false);
}
- public void put(Fqn fqn, Map attributes, boolean erase) throws Exception {
- Map m=erase? new HashMap() : loadAttributes(fqn);
- if(m == null) m=new HashMap();
- if(attributes != null)
+ public void put(Fqn fqn, Map attributes, boolean erase) throws Exception
+ {
+ Map m = erase ? new HashMap() : loadAttributes(fqn);
+ if (m == null) m = new HashMap();
+ if (attributes != null)
+ {
m.putAll(attributes);
+ }
storeAttributes(fqn, m);
}
- void put(Fqn fqn) throws Exception {
+ void put(Fqn fqn) throws Exception
+ {
getDirectory(fqn, true);
}
- /**
- *
- * @param modifications List<Modification>
- * @throws Exception
- */
- public void put(List modifications) throws Exception {
- if(modifications == null) return;
- 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 Object remove(Fqn fqn, Object key) throws Exception {
+ public Object remove(Fqn fqn, Object key) throws Exception
+ {
Object retval;
- Map m=loadAttributes(fqn);
- if(m == null) return null;
- retval=m.remove(key);
+ Map m = loadAttributes(fqn);
+ if (m == null) return null;
+ retval = m.remove(key);
storeAttributes(fqn, m);
return retval;
}
- public void remove(Fqn fqn) throws Exception {
- File dir=getDirectory(fqn, false);
- if(dir != null) {
- boolean flag=removeDirectory(dir, true);
- if(!flag)
+ public void remove(Fqn fqn) throws Exception
+ {
+ File dir = getDirectory(fqn, false);
+ if (dir != null)
+ {
+ boolean flag = removeDirectory(dir, true);
+ if (!flag)
+ {
log.warn("failed removing " + fqn);
}
}
+ }
- public void removeData(Fqn fqn) throws Exception {
- File f=getDirectory(fqn, false);
- if(f != null) {
- File data=new File(f, DATA);
- if(data.exists()) {
- boolean flag=data.delete();
- if(!flag)
+ public void removeData(Fqn fqn) throws Exception
+ {
+ File f = getDirectory(fqn, false);
+ if (f != null)
+ {
+ File data = new File(f, DATA);
+ if (data.exists())
+ {
+ boolean flag = data.delete();
+ if (!flag)
+ {
log.warn("failed removing file " + data.getName());
}
}
}
+ }
- public void prepare(Object tx, List<Modification> modifications, boolean one_phase) throws Exception {
- if(one_phase)
+ public void prepare(Object tx, List<Modification> modifications, boolean one_phase) throws Exception
+ {
+ if (one_phase)
+ {
put(modifications);
+ }
else
+ {
transactions.put(tx, modifications);
}
+ }
- public void commit(Object tx) throws Exception {
- List modifications=transactions.remove(tx);
+ public void commit(Object tx) throws Exception
+ {
+ List modifications = transactions.remove(tx);
if (modifications == null)
+ {
throw new Exception("transaction " + tx + " not found in transaction table");
+ }
put(modifications);
}
- public void rollback(Object tx) {
+ public void rollback(Object tx)
+ {
transactions.remove(tx);
}
-
-
/* ----------------------- Private methods ------------------------ */
- File getDirectory(Fqn fqn, boolean create) {
- File f=new File(getFullPath(fqn));
- if(!f.exists()) {
- if(create)
+ File getDirectory(Fqn fqn, boolean create)
+ {
+ File f = new File(getFullPath(fqn));
+ if (!f.exists())
+ {
+ if (create)
+ {
f.mkdirs();
+ }
else
+ {
return null;
}
+ }
return f;
}
@@ -238,75 +250,101 @@
* 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
+ *
* @return <code>true</code> if directory was removed,
* <code>false</code> if not.
*/
- boolean removeDirectory(File dir, boolean include_start_dir) {
- boolean success=true;
- File[] subdirs=dir.listFiles();
- for(int i=0; i < subdirs.length; i++) {
- File file=subdirs[i];
- if(file.isFile() && file.getName().equals(DATA)) {
- if(!file.delete())
- success=false;
+ boolean removeDirectory(File dir, boolean include_start_dir)
+ {
+ boolean success = true;
+ File[] subdirs = dir.listFiles();
+ for (int i = 0; i < subdirs.length; i++)
+ {
+ File file = subdirs[i];
+ if (file.isFile() && file.getName().equals(DATA))
+ {
+ if (!file.delete())
+ {
+ success = false;
+ }
continue;
}
- if(file.isDirectory() && file.getName().endsWith(DIR_SUFFIX)) {
- if(!removeDirectory(file, false))
- success=false;
- if(!file.delete())
- success=false;
+ if (file.isDirectory() && file.getName().endsWith(DIR_SUFFIX))
+ {
+ if (!removeDirectory(file, false))
+ {
+ success = false;
+ }
+ if (!file.delete())
+ {
+ success = false;
+ }
}
}
- if(include_start_dir) {
+ if (include_start_dir)
+ {
if (!dir.equals(root))
{
if (dir.delete())
- {return success;}
- success=false;
+ {
+ return success;
+ }
+ success = false;
}
}
return success;
}
- String getFullPath(Fqn fqn) {
- StringBuffer sb=new StringBuffer(root.getAbsolutePath() + File.separator);
- for(int i=0; i < fqn.size(); i++) {
- Object tmp=fqn.get(i);
+ String getFullPath(Fqn fqn)
+ {
+ StringBuffer sb = new StringBuffer(root.getAbsolutePath() + File.separator);
+ for (int i = 0; i < fqn.size(); i++)
+ {
+ Object tmp = fqn.get(i);
String tmp_dir;
- if(tmp instanceof String)
- tmp_dir=(String)tmp;
+ if (tmp instanceof String)
+ {
+ tmp_dir = (String) tmp;
+ }
else
- tmp_dir=tmp.toString();
+ {
+ tmp_dir = tmp.toString();
+ }
sb.append(tmp_dir).append(".").append(DIR_SUFFIX).append(File.separator);
}
return sb.toString();
}
- protected Map loadAttributes(Fqn fqn) throws Exception {
- File f=getDirectory(fqn, false);
- if(f == null) return null; // i.e., this node does not exist.
+ protected Map loadAttributes(Fqn fqn) throws Exception
+ {
+ File f = getDirectory(fqn, false);
+ if (f == null) return null; // i.e., this node does not exist.
// this node exists so we should never return a null after this... at worst case, an empty HashMap.
- File child=new File(f, DATA);
- if(!child.exists()) return new HashMap(0); // no node attribs exist hence the empty HashMap.
+ File child = new File(f, DATA);
+ if (!child.exists()) return new HashMap(0); // no node attribs exist hence the empty HashMap.
//if(!child.exists()) return null;
- FileInputStream in=new FileInputStream(child);
- MarshalledValueInputStream input=new MarshalledValueInputStream(in);
- Map m=(Map)input.readObject();
+ FileInputStream in = new FileInputStream(child);
+ MarshalledValueInputStream input = new MarshalledValueInputStream(in);
+ Map m = (Map) input.readObject();
in.close();
return m;
}
- protected void storeAttributes(Fqn fqn, Map attrs) throws Exception {
- File f=getDirectory(fqn, true);
- File child=new File(f, DATA);
+ protected void storeAttributes(Fqn fqn, Map attrs) throws Exception
+ {
+ File f = getDirectory(fqn, true);
+ File child = new File(f, DATA);
if (!child.exists())
+ {
if (!child.createNewFile())
+ {
throw new IOException("Unable to create file: " + child);
- FileOutputStream out=new FileOutputStream(child);
- ObjectOutputStream output=new ObjectOutputStream(out);
+ }
+ }
+ FileOutputStream out = new FileOutputStream(child);
+ ObjectOutputStream output = new ObjectOutputStream(out);
output.writeObject(attrs);
out.close();
}
1.16 +224 -242 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.15
retrieving revision 1.16
diff -u -b -r1.15 -r1.16
--- JDBCCacheLoader.java 15 Sep 2006 03:28:39 -0000 1.15
+++ JDBCCacheLoader.java 22 Sep 2006 16:27:56 -0000 1.16
@@ -10,10 +10,7 @@
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.Fqn;
import org.jboss.cache.Modification;
-import org.jboss.cache.buddyreplication.BuddyManager;
import org.jboss.invocation.MarshalledValue;
-import org.jboss.invocation.MarshalledValueInputStream;
-import org.jboss.invocation.MarshalledValueOutputStream;
import javax.naming.InitialContext;
import javax.naming.NamingException;
@@ -33,7 +30,15 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
/**
* JDBC CacheLoader implementation.
@@ -75,11 +80,11 @@
*
* @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.15 $</tt>
+ * @version <tt>$Revision: 1.16 $</tt>
*/
public class JDBCCacheLoader extends AbstractCacheLoader
{
- private static final Log log=LogFactory.getLog(JDBCCacheLoader.class);
+ private static final Log log = LogFactory.getLog(JDBCCacheLoader.class);
private static final ThreadLocal connection = new ThreadLocal();
@@ -106,14 +111,14 @@
public void setConfig(Properties props)
{
datasourceName = props.getProperty("cache.jdbc.datasource");
- if(datasourceName == null)
+ if (datasourceName == null)
{
this.drv = getRequiredProperty(props, "cache.jdbc.driver");
final String jdbcUrl = getRequiredProperty(props, "cache.jdbc.url");
final String jdbcUsr = getRequiredProperty(props, "cache.jdbc.user");
final String jdbcPwd = getRequiredProperty(props, "cache.jdbc.password");
- if(log.isDebugEnabled())
+ if (log.isDebugEnabled())
{
log.debug("Properties: " +
"cache.jdbc.url=" +
@@ -138,7 +143,7 @@
this.dropTable = (prop == null || Boolean.valueOf(prop).booleanValue());
this.table = props.getProperty("cache.jdbc.table.name", "jbosscache");
- String primaryKey =props.getProperty("cache.jdbc.table.primarykey", "jbosscache_pk");
+ String primaryKey = props.getProperty("cache.jdbc.table.primarykey", "jbosscache_pk");
String fqnColumn = props.getProperty("cache.jdbc.fqn.column", "fqn");
String fqnType = props.getProperty("cache.jdbc.fqn.type", "varchar(255)");
String nodeColumn = props.getProperty("cache.jdbc.node.column", "node");
@@ -195,7 +200,7 @@
ResultSet rs = null;
try
{
- if(log.isDebugEnabled())
+ if (log.isDebugEnabled())
{
log.debug("executing sql: " + selectChildNamesSql + " (" + fqn + ")");
}
@@ -204,7 +209,7 @@
ps = con.prepareStatement(selectChildNamesSql);
ps.setString(1, fqn.toString());
rs = ps.executeQuery();
- if(rs.next())
+ if (rs.next())
{
children = new HashSet();
do
@@ -216,10 +221,10 @@
//String name = (String) childFqn.get(childFqn.size() - 1);
children.add(name);
}
- while(rs.next());
+ while (rs.next());
}
}
- catch(SQLException e)
+ catch (SQLException e)
{
log.error("Failed to get children names for fqn " + fqn, e);
throw new IllegalStateException("Failed to get children names for fqn " + fqn + ": " + e.getMessage());
@@ -299,7 +304,7 @@
Object oldValue;
Map node;
- if(oldNode == null || oldNode == NULL_NODE_IN_ROW)
+ if (oldNode == null || oldNode == NULL_NODE_IN_ROW)
{
node = new HashMap();
}
@@ -309,18 +314,18 @@
}
oldValue = node.put(key, value);
- if(oldNode != null)
+ if (oldNode != null)
{
updateNode(name, node);
}
else
{
- if(name.size() > 1)
+ if (name.size() > 1)
{
- for(int i = 1; i < name.size(); ++i)
+ for (int i = 1; i < name.size(); ++i)
{
final Fqn parent = name.getFqnChild(i);
- if(!exists(parent))
+ if (!exists(parent))
{
insertNode(parent, null);
}
@@ -345,37 +350,6 @@
put(name, attributes, false);
}
- public void put(List<Modification> modifications) throws Exception
- {
- for(int i = 0; i < modifications.size(); ++i)
- {
- Modification m = modifications.get(i);
- 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:
- throw new IllegalStateException("Unexpected modification code: " + m.getType());
- }
- }
- }
-
/**
* Removes attribute's value for a key. If after removal the node contains no attributes, the node is nullified.
*
@@ -388,10 +362,10 @@
{
Object removedValue = null;
Map node = loadNode(name);
- if(node != null && node != NULL_NODE_IN_ROW)
+ if (node != null && node != NULL_NODE_IN_ROW)
{
removedValue = node.remove(key);
- if(node.isEmpty())
+ if (node.isEmpty())
{
updateNode(name, null);
}
@@ -416,9 +390,9 @@
PreparedStatement ps = null;
try
{
- if(name.size() == 0)
+ if (name.size() == 0)
{
- if(log.isDebugEnabled())
+ if (log.isDebugEnabled())
{
log.debug("executing sql: " + deleteAllSql);
}
@@ -427,7 +401,7 @@
ps = con.prepareStatement(deleteAllSql);
int deletedRows = ps.executeUpdate();
- if(log.isDebugEnabled())
+ if (log.isDebugEnabled())
{
log.debug("total rows deleted: " + deletedRows);
}
@@ -443,9 +417,9 @@
sql.append(')');
- if(fqns.size() == 1)
+ if (fqns.size() == 1)
{
- if(log.isDebugEnabled())
+ if (log.isDebugEnabled())
{
log.debug("executing sql: " + deleteNodeSql + "(" + name + ")");
}
@@ -456,14 +430,14 @@
}
else
{
- if(log.isDebugEnabled())
+ if (log.isDebugEnabled())
{
log.debug("executing sql: " + sql + " " + fqns);
}
con = cf.getConnection();
ps = con.prepareStatement(sql.toString());
- for(int i = 0; i < fqns.size(); ++i)
+ for (int i = 0; i < fqns.size(); ++i)
{
ps.setString(i + 1, (String) fqns.get(i));
}
@@ -471,13 +445,13 @@
int deletedRows = ps.executeUpdate();
- if(log.isDebugEnabled())
+ if (log.isDebugEnabled())
{
log.debug("total rows deleted: " + deletedRows);
}
}
}
- catch(SQLException e)
+ catch (SQLException e)
{
log.error("Failed to remove node " + name, e);
throw new IllegalStateException("Failed to remove node " + name + ": " + e.getMessage());
@@ -513,9 +487,10 @@
{
// start a tx
//JBCACHE-346 fix, we don't need to prepare a DataSource object (Managed connection)
- if(cf instanceof NonManagedConnectionFactory) {
+ if (cf instanceof NonManagedConnectionFactory)
+ {
Connection con = cf.prepare(tx);
- if(log.isTraceEnabled())
+ if (log.isTraceEnabled())
{
log.trace("openned tx connection: tx=" + tx + ", con=" + con);
}
@@ -526,12 +501,12 @@
put(modifications);
// commit if it's one phase only
- if(one_phase)
+ if (one_phase)
{
commit(tx);
}
}
- catch(Exception e)
+ catch (Exception e)
{
// todo should I rollback it here or rollback is supposed to be invoke by someone from outside?
rollback(tx);
@@ -569,7 +544,7 @@
public void start() throws Exception
{
- if(drv != null)
+ if (drv != null)
{
loadDriver(drv);
}
@@ -584,20 +559,20 @@
DataSource dataSource = (DataSource) ctx.lookup(datasourceName);
this.cf = new ManagedConnectionFactory(dataSource);
}
- catch(NamingException e)
+ catch (NamingException e)
{
log.error("Failed to lookup datasource " + datasourceName + ": " + e.getMessage(), e);
throw new IllegalStateException("Failed to lookup datasource " + datasourceName + ": " + e.getMessage());
}
finally
{
- if(ctx != null)
+ if (ctx != null)
{
try
{
ctx.close();
}
- catch(NamingException e)
+ catch (NamingException e)
{
log.warn("Failed to close naming context.", e);
}
@@ -612,11 +587,11 @@
{
con = cf.getConnection();
driverName = getDriverName(con);
- if(createTable)
+ if (createTable)
{
- if(!tableExists(table, con))
+ if (!tableExists(table, con))
{
- if(log.isDebugEnabled())
+ if (log.isDebugEnabled())
{
log.debug("executing ddl: " + createTableDdl);
}
@@ -634,13 +609,13 @@
public void stop()
{
- if(dropTable)
+ if (dropTable)
{
Connection con = null;
Statement st = null;
try
{
- if(log.isDebugEnabled())
+ if (log.isDebugEnabled())
{
log.debug("executing ddl: " + dropTableDdl);
}
@@ -650,7 +625,7 @@
st.executeUpdate(dropTableDdl);
safeClose(st);
}
- catch(SQLException e)
+ catch (SQLException e)
{
log.error("Failed to drop table: " + e.getMessage(), e);
}
@@ -678,7 +653,7 @@
ResultSet rs = null;
try
{
- if(log.isDebugEnabled())
+ if (log.isDebugEnabled())
{
log.debug("executing sql: " + selectChildFqnsSql + "(" + name + ")");
}
@@ -688,17 +663,17 @@
selChildrenPs.setString(1, name);
rs = selChildrenPs.executeQuery();
- if(rs.next())
+ if (rs.next())
{
do
{
String childStr = rs.getString(1);
addChildrenToDeleteSql(childStr, sql, fqns);
}
- while(rs.next());
+ while (rs.next());
}
- if(fqns.size() == 0)
+ if (fqns.size() == 0)
{
sql.append("?");
}
@@ -722,9 +697,9 @@
Map attrs = (attributes == null ? null : new HashMap(attributes));
Map oldNode = loadNode(name);
- if(oldNode != null)
+ if (oldNode != null)
{
- if(!override && oldNode != NULL_NODE_IN_ROW && attrs != null)
+ if (!override && oldNode != NULL_NODE_IN_ROW && attrs != null)
{
attrs.putAll(oldNode);
}
@@ -732,12 +707,12 @@
}
else
{
- if(name.size() > 1)
+ if (name.size() > 1)
{
- for(int i = 1; i < name.size(); ++i)
+ for (int i = 1; i < name.size(); ++i)
{
final Fqn parent = name.getFqnChild(i);
- if(!exists(parent))
+ if (!exists(parent))
{
insertNode(parent, null);
}
@@ -759,7 +734,7 @@
PreparedStatement ps = null;
try
{
- if(log.isDebugEnabled())
+ if (log.isDebugEnabled())
{
log.debug("executing sql: " + insertNodeSql + " (" + name + ")");
}
@@ -769,7 +744,7 @@
ps.setString(1, name.toString());
- if(node != null)
+ if (node != null)
{
Object marshalledNode = new MarshalledValue(node);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -782,15 +757,19 @@
else
{
// a hack to handles the incomp. of SQL server jdbc driver prior to SQL SERVER 2005
- if( driverName != null && (driverName.indexOf("SQLSERVER") >= 0
+ if (driverName != null && (driverName.indexOf("SQLSERVER") >= 0
|| driverName.indexOf("POSTGRESQL") >= 0))
- ps.setNull( 2, Types.LONGVARBINARY );
+ {
+ ps.setNull(2, Types.LONGVARBINARY);
+ }
else
+ {
ps.setNull(2, Types.BLOB);
+ }
//ps.setNull(2, Types.LONGVARBINARY);
}
- if(name.size() == 0)
+ if (name.size() == 0)
{
ps.setNull(3, Types.VARCHAR);
}
@@ -800,16 +779,16 @@
}
int rows = ps.executeUpdate();
- if(rows != 1)
+ if (rows != 1)
{
throw new IllegalStateException("Expected one insert row but got " + rows);
}
}
- catch(RuntimeException e)
+ catch (RuntimeException e)
{
throw e;
}
- catch(Exception e)
+ catch (Exception e)
{
log.error("Failed to insert node: " + e.getMessage(), e);
throw new IllegalStateException("Failed to insert node: " + e.getMessage());
@@ -833,7 +812,7 @@
PreparedStatement ps = null;
try
{
- if(log.isDebugEnabled())
+ if (log.isDebugEnabled())
{
log.debug("executing sql: " + updateNodeSql);
}
@@ -841,7 +820,7 @@
con = cf.getConnection();
ps = con.prepareStatement(updateNodeSql);
- if(node == null)
+ if (node == null)
{
//ps.setNull(1, Types.BLOB);
// ps.setNull(1, Types.LONGVARBINARY);
@@ -862,12 +841,12 @@
ps.setString(2, name.toString());
int rows = ps.executeUpdate();
- if(rows != 1)
+ if (rows != 1)
{
throw new IllegalStateException("Expected one updated row but got " + rows);
}
}
- catch(Exception e)
+ catch (Exception e)
{
log.error("Failed to update node for fqn " + name + ": " + e.getMessage(), e);
throw new IllegalStateException("Failed to update node for fqn " + name + ": " + e.getMessage());
@@ -896,7 +875,7 @@
ResultSet rs = null;
try
{
- if(log.isDebugEnabled())
+ if (log.isDebugEnabled())
{
log.debug("executing sql: " + selectNodeSql + " (" + name + ")");
}
@@ -907,11 +886,11 @@
rs = ps.executeQuery();
- if(rs.next())
+ if (rs.next())
{
rowExists = true;
InputStream is = rs.getBinaryStream(1);
- if(is != null && !rs.wasNull())
+ if (is != null && !rs.wasNull())
{
ObjectInputStream ois = null;
try
@@ -921,20 +900,20 @@
Object marshalledNode = ois.readObject();
// de-marshall value if possible
- if(marshalledNode instanceof MarshalledValue)
+ if (marshalledNode instanceof MarshalledValue)
{
oldNode = (Map) ((MarshalledValue) marshalledNode).get();
}
- else if(marshalledNode instanceof MarshalledObject)
+ else if (marshalledNode instanceof MarshalledObject)
{
oldNode = (Map) ((MarshalledObject) marshalledNode).get();
}
}
- catch(IOException e)
+ catch (IOException e)
{
throw new SQLException("Unable to load to deserialize result: " + e);
}
- catch(ClassNotFoundException e)
+ catch (ClassNotFoundException e)
{
throw new SQLException("Unable to load to deserialize result: " + e);
}
@@ -945,7 +924,7 @@
}
}
}
- catch(SQLException e)
+ catch (SQLException e)
{
log.error("Failed to load node for fqn " + name + ": " + e.getMessage(), e);
throw new IllegalStateException("Failed to load node for fqn " + name + ": " + e.getMessage());
@@ -962,13 +941,13 @@
private static void safeClose(InputStream is)
{
- if(is != null)
+ if (is != null)
{
try
{
is.close();
}
- catch(IOException e)
+ catch (IOException e)
{
log.warn("Failed to close input stream: " + e.getMessage());
}
@@ -977,13 +956,13 @@
private static void safeClose(Connection con)
{
- if(con != null)
+ if (con != null)
{
try
{
con.close();
}
- catch(SQLException e)
+ catch (SQLException e)
{
log.warn("Failed to close connection: " + e.getMessage());
}
@@ -992,13 +971,13 @@
private static void safeClose(Statement st)
{
- if(st != null)
+ if (st != null)
{
try
{
st.close();
}
- catch(SQLException e)
+ catch (SQLException e)
{
log.warn("Failed to close statement: " + e.getMessage());
}
@@ -1007,13 +986,13 @@
private static void safeClose(ResultSet rs)
{
- if(rs != null)
+ if (rs != null)
{
try
{
rs.close();
}
- catch(SQLException e)
+ catch (SQLException e)
{
log.warn("Failed to close result set: " + e.getMessage());
}
@@ -1026,7 +1005,7 @@
{
Class.forName(drv).newInstance();
}
- catch(Exception e)
+ catch (Exception e)
{
log.error("Failed to load driver " + drv, e);
throw new IllegalStateException("Failed to load driver " + drv + ": " + e.getMessage());
@@ -1036,11 +1015,12 @@
private static String getDriverName(Connection con)
{
if (con == null) return null;
- try{
+ try
+ {
DatabaseMetaData dmd = con.getMetaData();
return dmd.getDriverName().toUpperCase();
}
- catch(SQLException e)
+ catch (SQLException e)
{
// This should not happen. A J2EE compatiable JDBC driver is
// required to fully support metadata.
@@ -1052,7 +1032,7 @@
private static String getRequiredProperty(Properties props, String name)
{
String value = props.getProperty(name);
- if(value == null)
+ if (value == null)
{
throw new IllegalStateException("Missing required property: " + name);
}
@@ -1070,37 +1050,37 @@
String catalog = con.getCatalog();
String schema = null;
String quote = dmd.getIdentifierQuoteString();
- if(tableName.startsWith(quote))
+ if (tableName.startsWith(quote))
{
- if(!tableName.endsWith(quote))
+ if (!tableName.endsWith(quote))
{
throw new IllegalStateException("Mismatched quote in table name: " + tableName);
}
int quoteLength = quote.length();
tableName = tableName.substring(quoteLength, tableName.length() - quoteLength);
- if(dmd.storesLowerCaseQuotedIdentifiers())
+ if (dmd.storesLowerCaseQuotedIdentifiers())
{
tableName = tableName.toLowerCase();
}
- else if(dmd.storesUpperCaseQuotedIdentifiers())
+ else if (dmd.storesUpperCaseQuotedIdentifiers())
{
tableName = tableName.toUpperCase();
}
}
else
{
- if(dmd.storesLowerCaseIdentifiers())
+ if (dmd.storesLowerCaseIdentifiers())
{
tableName = tableName.toLowerCase();
}
- else if(dmd.storesUpperCaseIdentifiers())
+ else if (dmd.storesUpperCaseIdentifiers())
{
tableName = tableName.toUpperCase();
}
}
int dotIndex;
- if((dotIndex = tableName.indexOf('.')) != -1)
+ if ((dotIndex = tableName.indexOf('.')) != -1)
{
// Yank out schema name ...
schema = tableName.substring(0, dotIndex);
@@ -1110,7 +1090,7 @@
rs = dmd.getTables(catalog, schema, tableName, null);
return rs.next();
}
- catch(SQLException e)
+ catch (SQLException e)
{
// This should not happen. A J2EE compatiable JDBC driver is
// required fully support metadata.
@@ -1219,12 +1199,12 @@
Connection con = getConnection();
try
{
- if(con.getAutoCommit())
+ if (con.getAutoCommit())
{
con.setAutoCommit(false);
}
}
- catch(Exception e)
+ catch (Exception e)
{
log.error("Failed to set auto-commit: " + e.getMessage(), e);
throw new IllegalStateException("Failed to set auto-commit: " + e.getMessage());
@@ -1236,13 +1216,13 @@
public Connection getConnection()
{
Connection con = (Connection) connection.get();
- if(con == null)
+ if (con == null)
{
try
{
con = DriverManager.getConnection(url, usr, pwd);
}
- catch(SQLException e)
+ catch (SQLException e)
{
log.error("Failed to get connection for url=" + url + ", user=" + usr + ", password=" + pwd, e);
throw new IllegalStateException("Failed to get connection for url=" +
@@ -1256,7 +1236,7 @@
}
}
- if(log.isTraceEnabled())
+ if (log.isTraceEnabled())
{
log.debug("using connection: " + con);
}
@@ -1267,7 +1247,7 @@
public void commit(Object tx)
{
Connection con = (Connection) connection.get();
- if(con == null)
+ if (con == null)
{
throw new IllegalStateException("Failed to commit: thread is not associated with the connection!");
}
@@ -1275,12 +1255,12 @@
try
{
con.commit();
- if(log.isTraceEnabled())
+ if (log.isTraceEnabled())
{
log.trace("committed tx=" + tx + ", con=" + con);
}
}
- catch(SQLException e)
+ catch (SQLException e)
{
log.error("Failed to commit", e);
throw new IllegalStateException("Failed to commit: " + e.getMessage());
@@ -1294,7 +1274,7 @@
public void rollback(Object tx)
{
Connection con = (Connection) connection.get();
- if(con == null)
+ if (con == null)
{
// todo: prepare was not called. why is rollback called?
throw new IllegalStateException("Failed to rollback: thread is not associated with the connection!");
@@ -1303,12 +1283,12 @@
try
{
con.rollback();
- if(log.isTraceEnabled())
+ if (log.isTraceEnabled())
{
log.trace("rolledback tx=" + tx + ", con=" + con);
}
}
- catch(SQLException e)
+ catch (SQLException e)
{
log.error("Failed to rollback", e);
throw new IllegalStateException("Failed to rollback: " + e.getMessage());
@@ -1321,17 +1301,17 @@
public void close(Connection con)
{
- if(con != null && con != connection.get())
+ if (con != null && con != connection.get())
{
try
{
con.close();
- if(log.isTraceEnabled())
+ if (log.isTraceEnabled())
{
//log.trace("closed non tx connection: " + con);
}
}
- catch(SQLException e)
+ catch (SQLException e)
{
log.warn("Failed to close connection: " + e.getMessage());
}
@@ -1356,7 +1336,9 @@
// world as the JNDI lookup would fail, but this check here
// allows a unit test using DummyContext
if (dataSource == null)
+ {
throw new IllegalArgumentException("dataSource cannot be null");
+ }
this.dataSource = dataSource;
}
@@ -1371,7 +1353,7 @@
{
return getConnection();
}
- catch(SQLException e)
+ catch (SQLException e)
{
log.error("Failed to get connection: " + e.getMessage(), e);
throw new IllegalStateException("Failed to get connection: " + e.getMessage());
1.11 +38 -52 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.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- LocalDelegatingCacheLoader.java 31 Aug 2006 14:56:45 -0000 1.10
+++ LocalDelegatingCacheLoader.java 22 Sep 2006 16:27:56 -0000 1.11
@@ -6,14 +6,16 @@
*/
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.Modification;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
-import java.util.*;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
/**
* DelegatingCacheLoader implementation which delegates to a local (in the same VM) TreeCache. Sample code:
@@ -26,26 +28,31 @@
* secondLevel.start();
* firstLevel.start();
* </pre>
+ *
* @author Bela Ban
* @author Daniel Gredler
- * @version $Id: LocalDelegatingCacheLoader.java,v 1.10 2006/08/31 14:56:45 vblagojevic Exp $
+ * @version $Id: LocalDelegatingCacheLoader.java,v 1.11 2006/09/22 16:27:56 msurtani Exp $
*/
-public class LocalDelegatingCacheLoader extends DelegatingCacheLoader {
+public class LocalDelegatingCacheLoader extends DelegatingCacheLoader
+{
- TreeCache delegate=null;
+ TreeCache delegate = null;
public LocalDelegatingCacheLoader()
{
}
- public LocalDelegatingCacheLoader(TreeCache delegate) {
- this.delegate=delegate;
+ public LocalDelegatingCacheLoader(TreeCache delegate)
+ {
+ this.delegate = delegate;
}
- public void setConfig(Properties props) {
+ public void setConfig(Properties props)
+ {
}
- protected Set delegateGetChildrenNames(Fqn fqn) throws Exception {
+ protected Set delegateGetChildrenNames(Fqn fqn) throws Exception
+ {
return delegate.getChildrenNames(fqn);
}
@@ -54,11 +61,12 @@
// return delegate.get(name, key);
// }
- protected Map delegateGet(Fqn name) throws Exception {
- DataNode n=delegate.get(name);
- if(n == null) return null;
+ protected Map delegateGet(Fqn name) throws Exception
+ {
+ DataNode n = delegate.get(name);
+ if (n == null) return null;
// after this stage we know that the node exists. So never return a null - at worst, an empty map.
- Map m=n.getData();
+ Map m = n.getData();
if (m == null) m = new HashMap(0);
return m;
}
@@ -68,55 +76,33 @@
this.delegate = delegate;
}
- protected boolean delegateExists(Fqn name) throws Exception {
+ protected boolean delegateExists(Fqn name) throws Exception
+ {
return delegate.exists(name);
}
- protected Object delegatePut(Fqn name, Object key, Object value) throws Exception {
+ protected Object delegatePut(Fqn name, Object key, Object value) throws Exception
+ {
return delegate.put(name, key, value);
}
- protected void delegatePut(Fqn name, Map attributes) throws Exception {
+ protected void delegatePut(Fqn name, Map attributes) throws Exception
+ {
delegate.put(name, attributes);
}
- protected void delegatePut(List<Modification> modifications) throws Exception {
- for(Modification m: modifications) {
- 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;
- }
- }
- }
-
- protected Object delegateRemove(Fqn name, Object key) throws Exception {
+ protected Object delegateRemove(Fqn name, Object key) throws Exception
+ {
return delegate.remove(name, key);
}
- protected void delegateRemove(Fqn name) throws Exception {
+ protected void delegateRemove(Fqn name) throws Exception
+ {
delegate.remove(name);
}
- protected void delegateRemoveData(Fqn name) throws Exception {
+ protected void delegateRemoveData(Fqn name) throws Exception
+ {
delegate.removeData(name);
}
@@ -133,7 +119,7 @@
// false);
// Until flush is in place, use the old mechanism
// where we wait the full state retrieval timeout
- delegate.getStateTransferManager().getState(os,Fqn.ROOT, delegate.getConfiguration().getInitialStateRetrievalTimeout(), true, false);
+ delegate.getStateTransferManager().getState(os, Fqn.ROOT, delegate.getConfiguration().getInitialStateRetrievalTimeout(), true, false);
}
catch (Exception e)
{
1.11 +69 -52 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.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- RmiDelegatingCacheLoader.java 31 Aug 2006 14:56:45 -0000 1.10
+++ RmiDelegatingCacheLoader.java 22 Sep 2006 16:27:56 -0000 1.11
@@ -6,17 +6,15 @@
*/
package org.jboss.cache.loader;
+import org.jboss.cache.CacheSPI;
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.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.rmi.Naming;
-import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@@ -27,7 +25,7 @@
* the remote TreeCache delegated to is this cacheloader's cache's coordinator. If
* configured programmatically, this cacheloader may delegate to any remote cache that
* has been appropriately bound.
- *
+ * <p/>
* This CacheLoader expects three configuration properties: <tt>host</tt>, <tt>port</tt>
* and <tt>bindName</tt>. If the <tt>host</tt> propety is not specified, it defaults to
* <tt>localhost</tt>; if the <tt>port</tt> property is not specified, it defaults to
@@ -35,9 +33,10 @@
* cacheloader's cache's cluster name.
*
* @author Daniel Gredler
- * @version $Id: RmiDelegatingCacheLoader.java,v 1.10 2006/08/31 14:56:45 vblagojevic Exp $
+ * @version $Id: RmiDelegatingCacheLoader.java,v 1.11 2006/09/22 16:27:56 msurtani Exp $
*/
-public class RmiDelegatingCacheLoader extends DelegatingCacheLoader {
+public class RmiDelegatingCacheLoader extends DelegatingCacheLoader
+{
private String host;
private String port;
@@ -49,7 +48,8 @@
/**
* Default constructor.
*/
- public RmiDelegatingCacheLoader() {
+ public RmiDelegatingCacheLoader()
+ {
// Empty.
}
@@ -61,7 +61,8 @@
* @param port The port on which to look up the remote object.
* @param bindName The name to which the remote object is bound.
*/
- public RmiDelegatingCacheLoader(TreeCache cache, String host, int port, String bindName) {
+ public RmiDelegatingCacheLoader(TreeCache cache, String host, int port, String bindName)
+ {
this.host = host;
this.port = String.valueOf(port);
this.bindName = bindName;
@@ -72,15 +73,18 @@
/**
* Allows configuration via XML config file.
*
- * @see org.jboss.cache.loader.DelegatingCacheLoader#setConfig(java.util.Properties)
+ * @see DelegatingCacheLoader#setConfig(java.util.Properties)
*/
- public void setConfig(Properties props) {
+ public void setConfig(Properties props)
+ {
this.host = props.getProperty("host");
- if(this.host == null || this.host.length() == 0) {
+ if (this.host == null || this.host.length() == 0)
+ {
this.host = "localhost";
}
this.port = props.getProperty("port");
- if(this.port == null || this.port.length() == 0) {
+ if (this.port == null || this.port.length() == 0)
+ {
this.port = "1098";
}
this.port = props.getProperty("bindName");
@@ -90,9 +94,10 @@
/**
* Allows configuration via XML config file.
*
- * @see org.jboss.cache.loader.DelegatingCacheLoader#setCache(org.jboss.cache.CacheSPI)
+ * @see DelegatingCacheLoader#setCache(org.jboss.cache.CacheSPI)
*/
- public void setCache(CacheSPI cache) {
+ public void setCache(CacheSPI cache)
+ {
super.setCache(cache);
this.tryToInitRemoteCache();
}
@@ -100,8 +105,9 @@
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGetChildrenNames(org.jboss.cache.Fqn)
*/
- protected Set delegateGetChildrenNames(Fqn fqn) throws Exception {
- return ( this.remoteCache != null ? this.remoteCache.getChildrenNames(fqn) : null );
+ protected Set delegateGetChildrenNames(Fqn fqn) throws Exception
+ {
+ return (this.remoteCache != null ? this.remoteCache.getChildrenNames(fqn) : null);
}
// See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
@@ -115,57 +121,62 @@
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGet(org.jboss.cache.Fqn)
*/
- protected Map delegateGet(Fqn name) throws Exception {
- DataNode n=this.remoteCache != null ? this.remoteCache.get(name) : null;
- if(n == null)
+ protected Map delegateGet(Fqn name) throws Exception
+ {
+ DataNode n = this.remoteCache != null ? this.remoteCache.get(name) : null;
+ if (n == null)
+ {
return null;
+ }
return n.getData();
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateExists(org.jboss.cache.Fqn)
*/
- protected boolean delegateExists(Fqn name) throws Exception {
- return (this.remoteCache != null && this.remoteCache.exists(name) );
+ protected boolean delegateExists(Fqn name) throws Exception
+ {
+ return (this.remoteCache != null && this.remoteCache.exists(name));
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegatePut(org.jboss.cache.Fqn, java.lang.Object, java.lang.Object)
*/
- protected Object delegatePut(Fqn name, Object key, Object value) throws Exception {
- return ( this.remoteCache != null ? this.remoteCache.put(name, key, value) : null );
+ protected Object delegatePut(Fqn name, Object key, Object value) throws Exception
+ {
+ return (this.remoteCache != null ? this.remoteCache.put(name, key, value) : null);
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegatePut(org.jboss.cache.Fqn, java.util.Map)
*/
- protected void delegatePut(Fqn name, Map attributes) throws Exception {
- if( this.remoteCache != null ) this.remoteCache.put(name, attributes);
- }
-
- protected void delegatePut(List<Modification> modifications) throws Exception {
-
+ protected void delegatePut(Fqn name, Map attributes) throws Exception
+ {
+ if (this.remoteCache != null) this.remoteCache.put(name, attributes);
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemove(org.jboss.cache.Fqn, java.lang.Object)
*/
- protected Object delegateRemove(Fqn name, Object key) throws Exception {
- return ( this.remoteCache != null ? this.remoteCache.remove(name, key) : null );
+ protected Object delegateRemove(Fqn name, Object key) throws Exception
+ {
+ return (this.remoteCache != null ? this.remoteCache.remove(name, key) : null);
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemove(org.jboss.cache.Fqn)
*/
- protected void delegateRemove(Fqn name) throws Exception {
- if( this.remoteCache != null ) this.remoteCache.remove(name);
+ protected void delegateRemove(Fqn name) throws Exception
+ {
+ if (this.remoteCache != null) this.remoteCache.remove(name);
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemoveData(org.jboss.cache.Fqn)
*/
- protected void delegateRemoveData(Fqn name) throws Exception {
- if( this.remoteCache != null ) this.remoteCache.removeData(name);
+ protected void delegateRemoveData(Fqn name) throws Exception
+ {
+ if (this.remoteCache != null) this.remoteCache.removeData(name);
}
@Override
@@ -196,23 +207,29 @@
* Tries to initialize the remote cache object. If this cacheloader has been
* cofigured using an XML file
*/
- private void tryToInitRemoteCache() {
- if(this.host == null || this.port == null || this.cache == null) {
+ private void tryToInitRemoteCache()
+ {
+ if (this.host == null || this.port == null || this.cache == null)
+ {
return;
}
- if(this.bindName == null) {
+ if (this.bindName == null)
+ {
this.bindName = this.cache.getConfiguration().getClusterName();
}
- if(!this.programmaticInit && this.cache.isCoordinator()) {
+ if (!this.programmaticInit && this.cache.isCoordinator())
+ {
// CacheLoader specified via XML, but this cache is the coordinator!
this.remoteCache = null;
return;
}
String name = "//" + this.host + ":" + this.port + "/" + this.bindName;
- try {
+ try
+ {
this.remoteCache = (RemoteTreeCache) Naming.lookup(name);
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
log.error("Unable to lookup remote cache at '" + name + "'.", t);
}
}
1.11 +92 -97 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.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- RpcDelegatingCacheLoader.java 31 Aug 2006 14:56:45 -0000 1.10
+++ RpcDelegatingCacheLoader.java 22 Sep 2006 16:27:56 -0000 1.11
@@ -9,12 +9,11 @@
import org.jboss.cache.CacheSPI;
import org.jboss.cache.DataNode;
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.MethodCall;
import org.jboss.cache.marshall.MethodCallFactory;
import org.jgroups.Address;
-import org.jboss.cache.marshall.MethodCall;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
@@ -29,15 +28,16 @@
* DelegatingCacheLoader implementation which delegates to a remote (not in the same VM)
* TreeCache using JGroups' RPC mechanism. The remote TreeCache delegated to is this
* cacheloader's cache's coordinator.
- *
+ * <p/>
* This CacheLoader uses an optional configuration property, <tt>timeout</tt>, which
* specifies the timeout in milliseconds for each RPC call. If <tt>timeout</tt> is not
* specified, it defaults to <tt>5000</tt>.
*
* @author Daniel Gredler
- * @version $Id: RpcDelegatingCacheLoader.java,v 1.10 2006/08/31 14:56:45 vblagojevic Exp $
+ * @version $Id: RpcDelegatingCacheLoader.java,v 1.11 2006/09/22 16:27:56 msurtani Exp $
*/
-public class RpcDelegatingCacheLoader extends DelegatingCacheLoader {
+public class RpcDelegatingCacheLoader extends DelegatingCacheLoader
+{
private int timeout;
private Address localAddress;
@@ -58,19 +58,21 @@
* Initializes the <tt>Method</tt> instances needed by the operations
* delegated to the cacheloader's cache's coordinator.
*/
- static {
- try {
- METHOD_GET_STATE = TreeCache.class.getDeclaredMethod("getStateBytes", new Class[] {});
- METHOD_SET_STATE = TreeCache.class.getDeclaredMethod("setStateBytes", new Class[] { byte[].class });
- METHOD_GET_CHILDREN_NAMES = TreeCache.class.getDeclaredMethod("getChildrenNames", new Class[] { Fqn.class });
- METHOD_GET_WITH_2_PARAMS = TreeCache.class.getDeclaredMethod("get", new Class[] { Fqn.class, Object.class });
- METHOD_GET_WITH_1_PARAM = TreeCache.class.getDeclaredMethod("get", new Class[] { Fqn.class });
- METHOD_EXISTS = TreeCache.class.getDeclaredMethod("exists", new Class[] { Fqn.class });
- METHOD_PUT_WITH_3_PARAMS = TreeCache.class.getDeclaredMethod("put", new Class[] { Fqn.class, Object.class, Object.class });
- METHOD_PUT_WITH_2_PARAMS = TreeCache.class.getDeclaredMethod("put", new Class[] { Fqn.class, Map.class });
- 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 });
+ static
+ {
+ try
+ {
+ METHOD_GET_STATE = TreeCache.class.getDeclaredMethod("getStateBytes", new Class[]{});
+ METHOD_SET_STATE = TreeCache.class.getDeclaredMethod("setStateBytes", new Class[]{byte[].class});
+ METHOD_GET_CHILDREN_NAMES = TreeCache.class.getDeclaredMethod("getChildrenNames", new Class[]{Fqn.class});
+ METHOD_GET_WITH_2_PARAMS = TreeCache.class.getDeclaredMethod("get", new Class[]{Fqn.class, Object.class});
+ METHOD_GET_WITH_1_PARAM = TreeCache.class.getDeclaredMethod("get", new Class[]{Fqn.class});
+ METHOD_EXISTS = TreeCache.class.getDeclaredMethod("exists", new Class[]{Fqn.class});
+ METHOD_PUT_WITH_3_PARAMS = TreeCache.class.getDeclaredMethod("put", new Class[]{Fqn.class, Object.class, Object.class});
+ METHOD_PUT_WITH_2_PARAMS = TreeCache.class.getDeclaredMethod("put", new Class[]{Fqn.class, Map.class});
+ 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)
{
@@ -81,7 +83,8 @@
/**
* Default constructor.
*/
- public RpcDelegatingCacheLoader() {
+ public RpcDelegatingCacheLoader()
+ {
// Empty.
}
@@ -90,7 +93,8 @@
*
* @param timeout The timeout in milliseconds for each RPC call.
*/
- public RpcDelegatingCacheLoader(CacheSPI cache, int timeout) {
+ public RpcDelegatingCacheLoader(CacheSPI cache, int timeout)
+ {
setCache(cache);
this.timeout = timeout;
}
@@ -98,10 +102,11 @@
/**
* Allows configuration via XML config file.
*
- * @see org.jboss.cache.loader.DelegatingCacheLoader#setConfig(java.util.Properties)
+ * @see DelegatingCacheLoader#setConfig(java.util.Properties)
*/
- public void setConfig(Properties props) {
- if(props == null) return;
+ public void setConfig(Properties props)
+ {
+ if (props == null) return;
String t = props.getProperty("timeout");
this.timeout = (t == null || t.length() == 0 ? 5000 : Integer.parseInt(t));
}
@@ -109,8 +114,9 @@
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGetChildrenNames(org.jboss.cache.Fqn)
*/
- protected Set delegateGetChildrenNames(Fqn name) throws Exception {
- return (Set) this.doMethodCall( METHOD_GET_CHILDREN_NAMES, new Object[] { name } );
+ protected Set delegateGetChildrenNames(Fqn name) throws Exception
+ {
+ return (Set) this.doMethodCall(METHOD_GET_CHILDREN_NAMES, new Object[]{name});
}
// See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
@@ -124,83 +130,63 @@
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGet(org.jboss.cache.Fqn)
*/
- protected Map delegateGet(Fqn name) throws Exception {
- DataNode n=(DataNode)this.doMethodCall( METHOD_GET_WITH_1_PARAM, new Object[] { name } );
- if(n == null)
+ protected Map delegateGet(Fqn name) throws Exception
+ {
+ DataNode n = (DataNode) this.doMethodCall(METHOD_GET_WITH_1_PARAM, new Object[]{name});
+ if (n == null)
+ {
return null;
+ }
return n.getData();
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateExists(org.jboss.cache.Fqn)
*/
- protected boolean delegateExists(Fqn name) throws Exception {
- Boolean exists = (Boolean) this.doMethodCall( METHOD_EXISTS, new Object[] { name } );
+ protected boolean delegateExists(Fqn name) throws Exception
+ {
+ Boolean exists = (Boolean) this.doMethodCall(METHOD_EXISTS, new Object[]{name});
return exists != null && exists.booleanValue();
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegatePut(org.jboss.cache.Fqn, java.lang.Object, java.lang.Object)
*/
- protected Object delegatePut(Fqn name, Object key, Object value) throws Exception {
- return this.doMethodCall( METHOD_PUT_WITH_3_PARAMS, new Object[] { name, key, value } );
+ protected Object delegatePut(Fqn name, Object key, Object value) throws Exception
+ {
+ return this.doMethodCall(METHOD_PUT_WITH_3_PARAMS, new Object[]{name, key, value});
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegatePut(org.jboss.cache.Fqn, java.util.Map)
*/
- protected void delegatePut(Fqn name, Map attributes) throws Exception {
- this.doMethodCall( METHOD_PUT_WITH_2_PARAMS, new Object[] { name, attributes } );
- }
-
- protected void delegatePut(List<Modification> modifications) throws Exception {
- for (Modification m :modifications)
+ protected void delegatePut(Fqn name, Map attributes) throws Exception
{
- 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;
- }
- }
+ this.doMethodCall(METHOD_PUT_WITH_2_PARAMS, new Object[]{name, attributes});
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemove(org.jboss.cache.Fqn, java.lang.Object)
*/
- protected Object delegateRemove(Fqn name, Object key) throws Exception {
- return this.doMethodCall( METHOD_REMOVE_WITH_2_PARAMS, new Object[] { name, key } );
+ protected Object delegateRemove(Fqn name, Object key) throws Exception
+ {
+ return this.doMethodCall(METHOD_REMOVE_WITH_2_PARAMS, new Object[]{name, key});
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemove(org.jboss.cache.Fqn)
*/
- protected void delegateRemove(Fqn name) throws Exception {
- this.doMethodCall( METHOD_REMOVE_WITH_1_PARAM, new Object[] { name } );
+ protected void delegateRemove(Fqn name) throws Exception
+ {
+ this.doMethodCall(METHOD_REMOVE_WITH_1_PARAM, new Object[]{name});
}
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemoveData(org.jboss.cache.Fqn)
*/
- protected void delegateRemoveData(Fqn name) throws Exception {
- this.doMethodCall( METHOD_REMOVE_DATA, new Object[] { name } );
+ protected void delegateRemoveData(Fqn name) throws Exception
+ {
+ this.doMethodCall(METHOD_REMOVE_DATA, new Object[]{name});
}
@Override
@@ -238,38 +224,47 @@
* @param args The arguments to use for the remote method call.
* @return The value returned by the remote method call.
*/
- private Object doMethodCall( Method method, Object[] args ) throws Exception {
- if( this.cache.getRPCManager().isCoordinator() ) {
- if( log.isTraceEnabled() ) {
- log.trace( "Cannot delegate to the remote coordinator because the cache is itself the coordinator." );
+ private Object doMethodCall(Method method, Object[] args) throws Exception
+ {
+ if (this.cache.getRPCManager().isCoordinator())
+ {
+ if (log.isTraceEnabled())
+ {
+ log.trace("Cannot delegate to the remote coordinator because the cache is itself the coordinator.");
}
return null;
}
- if( this.localAddress == null ) {
+ if (this.localAddress == null)
+ {
this.localAddress = (Address) this.cache.getLocalAddress();
}
- if( this.localAddress == null ) {
- throw new Exception( "Cannot delegate to the remote coordinator because the cache has no local address." );
+ if (this.localAddress == null)
+ {
+ throw new Exception("Cannot delegate to the remote coordinator because the cache has no local address.");
}
Address coordinator = cache.getRPCManager().getCoordinator();
- if( coordinator == null ) {
- throw new Exception( "Cannot delegate to the remote coordinator because the cache has no coordinator." );
+ if (coordinator == null)
+ {
+ throw new Exception("Cannot delegate to the remote coordinator because the cache has no coordinator.");
}
Vector<Address> members = new Vector<Address>();
- members.add( coordinator );
- MethodCall methodCall = MethodCallFactory.create( method, args );
+ members.add(coordinator);
+ MethodCall methodCall = MethodCallFactory.create(method, args);
boolean synchronous = true;
boolean excludeSelf = true;
- 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!" );
- }
- Object response = responses.get( 0 );
- if( response instanceof TimeoutException ) {
- throw new Exception( "Remote method call [" + cache.getLocalAddress() + "]->[" + coordinator + "]." + methodCall.getMethod().getName() + "() timed out: " + response );
+ 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!");
}
- else if( response instanceof Throwable ) {
- throw new Exception( "Remote method call [" + cache.getLocalAddress() + "]->[" + coordinator + "]." + methodCall.getMethod().getName() + "() failed!", (Throwable) response );
+ Object response = responses.get(0);
+ if (response instanceof TimeoutException)
+ {
+ throw new Exception("Remote method call [" + cache.getLocalAddress() + "]->[" + coordinator + "]." + methodCall.getMethod().getName() + "() timed out: " + response);
+ }
+ else if (response instanceof Throwable)
+ {
+ throw new Exception("Remote method call [" + cache.getLocalAddress() + "]->[" + coordinator + "]." + methodCall.getMethod().getName() + "() failed!", (Throwable) response);
}
return response;
}
1.3 +130 -73 JBossCache/src/org/jboss/cache/loader/TcpDelegatingCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: TcpDelegatingCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/TcpDelegatingCacheLoader.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- TcpDelegatingCacheLoader.java 31 Aug 2006 14:56:45 -0000 1.2
+++ TcpDelegatingCacheLoader.java 22 Sep 2006 16:27:56 -0000 1.3
@@ -23,16 +23,17 @@
* 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>
+ * <attribute name="CacheLoaderConfig">
+ * host=localhost
+ * port=2099
+ * </attribute>
+ * </pre>
*
* @author Bela Ban
- * @version $Id: TcpDelegatingCacheLoader.java,v 1.2 2006/08/31 14:56:45 vblagojevic Exp $
+ * @version $Id: TcpDelegatingCacheLoader.java,v 1.3 2006/09/22 16:27:56 msurtani Exp $
*/
-public class TcpDelegatingCacheLoader extends DelegatingCacheLoader {
+public class TcpDelegatingCacheLoader extends DelegatingCacheLoader
+{
private Socket sock;
private String host;
private int port;
@@ -43,7 +44,8 @@
/**
* Default constructor.
*/
- public TcpDelegatingCacheLoader() {
+ public TcpDelegatingCacheLoader()
+ {
// Empty.
}
@@ -53,7 +55,8 @@
* @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) {
+ public TcpDelegatingCacheLoader(String host, int port)
+ {
this.host = host;
this.port = port;
}
@@ -61,45 +64,73 @@
/**
* Allows configuration via XML config file.
*
- * @see org.jboss.cache.loader.DelegatingCacheLoader#setConfig(java.util.Properties)
+ * @see DelegatingCacheLoader#setConfig(java.util.Properties)
*/
- public void setConfig(Properties props) {
+ public void setConfig(Properties props)
+ {
this.host = props.getProperty("host");
- if(this.host == null || this.host.length() == 0) {
+ if (this.host == null || this.host.length() == 0)
+ {
this.host = "localhost";
}
this.port = Integer.parseInt(props.getProperty("port"));
}
- public void start() throws Exception {
+ 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) {}
+ 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());
+ 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 {
+ 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;
+ 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.
@@ -117,105 +148,131 @@
/**
* @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGet(org.jboss.cache.Fqn)
*/
- protected Map delegateGet(Fqn name) throws Exception {
+ 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;
+ 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 {
+ 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();
+ 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 {
+ 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;
+ 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 {
+ 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;
+ Object retval = in.readObject();
+ if (retval instanceof Exception)
+ {
+ throw (Exception) retval;
+ }
}
- protected void delegatePut(List<Modification> modifications) throws Exception {
+ @Override
+ public void put(List<Modification> modifications) throws Exception
+ {
out.writeInt(DelegatingCacheLoader.putList);
- int length=modifications != null? modifications.size() : 0;
+ int length = modifications != null ? modifications.size() : 0;
out.writeInt(length);
- if(length > 0) {
- for(Modification m : modifications)
+ if (length > 0)
+ {
+ for (Modification m : modifications)
{
m.writeExternal(out);
}
}
out.flush();
- Object retval=in.readObject();
- if(retval instanceof Exception)
- throw (Exception)retval;
+ 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 {
+ 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;
+ 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 {
+ 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;
+ 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 {
+ 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;
+ Object retval = in.readObject();
+ if (retval instanceof Exception)
+ {
+ throw (Exception) retval;
+ }
}
@Override
More information about the jboss-cvs-commits
mailing list