summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDecebal Suiu <decebal.suiu@gmail.com>2019-04-12 21:44:44 +0300
committerDecebal Suiu <decebal.suiu@gmail.com>2019-04-12 21:44:44 +0300
commit76e8acce91fa11a3492191168be746d75022be10 (patch)
treebf8af25c3c186557a69a90202ede2d0b95424f98
parentfabecb360f8f4f34d1cb263720afd995a6063d20 (diff)
downloadpf4j-76e8acce91fa11a3492191168be746d75022be10.tar.gz
pf4j-76e8acce91fa11a3492191168be746d75022be10.zip
Add PluginJar
-rw-r--r--pf4j/src/test/java/org/pf4j/plugin/PluginJar.java114
1 files changed, 114 insertions, 0 deletions
diff --git a/pf4j/src/test/java/org/pf4j/plugin/PluginJar.java b/pf4j/src/test/java/org/pf4j/plugin/PluginJar.java
new file mode 100644
index 0000000..30b3c26
--- /dev/null
+++ b/pf4j/src/test/java/org/pf4j/plugin/PluginJar.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2012-present the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.pf4j.plugin;
+
+import org.pf4j.ManifestPluginDescriptorFinder;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.Map;
+import java.util.Set;
+import java.util.jar.Attributes;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+
+/**
+ /**
+ * Represents a plugin {@code jar} file.
+ * The {@code MANIFEST.MF} file is created on the fly from the information supplied in {@link Builder}.
+ *
+ * @author Decebal Suiu
+ */
+public class PluginJar {
+
+ private final Path path;
+ private final String pluginId;
+ private final String pluginVersion;
+
+ protected PluginJar(Builder builder) {
+ this.path = builder.path;
+ this.pluginId = builder.pluginId;
+ this.pluginVersion = builder.pluginVersion;
+ }
+
+ public Path path() {
+ return path;
+ }
+
+ public String pluginId() {
+ return pluginId;
+ }
+
+ public String pluginVersion() {
+ return pluginVersion;
+ }
+
+ public static class Builder {
+
+ private final Path path;
+ private final String pluginId;
+
+ private String pluginVersion;
+ private Map<String, String> attributes;
+
+ public Builder(Path path, String pluginId) {
+ this.path = path;
+ this.pluginId = pluginId;
+ }
+
+ public Builder pluginVersion(String pluginVersion) {
+ this.pluginVersion = pluginVersion;
+
+ return this;
+ }
+
+ /**
+ * Add extra attributes to the {@code manifest} file.
+ */
+ public Builder attributes(Map<String, String> attributes) {
+ this.attributes = attributes;
+
+ return this;
+ }
+
+ public PluginJar build() throws IOException {
+ createManifestFile();
+
+ return new PluginJar(this);
+ }
+
+ protected void createManifestFile() throws IOException {
+ Manifest manifest = new Manifest();
+ Attributes attrs = manifest.getMainAttributes();
+ attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
+ attrs.put(new Attributes.Name(ManifestPluginDescriptorFinder.PLUGIN_ID), pluginId);
+ attrs.put(new Attributes.Name(ManifestPluginDescriptorFinder.PLUGIN_VERSION), pluginVersion);
+ attrs.put(new Attributes.Name(ManifestPluginDescriptorFinder.PLUGIN_CLASS), "org.pf4j.plugin.TestPlugin");
+ if (attributes != null) {
+ Set<String> names = attributes.keySet();
+ for (String name : names) {
+ attrs.put(new Attributes.Name(name), attributes.get(name));
+ }
+ }
+
+ JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(path.toFile()), manifest);
+ outputStream.close();
+ }
+
+ }
+
+}