You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by jo...@apache.org on 2008/03/31 04:19:43 UTC
svn commit: r642855 -
/cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/flow/ContinuationsManagerImpl.java
Author: joerg
Date: Sun Mar 30 19:19:41 2008
New Revision: 642855
URL: http://svn.apache.org/viewvc?rev=642855&view=rev
Log:
fix synchronization
Modified:
cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/flow/ContinuationsManagerImpl.java
Modified: cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/flow/ContinuationsManagerImpl.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/flow/ContinuationsManagerImpl.java?rev=642855&r1=642854&r2=642855&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/flow/ContinuationsManagerImpl.java (original)
+++ cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/flow/ContinuationsManagerImpl.java Sun Mar 30 19:19:41 2008
@@ -18,7 +18,6 @@
import java.security.SecureRandom;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@@ -27,6 +26,7 @@
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
+
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
@@ -40,7 +40,6 @@
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.avalon.framework.thread.ThreadSafe;
-
import org.apache.cocoon.components.ContextHelper;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
@@ -91,7 +90,7 @@
* This set is used only for debugging purposes by
* {@link #displayAllContinuations()} method.
*/
- protected Set forest = Collections.synchronizedSet(new HashSet());
+ protected Set forest = new HashSet();
/**
* Main continuations holder. Used unless continuations are stored in user
@@ -104,7 +103,7 @@
* their expiration time. This is used by the background thread to
* invalidate continuations.
*/
- protected SortedSet expirations = Collections.synchronizedSortedSet(new TreeSet());
+ protected SortedSet expirations = new TreeSet();
protected boolean bindContinuationsToSession;
@@ -169,13 +168,15 @@
WebContinuation wk = generateContinuation(kont, parent, ttl, interpreterId, disposer);
- if (parent == null) {
- forest.add(wk);
- } else {
- handleParentContinuationExpiration(parent);
- }
+ synchronized (this) {
+ if (parent == null) {
+ forest.add(wk);
+ } else {
+ handleParentContinuationExpiration(parent);
+ }
- handleLeafContinuationExpiration(wk);
+ handleLeafContinuationExpiration(wk);
+ }
if (getLogger().isDebugEnabled()) {
getLogger().debug("WK: Created continuation " + wk.getId());
@@ -208,8 +209,10 @@
*/
public List getWebContinuationsDataBeanList() {
List beanList = new ArrayList();
- for(Iterator it = this.forest.iterator(); it.hasNext();) {
- beanList.add(new WebContinuationDataBean((WebContinuation) it.next()));
+ synchronized (this) {
+ for(Iterator it = this.forest.iterator(); it.hasNext();) {
+ beanList.add(new WebContinuationDataBean((WebContinuation) it.next()));
+ }
}
return beanList;
}
@@ -239,12 +242,14 @@
return null;
}
- // COCOON-2109: Sorting in the TreeSet happens on insert. So in order to re-sort the
- // continuation has to be re-added.
- expirations.remove(kont);
- kont.updateLastAccessTime();
- expirations.add(kont);
-
+ synchronized (this) {
+ // COCOON-2109: Sorting in the TreeSet happens on insert. So in order to re-sort the
+ // continuation has to be re-added.
+ expirations.remove(kont);
+ kont.updateLastAccessTime();
+ expirations.add(kont);
+ }
+
return kont;
}
@@ -324,8 +329,9 @@
getLogger().debug("WK: Manual expire of continuation " + wk.getId());
}
disposeContinuation(continuationsHolder, wk);
- expirations.remove(wk);
-
+ synchronized (this) {
+ expirations.remove(wk);
+ }
// Invalidate all the children continuations as well
List children = wk.getChildren();
int size = children.size();
@@ -344,7 +350,9 @@
protected void _detach(WebContinuation wk) {
WebContinuation parent = wk.getParentContinuation();
if (parent == null) {
- forest.remove(wk);
+ synchronized (this) {
+ forest.remove(wk);
+ }
} else
wk.detachFromParent();
}
@@ -375,7 +383,7 @@
return;
}
- // remove access to this contination
+ // remove access to this continuation
disposeContinuation(continuationsHolder, wk);
_detach(wk);
@@ -397,22 +405,21 @@
*/
protected void displayExpireSet() {
StringBuffer wkSet = new StringBuffer("\nWK; Expire set size: " + expirations.size());
- Iterator i = expirations.iterator();
- while (i.hasNext()) {
- final WebContinuation wk = (WebContinuation) i.next();
- final long lat = wk.getLastAccessTime() + wk.getTimeToLive();
- wkSet.append("\nWK: ")
- .append(wk.getId())
- .append(" ExpireTime [");
-
- if (lat < System.currentTimeMillis()) {
- wkSet.append("Expired");
- } else {
- wkSet.append(lat);
+ synchronized (this) {
+ Iterator i = expirations.iterator();
+ while (i.hasNext()) {
+ final WebContinuation wk = (WebContinuation) i.next();
+ final long lat = wk.getLastAccessTime() + wk.getTimeToLive();
+ wkSet.append("\nWK: ").append(wk.getId()).append(" ExpireTime [");
+
+ if (lat < System.currentTimeMillis()) {
+ wkSet.append("Expired");
+ } else {
+ wkSet.append(lat);
+ }
+ wkSet.append("]");
}
- wkSet.append("]");
}
-
getLogger().debug(wkSet.toString());
}
@@ -421,9 +428,11 @@
* in the system
*/
public void displayAllContinuations() {
- final Iterator i = forest.iterator();
- while (i.hasNext()) {
- ((WebContinuation) i.next()).display();
+ synchronized (this) {
+ final Iterator i = forest.iterator();
+ while (i.hasNext()) {
+ ((WebContinuation) i.next()).display();
+ }
}
}
@@ -445,18 +454,21 @@
// Clean up expired continuations
int count = 0;
WebContinuation wk;
- Iterator i = expirations.iterator();
- while (i.hasNext() && ((wk = (WebContinuation) i.next()).hasExpired())) {
- i.remove();
- WebContinuationsHolder continuationsHolder;
- if (wk instanceof HolderAwareWebContinuation)
- continuationsHolder = ((HolderAwareWebContinuation) wk).getContinuationsHolder();
- else
- continuationsHolder = this.continuationsHolder;
- removeContinuation(continuationsHolder, wk);
- count++;
+ synchronized (this) {
+ Iterator i = expirations.iterator();
+ while (i.hasNext()
+ && ((wk = (WebContinuation) i.next()).hasExpired())) {
+ i.remove();
+ WebContinuationsHolder continuationsHolder;
+ if (wk instanceof HolderAwareWebContinuation)
+ continuationsHolder =
+ ((HolderAwareWebContinuation) wk).getContinuationsHolder();
+ else
+ continuationsHolder = this.continuationsHolder;
+ removeContinuation(continuationsHolder, wk);
+ count++;
+ }
}
-
if (getLogger().isDebugEnabled()) {
getLogger().debug("WK Cleaned up " + count + " continuations in " +
(System.currentTimeMillis() - now));
@@ -474,18 +486,15 @@
* about session invalidation. Invalidates all continuations held by passed
* continuationsHolder.
*/
- protected void invalidateContinuations(
- WebContinuationsHolder continuationsHolder) {
- // TODO: this avoids ConcurrentModificationException, still this is not
- // the best solution and should be changed
- Object[] continuationIds = continuationsHolder.getContinuationIds()
- .toArray();
-
- for (int i = 0; i < continuationIds.length; i++) {
- WebContinuation wk = continuationsHolder.get(continuationIds[i]);
- if (wk != null) {
- _detach(wk);
- _invalidate(continuationsHolder, wk);
+ protected void invalidateContinuations(WebContinuationsHolder continuationsHolder) {
+ synchronized (continuationsHolder) {
+ Set continuationIds = continuationsHolder.getContinuationIds();
+ for (Iterator iter = continuationIds.iterator(); iter.hasNext();) {
+ WebContinuation wk = continuationsHolder.get((Object)iter.next());
+ if (wk != null) {
+ _detach(wk);
+ _invalidate(continuationsHolder, wk);
+ }
}
}
}
@@ -532,26 +541,36 @@
private final static String CONTINUATIONS_HOLDER =
"o.a.c.c.f.SCMI.WebContinuationsHolder";
- private Map holder = Collections.synchronizedMap(new HashMap());
+ private Map holder = new HashMap();
public WebContinuation get(Object id) {
- return (WebContinuation) this.holder.get(id);
+ synchronized (this) {
+ return (WebContinuation) this.holder.get(id);
+ }
}
public void addContinuation(WebContinuation wk) {
- this.holder.put(wk.getId(), wk);
+ synchronized (this) {
+ this.holder.put(wk.getId(), wk);
+ }
}
public void removeContinuation(WebContinuation wk) {
- this.holder.remove(wk.getId());
+ synchronized (this) {
+ this.holder.remove(wk.getId());
+ }
}
public Set getContinuationIds() {
- return holder.keySet();
+ synchronized (this) {
+ return holder.keySet();
+ }
}
public boolean contains(String continuationId) {
- return this.holder.containsKey(continuationId);
+ synchronized (this) {
+ return this.holder.containsKey(continuationId);
+ }
}
public boolean contains(WebContinuation wk) {
@@ -589,10 +608,6 @@
return continuationsHolder;
}
- //retain comparation logic from parent
- public int compareTo(Object other) {
- return super.compareTo(other);
- }
}
}
Re: svn commit: r642855 - /cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/flow/ContinuationsManagerImpl.java
Posted by Joerg Heinicke <jo...@gmx.de>.
On 30.03.2008 22:19, joerg@apache.org wrote:
> Author: joerg
> Date: Sun Mar 30 19:19:41 2008
> New Revision: 642855
>
> URL: http://svn.apache.org/viewvc?rev=642855&view=rev
> Log:
> fix synchronization
Can you please review this [1]? It's too easy to mess this up ... I hope
I did not introduce any deadlock.
One thing I came across and did not like is the maintenance of the Set
"forest" which is according to Javadoc only done for debugging reason,
on the other hand exposed even via the interface. I would rather clean
up the interface and remove the following 2 methods:
/**
* Prints debug information about all web continuations into the log file.
* @see WebContinuation#display()
*/
public void displayAllContinuations();
/**
* Get a list of all continuations as
<code>WebContinuationDataBean</code> objects.
*/
public List getWebContinuationsDataBeanList();
At the moment it's only needed for StatusGenerator. We can therefore add
another method getContinuations() which only returns a clone of the
continuations. Also we should not maintain "forest", but just reuse the
Set "expirations". We should be able to reconstruct everything else on
demand.
The current interface makes the implementation overly complex. In 2.1 it
is even worse due to maintenance of Instrumentable variables.
WDYT? If we don't want to remove the 2 methods for compatibility reasons
we can deprecate them and change the implementation to just print a
deprecation warning.
Joerg
[1] http://marc.info/?l=xml-cocoon-cvs&m=120693001028779&w=4