📄 plugin.java
字号:
/*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Created on 2006/8/7
*
* @Author: Xiaojun Chen
* $Revision$ 1.0
*
*/
package eti.bi.alphaminer.core.Plugin;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import eti.bi.common.PluginResource;
import eti.bi.common.Locale.Resource;
import eti.bi.common.System.SysConfig;
public class Plugin {
private PluginDescription pluginDescription = new PluginDescription();
private PluginClassLoader classloader;
public static String LOCALEDir = "config/Locale";
private Dependencies dependencies;
/** Creates a new pluging based on the plug-in .jar file. */
public Plugin(File file, PluginClassLoader classloader) throws IOException {
pluginDescription.archive = new JarFile(file);
this.classloader = classloader;
getMetaData();
}
/** Collects all meta data of the plugin from the manifest file. */
private void getMetaData() {
try {
java.util.jar.Attributes atts = pluginDescription.archive.getManifest().getMainAttributes();
String classpath = atts.getValue("Class-Path");
if(classpath!=null) {
createTempLib(classpath);
//classloader.addDependingURL(new URL("jar:file:/"+pluginDescription.archive.getName()+"!/"+classpath));
}
pluginDescription.name = atts.getValue("Plugin-Name");
if (pluginDescription.name == null) {
pluginDescription.name = pluginDescription.archive.getName();
}
pluginDescription.description = atts.getValue("Plugin-Description");
if (pluginDescription.description == null) {
pluginDescription.description = "";
}
pluginDescription.version = atts.getValue("Plugin-Version");
if (pluginDescription.version == null)
pluginDescription.version = "";
pluginDescription.url = atts.getValue("Plugin-URL");
pluginDescription.vendor = atts.getValue("Plugin-Vendor");
pluginDescription.neededAlphaMinerVersion = atts.getValue("AlphaMiner-Version");
if (pluginDescription.neededAlphaMinerVersion == null)
pluginDescription.neededAlphaMinerVersion = "0";
pluginDescription.localepath = atts.getValue("Locale-Dir");
if (pluginDescription.localepath != null) {
pluginDescription.localepath = pluginDescription.localepath.replace(File.separator, "/");
} else {
pluginDescription.localepath = LOCALEDir;
}
String dependencies = atts.getValue("Plugin-Dependency");
if (dependencies == null)
dependencies = "";
this.dependencies = new Dependencies(pluginDescription.name,dependencies, pluginDescription.neededAlphaMinerVersion);
} catch (Exception e) {
e.printStackTrace();
}
}
private void createTempLib(String classpath) throws IOException {
String[] cps = classpath.trim().split(" ");
String file = getArchive().getName();
int index = file.lastIndexOf(File.separator);
file = file.substring(index+1);
index = file.lastIndexOf(".");
file = file.substring(0, index);
file = SysConfig.getProperty("TEMP_PLUGIN_LIB")+File.separator+file;
File temp = new File(file);
if(!(temp.exists()&&temp.isDirectory())) {
temp.mkdirs();
}
if(!temp.exists()) {
System.err.println("Can't create the temp plug-in lib directory: "+file);
}
file += File.separator;
for(int i=0;i<cps.length;i++) {
if(cps[i].endsWith(".jar")) {
//write the library to temp file
//classloader.addDependingURL(new URL("jar:file:/"+pluginDescription.archive.getName()+"!/"+cps[i]));
cps[i] = cps[i].replaceAll("/", "_");
cps[i] = file+cps[i];
File out = new File(cps[i]);
InputStream in=null;
BufferedOutputStream bf=null;
boolean flag = false;
try {
JarEntry jarentry = pluginDescription.archive.getJarEntry(classpath);
in = pluginDescription.archive.getInputStream(jarentry);
int size = in.available();
int step = 50000;
int write=0;
int read;
boolean hafile = false;
if(out.exists()&&out.isFile()) {
if(out.length()!=size) {
out.delete();
}else {
hafile = true;
}
}
if(!hafile) {
out = new File(cps[i]);
if(!out.createNewFile()) {
throw new Exception("Can't create temp lib file: "+cps[i]);
}
}
bf = new BufferedOutputStream(new FileOutputStream(cps[i]));
byte[] bytes = new byte[step];
while(write+step<size) {
read = 0;
while(read<step) {
read += in.read(bytes, read, step-read);
}
bf.write(bytes, 0, step);
write += step;
}
read = 0;
step = size-write;
while(read<step) {
read += in.read(bytes, read, step-read);
}
bf.write(bytes, 0, step);
classloader.addDependingURL(new URL("file",null,cps[i]));
flag = true;
}
catch(Exception e) {
e.printStackTrace();
}
finally{
try {
if(in!=null) {
in.close();
}
if(bf!=null) {
bf.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
if(!flag) {
try {
out.delete();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
}
}
/**
* @param aCONFIGURATIONDir the default configuration path of the plug-in
* */
public static void setDefaultLocaleDir(String aLOCALEDir) {
LOCALEDir = aLOCALEDir;
}
public boolean isValid() {
if(!pluginDescription.isValid()||classloader==null) {
return false;
}
return true;
}
/**
* @param plugins
* the plugins collected
* @return true if this plugin's all dependency included in this plugins,
* else false
*/
public boolean checkDependency(List<Plugin> plugins) {
return dependencies.checkDependency(plugins);
}
/**
* @return <code>true<code> if this plug-in equal the input plug-in
*/
public boolean equal(Plugin plugin) {
if (plugin == null) {
return false;
}
return pluginDescription.equal(plugin.getPluginDescription());
}
/**
* @return the JarFile
*/
public JarFile getArchive() {
return pluginDescription.archive;
}
/**
* @return the name of the plug-in.
*/
public String getName() {
return pluginDescription.name;
}
/**
* @return the description of this plug-in
*/
public String getDescription() {
return pluginDescription.description;
}
/**
* @return the version of this plug-in.
*/
public String getVersion() {
return pluginDescription.version;
}
/**
* @return the Vendor.
*/
public String getVendor() {
return pluginDescription.vendor;
}
/**
* @return the website.
*/
public String getUrl() {
return pluginDescription.url;
}
/**
* @return the needed Alphaminer version.
*/
public String getNeededAlphaminerVersion() {
return pluginDescription.neededAlphaMinerVersion;
}
/**
* @return the PluginDescription of this plug-in
*/
PluginDescription getPluginDescription() {
return pluginDescription;
}
public String getPluginLocalePath() {
return pluginDescription.localepath;
}
public String getPluginConfigPath() {
String temp = pluginDescription.localepath;
int index = temp.indexOf("/");
if(index>=0) {
temp = temp.substring(0, index);
}
else {
temp = "";
}
return temp;
}
PluginResource getPluginResource() {
PluginResource pluginresource = new PluginResource();
pluginresource.jarfile = pluginDescription.archive;
pluginresource.localepath = pluginDescription.localepath;
return pluginresource;
}
/**
* @return the classloader of this plug-in
*/
public ClassLoader getClassLoader() {
return classloader;
}
/**
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(Resource.srcStr("plugininfo") + "\n" + Resource.srcStr("plugininfo_name") + ": "
+ pluginDescription.name + "\n" + Resource.srcStr("plugininfo_description") + ": "
+ pluginDescription.description + "\n" + Resource.srcStr("plugininfo_vendor") + ": "
+ pluginDescription.vendor + "\n" + Resource.srcStr("plugininfo_version") + ": "
+ pluginDescription.version + "\n" + Resource.srcStr("plugininfo_needAlphaMinerversion") + ": "
+ pluginDescription.neededAlphaMinerVersion + "\n" + Resource.srcStr("plugininfo_url") + ": "
+ pluginDescription.url);
return sb.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -