Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move model creation outside of synchronized block #1289

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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