FileDocCategorySizeDatePackage
DistributedMapState.javaAPI DocJBoss 4.2.114502Fri Jul 13 21:02:32 BST 2007org.jboss.aspects.versioned

DistributedMapState

public class DistributedMapState extends CollectionStateManager implements Externalizable, DistributedState, Map
author
Bill Burke
version
$Revision: 57186 $

Fields Summary
private static final long
serialVersionUID
private static HashMap
mapMethodMap
protected static Logger
log
protected volatile long
versionId
protected HashMap
updates
protected String
classname
protected transient Map
base
protected transient org.jboss.tm.TransactionLocal
txState
protected transient org.jboss.tm.TransactionLocal
txVersion
protected transient DistributedVersionManager
versionManager
protected transient SynchronizationManager
synchManager
protected transient TransactionManager
tm
protected transient org.jboss.aop.proxy.ClassProxy
proxy
Constructors Summary
public DistributedMapState()
For serialization


         
     
public DistributedMapState(org.jboss.util.id.GUID guid, long timeout, org.jboss.aop.proxy.ClassProxy proxy, Map obj, DistributedVersionManager versionManager, SynchronizationManager synchManager)

      super(guid, timeout, mapMethodMap);
      this.base = obj;
      this.classname = obj.getClass().getName();
      this.versionManager = versionManager;
      this.synchManager = synchManager;
      this.proxy = proxy;
      InitialContext ctx = new InitialContext();
      this.tm = (TransactionManager)ctx.lookup("java:/TransactionManager");
      this.updates = createMapUpdates(base);
   
Methods Summary
public org.jboss.aop.InstanceAdvisedbuildObject(SynchronizationManager manager, DistributedVersionManager versionManager)

      log.trace("building a Map");
      this.versionManager = versionManager;
      this.synchManager = manager;
      log.trace("DistributedMaptState: classname: " + classname);
      Class clazz = Thread.currentThread().getContextClassLoader().loadClass(classname);
      base = (Map)clazz.newInstance();
      Iterator it = updates.entrySet().iterator();
      while (it.hasNext())
      {
         Map.Entry entry = (Map.Entry)it.next();
         Object val = entry.getValue();
         if (val instanceof VersionReference)
         {
            VersionReference ref = (VersionReference)val;
            val = manager.getObject(ref.getGUID());
            if (val == null)
            {
               DistributedState fieldVal = manager.getState(ref.getGUID());
               val = fieldVal.buildObject(manager, versionManager);
               ref.set((InstanceAdvised)val);
            }
         }
         base.put(entry.getKey(), val);
      }
      proxy = versionManager.addMapVersioning(base, this);
      return proxy;
   
public voidcheckOptimisticLock(javax.transaction.Transaction tx)

      // NOTE THIS CODE ASSUMES THAT A WRITELOCK HAS BEEN ACQUIRED!!!!
      Long version = (Long)txVersion.get(tx);
      if (version.longValue() <= versionId)
         throw new OptimisticLockFailure("optimistic lock failure for list");
   
