[FELIX-4071] ConcurrentModificationException in DirectoryWatcher.bundleChanged
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1548487 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/DirectoryWatcher.java b/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/DirectoryWatcher.java
index d888d41..8d32526 100644
--- a/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/DirectoryWatcher.java
+++ b/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/DirectoryWatcher.java
@@ -337,10 +337,9 @@
{
if (bundleEvent.getType() == BundleEvent.UNINSTALLED)
{
- for (Iterator it = currentManagedArtifacts.entrySet().iterator(); it.hasNext();)
+ for (Iterator it = getArtifacts().iterator(); it.hasNext();)
{
- Map.Entry entry = (Map.Entry) it.next();
- Artifact artifact = (Artifact) entry.getValue();
+ Artifact artifact = (Artifact) it.next();
if (artifact.getBundleId() == bundleEvent.getBundle().getBundleId())
{
log(Logger.LOG_DEBUG, "Bundle " + bundleEvent.getBundle().getBundleId()
@@ -370,7 +369,7 @@
{
File file = (File) it.next();
boolean exists = file.exists();
- Artifact artifact = (Artifact) currentManagedArtifacts.get(file);
+ Artifact artifact = getArtifact(file);
// File has been deleted
if (!exists)
{
@@ -787,7 +786,7 @@
{
this.context.removeBundleListener(this);
interrupt();
- for (Iterator iter = currentManagedArtifacts.values().iterator(); iter.hasNext();)
+ for (Iterator iter = getArtifacts().iterator(); iter.hasNext();)
{
Artifact artifact = (Artifact) iter.next();
deleteTransformedFile(artifact);
@@ -853,7 +852,7 @@
artifact.setChecksum(Util.loadChecksum(bundles[i], context));
artifact.setListener(null);
artifact.setPath(new File(path));
- currentManagedArtifacts.put(new File(path), artifact);
+ setArtifact(new File(path), artifact);
checksums.put(new File(path), new Long(artifact.getChecksum()));
}
}
@@ -988,7 +987,7 @@
artifact.setBundleId(bundle.getBundleId());
}
installationFailures.remove(path);
- currentManagedArtifacts.put(path, artifact);
+ setArtifact(path, artifact);
log(Logger.LOG_INFO, "Installed " + path, null);
}
catch (Exception e)
@@ -1071,7 +1070,7 @@
artifact.setListener(findListener(path, FileInstall.getListeners()));
}
// Forget this artifact
- currentManagedArtifacts.remove(path);
+ removeArtifact(path);
// Delete transformed file
deleteTransformedFile(artifact);
// if the listener is an installer, uninstall the artifact
@@ -1205,7 +1204,7 @@
private void startAllBundles()
{
List bundles = new ArrayList();
- for (Iterator it = currentManagedArtifacts.values().iterator(); it.hasNext();)
+ for (Iterator it = getArtifacts().iterator(); it.hasNext();)
{
Artifact artifact = (Artifact) it.next();
if (artifact.getBundleId() > 0)
@@ -1282,7 +1281,7 @@
// Go through managed bundles
else if (SCOPE_MANAGED.equals(scope)) {
Set bundles = new HashSet();
- for (Iterator it = currentManagedArtifacts.values().iterator(); it.hasNext();) {
+ for (Iterator it = getArtifacts().iterator(); it.hasNext();) {
Artifact artifact = (Artifact) it.next();
if (artifact.getBundleId() > 0) {
Bundle bundle = context.getBundle(artifact.getBundleId());
@@ -1418,7 +1417,7 @@
{
if (updateWithListeners)
{
- for (Iterator it = currentManagedArtifacts.values().iterator(); it.hasNext(); )
+ for (Iterator it = getArtifacts().iterator(); it.hasNext(); )
{
Artifact artifact = (Artifact) it.next();
if (artifact.getListener() == null && artifact.getBundleId() > 0)
@@ -1446,7 +1445,7 @@
public void removeListener(ArtifactListener listener)
{
- for (Iterator it = currentManagedArtifacts.values().iterator(); it.hasNext(); )
+ for (Iterator it = getArtifacts().iterator(); it.hasNext(); )
{
Artifact artifact = (Artifact) it.next();
if (artifact.getListener() == listener)
@@ -1460,4 +1459,36 @@
}
}
+ private Artifact getArtifact(File file)
+ {
+ synchronized (currentManagedArtifacts)
+ {
+ return (Artifact) currentManagedArtifacts.get(file);
+ }
+ }
+
+ private List getArtifacts()
+ {
+ synchronized (currentManagedArtifacts)
+ {
+ return new ArrayList(currentManagedArtifacts.values());
+ }
+ }
+
+ private void setArtifact(File file, Artifact artifact)
+ {
+ synchronized (currentManagedArtifacts)
+ {
+ currentManagedArtifacts.put(file, artifact);
+ }
+ }
+
+ private void removeArtifact(File file)
+ {
+ synchronized (currentManagedArtifacts)
+ {
+ currentManagedArtifacts.remove(file);
+ }
+ }
+
}