1 22 package org.jboss.aspects.versioned; 23 24 import EDU.oswego.cs.dl.util.concurrent.ReadWriteLock; 25 import EDU.oswego.cs.dl.util.concurrent.WriterPreferenceReadWriteLock; 26 import org.jboss.aop.InstanceAdvised; 27 import org.jboss.aop.util.PayloadKey; 28 import org.jboss.logging.Logger; 29 import org.jboss.util.id.GUID; 30 31 36 public abstract class StateManager implements java.io.Externalizable 37 { 38 public static final String STATE_MANAGER = "STATE_MANAGER"; 39 protected static Logger log = Logger.getLogger(StateManager.class); 40 41 42 public static StateManager getStateManager(InstanceAdvised obj) 43 { 44 return (StateManager)obj._getInstanceAdvisor().getMetaData().getMetaData(STATE_MANAGER, STATE_MANAGER); 45 } 46 47 public static void setStateManager(InstanceAdvised obj, StateManager manager) 48 { 49 obj._getInstanceAdvisor().getMetaData().addMetaData(STATE_MANAGER, STATE_MANAGER, manager, PayloadKey.TRANSIENT); 50 } 51 52 protected GUID guid; 53 protected long timeout; 54 transient protected ReadWriteLock lock = new WriterPreferenceReadWriteLock(); 55 56 public StateManager() {} 57 58 public StateManager(GUID guid, long timeout) 59 { 60 this.guid = guid; 61 this.timeout = timeout; 62 } 63 64 public ReadWriteLock getLock() { return lock; } 65 public GUID getGUID() { return guid; } 66 67 public void acquireWriteLock() 68 { 69 try 70 { 71 if (!lock.writeLock().attempt(timeout)) 72 { 73 throw new LockAttemptFailure("failed to acquire write lock: " + guid); 74 } 75 log.trace("writelock acquired for: " + guid); 76 } 77 catch (InterruptedException ignored) 78 { 79 throw new LockAttemptFailure("failed to acquire write lock: " + guid); 80 } 81 } 82 83 public void acquireReadLock() 84 { 85 try 86 { 87 if (!lock.readLock().attempt(timeout)) 88 { 89 throw new LockAttemptFailure("failed to acquire read lock: " + guid); 90 } 91 log.trace("readlock acquired for: " + guid); 92 } 93 catch (InterruptedException ignored) 94 { 95 throw new LockAttemptFailure("failed to acquire read lock: " + guid); 96 } 97 } 98 99 public void releaseReadLock() 100 { 101 lock.readLock().release(); 102 log.trace("readlock released for: " + guid); 103 } 104 public void releaseWriteLock() 105 { 106 lock.writeLock().release(); 107 log.trace("writelock released for: " + guid); 108 } 109 110 public void writeExternal(java.io.ObjectOutput out) 111 throws java.io.IOException 112 { 113 out.writeObject(guid); 114 out.writeLong(timeout); 115 } 116 117 public void readExternal(java.io.ObjectInput in) 118 throws java.io.IOException , ClassNotFoundException 119 { 120 this.guid = (GUID)in.readObject(); 121 this.timeout = in.readLong(); 122 this.lock = new WriterPreferenceReadWriteLock(); 123 } 124 125 126 } 127 | Popular Tags |