public voidclear()

      try
      {
         lock.readLock().acquire();
         try
         {
            Map state = getCurrentState(true);
            state.clear();
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public booleancontainsKey(java.lang.Object o)

      try
      {
         lock.readLock().acquire();
         try
         {
            Map state = getCurrentState(false);
            return state.containsKey(o);
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public booleancontainsValue(java.lang.Object o)

      try
      {
         lock.readLock().acquire();
         try
         {
            Map state = getCurrentState(false);
            return state.containsKey(o);
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
protected java.util.HashMapcreateMapUpdates(java.util.Map state)

      HashMap mapUpdates = new HashMap();
      Iterator it = state.entrySet().iterator();
      while (it.hasNext())
      {
         Map.Entry entry = (Map.Entry)it.next();
         Object obj = entry.getValue();
         if (versionManager.isVersioned(obj))
         {
            mapUpdates.put(entry.getKey(), new VersionReference(VersionManager.getGUID((InstanceAdvised)obj)));
         }
         else
         {
            mapUpdates.put(entry.getKey(), obj);
         }
      }
      return mapUpdates;
   
public DistributedUpdatecreateTxUpdate(javax.transaction.Transaction tx)

      Map state = (Map)txState.get(tx);
      long newId = ((Long)txVersion.get(tx)).longValue();
      DistributedMapUpdate update = new DistributedMapUpdate(guid, createMapUpdates(state), newId);
      return update;
   
public java.util.SetentrySet()

      try
      {
         lock.readLock().acquire();
         try
         {
            Map state = getCurrentState(false);
            return state.entrySet();
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public booleanequals(java.lang.Object o)

      try
      {
         lock.readLock().acquire();
         try
         {
            Map state = getCurrentState(false);
            return state.equals(o);
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public java.lang.Objectget(java.lang.Object o)

      try
      {
         lock.readLock().acquire();
         try
         {
            Map state = getCurrentState(false);
            return state.get(o);
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
protected java.util.MapgetCurrentState(boolean forUpdate)

      Transaction tx = tm.getTransaction();
      if (tx == null)
      {
         if (forUpdate) versionId++;
         return base;
      }

      Map state = (Map)txState.get(tx);
      if (state == null && forUpdate)
      {
         state = (Map)base.getClass().newInstance();
         state.putAll(base);
         txState.set(tx, state);
         long newId = versionId + 1;
         synchManager.registerUpdate(tx, this);
         txVersion.set(tx, new Long(newId));
         return state;
      }
      return base;
   
public java.util.HashMapgetMethodMap()

      return ClassProxyFactory.getMethodMap(base.getClass().getName());
   
public org.jboss.aop.InstanceAdvisedgetObject()

 return proxy; 
public inthashCode()

      try
      {
         lock.readLock().acquire();
         try
         {
            Map state = getCurrentState(false);
            return state.hashCode();
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public booleanisEmpty()

      try
      {
         lock.readLock().acquire();
         try
         {
            Map state = getCurrentState(false);
            return state.isEmpty();
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public java.util.SetkeySet()

      try
      {
         lock.readLock().acquire();
         try
         {
            Map state = getCurrentState(false);
            return state.keySet();
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public voidmergeState(javax.transaction.Transaction tx)

      // NOTE THIS CODE ASSUMES THAT A WRITELOCK HAS BEEN ACQUIRED!!!!
      Map current = (Map)txState.get(tx);
      base = current;
      Long version = (Long)txVersion.get(tx);
      versionId = version.longValue();
   
public voidmergeState(DistributedUpdate update)

      DistributedMapUpdate mapUpdate = (DistributedMapUpdate)update;
      this.versionId = mapUpdate.versionId;
      base.clear();
      Iterator it = mapUpdate.mapUpdates.entrySet().iterator();
      while (it.hasNext())
      {
         Map.Entry entry = (Map.Entry)it.next();
         Object val = entry.getValue();
         if (val instanceof VersionReference)
         {
            VersionReference ref = (VersionReference)val;
            val = synchManager.getObject(ref.getGUID());
            ref.set((InstanceAdvised)val);
         }
         base.put(entry.getKey(), val);
      }
      updates = mapUpdate.mapUpdates;
   
public java.lang.Objectput(java.lang.Object key, java.lang.Object val)

      try
      {
         lock.readLock().acquire();
         try
         {
            val = versionManager.makeVersioned(val);
            Map state = getCurrentState(true);
            return state.put(key, val);
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public voidputAll(java.util.Map c)

      try
      {
         lock.readLock().acquire();
         try
         {
            Map state = getCurrentState(true);
            Iterator it = state.entrySet().iterator();
            while (it.hasNext())
            {
               Map.Entry entry = (Map.Entry)it.next();
               Object val = versionManager.makeVersioned(entry.getValue());
               state.put(entry.getKey(), val);
            }
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public voidreadExternal(java.io.ObjectInput in)

      super.readExternal(in);
      versionId = in.readLong();
      this.updates = (HashMap)in.readObject();
      this.classname = (String)in.readObject();
      try
      {
         InitialContext ctx = new InitialContext();
         this.tm = (TransactionManager)ctx.lookup("java:/TransactionManager");
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
      this.txState = new TransactionLocal();
      this.txVersion = new TransactionLocal();
      this.methodMap = mapMethodMap;
   
public java.lang.Objectremove(java.lang.Object key)

      try
      {
         lock.readLock().acquire();
         try
         {
            Map state = getCurrentState(true);
            return state.remove(key);
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public intsize()

      try
      {
         lock.readLock().acquire();
         try
         {
            Map state = getCurrentState(false);
            return state.size();
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public java.util.Collectionvalues()

      try
      {
         lock.readLock().acquire();
         try
         {
            Map state = getCurrentState(false);
            return state.values();
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public voidwriteExternal(java.io.ObjectOutput out)

      super.writeExternal(out);
      out.writeLong(versionId);
      out.writeObject(updates);
      out.writeObject(classname);