summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2006-11-13 05:07:18 (EST)
committerEike Stepper2006-11-13 05:07:18 (EST)
commitc1aeefd10a5eaf5e714952d9eb98b8215e87ee1d (patch)
tree0d150a74feb0da5a387ae26cd2f8e453cb5c9411
parent4d9f313de71f04d621ac122b7f50aecee1c530be (diff)
downloadcdo-c1aeefd10a5eaf5e714952d9eb98b8215e87ee1d.zip
cdo-c1aeefd10a5eaf5e714952d9eb98b8215e87ee1d.tar.gz
cdo-c1aeefd10a5eaf5e714952d9eb98b8215e87ee1d.tar.bz2
Made IRegistry transactional
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractMappingRegistry.java59
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractRegistry.java214
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/HashMapRegistry.java2
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/RegistryDelta.java40
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/RegistryEvent.java29
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/AbstractCachingRegistry.java (renamed from plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractCachingRegistry.java)3
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/DelegatingRegistry.java (renamed from plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/DelegatingRegistry.java)13
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/HashCacheRegistry.java (renamed from plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/HashCacheRegistry.java)4
8 files changed, 192 insertions, 172 deletions
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractMappingRegistry.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractMappingRegistry.java
deleted file mode 100644
index a997c0c..0000000
--- a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractMappingRegistry.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/***************************************************************************
- * Copyright (c) 2004, 2005, 2006 Eike Stepper, Germany.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Eike Stepper - initial API and implementation
- **************************************************************************/
-package org.eclipse.net4j.util.registry;
-
-import java.util.Collection;
-import java.util.Map;
-import java.util.Set;
-
-/**
- * @author Eike Stepper
- */
-public abstract class AbstractMappingRegistry<K, V> extends AbstractRegistry<K, V>
-{
- public AbstractMappingRegistry()
- {
- }
-
- @Override
- protected V deregister(Object key)
- {
- return getMap().remove(key);
- }
-
- @Override
- protected V register(K key, V value)
- {
- return getMap().put(key, value);
- }
-
- public Set<Entry<K, V>> entrySet()
- {
- return getMap().entrySet();
- }
-
- public V get(Object key)
- {
- return getMap().get(key);
- }
-
- public Set<K> keySet()
- {
- return getMap().keySet();
- }
-
- public Collection<V> values()
- {
- return getMap().values();
- }
-
- protected abstract Map<K, V> getMap();
-}
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractRegistry.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractRegistry.java
index 76499a1..a5c5170 100644
--- a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractRegistry.java
+++ b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractRegistry.java
@@ -15,6 +15,7 @@ import org.eclipse.net4j.util.om.trace.ContextTracer;
import org.eclipse.internal.net4j.bundle.Net4j;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -37,8 +38,6 @@ public abstract class AbstractRegistry<K, V> implements IRegistry<K, V>
private Transaction transaction;
- private Object transactionLock = new Object();
-
protected AbstractRegistry()
{
}
@@ -53,13 +52,29 @@ public abstract class AbstractRegistry<K, V> implements IRegistry<K, V>
listeners.remove(listener);
}
- public void clear()
+ public boolean isEmpty()
{
- Set<K> keys = keySet();
- for (K key : keys)
- {
- deregister(key);
- }
+ return keySet().isEmpty();
+ }
+
+ public int size()
+ {
+ return keySet().size();
+ }
+
+ public Set<Entry<K, V>> entrySet()
+ {
+ return getMap().entrySet();
+ }
+
+ public Set<K> keySet()
+ {
+ return getMap().keySet();
+ }
+
+ public Collection<V> values()
+ {
+ return getMap().values();
}
public boolean containsKey(Object key)
@@ -72,17 +87,23 @@ public abstract class AbstractRegistry<K, V> implements IRegistry<K, V>
return values().contains(value);
}
- public boolean isEmpty()
+ public V get(Object key)
{
- return keySet().isEmpty();
+ return getMap().get(key);
}
- public V put(K key, V value)
+ /**
+ * Requires {@link #commit()} to be called later.
+ */
+ public synchronized V put(K key, V value)
{
return getTransaction().put(key, value);
}
- public void putAll(Map<? extends K, ? extends V> t)
+ /**
+ * Requires {@link #commit()} to be called later.
+ */
+ public synchronized void putAll(Map<? extends K, ? extends V> t)
{
if (!t.isEmpty())
{
@@ -96,49 +117,95 @@ public abstract class AbstractRegistry<K, V> implements IRegistry<K, V>
}
}
- public V remove(Object key)
+ /**
+ * Requires {@link #commit()} to be called later.
+ */
+ public synchronized V remove(Object key)
{
return getTransaction().remove(key);
}
- public int size()
+ /**
+ * Requires {@link #commit()} to be called later.
+ */
+ public synchronized void clear()
{
- return keySet().size();
+ if (isEmpty())
+ {
+ Transaction transaction = getTransaction();
+ Set<K> keys = keySet();
+ for (K key : keys)
+ {
+ transaction.remove(key);
+ }
+ }
}
- public void commit()
+ public synchronized void commit()
{
- synchronized (transactionLock)
+ if (transaction != null && transaction.isOwned())
{
- if (transaction != null)
- {
- transaction.commit();
- }
+ transaction.commit();
+ transaction = null;
+ notifyAll();
}
}
+ public synchronized void dispose()
+ {
+ listeners.clear();
+ }
+
+ protected V register(K key, V value)
+ {
+ return getMap().put(key, value);
+ }
+
+ protected V deregister(Object key)
+ {
+ return getMap().remove(key);
+ }
+
protected Transaction getTransaction()
{
- synchronized (transactionLock)
+ for (;;)
{
- if (transaction != null)
+ if (transaction == null)
{
transaction = new Transaction();
+ return transaction;
}
- transaction.increaseNesting();
- }
-
- return transaction;
- }
+ if (transaction.isOwned())
+ {
+ transaction.increaseNesting();
+ return transaction;
+ }
- public void dispose()
- {
- listeners.clear();
+ try
+ {
+ wait();
+ }
+ catch (InterruptedException ex)
+ {
+ return null;
+ }
+ }
}
protected void fireRegistryEvent(IRegistryEvent<K, V> event)
{
+ if (TRACER.isEnabled())
+ {
+ for (IRegistryDelta<K, V> delta : event.getDeltas())
+ {
+ K key = delta.getKey();
+ V value = delta.getValue();
+ int kind = delta.getKind();
+ TRACER.trace("Registry delta " + key + " = " + value + " (" + kind + ")");
+ }
+ }
+
for (IRegistryListener listener : listeners)
{
try
@@ -147,29 +214,32 @@ public abstract class AbstractRegistry<K, V> implements IRegistry<K, V>
}
catch (Exception ex)
{
- if (TRACER.isEnabled())
- {
- TRACER.trace(ex);
- }
+ Net4j.LOG.error(ex);
}
}
}
- protected abstract V register(K key, V value);
-
- protected abstract V deregister(Object key);
+ protected abstract Map<K, V> getMap();
/**
* @author Eike Stepper
*/
protected class Transaction
{
- private int nesting;
+ private int nesting = 1;
private List<IRegistryDelta<K, V>> deltas = new ArrayList();
+ private Thread owner;
+
public Transaction()
{
+ owner = Thread.currentThread();
+ }
+
+ public boolean isOwned()
+ {
+ return owner == Thread.currentThread();
}
public void increaseNesting()
@@ -210,7 +280,6 @@ public abstract class AbstractRegistry<K, V> implements IRegistry<K, V>
}
deltas = null;
- transaction = null;
}
}
@@ -224,4 +293,69 @@ public abstract class AbstractRegistry<K, V> implements IRegistry<K, V>
deltas.add(new RegistryDelta(key, value, IRegistryDelta.DEREGISTERING));
}
}
+
+ /**
+ * @author Eike Stepper
+ */
+ public static class RegistryEvent<K, V> implements IRegistryEvent<K, V>
+ {
+ private IRegistry<K, V> registry;
+
+ private List<IRegistryDelta<K, V>> deltas;
+
+ public RegistryEvent(IRegistry<K, V> registry, List<IRegistryDelta<K, V>> deltas)
+ {
+ this.registry = registry;
+ this.deltas = deltas;
+ }
+
+ public IRegistry<K, V> getRegistry()
+ {
+ return registry;
+ }
+
+ public IRegistryDelta<K, V>[] getDeltas()
+ {
+ return deltas.toArray(new IRegistryDelta[deltas.size()]);
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ public static class RegistryDelta<K, V> implements IRegistryDelta<K, V>
+ {
+ private K key;
+
+ private V value;
+
+ private int kind;
+
+ public RegistryDelta(K key, V value, int kind)
+ {
+ this.key = key;
+ this.value = value;
+ this.kind = kind;
+ }
+
+ public K getKey()
+ {
+ return key;
+ }
+
+ public V getValue()
+ {
+ return value;
+ }
+
+ public V setValue(V value)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public int getKind()
+ {
+ return kind;
+ }
+ }
}
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/HashMapRegistry.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/HashMapRegistry.java
index 23bd70b..b1b9eea 100644
--- a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/HashMapRegistry.java
+++ b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/HashMapRegistry.java
@@ -16,7 +16,7 @@ import java.util.Map;
/**
* @author Eike Stepper
*/
-public class HashMapRegistry<K, V> extends AbstractMappingRegistry<K, V>
+public class HashMapRegistry<K, V> extends AbstractRegistry<K, V>
{
private Map<K, V> map;
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/RegistryDelta.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/RegistryDelta.java
deleted file mode 100644
index 4506606..0000000
--- a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/RegistryDelta.java
+++ /dev/null
@@ -1,40 +0,0 @@
-package org.eclipse.net4j.util.registry;
-
-/**
- * @author Eike Stepper
- */
-public class RegistryDelta<K, V> implements IRegistryDelta<K, V>
-{
- private K key;
-
- private V value;
-
- private int kind;
-
- public RegistryDelta(K key, V value, int kind)
- {
- this.key = key;
- this.value = value;
- this.kind = kind;
- }
-
- public K getKey()
- {
- return key;
- }
-
- public V getValue()
- {
- return value;
- }
-
- public V setValue(V value)
- {
- throw new UnsupportedOperationException();
- }
-
- public int getKind()
- {
- return kind;
- }
-} \ No newline at end of file
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/RegistryEvent.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/RegistryEvent.java
deleted file mode 100644
index 8cf30b5..0000000
--- a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/RegistryEvent.java
+++ /dev/null
@@ -1,29 +0,0 @@
-package org.eclipse.net4j.util.registry;
-
-import java.util.List;
-
-/**
- * @author Eike Stepper
- */
-public class RegistryEvent<K, V> implements IRegistryEvent<K, V>
-{
- private IRegistry<K, V> registry;
-
- private List<IRegistryDelta<K, V>> deltas;
-
- public RegistryEvent(IRegistry<K, V> registry, List<IRegistryDelta<K, V>> deltas)
- {
- this.registry = registry;
- this.deltas = deltas;
- }
-
- public IRegistry<K, V> getRegistry()
- {
- return registry;
- }
-
- public IRegistryDelta<K, V>[] getDeltas()
- {
- return deltas.toArray(new IRegistryDelta[deltas.size()]);
- }
-} \ No newline at end of file
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractCachingRegistry.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/AbstractCachingRegistry.java
index ac4e070..7ea2c84 100644
--- a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractCachingRegistry.java
+++ b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/AbstractCachingRegistry.java
@@ -8,8 +8,9 @@
* Contributors:
* Eike Stepper - initial API and implementation
**************************************************************************/
-package org.eclipse.net4j.util.registry;
+package org.eclipse.net4j.util.registry.delegating;
+import org.eclipse.net4j.util.registry.IRegistry;
import org.eclipse.net4j.util.registry.IRegistryElement.Descriptor;
import org.eclipse.net4j.util.registry.IRegistryListener.EventType;
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/DelegatingRegistry.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/DelegatingRegistry.java
index 917704e..4ef308a 100644
--- a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/DelegatingRegistry.java
+++ b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/DelegatingRegistry.java
@@ -8,8 +8,11 @@
* Contributors:
* Eike Stepper - initial API and implementation
**************************************************************************/
-package org.eclipse.net4j.util.registry;
+package org.eclipse.net4j.util.registry.delegating;
+import org.eclipse.net4j.util.registry.AbstractRegistry;
+import org.eclipse.net4j.util.registry.IRegistry;
+import org.eclipse.net4j.util.registry.IRegistryListener;
import org.eclipse.net4j.util.registry.IRegistryListener.EventType;
import java.util.Set;
@@ -112,4 +115,12 @@ public class DelegatingRegistry<ID, E extends IRegistryElement<ID>> extends Abst
{
// Do nothing
}
+
+ /* (non-Javadoc)
+ * @see org.eclipse.net4j.util.registry.AbstractRegistry#getMap()
+ */
+ protected Map<ID, E> getMap()
+ {
+ return null;
+ }
}
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/HashCacheRegistry.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/HashCacheRegistry.java
index fb0c2e0..88ec562 100644
--- a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/HashCacheRegistry.java
+++ b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/HashCacheRegistry.java
@@ -8,7 +8,9 @@
* Contributors:
* Eike Stepper - initial API and implementation
**************************************************************************/
-package org.eclipse.net4j.util.registry;
+package org.eclipse.net4j.util.registry.delegating;
+
+import org.eclipse.net4j.util.registry.IRegistry;
import java.util.HashMap;
import java.util.Map;