From 506fb7a70eee7f970682e9218d599ea63bd0d88d Mon Sep 17 00:00:00 2001 From: Jens Baumgart Date: Thu, 27 May 2010 18:30:19 +0200 Subject: [PATCH] Extract public class RepositoryCache from GitProjectData 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 --- .../src/org/eclipse/egit/core/Activator.java | 10 ++++ .../src/org/eclipse/egit/core/RepositoryCache.java | 63 ++++++++++++++++++++++ .../eclipse/egit/core/project/GitProjectData.java | 32 ++--------- 3 files changed, 77 insertions(+), 28 deletions(-) create mode 100644 org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryCache.java 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 cf78246e..4ef9aa18 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 00000000..ca7f96c3 --- /dev/null +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryCache.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (C) 2010, Jens Baumgart + * Copyright (C) 2008, Robin Rosenberg + * Copyright (C) 2008, Shawn O. Pearce + * 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> repositoryCache = new HashMap>(); + + RepositoryCache() { + // package private constructor + } + + /** + * + * @param gitDir + * @return an existing instance of Repository for gitDir or a + * new one if no Repository instance for gitDir exists + * in the cache. + * @throws IOException + */ + public synchronized Repository lookupRepository(final File gitDir) + throws IOException { + Reference r = repositoryCache.get(gitDir); + Repository d = r != null ? r.get() : null; + if (d == null) { + d = new Repository(gitDir); + repositoryCache.put(gitDir, new WeakReference(d)); + } + prune(repositoryCache); + return d; + } + + private static void prune(Map> map) { + for (final Iterator>> 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 f9b21fa5..60550af6 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 projectDataCache = new HashMap(); - private static final Map> repositoryCache = new HashMap>(); - private static Set repositoryChangeListeners = new HashSet(); @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 r = repositoryCache.get(gitDir); - Repository d = r != null ? r.get() : null; - if (d == null) { - d = new Repository(gitDir); - repositoryCache.put(gitDir, new WeakReference(d)); - } - prune(repositoryCache); - return d; - } - - private static void prune(Map> map) { - for (final Iterator>> 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())); -- 2.11.4.GIT