⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pluginfactory.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 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.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import eti.bi.alphaminer.tools.OrdiTools;
import eti.bi.common.PluginResource;
import eti.bi.common.Locale.Resource;
import eti.bi.common.System.FileOperation;
import eti.bi.common.System.SysConfig;
import eti.bi.common.System.SysLog;
import eti.bi.common.System.SystemVariable;

public final class PluginFactory {
	
	private static FilenameFilter filter = new FilenameFilter(){
		public boolean accept(File dir, String name) {
			return name.endsWith(".jar")||name.endsWith(".zip");
		}
	};
	private static List<Plugin> registeredplugins = new LinkedList<Plugin>();
	private static List<Plugin> plugins = new LinkedList<Plugin>();
	
	private static boolean hasload = false;
	
	/**
	 * load the plugins in default location
	 * @throws IOException 
	 * */
	public static void loadPlugins() throws IOException{
		loadPlugins(new File(SysConfig.getProperty("PLUGIN_HOME")));
	}
	
	/**
	 * Load plugins in a specific directory or file of jar or zip type
	 * @param plugindir the dir of plugins or file of plugin
	 * @throws NullPointerException if plugindir is null
	 * @throws FileNotFoundException if plugindir or plugin file is not found
	 * @throws MalformedURLException if creating URL whit plugindir failure
	 * @throws IOException if load plugin failure
	 * */
	public static void loadPlugins(File plugindir) throws IOException{
		if(plugindir==null){
			throw new NullPointerException("plugindir is null!");
		}
		
		if(!plugindir.exists()){
			throw new FileNotFoundException("plugindir is not found!");
		}
		
		String path = plugindir.getAbsolutePath();
		
		PluginClassLoader pcloader = new PluginClassLoader(new URL[0],PluginFactory.class.getClassLoader());
		if(plugindir.isFile()){
			//file
			if(!(path.endsWith(".jar")||path.endsWith(".zip"))){
				throw new FileNotFoundException("The file type of plugindir should be jar or zip!");
			}
			loadPlugin(plugindir.getAbsolutePath(),pcloader);
		}
		else{
			//dir
			File file = new File(path);
			String[] files = file.list(filter);
			if(files==null||files.length==0){
				throw new FileNotFoundException("No plugin");
			}
			
			int i=0;
			for(i=0;i<files.length;i++){
				loadPlugin(path+File.separator+files[i],pcloader);
			}
		}
		hasload = true;
	}
	
	/**
	 * Load plugins in a specific file of jar or zip type
	 * @param filename the file of plugin
	 * @throws IOException if loading plugin fail
	 * */
	public static void loadPlugin(String filename, PluginClassLoader pcloader) throws IOException{
		File file = new File(filename);
		if(!file.isFile()){
			return;
		}
		pcloader.addDependingURL(new URL("file",null,filename));
		Plugin plugin = new Plugin(file,pcloader);
		plugins.add(plugin);
		hasload = true;
	}
	
	/**
	 * get the specific plugin
	 * @param id the index of plugin
	 * @return the plugin with specific index
	 * */
	public static Plugin getPlugin(int id){
		if(id<0||id>=registeredplugins.size()){
			return null;
		}
		return registeredplugins.get(id);
	}
	
	/**
	 * @param name the plugin name
	 * @param IgnoreCase true if ignore the plugin name' case
	 * @return Return the plugin with given name, null if not exist
	 * */
	public static Plugin getPlugin(String name, boolean IgnoreCase){
		int size = registeredplugins.size();
		Plugin plugin;
		if(IgnoreCase){
			for(int i=0;i<size;i++){
				plugin = registeredplugins.get(i);
				if(plugin.getName().equalsIgnoreCase(name)){
					return plugin;
				}
			}
		}
		else{
			for(int i=0;i<size;i++){
				plugin = registeredplugins.get(i);
				if(plugin.getName().equals(name)){
					return plugin;
				}
			}
		}
		
		return null;
	}
	
	/**
	 * @param name the plugin name
	 * @param IgnoreCase true if ignore the plugin name' case
	 * @param version the version of plugin			
	 * 			
	 * @return Return the plugin with given name, null if not exist
	 * */
	public static Plugin getPlugin(String name, boolean IgnoreCase, String version){
		int size = registeredplugins.size();
		Plugin plugin;
		if(IgnoreCase){
			for(int i=0;i<size;i++){
				plugin = registeredplugins.get(i);
				if(plugin.getName().equalsIgnoreCase(name)){
					return plugin;
				}
			}
		}
		else{
			for(int i=0;i<size;i++){
				plugin = registeredplugins.get(i);
				if(plugin.getName().equals(name)){
					return plugin;
				}
			}
		}
		
		return null;
	}
	
	/** 
	 * @return Return the collection of all non-registered plugins. 
	 * */
	public static List<Plugin> getNRPlugins() {
		return plugins;
	}
	
