KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aspects > versioned > StateManager


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

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 /**
32  *
33  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
34  * @version $Revision: 37406 $
35  */

36 public abstract class StateManager implements java.io.Externalizable JavaDoc
37 {
38    public static final String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc out)
111       throws java.io.IOException JavaDoc
112    {
113       out.writeObject(guid);
114       out.writeLong(timeout);
115    }
116
117    public void readExternal(java.io.ObjectInput JavaDoc in)
118       throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc
119    {
120       this.guid = (GUID)in.readObject();
121       this.timeout = in.readLong();
122       this.lock = new WriterPreferenceReadWriteLock();
123    }
124
125
126 }
127
Popular Tags