gpt4 book ai didi

java - 从未包含在类路径中的外部 jar 读取 xml

转载 作者:太空宇宙 更新时间:2023-11-04 13:32:24 24 4
gpt4 key购买 nike

我使用 Netbeans 创建了一个 javafx 项目,该项目本身运行得很好。

我现在正在尝试实现一个自定义的轻量级插件系统,这些插件是位于主项目的 plugins/ 目录内的外部 JAR。我正在使用 javax.security 包对插件进行沙箱处理。

这是主要项目的结构:

MainProject
|
|---plugins/
| |---MyPlugin.jar
|
|---src/
| |---main.app.plugin
| |---Plugin.java
| |---PluginSecurityPolicy.java
| |---PluginClassLoader.java
| |---PluginContainer.java
....

插件的一个:

Plugin
|
|---src/
| |---my.plugin
| | |---MyPlugin.java
| |--settings.xml
|
|---dist/
|---MyPlugin.jar
|---META-INF/
| |---MANIFEST.MF
|---my.plugin
| |---MyPlugin.class
|---settings.xml

为了将插件加载到程序中,我创建了一个 PluginContainer 类,该类从插件目录中获取所有 jar 文件,列出 jar 内的所有文件并查找插件文件和设置文件。

我可以加载并创建插件类的实例,但是当涉及到 XML 时,我什至无法将其列在 jar 内容中。

这是代码,也许有人可以看到我哪里做错了。

PluginSecurityPolicy.java

import java.security.AllPermission;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Policy;
import java.security.ProtectionDomain;

public class PluginSecurityPolicy extends Policy {

@Override
public PermissionCollection getPermissions(ProtectionDomain domain) {
if (isPlugin(domain)) {
return pluginPermissions();
} else {
return applicationPermissions();
}
}

private boolean isPlugin(ProtectionDomain domain) {
return domain.getClassLoader() instanceof PluginClassLoader;
}

private PermissionCollection pluginPermissions() {
Permissions permissions = new Permissions();
//
return permissions;
}

private PermissionCollection applicationPermissions() {
Permissions permissions = new Permissions();
permissions.add(new AllPermission());
return permissions;
}
}

PluginClassLoader.java

import java.net.URL;
import java.net.URLClassLoader;

public class PluginClassLoader extends URLClassLoader {

public PluginClassLoader(URL jarFileUrl) {
super(new URL[] {jarFileUrl});
}
}

PluginContainer.java,#load 方法就是其中之一

