@BeforeEach | @BeforeEach | ||||
public void setUp() throws IOException { | public void setUp() throws IOException { | ||||
Path plugin1Path = Files.createDirectories(pluginsPath.resolve("plugin-1")); | |||||
Path plugin1Path = Files.createDirectory(pluginsPath.resolve("plugin-1")); | |||||
// Prove that we can delete a folder with a file inside | // Prove that we can delete a folder with a file inside | ||||
Files.createFile(plugin1Path.resolve("myfile")); | Files.createFile(plugin1Path.resolve("myfile")); | ||||
// Create a zip file for plugin-1 to test that it is deleted when plugin is deleted | // Create a zip file for plugin-1 to test that it is deleted when plugin is deleted | ||||
Files.createFile(pluginsPath.resolve("plugin-1.zip")); | Files.createFile(pluginsPath.resolve("plugin-1.zip")); | ||||
Files.createDirectories(pluginsPath.resolve("plugin-2")); | |||||
Files.createDirectories(pluginsPath.resolve("plugin-3")); | |||||
Files.createDirectory(pluginsPath.resolve("plugin-2")); | |||||
Files.createDirectory(pluginsPath.resolve("plugin-3")); | |||||
// standard maven/gradle bin folder - these should be skipped in development mode because the cause errors | // standard maven/gradle bin folder - these should be skipped in development mode because the cause errors | ||||
Files.createDirectories(pluginsPath.resolve("target")); | |||||
Files.createDirectories(pluginsPath.resolve("build")); | |||||
Files.createDirectory(pluginsPath.resolve("target")); | |||||
Files.createDirectory(pluginsPath.resolve("build")); | |||||
} | } | ||||
/** | /** |
import org.junit.jupiter.api.BeforeEach; | import org.junit.jupiter.api.BeforeEach; | ||||
import org.junit.jupiter.api.Test; | import org.junit.jupiter.api.Test; | ||||
import org.junit.jupiter.api.io.TempDir; | import org.junit.jupiter.api.io.TempDir; | ||||
import org.pf4j.plugin.PluginJar; | |||||
import java.io.FileOutputStream; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.nio.charset.Charset; | |||||
import java.io.OutputStream; | |||||
import java.nio.file.Files; | import java.nio.file.Files; | ||||
import java.nio.file.Path; | import java.nio.file.Path; | ||||
import java.nio.file.Paths; | |||||
import java.util.Arrays; | |||||
import java.util.List; | |||||
import java.util.LinkedHashMap; | |||||
import java.util.Map; | |||||
import java.util.jar.Manifest; | |||||
import static org.junit.jupiter.api.Assertions.assertEquals; | import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
import static org.junit.jupiter.api.Assertions.assertThrows; | import static org.junit.jupiter.api.Assertions.assertThrows; | ||||
@BeforeEach | @BeforeEach | ||||
public void setUp() throws IOException { | public void setUp() throws IOException { | ||||
Charset charset = Charset.forName("UTF-8"); | |||||
Path pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-1")); | |||||
storeManifestToPath(getPlugin1Manifest(), pluginPath); | |||||
Path pluginPath = Files.createDirectories(pluginsPath.resolve(Paths.get("test-plugin-1", "classes", "META-INF"))); | |||||
Files.write(pluginPath.resolve("extensions.idx"), "org.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes()); | |||||
Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin1Manifest(), charset); | |||||
pluginPath = Files.createDirectories(pluginsPath.resolve(Paths.get("test-plugin-2", "classes", "META-INF"))); | |||||
Files.write(pluginPath.resolve("extensions.idx"), "org.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes()); | |||||
Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin2Manifest(), charset); | |||||
pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-2")); | |||||
storeManifestToPath(getPlugin2Manifest(), pluginPath); | |||||
// empty plugin | // empty plugin | ||||
Files.createDirectories(pluginsPath.resolve("test-plugin-3")); | Files.createDirectories(pluginsPath.resolve("test-plugin-3")); | ||||
// no plugin class | // no plugin class | ||||
pluginPath = Files.createDirectories(pluginsPath.resolve(Paths.get("test-plugin-4", "classes", "META-INF"))); | |||||
Files.write(pluginPath.resolve("extensions.idx"), "org.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes()); | |||||
Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin4Manifest(), charset); | |||||
pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-4")); | |||||
storeManifestToPath(getPlugin4Manifest(), pluginPath); | |||||
// no plugin version | // no plugin version | ||||
pluginPath = Files.createDirectories(pluginsPath.resolve(Paths.get("test-plugin-5", "classes", "META-INF"))); | |||||
Files.write(pluginPath.resolve("extensions.idx"), "org.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes()); | |||||
Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin5Manifest(), charset); | |||||
pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-5")); | |||||
storeManifestToPath(getPlugin5Manifest(), pluginPath); | |||||
// no plugin id | // no plugin id | ||||
pluginPath = Files.createDirectories(pluginsPath.resolve(Paths.get("test-plugin-6", "classes", "META-INF"))); | |||||
Files.write(pluginPath.resolve("extensions.idx"), "org.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes()); | |||||
Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin6Manifest(), charset); | |||||
pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-6")); | |||||
storeManifestToPath(getPlugin6Manifest(), pluginPath); | |||||
versionManager = new DefaultVersionManager(); | versionManager = new DefaultVersionManager(); | ||||
} | } | ||||
assertThrows(PluginException.class, () -> descriptorFinder.find(pluginsPath.resolve("test-plugin-3"))); | assertThrows(PluginException.class, () -> descriptorFinder.find(pluginsPath.resolve("test-plugin-3"))); | ||||
} | } | ||||
private List<String> getPlugin1Manifest() { | |||||
String[] lines = new String[] { | |||||
"Manifest-Version: 1.0\n" | |||||
+ "Implementation-Title: Test Plugin #1\n" | |||||
+ "Implementation-Version: 0.10.0-SNAPSHOT\n" | |||||
+ "Archiver-Version: Plexus Archiver\n" | |||||
+ "Built-By: Mario Franco\n" | |||||
+ "Specification-Title: Test Plugin #1\n" | |||||
+ "Implementation-Vendor-Id: org.pf4j.demo\n" | |||||
+ "Plugin-Version: 0.0.1\n" | |||||
+ "Plugin-Id: test-plugin-1\n" | |||||
+ "Plugin-Description: Test Plugin 1\n" | |||||
+ "Plugin-Provider: Decebal Suiu\n" | |||||
+ "Plugin-Class: org.pf4j.plugin.TestPlugin\n" | |||||
+ "Plugin-Dependencies: test-plugin-2,test-plugin-3@~1.0\n" | |||||
+ "Plugin-Requires: *\n" | |||||
+ "Plugin-License: Apache-2.0\n" | |||||
+ "Created-By: Apache Maven 3.0.5\n" | |||||
+ "Build-Jdk: 1.8.0_45\n" | |||||
+ "Specification-Version: 0.10.0-SNAPSHOT\n" | |||||
+ "\n" | |||||
+ "" | |||||
}; | |||||
return Arrays.asList(lines); | |||||
private Manifest getPlugin1Manifest() { | |||||
Map<String, String> map = new LinkedHashMap<>(8); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_ID, "test-plugin-1"); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin"); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1"); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_DESCRIPTION, "Test Plugin 1"); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "test-plugin-2,test-plugin-3@~1.0"); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_REQUIRES, "*"); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_LICENSE, "Apache-2.0"); | |||||
return PluginJar.createManifest(map); | |||||
} | } | ||||
private List<String> getPlugin2Manifest() { | |||||
String[] lines = new String[] { | |||||
"Manifest-Version: 1.0\n" | |||||
+ "Plugin-Dependencies: \n" | |||||
+ "Implementation-Title: Test Plugin #2\n" | |||||
+ "Implementation-Version: 0.10.0-SNAPSHOT\n" | |||||
+ "Archiver-Version: Plexus Archiver\n" | |||||
+ "Built-By: Mario Franco\n" | |||||
+ "Specification-Title: Test Plugin #2\n" | |||||
+ "Implementation-Vendor-Id: org.pf4j.demo\n" | |||||
+ "Plugin-Version: 0.0.1\n" | |||||
+ "Plugin-Id: test-plugin-2\n" | |||||
+ "Plugin-Provider: Decebal Suiu\n" | |||||
+ "Plugin-Class: org.pf4j.plugin.TestPlugin\n" | |||||
+ "Created-By: Apache Maven 3.0.5\n" | |||||
+ "Build-Jdk: 1.8.0_45\n" | |||||
+ "Specification-Version: 0.10.0-SNAPSHOT\n" | |||||
+ "\n" | |||||
+ "" | |||||
}; | |||||
return Arrays.asList(lines); | |||||
private Manifest getPlugin2Manifest() { | |||||
Map<String, String> map = new LinkedHashMap<>(5); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2"); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin"); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1"); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_DEPENDENCIES, ""); | |||||
return PluginJar.createManifest(map); | |||||
} | } | ||||
private List<String> getPlugin4Manifest() { | |||||
String[] lines = new String[] { | |||||
"Manifest-Version: 1.0\n" | |||||
+ "Implementation-Title: Test Plugin #4\n" | |||||
+ "Implementation-Version: 0.10.0-SNAPSHOT\n" | |||||
+ "Archiver-Version: Plexus Archiver\n" | |||||
+ "Built-By: Mario Franco\n" | |||||
+ "Specification-Title: Test Plugin #4\n" | |||||
+ "Implementation-Vendor-Id: org.pf4j.demo\n" | |||||
+ "Plugin-Version: 0.0.1\n" | |||||
+ "Plugin-Id: test-plugin-2\n" | |||||
+ "Plugin-Provider: Decebal Suiu\n" | |||||
+ "Created-By: Apache Maven 3.0.5\n" | |||||
+ "Build-Jdk: 1.8.0_45\n" | |||||
+ "Specification-Version: 0.10.0-SNAPSHOT\n" | |||||
+ "\n" | |||||
+ "" | |||||
}; | |||||
return Arrays.asList(lines); | |||||
private Manifest getPlugin4Manifest() { | |||||
Map<String, String> map = new LinkedHashMap<>(3); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_ID, "test-plugin-1"); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1"); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); | |||||
return PluginJar.createManifest(map); | |||||
} | } | ||||
private List<String> getPlugin5Manifest() { | |||||
String[] lines = new String[] { | |||||
"Manifest-Version: 1.0\n" | |||||
+ "Implementation-Title: Test Plugin #5\n" | |||||
+ "Implementation-Version: 0.10.0-SNAPSHOT\n" | |||||
+ "Archiver-Version: Plexus Archiver\n" | |||||
+ "Built-By: Mario Franco\n" | |||||
+ "Specification-Title: Test Plugin #5\n" | |||||
+ "Implementation-Vendor-Id: org.pf4j.demo\n" | |||||
+ "Plugin-Id: test-plugin-2\n" | |||||
+ "Plugin-Provider: Decebal Suiu\n" | |||||
+ "Plugin-Class: org.pf4j.plugin.TestPlugin\n" | |||||
+ "Created-By: Apache Maven 3.0.5\n" | |||||
+ "Build-Jdk: 1.8.0_45\n" | |||||
+ "Specification-Version: 0.10.0-SNAPSHOT\n" | |||||
+ "\n" | |||||
+ "" | |||||
}; | |||||
return Arrays.asList(lines); | |||||
private Manifest getPlugin5Manifest() { | |||||
Map<String, String> map = new LinkedHashMap<>(3); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2"); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin"); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); | |||||
return PluginJar.createManifest(map); | |||||
} | |||||
private Manifest getPlugin6Manifest() { | |||||
Map<String, String> map = new LinkedHashMap<>(2); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin"); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); | |||||
return PluginJar.createManifest(map); | |||||
} | } | ||||
private List<String> getPlugin6Manifest() { | |||||
String[] lines = new String[] { | |||||
"Manifest-Version: 1.0\n" | |||||
+ "Implementation-Title: Test Plugin #6\n" | |||||
+ "Implementation-Version: 0.10.0-SNAPSHOT\n" | |||||
+ "Archiver-Version: Plexus Archiver\n" | |||||
+ "Built-By: Mario Franco\n" | |||||
+ "Specification-Title: Test Plugin #6\n" | |||||
+ "Implementation-Vendor-Id: org.pf4j.demo\n" | |||||
+ "Plugin-Provider: Decebal Suiu\n" | |||||
+ "Plugin-Class: org.pf4j.plugin.TestPlugin\n" | |||||
+ "Created-By: Apache Maven 3.0.5\n" | |||||
+ "Build-Jdk: 1.8.0_45\n" | |||||
+ "Specification-Version: 0.10.0-SNAPSHOT\n" | |||||
+ "\n" | |||||
+ "" | |||||
}; | |||||
return Arrays.asList(lines); | |||||
private void storeManifestToPath(Manifest manifest, Path pluginPath) throws IOException { | |||||
Path path = Files.createDirectory(pluginPath.resolve("META-INF")); | |||||
try (OutputStream output = new FileOutputStream(path.resolve("MANIFEST.MF").toFile())) { | |||||
manifest.write(output); | |||||
} | |||||
} | } | ||||
} | } |
import org.junit.jupiter.api.BeforeEach; | import org.junit.jupiter.api.BeforeEach; | ||||
import org.junit.jupiter.api.Test; | import org.junit.jupiter.api.Test; | ||||
import org.junit.jupiter.api.io.TempDir; | import org.junit.jupiter.api.io.TempDir; | ||||
import org.pf4j.plugin.PluginZip; | |||||
import java.io.FileOutputStream; | import java.io.FileOutputStream; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.nio.charset.StandardCharsets; | import java.nio.charset.StandardCharsets; | ||||
import java.nio.file.Files; | import java.nio.file.Files; | ||||
import java.nio.file.Path; | import java.nio.file.Path; | ||||
import java.util.LinkedHashMap; | |||||
import java.util.Map; | |||||
import java.util.Properties; | import java.util.Properties; | ||||
import static org.junit.jupiter.api.Assertions.assertEquals; | import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
@BeforeEach | @BeforeEach | ||||
public void setUp() throws IOException { | public void setUp() throws IOException { | ||||
Path pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-1")); | |||||
Path pluginPath = Files.createDirectory(pluginsPath.resolve("test-plugin-1")); | |||||
storePropertiesToPath(getPlugin1Properties(), pluginPath); | storePropertiesToPath(getPlugin1Properties(), pluginPath); | ||||
pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-2")); | |||||
pluginPath = Files.createDirectory(pluginsPath.resolve("test-plugin-2")); | |||||
storePropertiesToPath(getPlugin2Properties(), pluginPath); | storePropertiesToPath(getPlugin2Properties(), pluginPath); | ||||
// empty plugin | // empty plugin | ||||
Files.createDirectories(pluginsPath.resolve("test-plugin-3")); | Files.createDirectories(pluginsPath.resolve("test-plugin-3")); | ||||
// no plugin class | // no plugin class | ||||
pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-4")); | |||||
pluginPath = Files.createDirectory(pluginsPath.resolve("test-plugin-4")); | |||||
storePropertiesToPath(getPlugin4Properties(), pluginPath); | storePropertiesToPath(getPlugin4Properties(), pluginPath); | ||||
// no plugin version | // no plugin version | ||||
pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-5")); | |||||
pluginPath = Files.createDirectory(pluginsPath.resolve("test-plugin-5")); | |||||
storePropertiesToPath(getPlugin5Properties(), pluginPath); | storePropertiesToPath(getPlugin5Properties(), pluginPath); | ||||
// no plugin id | // no plugin id | ||||
pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-6")); | |||||
pluginPath = Files.createDirectory(pluginsPath.resolve("test-plugin-6")); | |||||
storePropertiesToPath(getPlugin6Properties(), pluginPath); | storePropertiesToPath(getPlugin6Properties(), pluginPath); | ||||
versionManager = new DefaultVersionManager(); | versionManager = new DefaultVersionManager(); | ||||
} | } | ||||
private Properties getPlugin1Properties() { | private Properties getPlugin1Properties() { | ||||
Properties properties = new Properties(); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-1"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_DESCRIPTION, "Test Plugin 1"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "test-plugin-2,test-plugin-3@~1.0"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, ">=1"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_LICENSE, "Apache-2.0"); | |||||
return properties; | |||||
Map<String, String> map = new LinkedHashMap<>(8); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-1"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_DESCRIPTION, "Test Plugin 1"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "test-plugin-2,test-plugin-3@~1.0"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, ">=1"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_LICENSE, "Apache-2.0"); | |||||
return PluginZip.createProperties(map); | |||||
} | } | ||||
private Properties getPlugin2Properties() { | private Properties getPlugin2Properties() { | ||||
Properties properties = new Properties(); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, ""); | |||||
return properties; | |||||
Map<String, String> map = new LinkedHashMap<>(5); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, ""); | |||||
return PluginZip.createProperties(map); | |||||
} | } | ||||
private Properties getPlugin4Properties() { | private Properties getPlugin4Properties() { | ||||
Properties properties = new Properties(); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, ""); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*"); | |||||
return properties; | |||||
Map<String, String> map = new LinkedHashMap<>(5); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, ""); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*"); | |||||
return PluginZip.createProperties(map); | |||||
} | } | ||||
private Properties getPlugin5Properties() { | private Properties getPlugin5Properties() { | ||||
Properties properties = new Properties(); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, ""); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*"); | |||||
return properties; | |||||
Map<String, String> map = new LinkedHashMap<>(5); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, ""); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*"); | |||||
return PluginZip.createProperties(map); | |||||
} | } | ||||
private Properties getPlugin6Properties() { | private Properties getPlugin6Properties() { | ||||
Properties properties = new Properties(); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin"); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, ""); | |||||
properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*"); | |||||
return properties; | |||||
Map<String, String> map = new LinkedHashMap<>(5); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin"); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, ""); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*"); | |||||
return PluginZip.createProperties(map); | |||||
} | } | ||||
private void storePropertiesToPath(Properties properties, Path pluginPath) throws IOException { | private void storePropertiesToPath(Properties properties, Path pluginPath) throws IOException { |
import java.io.FileOutputStream; | import java.io.FileOutputStream; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.nio.file.Path; | import java.nio.file.Path; | ||||
import java.util.LinkedHashMap; | |||||
import java.util.Map; | import java.util.Map; | ||||
import java.util.Set; | |||||
import java.util.jar.Attributes; | import java.util.jar.Attributes; | ||||
import java.util.jar.JarOutputStream; | import java.util.jar.JarOutputStream; | ||||
import java.util.jar.Manifest; | import java.util.jar.Manifest; | ||||
return pluginVersion; | return pluginVersion; | ||||
} | } | ||||
public static Manifest createManifest(Map<String, String> map) { | |||||
Manifest manifest = new Manifest(); | |||||
Attributes attributes = manifest.getMainAttributes(); | |||||
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0"); | |||||
for (Map.Entry<String, String> entry : map.entrySet()) { | |||||
attributes.put(new Attributes.Name(entry.getKey()), entry.getValue()); | |||||
} | |||||
return manifest; | |||||
} | |||||
public static class Builder { | public static class Builder { | ||||
private final Path path; | private final Path path; | ||||
} | } | ||||
protected void createManifestFile() throws IOException { | 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"); | |||||
Map<String, String> map = new LinkedHashMap<>(); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_ID, pluginId); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_VERSION, pluginVersion); | |||||
map.put(ManifestPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin"); | |||||
if (attributes != null) { | if (attributes != null) { | ||||
Set<String> names = attributes.keySet(); | |||||
for (String name : names) { | |||||
attrs.put(new Attributes.Name(name), attributes.get(name)); | |||||
} | |||||
map.putAll(attributes); | |||||
} | } | ||||
JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(path.toFile()), manifest); | |||||
JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(path.toFile()), createManifest(map)); | |||||
outputStream.close(); | outputStream.close(); | ||||
} | } | ||||
*/ | */ | ||||
package org.pf4j.plugin; | package org.pf4j.plugin; | ||||
import org.pf4j.PropertiesPluginDescriptorFinder; | |||||
import java.io.FileOutputStream; | import java.io.FileOutputStream; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.nio.file.Path; | import java.nio.file.Path; | ||||
import java.util.LinkedHashMap; | |||||
import java.util.Map; | import java.util.Map; | ||||
import java.util.Properties; | import java.util.Properties; | ||||
import java.util.zip.ZipEntry; | import java.util.zip.ZipEntry; | ||||
return path.getParent().resolve(fileName.substring(0, fileName.length() - 4)); // without ".zip" suffix | return path.getParent().resolve(fileName.substring(0, fileName.length() - 4)); // without ".zip" suffix | ||||
} | } | ||||
public static Properties createProperties(Map<String, String> map) { | |||||
Properties properties = new Properties(); | |||||
properties.putAll(map); | |||||
return properties; | |||||
} | |||||
public static class Builder { | public static class Builder { | ||||
private final Path path; | private final Path path; | ||||
} | } | ||||
protected void createPropertiesFile() throws IOException { | protected void createPropertiesFile() throws IOException { | ||||
Properties props = new Properties(); | |||||
props.setProperty("plugin.id", pluginId); | |||||
props.setProperty("plugin.version", pluginVersion); | |||||
props.setProperty("plugin.class", "org.pf4j.plugin.TestPlugin"); | |||||
Map<String, String> map = new LinkedHashMap<>(); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, pluginId); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, pluginVersion); | |||||
map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin"); | |||||
if (properties != null) { | if (properties != null) { | ||||
props.putAll(properties); | |||||
map.putAll(properties); | |||||
} | } | ||||
ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(path.toFile())); | ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(path.toFile())); | ||||
ZipEntry propertiesFile = new ZipEntry("plugin.properties"); | |||||
ZipEntry propertiesFile = new ZipEntry(PropertiesPluginDescriptorFinder.DEFAULT_PROPERTIES_FILE_NAME); | |||||
outputStream.putNextEntry(propertiesFile); | outputStream.putNextEntry(propertiesFile); | ||||
props.store(outputStream, ""); | |||||
createProperties(map).store(outputStream, ""); | |||||
outputStream.closeEntry(); | outputStream.closeEntry(); | ||||
outputStream.close(); | outputStream.close(); | ||||
} | } |