📄 plugin.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package edu.udo.cs.yale.tools.plugin;
import edu.udo.cs.yale.Yale;
import edu.udo.cs.yale.tools.Tools;
import edu.udo.cs.yale.tools.ParameterService;
import edu.udo.cs.yale.tools.OperatorService;
import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.gui.AboutBox;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;
import java.util.List;
import java.util.Iterator;
import java.util.LinkedList;
import java.io.File;
import java.io.Reader;
import java.io.InputStreamReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLClassLoader;
import java.awt.Image;
import java.awt.Frame;
/** <p>The class for Yale plugins. This class is used to encapsulate the .jar file which must be in the
* <code>lib/plugins</code> subdirectory of Yale. Provides methods for plugin checks, operator registering,
* and getting information about the plugin.</p>
* <p>Plugin dependencies must be defined in the form <br />
* plugin_name1 (plugin_version1) # ... # plugin_nameM (plugin_versionM) < /br>
* of the manifest parameter <code>Plugin-Dependencies</code>. You must define both the name and the version
* of the desired plugins and separate them with "#".</p>
*
* @version $Id: Plugin.java,v 1.13 2004/09/10 19:07:40 ingomierswa Exp $
*/
public class Plugin {
/** The jar archive of the plugin which must be placed in the <code>lib/plugins</code> subdirectory of Yale. */
private JarFile archive;
/** The class loader based on the plugin file. */
private ClassLoader classLoader;
/** The name of the plugin. */
private String name;
/** The version of the plugin. */
private String version;
/** The vendor of the plugin. */
private String vendor;
/** The url for this plugin (in WWW). */
private String url;
/** The Yale version which is needed for this plugin. */
private String neededYaleVersion = "0";
/** The plugins and their versions which are needed for this plugin. */
private List pluginDependencies = new LinkedList();
/** The collection of all plugins. */
private static List allPlugins = new LinkedList();
/** Creates a new pluging based on the plugin .jar file. */
public Plugin(File file) throws IOException {
archive = new JarFile(file);
URL url = new URL("file", null, file.getAbsolutePath());
this.classLoader = new URLClassLoader(new URL[] { url });
getMetaData();
}
/** Returns the name of the plugin. */
public String getName() {
return name;
}
/** Returns the version of this plugin. */
public String getVersion() {
return version;
}
/** Returns the needed Yale version. */
public String getNeededYaleVersion() {
return neededYaleVersion;
}
/** Returns the plugin dependencies of this plugin. */
public List getPluginDependencies() {
return pluginDependencies;
}
/** Returns the class loader of this plugin. */
public ClassLoader getClassLoader() {
return classLoader;
}
/** Checks the Yale version and plugin dependencies. */
private boolean checkDependencies(List plugins) {
// Yale version
if (Yale.getVersion().compareTo(neededYaleVersion) < 0)
return false;
// other plugins
Iterator i = pluginDependencies.iterator();
while (i.hasNext()) {
Dependency dependency = (Dependency)i.next();
if (!dependency.isFulfilled(plugins))
return false;
}
// all ok
return true;
}
/** Collects all meta data of the plugin from the manifest file. */
public void getMetaData() {
try {
java.util.jar.Attributes atts = archive.getManifest().getMainAttributes();
name = atts.getValue("Implementation-Title");
if (name == null) {
name = archive.getName();
}
version = atts.getValue("Implementation-Version");
if (version == null) version = "";
url = atts.getValue("Implementation-URL");
vendor = atts.getValue("Implementation-Vendor");
neededYaleVersion = atts.getValue("Yale-Version");
if (neededYaleVersion == null) neededYaleVersion = "0";
String dependencies = atts.getValue("Plugin-Dependencies");
if (dependencies == null) dependencies = "";
addDependencies(dependencies);
Yale.splashMessage("Loading "+name);
} catch (Exception e) { e.printStackTrace(); }
}
/** Register plugin dependencies. */
private void addDependencies(String dependencies) {
String[] singleDependencies = dependencies.trim().split("#");
for (int i = 0; i < singleDependencies.length; i++) {
if (singleDependencies[i].trim().length() > 0) {
String dependencyName = singleDependencies[i].trim();
String dependencyVersion = "0";
if (singleDependencies[i].trim().indexOf("[") > 0) {
dependencyName =
singleDependencies[i].trim().substring(0, singleDependencies[i].trim().indexOf("[")).trim();
dependencyVersion =
singleDependencies[i].trim().substring(singleDependencies[i].trim().indexOf("[") + 1,
singleDependencies[i].trim().indexOf("]")).trim();
}
pluginDependencies.add(new Dependency(dependencyName, dependencyVersion));
}
}
}
/** Register the operators of this plugin in Yale. */
public void register() {
URL operatorsURL = classLoader.getResource("META-INF/operators.xml");
if (operatorsURL == null) {
operatorsURL = classLoader.getResource("operators.xml");
if (operatorsURL != null) {
LogService.logMessage(name + ": putting operators.xml in root directory of jar is deprecated. Use META-INF directory instead!", LogService.WARNING);
}
}
if (operatorsURL == null) {
LogService.logMessage("Plugin '"+archive.getName()+"' does not contain operators.xml!",
LogService.ERROR);
} else {
InputStream in = null;
try {
in = operatorsURL.openStream();
} catch (IOException e) {
LogService.logMessage("Cannot read operators.xml from '"+archive.getName()+"'!",
LogService.ERROR);
}
LogService.logMessage("Loading "+name, LogService.INIT);
OperatorService.registerOperators(archive.getName(),
in,
classLoader);
}
}
/** Creates the about box for this plugin. */
public AboutBox createAboutBox(Frame owner) {
String about = "";
try {
URL url = classLoader.getResource("META-INF/ABOUT.NFO");
if (url != null)
about = Tools.readTextFile(new InputStreamReader(url.openStream()));
} catch (Throwable e) {
}
return new AboutBox(owner, name, version,
"Vendor: "+((vendor != null) ? vendor : "unknown")+"\n"+
"URL: "+((url != null) ? url : "unknown")+"\n\n"+
about,
new Image[0]);
}
/** Returns a list of Plugins found in the plugins directory. */
public static void findPlugins() {
File pluginDir = ParameterService.getPluginDir();
if (!(pluginDir.exists() &&
pluginDir.isDirectory())) return;
File[] files = pluginDir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".jar");
}
});
allPlugins = new LinkedList();
for (int i = 0; i < files.length; i++) {
try {
allPlugins.add(new Plugin(files[i]));
} catch (Throwable e) {
LogService.logException("Cannot load plugin '"+files[i]+"'", e);
}
}
}
/** Returns a list of Plugins found in the plugins directory. */
public static void registerAllPlugins() {
findPlugins();
Iterator i = allPlugins.iterator();
while (i.hasNext()) {
Plugin plugin = (Plugin)i.next();
if (!plugin.checkDependencies(allPlugins)) {
LogService.logMessage("Cannot register operators from '"+plugin.getName()+"': Dependencies not fulfilled! This plugin need Yale version " + plugin.getNeededYaleVersion() + " and the following plugins:\n" + plugin.getPluginDependencies(),
LogService.ERROR);
i.remove();
}
}
if (allPlugins.size() > 0) {
LogService.logMessage("Found "+allPlugins.size()+" plugins in "+ParameterService.getPluginDir(),
LogService.INIT);
i = allPlugins.iterator();
while(i.hasNext()) {
((Plugin)i.next()).register();
}
}
}
/** Returns the collection of all plugins. */
public static List getAllPlugins() {
return allPlugins;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -