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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.Map;
  22. import java.util.Set;
  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 class Builder {
  52. private final Path path;
  53. private final String pluginId;
  54. private String pluginVersion;
  55. private Map<String, String> attributes;
  56. public Builder(Path path, String pluginId) {
  57. this.path = path;
  58. this.pluginId = pluginId;
  59. }
  60. public Builder pluginVersion(String pluginVersion) {
  61. this.pluginVersion = pluginVersion;
  62. return this;
  63. }
  64. /**
  65. * Add extra attributes to the {@code manifest} file.
  66. */
  67. public Builder attributes(Map<String, String> attributes) {
  68. this.attributes = attributes;
  69. return this;
  70. }
  71. public PluginJar build() throws IOException {
  72. createManifestFile();
  73. return new PluginJar(this);
  74. }
  75. protected void createManifestFile() throws IOException {
  76. Manifest manifest = new Manifest();
  77. Attributes attrs = manifest.getMainAttributes();
  78. attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
  79. attrs.put(new Attributes.Name(ManifestPluginDescriptorFinder.PLUGIN_ID), pluginId);
  80. attrs.put(new Attributes.Name(ManifestPluginDescriptorFinder.PLUGIN_VERSION), pluginVersion);
  81. attrs.put(new Attributes.Name(ManifestPluginDescriptorFinder.PLUGIN_CLASS), "org.pf4j.plugin.TestPlugin");
  82. if (attributes != null) {
  83. Set<String> names = attributes.keySet();
  84. for (String name : names) {
  85. attrs.put(new Attributes.Name(name), attributes.get(name));
  86. }
  87. }
  88. JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(path.toFile()), manifest);
  89. outputStream.close();
  90. }
  91. }
  92. }