001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.server.aspects;
023:
024: import edu.emory.mathcs.backport.java.util.concurrent.locks.Lock;
025: import edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock;
026: import org.jboss.portal.common.invocation.Interceptor;
027: import org.jboss.portal.common.invocation.Invocation;
028: import org.jboss.portal.common.invocation.InvocationException;
029:
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: /**
034: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
035: * @version $Revision: 8784 $
036: */
037: public abstract class LockInterceptor implements Interceptor {
038:
039: /** . */
040: private Map map = new HashMap();
041:
042: /** . */
043: private Lock mapLock = new ReentrantLock();
044:
045: public static class InternalLock {
046:
047: /** . */
048: private final Object id;
049:
050: /** . */
051: private final Lock lock = new ReentrantLock();
052:
053: /** . */
054: private int waiters = 0;
055:
056: public InternalLock(Object id) {
057: this .id = id;
058: }
059:
060: Object invoke(Invocation invocation) throws Exception,
061: InvocationException {
062: lock.lock();
063: try {
064: return invocation.invokeNext();
065: } finally {
066: lock.unlock();
067: }
068: }
069: }
070:
071: protected InternalLock acquire(Object lockId) {
072: mapLock.lock();
073: try {
074: InternalLock lock;
075: if (map.containsKey(lockId)) {
076: lock = (InternalLock) map.get(lockId);
077: } else {
078: lock = new InternalLock(lockId);
079: map.put(lockId, lock);
080: }
081: lock.waiters++;
082: return lock;
083: } finally {
084: mapLock.unlock();
085: }
086: }
087:
088: protected void release(InternalLock internalLock) {
089: mapLock.lock();
090: try {
091: if (--internalLock.waiters == 0) {
092: map.remove(internalLock.id);
093: }
094: } finally {
095: mapLock.unlock();
096: }
097: }
098:
099: protected abstract Object getLockId(Invocation invocation);
100:
101: public Object invoke(Invocation invocation) throws Exception,
102: InvocationException {
103: Object lockId = getLockId(invocation);
104:
105: //
106: if (lockId != null) {
107: InternalLock internalLock = acquire(lockId);
108: try {
109: return internalLock.invoke(invocation);
110: } finally {
111: release(internalLock);
112: }
113: } else {
114: return invocation.invokeNext();
115: }
116: }
117: }
|