Skip to content

Commit

Permalink
Move model creation outside of synchronized block
Browse files Browse the repository at this point in the history
  • Loading branch information
reinsch82 authored and donat committed Jan 3, 2024
1 parent af0501b commit fd0f7de
Showing 1 changed file with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -58,9 +60,7 @@ private DefaultModelPersistence() {

@Override
public PersistentModel load(IProject project) throws Exception {
synchronized (DefaultModelPersistence.this.lock) {
return doLoadModel(project);
}
return doLoadModel(project);
}
});
}
Expand Down Expand Up @@ -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<Properties> 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<IProject, PersistentModel> modelCacheMap = this.modelCache.asMap();
for (Entry<IProject, PersistentModel> entry : modelCacheMap.entrySet()) {
Expand Down

0 comments on commit fd0f7de

Please sign in to comment.