summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorJonathan Nieder2013-05-28 19:51:32 (EDT)
committer Jonathan Nieder2013-05-28 19:51:32 (EDT)
commit68d92182e6ce177c4ee97bab470ae867997a5530 (patch)
treefa651f097a677c0f5a3bfaa18c91e35883c7f511
parentf99fa9d23e93fdd34124e2100d629680894477d7 (diff)
downloadjgit-68d92182e6ce177c4ee97bab470ae867997a5530.zip
jgit-68d92182e6ce177c4ee97bab470ae867997a5530.tar.gz
jgit-68d92182e6ce177c4ee97bab470ae867997a5530.tar.bz2
Maintain list of archive formats in one placerefs/changes/07/13307/3
Add a static start() method to FormatActivator to allow outside classes such as the Archive subcommand of the jgit program to use it without a BundleContext. This way, the list of formats only has to be maintained in one place. While at it, build a list of registered formats at start() time, so stop() doesn't have to repeat the same list of formats. Suggested-by: Shawn Pearce <spearce@spearce.org> Change-Id: I55cb3095043568740880cc9e4f7cde05f49c363c
-rw-r--r--org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/FormatActivator.java55
-rw-r--r--org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Archive.java6
2 files changed, 53 insertions, 8 deletions
diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/FormatActivator.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/FormatActivator.java
index 8c0a581..bf027d0 100644
--- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/FormatActivator.java
+++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/FormatActivator.java
@@ -42,18 +42,65 @@
*/
package org.eclipse.jgit.archive;
+import java.util.ArrayList;
+import java.util.List;
+
import org.eclipse.jgit.api.ArchiveCommand;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
+/**
+ * This activator registers all format types from the
+ * org.eclipse.jgit.archive package for use via the ArchiveCommand
+ * API.
+ *
+ * This registration happens automatically behind the scenes
+ * when the package is loaded as an OSGi bundle (and the corresponding
+ * deregistration happens when the bundle is unloaded, to avoid
+ * leaks).
+ *
+ * The static start() and stop() methods allow registering the same
+ * list of formats manually in cases where OSGi bundle activation
+ * cannot be used.
+ */
public class FormatActivator implements BundleActivator {
+ private static final List<String> myFormats = new ArrayList<String>();
+
+ private static final void register(String name, ArchiveCommand.Format<?> fmt) {
+ myFormats.add(name);
+ ArchiveCommand.registerFormat(name, fmt);
+ }
+
+ /**
+ * Register all included archive formats so they can be used
+ * as arguments to the ArchiveCommand.setFormat() method.
+ *
+ * Should not be called twice without a call to stop() in between.
+ * Not thread-safe.
+ */
+ public static void start() {
+ register("tar", new TarFormat());
+ register("zip", new ZipFormat());
+ }
+
+ /**
+ * Clean up by deregistering all formats that were registered
+ * by start().
+ *
+ * Not thread-safe.
+ */
+ public static void stop() {
+ for (String name : myFormats) {
+ ArchiveCommand.unregisterFormat(name);
+ }
+ myFormats.clear();
+ }
+
public void start(BundleContext context) throws Exception {
- ArchiveCommand.registerFormat("tar", new TarFormat());
- ArchiveCommand.registerFormat("zip", new ZipFormat());
+ start();
}
public void stop(BundleContext context) throws Exception {
- ArchiveCommand.unregisterFormat("zip");
- ArchiveCommand.unregisterFormat("tar");
+ stop();
}
}
diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Archive.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Archive.java
index 9aa09b4..62341a9 100644
--- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Archive.java
+++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Archive.java
@@ -46,8 +46,7 @@ package org.eclipse.jgit.pgm;
import org.eclipse.jgit.api.ArchiveCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
-import org.eclipse.jgit.archive.TarFormat;
-import org.eclipse.jgit.archive.ZipFormat;
+import org.eclipse.jgit.archive.FormatActivator;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.pgm.TextBuiltin;
import org.eclipse.jgit.pgm.internal.CLIText;
@@ -57,8 +56,7 @@ import org.kohsuke.args4j.Option;
@Command(common = true, usage = "usage_archive")
class Archive extends TextBuiltin {
static {
- ArchiveCommand.registerFormat("tar", new TarFormat());
- ArchiveCommand.registerFormat("zip", new ZipFormat());
+ FormatActivator.start();
}
@Argument(index = 0, metaVar = "metaVar_treeish")