From 97e1cce284ff09f3d208fbc776fc9692af9bde0a Mon Sep 17 00:00:00 2001
From: Kai Reinhard <K.Reinhard@micromata.de>
Date: Sat, 05 Jan 2019 17:15:44 +0000
Subject: [PATCH] Some required synchronizations...

---
 borgbutler-core/src/main/java/de/micromata/borgbutler/data/Repository.java |   30 +++++++++++++++++++++++-------
 1 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/data/Repository.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/data/Repository.java
index 103c9d8..67b0a10 100644
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/data/Repository.java
+++ b/borgbutler-core/src/main/java/de/micromata/borgbutler/data/Repository.java
@@ -11,6 +11,8 @@
 import org.slf4j.LoggerFactory;
 
 import java.io.Serializable;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.SortedSet;
 import java.util.TreeSet;
 
@@ -69,15 +71,18 @@
     /**
      * Might be null.
      */
-    @Getter
     @Setter
     private SortedSet<Archive> archives;
 
     public Repository add(Archive archive) {
-        if (this.archives == null) {
-            this.archives = new TreeSet<>();
+        synchronized (this) {
+            if (this.archives == null) {
+                this.archives = new TreeSet<>();
+            }
         }
-        this.archives.add(archive);
+        synchronized (this.archives) {
+            this.archives.add(archive);
+        }
         return this;
     }
 
@@ -86,9 +91,11 @@
             log.warn("Can't get archive '" + idOrName + "' from repository '" + name + "'. Archives not yet loaded or don't exist.");
             return null;
         }
-        for (Archive archive : archives) {
-            if (StringUtils.equals(idOrName, archive.getId()) || StringUtils.equals(idOrName, archive.getName())) {
-                return archive;
+        synchronized (this.archives) {
+            for (Archive archive : archives) {
+                if (StringUtils.equals(idOrName, archive.getId()) || StringUtils.equals(idOrName, archive.getName())) {
+                    return archive;
+                }
             }
         }
         log.warn("Archive '" + idOrName + "' not found in repository '" + name + "'.");
@@ -103,4 +110,13 @@
     public boolean isArchivesLoaded() {
         return CollectionUtils.isNotEmpty(archives);
     }
+
+    public Collection<Archive> getArchives() {
+        if (this.archives == null) {
+            return null;
+        }
+        synchronized (this.archives) {
+            return Collections.unmodifiableSet(this.archives);
+        }
+    }
 }

--
Gitblit v1.10.0