Author: julien(a)jboss.com
Date: 2007-04-04 07:38:41 -0400 (Wed, 04 Apr 2007)
New Revision: 6915
Added:
trunk/server/src/main/org/jboss/portal/server/aspects/LockInterceptor.java
trunk/server/src/main/org/jboss/portal/server/aspects/server/SessionLockInterceptor.java
Removed:
trunk/server/src/main/org/jboss/portal/server/aspects/server/NavigationInterceptor.java
Modified:
trunk/core/src/resources/portal-core-sar/META-INF/jboss-service.xml
trunk/server/build.xml
trunk/server/src/main/org/jboss/portal/server/impl/invocation/JBossInterceptorStack.java
Log:
adding a server interceptor that perform request locking based on the provided jsessionid
in order to avoid concurrent modifications of the same session state.
Modified: trunk/core/src/resources/portal-core-sar/META-INF/jboss-service.xml
===================================================================
--- trunk/core/src/resources/portal-core-sar/META-INF/jboss-service.xml 2007-04-04
10:43:17 UTC (rev 6914)
+++ trunk/core/src/resources/portal-core-sar/META-INF/jboss-service.xml 2007-04-04
11:38:41 UTC (rev 6915)
@@ -94,6 +94,13 @@
<!-- Server stack -->
<mbean
+ code="org.jboss.portal.server.aspects.server.SessionLockInterceptor"
+ name="portal:service=Interceptor,type=Server,name=SessionLock"
+ xmbean-dd=""
+ xmbean-code="org.jboss.portal.jems.as.system.JBossServiceModelMBean">
+ <xmbean/>
+ </mbean>
+ <mbean
code="org.jboss.portal.core.aspects.server.TransactionInterceptor"
name="portal:service=Interceptor,type=Server,name=Transaction"
xmbean-dd=""
@@ -144,6 +151,7 @@
xmbean-code="org.jboss.portal.jems.as.system.JBossServiceModelMBean">
<xmbean/>
<depends-list optional-attribute-name="InterceptorNames">
+
<depends-list-element>portal:service=Interceptor,type=Server,name=SessionLock</depends-list-element>
<depends-list-element>portal:service=Interceptor,type=Server,name=Transaction</depends-list-element>
<depends-list-element>portal:service=Interceptor,type=Server,name=UserEvent</depends-list-element>
<depends-list-element>portal:service=Interceptor,type=Server,name=SessionInvalidator</depends-list-element>
Modified: trunk/server/build.xml
===================================================================
--- trunk/server/build.xml 2007-04-04 10:43:17 UTC (rev 6914)
+++ trunk/server/build.xml 2007-04-04 11:38:41 UTC (rev 6915)
@@ -102,6 +102,7 @@
<path refid="junit.junit.classpath"/>
<path refid="jboss.test.classpath"/>
<path refid="sun.servlet.classpath"/>
+ <path refid="jboss/backport.concurrent.classpath"/>
<pathelement location="jboss-bean-deployer.jar"/>
<pathelement location="jboss-container.jar"/>
<pathelement location="jboss-dependency.jar"/>
Added: trunk/server/src/main/org/jboss/portal/server/aspects/LockInterceptor.java
===================================================================
--- trunk/server/src/main/org/jboss/portal/server/aspects/LockInterceptor.java
(rev 0)
+++ trunk/server/src/main/org/jboss/portal/server/aspects/LockInterceptor.java 2007-04-04
11:38:41 UTC (rev 6915)
@@ -0,0 +1,143 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.server.aspects;
+
+import org.jboss.portal.common.invocation.InvocationException;
+import org.jboss.portal.common.invocation.Interceptor;
+import org.jboss.portal.common.invocation.Invocation;
+
+import edu.emory.mathcs.backport.java.util.concurrent.locks.Lock;
+import edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock;
+
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class LockInterceptor implements Interceptor
+{
+
+ /** . */
+ private Map map = new HashMap();
+
+ /** . */
+ private Lock mapLock = new ReentrantLock();
+
+ public static class InternalLock
+ {
+
+ /** . */
+ private final Object id;
+
+ /** . */
+ private final Lock lock = new ReentrantLock();
+
+ /** . */
+ private int waiters = 0;
+
+ public InternalLock(Object id)
+ {
+ this.id = id;
+ }
+
+ Object invoke(Invocation invocation) throws Exception, InvocationException
+ {
+ lock.lock();
+ try
+ {
+ return invocation.invokeNext();
+ }
+ finally
+ {
+ lock.unlock();
+ }
+ }
+ }
+
+ protected InternalLock acquire(Object lockId)
+ {
+ mapLock.lock();
+ try
+ {
+ InternalLock lock;
+ if (map.containsKey(lockId))
+ {
+ lock = (InternalLock)map.get(lockId);
+ }
+ else
+ {
+ lock = new InternalLock(lockId);
+ map.put(lockId, lock);
+ }
+ lock.waiters++;
+ return lock;
+ }
+ finally
+ {
+ mapLock.unlock();
+ }
+ }
+
+ protected void release(InternalLock internalLock)
+ {
+ mapLock.lock();
+ try
+ {
+ if (--internalLock.waiters == 0)
+ {
+ map.remove(internalLock.id);
+ }
+ }
+ finally
+ {
+ mapLock.unlock();
+ }
+ }
+
+ protected abstract Object getLockId(Invocation invocation);
+
+ public Object invoke(Invocation invocation) throws Exception, InvocationException
+ {
+ Object lockId = getLockId(invocation);
+
+ //
+ if (lockId != null)
+ {
+ InternalLock internalLock = acquire(lockId);
+ try
+ {
+ return internalLock.invoke(invocation);
+ }
+ finally
+ {
+ release(internalLock);
+ }
+ }
+ else
+ {
+ return invocation.invokeNext();
+ }
+ }
+}
Deleted:
trunk/server/src/main/org/jboss/portal/server/aspects/server/NavigationInterceptor.java
===================================================================
---
trunk/server/src/main/org/jboss/portal/server/aspects/server/NavigationInterceptor.java 2007-04-04
10:43:17 UTC (rev 6914)
+++
trunk/server/src/main/org/jboss/portal/server/aspects/server/NavigationInterceptor.java 2007-04-04
11:38:41 UTC (rev 6915)
@@ -1,31 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2006, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * This is free software; you can redistribute it and/or modify it *
- * under the terms of the GNU Lesser General Public License as *
- * published by the Free Software Foundation; either version 2.1 of *
- * the License, or (at your option) any later version. *
- * *
- * This software is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
- * Lesser General Public License for more details. *
- * *
- * You should have received a copy of the GNU Lesser General Public *
- * License along with this software; if not, write to the Free *
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
- ******************************************************************************/
-package org.jboss.portal.server.aspects.server;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision$
- */
-public class NavigationInterceptor
-{
-}
Added:
trunk/server/src/main/org/jboss/portal/server/aspects/server/SessionLockInterceptor.java
===================================================================
---
trunk/server/src/main/org/jboss/portal/server/aspects/server/SessionLockInterceptor.java
(rev 0)
+++
trunk/server/src/main/org/jboss/portal/server/aspects/server/SessionLockInterceptor.java 2007-04-04
11:38:41 UTC (rev 6915)
@@ -0,0 +1,47 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.server.aspects.server;
+
+import org.jboss.portal.server.ServerInvocation;
+import org.jboss.portal.server.aspects.LockInterceptor;
+import org.jboss.portal.common.invocation.Invocation;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class SessionLockInterceptor extends LockInterceptor
+{
+ protected Object getLockId(Invocation invocation)
+ {
+ ServerInvocation si = (ServerInvocation)invocation;
+
+ //
+ HttpServletRequest req = si.getServerContext().getClientRequest();
+
+ // We lock only if the client provides a session id
+ return req.getRequestedSessionId();
+ }
+}
Modified:
trunk/server/src/main/org/jboss/portal/server/impl/invocation/JBossInterceptorStack.java
===================================================================
---
trunk/server/src/main/org/jboss/portal/server/impl/invocation/JBossInterceptorStack.java 2007-04-04
10:43:17 UTC (rev 6914)
+++
trunk/server/src/main/org/jboss/portal/server/impl/invocation/JBossInterceptorStack.java 2007-04-04
11:38:41 UTC (rev 6915)
@@ -38,6 +38,11 @@
/** . */
private final Interceptor[] interceptors;
+ public JBossInterceptorStack(Interceptor interceptor)
+ {
+ this.interceptors = new Interceptor[]{interceptor};
+ }
+
public JBossInterceptorStack(Interceptor[] interceptors)
{
if (interceptors == null)