From 90bd252cf03854e7ffcfb288cdb10dee90be4516 Mon Sep 17 00:00:00 2001 From: Reinhold Degenfellner Date: Wed, 3 Jan 2024 12:55:01 +0100 Subject: [PATCH] Move model creation outside of synchronized block --- .../preferences/DefaultModelPersistence.java | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/org.eclipse.buildship.core/src/main/java/org/eclipse/buildship/core/internal/preferences/DefaultModelPersistence.java b/org.eclipse.buildship.core/src/main/java/org/eclipse/buildship/core/internal/preferences/DefaultModelPersistence.java index b26815a9b..7f800a7fa 100644 --- a/org.eclipse.buildship.core/src/main/java/org/eclipse/buildship/core/internal/preferences/DefaultModelPersistence.java +++ b/org.eclipse.buildship.core/src/main/java/org/eclipse/buildship/core/internal/preferences/DefaultModelPersistence.java @@ -9,9 +9,10 @@ ******************************************************************************/ package org.eclipse.buildship.core.internal.preferences; +import static com.google.common.base.Optional.absent; + import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; @@ -24,6 +25,7 @@ import java.util.concurrent.ExecutionException; import com.google.common.base.Charsets; +import com.google.common.base.Optional; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; @@ -58,9 +60,7 @@ private DefaultModelPersistence() { @Override public PersistentModel load(IProject project) throws Exception { - synchronized (DefaultModelPersistence.this.lock) { - return doLoadModel(project); - } + return doLoadModel(project); } }); } @@ -120,20 +120,25 @@ private void deleteProjectPreferences(ProjectDeletedEvent event) { deleteModel(event.getProject()); } - private static PersistentModel doLoadModel(IProject project) throws IOException, FileNotFoundException { - String projectName = project.getName(); - File preferencesFile = preferencesFile(projectName); - if (preferencesFile.exists()) { - try (Reader reader = new InputStreamReader(new FileInputStream(preferencesFile(projectName)), Charsets.UTF_8)) { - Properties props = new Properties(); - props.load(reader); - return PersistentModelConverter.toModel(project, props); + private Optional loadPreferencesForProject(IProject project) throws IOException { + synchronized (DefaultModelPersistence.this.lock) { + File preferencesFile = preferencesFile(project.getName()); + if (preferencesFile.exists()) { + try (Reader reader = new InputStreamReader(new FileInputStream(preferencesFile), Charsets.UTF_8)) { + Properties props = new Properties(); + props.load(reader); + return Optional.of(props); + } } - } else { - return new AbsentPersistentModel(project); + return absent(); } } + private PersistentModel doLoadModel(IProject project) throws IOException { + return loadPreferencesForProject(project).transform(props -> PersistentModelConverter.toModel(project, props)).or(() -> new AbsentPersistentModel(project)); + + } + private void persistAllProjectPrefs() { Map modelCacheMap = this.modelCache.asMap(); for (Entry entry : modelCacheMap.entrySet()) {