	/** 
	 * @return Return the collection of all registered plugins. 
	 * */
	public static List<Plugin> getAllRegisteredPlugins() {
		return registeredplugins;
	}
	
	
	/**
	 * @return Return all registered plugins' operator definition's inputstream
	 * */
	public static Iterator<NodeInfor_Plugin> getAllRegisteredPluginsNodeDefition(){
				
		return new Iterator<NodeInfor_Plugin>(){
			private Iterator<Plugin> iterator = registeredplugins.iterator();
			private boolean isvalid;
			/**
			 * @author Xiaojun Chen
			 * */
			public boolean hasNext() {
				return iterator.hasNext();
			}
			
			/**
			 * @author Xiaojun Chen
			 * */
			public NodeInfor_Plugin next() {
				isvalid = true;
				Plugin plugin = iterator.next();
				PluginDescription pd = plugin.getPluginDescription();
				JarFile jarfile = pd.archive;
				
				String path = Resource.getLocalePathinJar(pd.localepath)+"NodeDefinition.xml";
				ZipEntry zipentry = jarfile.getEntry(path);
				InputStream in = null;
				try{
					in = jarfile.getInputStream(zipentry);
				}
				catch(Exception e){
					e.printStackTrace();
					SysLog.error(null,e);
				}
				if(in==null) {
					isvalid = false;
					return null;
				}
				
				NodeInfor_Plugin op = new NodeInfor_Plugin();
				op.in = in;
				op.cl = plugin.getClassLoader();
				op.size = (int) zipentry.getSize();
				op.jarfile = pd.archive;
				op.localePath = pd.localepath;
				return op;

			}
			
			/**
			 * @author Xiaojun Chen
			 * if current plugin isn't valid, just remove it by call this method
			 * */
			public void remove() {
				if(!isvalid) {
					iterator.remove();
				}
			}
			
		};
	} 
	
	/** 
	 * @return Return the collection of all plugins. 
	 * */
	public static List<Plugin> getAllPlugins() {
		List<Plugin> list = new LinkedList<Plugin>();
		list.addAll(registeredplugins);
		list.addAll(plugins);
		return list;
	}
	
	/**
	 * register all plugin loaded
	 * @throws IOException 
	 * */
	public static void registerAllPlugins() throws IOException {
		Plugin plugin;
		
		Iterator<Plugin> iterator = plugins.iterator();
		while(iterator.hasNext()){
			plugin = iterator.next();
			if(plugin.checkDependency(plugins)){
				iterator.remove();
				registeredplugins.add(plugin);
				PluginResource plr = plugin.getPluginResource();
				//register resource				
				try {
					Resource.registerPluginResource(plr);
				}
				catch(Exception e) {
					SysLog.info("Plugin "+plugin.getName()+" hasn't resource.");
					System.err.println("Plugin "+plugin.getName()+" hasn't resource.");
					e.printStackTrace();
				}
				
				//register system viariable
				try {
					JarEntry jarentry;
					JarFile jarfile = plr.jarfile;
					jarentry = jarfile.getJarEntry(plugin.getPluginConfigPath()+"/SystemVariable/Variable.properties");
					InputStream in = jarfile.getInputStream(jarentry);
					
					String field =OrdiTools.shortFilename(jarfile.getName());
					
					SystemVariable.addViariableProperty(in,field);
					in.close();
				}
				catch(Exception e) {
					SysLog.info("Plugin "+plugin.getName()+" hasn't system viariable.");
					System.err.println("Plugin "+plugin.getName()+" hasn't system viariable.");
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * return if the plugin has been loaded, used for {@link eti.bi.alphaminer.operation.operator.OperatorFactory#loadOperator()}
	 * @return true if the plugin has been loaded
	 * */
	public static boolean hasLoad(){
		return hasload;
	}
	
	public static boolean ImportPlugin() {
		JFileChooser chooser = new JFileChooser();
		chooser.setFileFilter(new FileFilter() {

			@Override
			public boolean accept(File f) {
				if(f.isDirectory()) {
					return true;
				}
				else if(f.getName().endsWith(".jar")) {
					return true;
				}
				return false;
			}

			@Override
			public String getDescription() {
				return ".jar";
			}});
		String user = System.getProperty("user.dir");
		if(user==null) {
			user = "c:";
		}
		chooser.setCurrentDirectory(new File(user));
		
		if(chooser.showOpenDialog(null)!=JFileChooser.APPROVE_OPTION) {
			return false;
		}
		File file = chooser.getSelectedFile();
		if((!file.exists())||(!file.canRead())) {
			return false;
		}
		
		File newFile = new File(SysConfig.getProperty("PLUGIN_HOME")+File.separator+file.getName());
		if(newFile.exists()) {
			JOptionPane.showMessageDialog(null,"The plugin: "+newFile.getAbsolutePath()+" already exists!","",JOptionPane.ERROR_MESSAGE);
			return false;
		}
		
		try {
			if(!FileOperation.copyFile(file, newFile)) {
				JOptionPane.showMessageDialog(null,"Importing plugin : "+newFile.getName()+" fail!","",JOptionPane.ERROR_MESSAGE);
				return false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,"Importing plugin : "+newFile.getName()+" fail!","",JOptionPane.ERROR_MESSAGE);
			return false;
		}
		JOptionPane.showMessageDialog(null,"Importing plugin : "+newFile.getName()+" succeed!\nAlphaminer will load this plugin next start.","",JOptionPane.INFORMATION_MESSAGE);
		
		return true;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -