You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PluginJar.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.pf4j.test;
  17. import org.pf4j.ManifestPluginDescriptorFinder;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. import java.io.PrintWriter;
  24. import java.nio.file.Path;
  25. import java.util.LinkedHashMap;
  26. import java.util.LinkedHashSet;
  27. import java.util.Map;
  28. import java.util.Set;
  29. import java.util.jar.Attributes;
  30. import java.util.jar.JarEntry;
  31. import java.util.jar.JarOutputStream;
  32. import java.util.jar.Manifest;
  33. /**
  34. * Represents a plugin {@code jar} file.
  35. * The {@code MANIFEST.MF} file is created on the fly from the information supplied in {@link Builder}.
  36. *
  37. * @author Decebal Suiu
  38. */
  39. public class PluginJar {
  40. private final Path path;
  41. private final String pluginId;
  42. private final String pluginClass;
  43. private final String pluginVersion;
  44. protected PluginJar(Builder builder) {
  45. this.path = builder.path;
  46. this.pluginId = builder.pluginId;
  47. this.pluginClass = builder.pluginClass;
  48. this.pluginVersion = builder.pluginVersion;
  49. }
  50. public Path path() {
  51. return path;
  52. }
  53. public File file() {
  54. return path.toFile();
  55. }
  56. public String pluginClass() {
  57. return pluginClass;
  58. }
  59. public String pluginId() {
  60. return pluginId;
  61. }
  62. public String pluginVersion() {
  63. return pluginVersion;
  64. }
  65. public static Manifest createManifest(Map<String, String> map) {
  66. Manifest manifest = new Manifest();
  67. Attributes attributes = manifest.getMainAttributes();
  68. attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
  69. for (Map.Entry<String, String> entry : map.entrySet()) {
  70. attributes.put(new Attributes.Name(entry.getKey()), entry.getValue());
  71. }
  72. return manifest;
  73. }
  74. public static class Builder {
  75. private final Path path;
  76. private final String pluginId;
  77. private String pluginClass;
  78. private String pluginVersion;
  79. private Map<String, String> manifestAttributes = new LinkedHashMap<>();
  80. private Set<String> extensions = new LinkedHashSet<>();
  81. private ClassDataProvider classDataProvider = new DefaultClassDataProvider();
  82. public Builder(Path path, String pluginId) {
  83. this.path = path;
  84. this.pluginId = pluginId;
  85. }
  86. public Builder pluginClass(String pluginClass) {
  87. this.pluginClass = pluginClass;
  88. return this;
  89. }
  90. public Builder pluginVersion(String pluginVersion) {
  91. this.pluginVersion = pluginVersion;
  92. return this;
  93. }
  94. /**
  95. * Add extra attributes to the {@code manifest} file.
  96. * As possible attribute name please see {@link ManifestPluginDescriptorFinder}.
  97. */
  98. public Builder manifestAttributes(Map<String, String> manifestAttributes) {
  99. this.manifestAttributes.putAll(manifestAttributes);
  100. return this;
  101. }
  102. /**
  103. * Add extra attribute to the {@code manifest} file.
  104. * As possible attribute name please see {@link ManifestPluginDescriptorFinder}.
  105. */
  106. public Builder manifestAttribute(String name, String value) {
  107. manifestAttributes.put(name, value);
  108. return this;
  109. }
  110. public Builder extension(String extensionClassName) {
  111. extensions.add(extensionClassName);
  112. return this;
  113. }
  114. public Builder classDataProvider(ClassDataProvider classDataProvider) {
  115. this.classDataProvider = classDataProvider;
  116. return this;
  117. }
  118. public PluginJar build() throws IOException {
  119. Manifest manifest = createManifest();
  120. try (OutputStream outputStream = new FileOutputStream(path.toFile());
  121. JarOutputStream jarOutputStream = new JarOutputStream(outputStream, manifest)) {
  122. if (!extensions.isEmpty()) {
  123. // add extensions.idx
  124. JarEntry jarEntry = new JarEntry("META-INF/extensions.idx");
  125. jarOutputStream.putNextEntry(jarEntry);
  126. jarOutputStream.write(extensionsAsByteArray());
  127. jarOutputStream.closeEntry();
  128. // add extensions classes
  129. for (String extension : extensions) {
  130. String extensionPath = extension.replace('.', '/') + ".class";
  131. JarEntry classEntry = new JarEntry(extensionPath);
  132. jarOutputStream.putNextEntry(classEntry);
  133. jarOutputStream.write(classDataProvider.getClassData(extension));
  134. jarOutputStream.closeEntry();
  135. }
  136. }
  137. }
  138. return new PluginJar(this);
  139. }
  140. private Manifest createManifest() {
  141. Map<String, String> map = new LinkedHashMap<>();
  142. map.put(ManifestPluginDescriptorFinder.PLUGIN_ID, pluginId);
  143. map.put(ManifestPluginDescriptorFinder.PLUGIN_VERSION, pluginVersion);
  144. if (pluginClass != null) {
  145. map.put(ManifestPluginDescriptorFinder.PLUGIN_CLASS, pluginClass);
  146. }
  147. if (manifestAttributes != null) {
  148. map.putAll(manifestAttributes);
  149. }
  150. return PluginJar.createManifest(map);
  151. }
  152. private byte[] extensionsAsByteArray() throws IOException {
  153. try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
  154. PrintWriter writer = new PrintWriter(outputStream);
  155. for (String extension : extensions) {
  156. writer.println(extension);
  157. }
  158. writer.flush();
  159. return outputStream.toByteArray();
  160. }
  161. }
  162. }
  163. }