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

DistributedSetState

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

Fields Summary
private static final long
serialVersionUID
private static HashMap
setMethodMap
protected static Logger
log
protected volatile long
versionId
protected HashSet
updates
protected String
classname
protected transient Set
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 DistributedSetState()
For serialization


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

      super(guid, timeout, setMethodMap);
      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 = createSetUpdates(base);
   
Methods Summary
public booleanadd(java.lang.Object val)

      try
      {
         lock.readLock().acquire();
         try
         {
            val = versionManager.makeVersioned(val);
            Set state = getCurrentState(true);
            return state.add(val);
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public booleanaddAll(java.util.Collection c)

      try
      {
         lock.readLock().acquire();
         try
         {
            Set state = getCurrentState(true);
            Object[] copy = c.toArray();
            for (int i = 0; i < copy.length; i++)
            {
               Object item = versionManager.makeVersioned(copy[i]);
               state.add(item);
            }
            return true;
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public org.jboss.aop.InstanceAdvisedbuildObject(SynchronizationManager manager, DistributedVersionManager versionManager)

      log.trace("building a Set");
      this.versionManager = versionManager;
      this.synchManager = manager;
      log.trace("DistributedSetState: classname: " + classname);
      Class clazz = Thread.currentThread().getContextClassLoader().loadClass(classname);
      base = (Set)clazz.newInstance();
      Iterator it = updates.iterator();
      while (it.hasNext())
      {
         Object val = it.next();
         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.add(val);
      }
      proxy = versionManager.addSetVersioning(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 set");
   
public voidclear()

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

      try
      {
         lock.readLock().acquire();
         try
         {
            Set state = getCurrentState(false);
            return state.contains(o);
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public booleancontainsAll(java.util.Collection c)

      try
      {
         lock.readLock().acquire();
         try
         {
            Set state = getCurrentState(false);
            return state.containsAll(c);
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
protected java.util.HashSetcreateSetUpdates(java.util.Set state)

      HashSet setUpdates = new HashSet(state.size());
      Iterator it = state.iterator();
      while (it.hasNext())
      {
         Object obj = it.next();
         if (versionManager.isVersioned(obj))
         {
            setUpdates.add(new VersionReference(VersionManager.getGUID((InstanceAdvised)obj)));
         }
         else
         {
            setUpdates.add(obj);
         }
      }
      return setUpdates;
   
public DistributedUpdatecreateTxUpdate(javax.transaction.Transaction tx)

      Set state = (Set)txState.get(tx);
      long newId = ((Long)txVersion.get(tx)).longValue();
      DistributedSetUpdate update = new DistributedSetUpdate(guid, createSetUpdates(state), newId);
      return update;
   
public booleanequals(java.lang.Object o)

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

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

      Set state = (Set)txState.get(tx);
      if (state == null && forUpdate)
      {
         state = (Set)base.getClass().newInstance();
         state.addAll(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
         {
            Set state = getCurrentState(false);
            return state.hashCode();
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public booleanisEmpty()

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

      try
      {
         lock.readLock().acquire();
         try
         {
            Set state = getCurrentState(false);
            return state.iterator();
         }
         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!!!!
      Set current = (Set)txState.get(tx);
      base = current;
      Long version = (Long)txVersion.get(tx);
      versionId = version.longValue();
   
public voidmergeState(DistributedUpdate update)

      DistributedSetUpdate setUpdate = (DistributedSetUpdate)update;
      this.versionId = setUpdate.versionId;
      base.clear();
      Iterator it = setUpdate.setUpdates.iterator();
      while (it.hasNext())
      {
         Object val = it.next();
         if (val instanceof VersionReference)
         {
            VersionReference ref = (VersionReference)val;
            val = synchManager.getObject(ref.getGUID());
            ref.set((InstanceAdvised)val);
         }
         base.add(val);
      }
      updates = setUpdate.setUpdates;
   
public voidreadExternal(java.io.ObjectInput in)

      super.readExternal(in);
      versionId = in.readLong();
      this.updates = (HashSet)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 = setMethodMap;
   
public booleanremove(java.lang.Object o)

      try
      {
         lock.readLock().acquire();
         try
         {
            Set state = getCurrentState(true);
            return state.remove(o);
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public booleanremoveAll(java.util.Collection c)

      try
      {
         lock.readLock().acquire();
         try
         {
            Set state = getCurrentState(true);
            return state.removeAll(c);
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public booleanretainAll(java.util.Collection c)

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

      try
      {
         lock.readLock().acquire();
         try
         {
            Set state = getCurrentState(false);
            return state.size();
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public java.lang.Object[]toArray()

      try
      {
         lock.readLock().acquire();
         try
         {
            Set state = getCurrentState(false);
            return state.toArray();
         }
         finally
         {
            lock.readLock().release();
         }
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public java.lang.Object[]toArray(java.lang.Object[] a)

      try
      {
         lock.readLock().acquire();
         try
         {
            Set state = getCurrentState(false);
            return state.toArray(a);
         }
         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);