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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.plugin;
  17. import org.pf4j.ManifestPluginDescriptorFinder;
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.nio.file.Path;
  22. import java.util.LinkedHashMap;
  23. import java.util.Map;
  24. import java.util.jar.Attributes;
  25. import java.util.jar.JarOutputStream;
  26. import java.util.jar.Manifest;
  27. /**
  28. /**
  29. * Represents a plugin {@code jar} file.
  30. * The {@code MANIFEST.MF} file is created on the fly from the information supplied in {@link Builder}.
  31. *
  32. * @author Decebal Suiu
  33. */
  34. public class PluginJar {
  35. private final Path path;
  36. private final String pluginId;
  37. private final String pluginClass;
  38. private final String pluginVersion;
  39. protected PluginJar(Builder builder) {
  40. this.path = builder.path;
  41. this.pluginId = builder.pluginId;
  42. this.pluginClass = builder.pluginClass;
  43. this.pluginVersion = builder.pluginVersion;
  44. }
  45. public Path path() {
  46. return path;
  47. }
  48. public File file() {
  49. return path.toFile();
  50. }
  51. public String pluginClass() {
  52. return pluginClass;
  53. }
  54. public String pluginId() {
  55. return pluginId;
  56. }
  57. public String pluginVersion() {
  58. return pluginVersion;
  59. }
  60. public static Manifest createManifest(Map<String, String> map) {
  61. Manifest manifest = new Manifest();
  62. Attributes attributes = manifest.getMainAttributes();
  63. attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
  64. for (Map.Entry<String, String> entry : map.entrySet()) {
  65. attributes.put(new Attributes.Name(entry.getKey()), entry.getValue());
  66. }
  67. return manifest;
  68. }
  69. public static class Builder {
  70. private final Path path;
  71. private final String pluginId;
  72. private String pluginClass;
  73. private String pluginVersion;
  74. private Map<String, String> manifestAttributes = new LinkedHashMap<>();
  75. public Builder(Path path, String pluginId) {
  76. this.path = path;
  77. this.pluginId = pluginId;
  78. }
  79. public Builder pluginClass(String pluginClass) {
  80. this.pluginClass = pluginClass;
  81. return this;
  82. }
  83. public Builder pluginVersion(String pluginVersion) {
  84. this.pluginVersion = pluginVersion;
  85. return this;
  86. }
  87. /**
  88. * Add extra attributes to the {@code manifest} file.
  89. * As possible attribute name please see {@link ManifestPluginDescriptorFinder}.
  90. */
  91. public Builder manifestAttributes(Map<String, String> manifestAttributes) {
  92. this.manifestAttributes.putAll(manifestAttributes);
  93. return this;
  94. }
  95. /**
  96. * Add extra attribute to the {@code manifest} file.
  97. * As possible attribute name please see {@link ManifestPluginDescriptorFinder}.
  98. */
  99. public Builder manifestAttribute(String name, String value) {
  100. manifestAttributes.put(name, value);
  101. return this;
  102. }
  103. public PluginJar build() throws IOException {
  104. createManifestFile();
  105. return new PluginJar(this);
  106. }
  107. protected void createManifestFile() throws IOException {
  108. Map<String, String> map = new LinkedHashMap<>();
  109. map.put(ManifestPluginDescriptorFinder.PLUGIN_ID, pluginId);
  110. map.put(ManifestPluginDescriptorFinder.PLUGIN_VERSION, pluginVersion);
  111. if (pluginClass != null) {
  112. map.put(ManifestPluginDescriptorFinder.PLUGIN_CLASS, pluginClass);
  113. }
  114. if (manifestAttributes != null) {
  115. map.putAll(manifestAttributes);
  116. }
  117. Manifest manifest = createManifest(map);
  118. JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(path.toFile()), manifest);
  119. outputStream.close();
  120. }
  121. }
  122. }