aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJens Baumgart2010-05-27 12:30:19 (EDT)
committerJens Baumgart2010-05-27 12:30:19 (EDT)
commit506fb7a70eee7f970682e9218d599ea63bd0d88d (patch)
tree85c7f31f1905bc0fff0757e2b39f54e63127e3bd
parent4e4fd9228e380be07b0b2a469db268d6da213b69 (diff)
downloadegit-506fb7a70eee7f970682e9218d599ea63bd0d88d.zip
egit-506fb7a70eee7f970682e9218d599ea63bd0d88d.tar.gz
egit-506fb7a70eee7f970682e9218d599ea63bd0d88d.tar.bz2
Extract public class RepositoryCache from GitProjectDatarefs/changes/58/758/4
GitProjectData contains a cache for Repository instances. This cache is used for Reposiotry instances related to projects. Since there are also Repository instances with no related project (e.g. in RepositoriesView) the cache was moved to a public class RepositoryCache to enable sharing Repository instances even if there is no related project. Change-Id: I5526362279d87fd6f2651ee991bb08ea4cd65c2b Signed-off-by: Jens Baumgart <jens.baumgart@sap.com>
-rw-r--r--org.eclipse.egit.core/src/org/eclipse/egit/core/Activator.java10
-rw-r--r--org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryCache.java63
-rw-r--r--org.eclipse.egit.core/src/org/eclipse/egit/core/project/GitProjectData.java32
3 files changed, 77 insertions, 28 deletions
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/Activator.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/Activator.java
index cf78246..4ef9aa1 100644
--- a/org.eclipse.egit.core/src/org/eclipse/egit/core/Activator.java
+++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/Activator.java
@@ -24,6 +24,7 @@ import org.osgi.util.tracker.ServiceTracker;
*/
public class Activator extends Plugin {
private static Activator plugin;
+ private RepositoryCache repositoryCache;
/**
* @return the singleton {@link Activator}
@@ -80,12 +81,21 @@ public class Activator extends Plugin {
GitTraceLocation.initializeFromOptions(opts, true);
}
+ repositoryCache = new RepositoryCache();
GitProjectData.reconfigureWindowCache();
GitProjectData.attachToWorkspace(true);
}
+ /**
+ * @return cache for Repository objects
+ */
+ public RepositoryCache getRepositoryCache() {
+ return repositoryCache;
+ }
+
public void stop(final BundleContext context) throws Exception {
GitProjectData.detachFromWorkspace();
+ repositoryCache = null;
super.stop(context);
plugin = null;
}
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryCache.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryCache.java
new file mode 100644
index 0000000..ca7f96c
--- /dev/null
+++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryCache.java
@@ -0,0 +1,63 @@
+/*******************************************************************************
+ * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com>
+ * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
+ * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
+ * Copyright (C) 2008, Google Inc.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+package org.eclipse.egit.core;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.ref.Reference;
+import java.lang.ref.WeakReference;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.eclipse.jgit.lib.Repository;
+
+/**
+ * Central cache for Repository instances
+ *
+ */
+public class RepositoryCache {
+ private final Map<File, Reference<Repository>> repositoryCache = new HashMap<File, Reference<Repository>>();
+
+ RepositoryCache() {
+ // package private constructor
+ }
+
+ /**
+ *
+ * @param gitDir
+ * @return an existing instance of Repository for <code>gitDir</code> or a
+ * new one if no Repository instance for <code>gitDir</code> exists
+ * in the cache.
+ * @throws IOException
+ */
+ public synchronized Repository lookupRepository(final File gitDir)
+ throws IOException {
+ Reference<Repository> r = repositoryCache.get(gitDir);
+ Repository d = r != null ? r.get() : null;
+ if (d == null) {
+ d = new Repository(gitDir);
+ repositoryCache.put(gitDir, new WeakReference<Repository>(d));
+ }
+ prune(repositoryCache);
+ return d;
+ }
+
+ private static <K, V> void prune(Map<K, Reference<V>> map) {
+ for (final Iterator<Map.Entry<K, Reference<V>>> i = map.entrySet()
+ .iterator(); i.hasNext();) {
+ if (i.next().getValue().get() == null)
+ i.remove();
+ }
+ }
+
+}
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/project/GitProjectData.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/project/GitProjectData.java
index f9b21fa..60550af 100644
--- a/org.eclipse.egit.core/src/org/eclipse/egit/core/project/GitProjectData.java
+++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/project/GitProjectData.java
@@ -15,13 +15,10 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.lang.ref.Reference;
-import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@@ -40,12 +37,12 @@ import org.eclipse.egit.core.CoreText;
import org.eclipse.egit.core.GitCorePreferences;
import org.eclipse.egit.core.GitProvider;
import org.eclipse.egit.core.internal.trace.GitTraceLocation;
-import org.eclipse.osgi.util.NLS;
-import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.WindowCache;
import org.eclipse.jgit.lib.WindowCacheConfig;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.team.core.RepositoryProvider;
/**
* This class keeps information about how a project is mapped to
@@ -54,8 +51,6 @@ import org.eclipse.jgit.lib.WindowCacheConfig;
public class GitProjectData {
private static final Map<IProject, GitProjectData> projectDataCache = new HashMap<IProject, GitProjectData>();
- private static final Map<File, Reference<Repository>> repositoryCache = new HashMap<File, Reference<Repository>>();
-
private static Set<RepositoryChangeListener> repositoryChangeListeners = new HashSet<RepositoryChangeListener>();
@SuppressWarnings("synthetic-access")
@@ -215,26 +210,6 @@ public class GitProjectData {
return projectDataCache.get(p);
}
- private synchronized static Repository lookupRepository(final File gitDir)
- throws IOException {
- Reference<Repository> r = repositoryCache.get(gitDir);
- Repository d = r != null ? r.get() : null;
- if (d == null) {
- d = new Repository(gitDir);
- repositoryCache.put(gitDir, new WeakReference<Repository>(d));
- }
- prune(repositoryCache);
- return d;
- }
-
- private static <K, V> void prune(Map<K, Reference<V>> map) {
- for (final Iterator<Map.Entry<K, Reference<V>>> i = map.entrySet()
- .iterator(); i.hasNext();) {
- if (i.next().getValue().get() == null)
- i.remove();
- }
- }
-
/**
* Update the settings for the global window cache of the workspace.
*/
@@ -470,7 +445,8 @@ public class GitProjectData {
}
try {
- m.setRepository(lookupRepository(git));
+ m.setRepository(Activator.getDefault().getRepositoryCache()
+ .lookupRepository(git));
} catch (IOException ioe) {
Activator.logError(CoreText.GitProjectData_mappedResourceGone,
new FileNotFoundException(m.getContainerPath().toString()));