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

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