import main.app.plugin.PluginClassLoader;
import main.app.plugin.PluginSecurityPolicy;
import java.io.File;
import java.net.URL;
import java.security.Policy;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class PluginContainer {
private ArrayList<Plugin> plugins;
private ManifestParser parser;

public PluginContainer() {
Policy.setPolicy(new PluginSecurityPolicy());
System.setSecurityManager(new SecurityManager());
plugins = new ArrayList<>();
parser = new ManifestParser();
}

public void init() {
File[] dir = new File(System.getProperty("user.dir") + "/plugins").listFiles();
for (File pluginJarFile : dir) {
try {
Plugin plugin = load(pluginJarFile.getCanonicalPath());
plugins.add(plugin);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}

public <T extends Plugin> T getPlugin(Class<T> plugin) {
for (Plugin p : plugins) {
if (p.getClass().equals(plugin)) {
return (T)p;
}
}
return null;
}

private Plugin load(String pluginJarFile) throws Exception {
PluginManifest manifest = null;
Plugin plugin = null;

// Load the jar file
ZipFile jarFile = new ZipFile(pluginJarFile);

// Get all jar entries
Enumeration allEntries = jarFile.entries();
String pluginClassName = null;

while (allEntries.hasMoreElements()) {

// Get single file
ZipEntry entry = (ZipEntry) allEntries.nextElement();
String file = entry.getName();

// Look for classfiles
if (file.endsWith(".class")) {

// Set class name
String classname = file.replace('/', '.').substring(0, file.length() - 6);

// Look for plugin class
if (classname.endsWith("Plugin")) {

// Set the class name and exit loop
pluginClassName = classname;
break;
}
}
}

// Load the class
ClassLoader pluginLoader = new PluginClassLoader(new URL("file:///" + pluginJarFile));
Class<?> pluginClass = pluginLoader.loadClass(pluginClassName);

// Edit as suggested by KDM, still null
URL settingsUrl = pluginClass.getResource("/settings.xml");
manifest = parser.load(settingsUrl);

// Check if manifest has been created
if (null == manifest) {
throw new RuntimeException("Manifest file not found in " + pluginJarFile);
}

// Create the plugin
plugin = (Plugin) pluginClass.newInstance();
plugin.load(manifest);

return plugin;
}
}

以及自动生成的 MANIFEST.MF

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.8.0_25-b18 (Oracle Corporation)

缺少 Class-Path 指令,但如果我强制将其设置为 ../settings.xmlsettings.xml(通过手动编辑 MANIFEST.MF 文件),它也将不起作用。

这就是我能想到的,提前感谢您的帮助

[编辑]我在插件 jar 根目录中创建了一个 images/monitor-16.png,并将 #load2 方法添加到 PluginContainer 中。

由于该方法是在循环内调用的,因此我将 Policy.setPolicy(new PluginSecurityPolicy());System.setSecurityManager(new SecurityManager()); 留在了构造函数内。

这是新的插件 jar 结构:

TestPlugin.jar
|
|---META-INF/
| |---MANIFEST.MF
|
|---dev.jimbo
| |---TestPlugin.class
|
|---images
| |---monitor-16.png
|
|---settings.xml

新方法代码:

private Plugin load2(String pluginJarFile) throws MalformedURLException, ClassNotFoundException {        
PluginClassLoader urlCL = new PluginClassLoader(new File(pluginJarFile).toURL());
Class<?> loadClass = urlCL.loadClass("dev.jimbo.TestPlugin");

System.out.println(loadClass);
System.out.println("Loading the class using the class loader object. Resource = " + urlCL.getResource("images/monitor-16.png"));
System.out.println("Loading the class using the class loader object with absolute path. Resource = " + urlCL.getResource("/images/monitor-16.png"));
System.out.println("Loading the class using the class object. Resource = " + loadClass.getResource("images/monitor-16.png"));
System.out.println();

return null;
}

这是输出

class dev.jimbo.TestPlugin
Loading the class using the class loader object. Resource = null
Loading the class using the class loader object with absolute path. Resource = null
Loading the class using the class object. Resource = null

最佳答案

以下程序:

public static void main(String[] args) throws MalformedURLException, ClassNotFoundException {
Policy.setPolicy(new PluginSecurityPolicy());
System.setSecurityManager(new SecurityManager());
PluginClassLoader urlCL = new PluginClassLoader(new File(
"A Jar containing images/load.gif and SampleApp class").toURL());
Class<?> loadClass = urlCL.loadClass("net.sourceforge.marathon.examples.SampleApp");
System.out.println(loadClass);
System.out.println("Loading the class using the class loader object. Resource = " + urlCL.getResource("images/load.gif"));
System.out.println("Loading the class using the class loader object with absolute path. Resource = " + urlCL.getResource("/images/load.gif"));
System.out.println("Loading the class using the class object. Resource = " + loadClass.getResource("images/load.gif"));
}

产生以下输出:

class net.sourceforge.marathon.examples.SampleApp
Loading the class using the class loader object. Resource = jar:file:/Users/dakshinamurthykarra/Projects/install/marathon/sampleapp.jar!/images/load.gif
Loading the class using the class loader object with absolute path. Resource = null
Loading the class using the class object. Resource = null

所以我认为你的类加载器没有任何问题。将其作为答案,以便代码可以正确格式化。

关于java - 从未包含在类路径中的外部 jar 读取 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32047598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com