📄 jarclassloader.java
字号:
/* * JARClassLoader.java - Loads classes from JAR files * Copyright (C) 1999 Slava Pestov * Portions copyright (C) 1999 mike dillon, (C) 2000 Romain Guy * * 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 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 org.jext;import java.io.*;import java.net.*;import java.util.*;import java.util.zip.*;import javax.swing.JOptionPane;import java.lang.reflect.Modifier;/** * A class loader implementation that loads classes from JAR files. * @author Slava Pestov */public class JARClassLoader extends ClassLoader{ public static ArrayList pluginsNames = new ArrayList(); //public static ArrayList pluginsRealNames = new ArrayList(); /* This is the prefix for entries inside .jar's containing only translations. * I.e., .xml files for translations must be inside trans/<languagecode>/ * folder. Note that any file which name starts with "trans" will be ignored. * The prefix here was "trans" + File.separator, but entries were always read * with forward slashes(I don't know why, it's a characteristic of ZIP file, * it seems)*/ private static final String langsPrefix = "trans"; public JARClassLoader(String path) throws IOException { this(path, true, null); } //Now I've added the parameter isPlugin(which defaults to true) to use the code //for the plugin downloader, which hot-loads a JAR. public JARClassLoader(String path, boolean isPlugin, ClassLoader parent) throws IOException { super(parent); url = new File(path).toURL(); zipFile = new ZipFile(path); if (isPlugin) { String langSearchPrefix = langsPrefix + File.separator + Jext.getLanguage() + File.separator; Enumeration entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); String name = entry.getName(); String lowName = name.toLowerCase(); //the files in trans/* dirs must be loaded only as translations; //this loop over entries elements must not iterate over them. if (lowName.startsWith(langsPrefix)) continue; if (lowName.endsWith(".props")) { Jext.loadProps(zipFile.getInputStream(entry));//This could be removed(no plugin using it). } else if (lowName.endsWith(".props.xml")) { InputStream in; //We load first the untranslated one, then the translated which contains //only some properties! in = zipFile.getInputStream(entry); Jext.loadXMLProps(in, name, false); //not to translate. //First search translation inside the plugin's jar ZipEntry translEntry = zipFile.getEntry(langSearchPrefix + name); if (translEntry != null) in = zipFile.getInputStream(translEntry); else //fall back to old search method. in = Jext.getLanguageStream(zipFile.getInputStream(entry), name); Jext.loadXMLProps(in, name, false);//already translated. } else if (lowName.endsWith(".actions.xml")) { Jext.loadXMLActions(zipFile.getInputStream(entry), name); } else if (name.endsWith("Plugin.class")) { pluginClasses.add(name); pluginsNames.add(name); //pluginsRealNames.add(baseName); } } // If this is done before the above while() statement // and an exception is thrown while the ZIP file is // being loaded, weird things happen... index = classLoaders.size(); classLoaders.add(this); } } private static ArrayList disabledPlugins = new ArrayList(); public static void setEnabled(String name, boolean toEnable) { /*int i = disabledPlugins.indexOf(name); if (toEnable) { if (i != -1) disabledPlugins.remove(i); } else { if (i == -1) disabledPlugins.add(name); }*/ Jext.setProperty("plugin." + name + ".disabled", toEnable? "no" : "yes"); } public static boolean isEnabled(String name) { return ! ("yes".equals(Jext.getProperty("plugin." + name + ".disabled"))); //return disabledPlugins.indexOf(name) == -1; } /*private final static String DISABLED_LIST_PATH = Jext.SETTINGS_DIRECTORY + ".disabledPlugins"; static { try { File f = new File(DISABLED_LIST_PATH); if (f.exists()) { BufferedReader in = new BufferedReader(new FileReader(f)); String line; while ( (line = in.readLine()) != null) disabledPlugins.add(line); in.close(); } } catch (IOException ioe) {ioe.printStackTrace();} } static void saveDisabledList() { try { PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(DISABLED_LIST_PATH))); for (Iterator i = disabledPlugins.iterator(); i.hasNext(); ) { out.println((String) i.next()); } out.close(); } catch (IOException ioe) {} }*/ /** * @exception ClassNotFoundException if the class could not be found */ public Class loadClass(String clazz, boolean resolveIt) throws ClassNotFoundException { return loadClassFromZip(clazz, resolveIt, true); } public InputStream getResourceAsStream(String name) { try { ZipEntry entry = zipFile.getEntry(name); if (entry == null) return getSystemResourceAsStream(name); else return zipFile.getInputStream(entry); } catch (IOException io) { return null; } } public URL getResource(String name) { try { return new URL(getResourceAsPath(name)); } catch (MalformedURLException mu) { return null; } } public String getResourceAsPath(String name) { return "jextresource:" + index + "/" + name; } public String getPath() { return zipFile.getName(); } public static void initPlugins() { for (int i = 0; i < classLoaders.size(); i++) { JARClassLoader classLoader = (JARClassLoader) classLoaders.get(i); classLoader.loadAllPlugins(); } } public static JARClassLoader getClassLoader(int index) { return (JARClassLoader) classLoaders.get(index); } public static int getClassLoaderCount() { return classLoaders.size(); } public static void reloadPluginsProperties() throws IOException { for (int i = 0; i < classLoaders.size(); i++) { JARClassLoader classLoader = (JARClassLoader) classLoaders.get(i); ZipFile zipFile = classLoader.getZipFile(); Enumeration entries = zipFile.entries(); String langSearchPrefix = langsPrefix + File.separator + Jext.getLanguage() + File.separator; while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); String name = entry.getName(); String lowName = name.toLowerCase(); if (! lowName.startsWith(langsPrefix)) {//the files in trans/* dirs must be loaded only as translations; //this loop over entries elements must not iterate over them. if (lowName.endsWith(".props")) Jext.loadProps(zipFile.getInputStream(entry)); else if (lowName.endsWith(".props.xml")) { InputStream in; //We load first the untranslated one, then the translated which contains //only some properties! in = zipFile.getInputStream(entry); Jext.loadXMLProps(in, name, false); //not to translate. //First search translation inside the plugin's jar ZipEntry translEntry = zipFile.getEntry(langSearchPrefix + name); if (translEntry != null) in = zipFile.getInputStream(translEntry); else //fall back to old search method. in = Jext.getLanguageStream(zipFile.getInputStream(entry), name); Jext.loadXMLProps(in, name, false);//already translated. } } } } } public static void executeScripts(JextFrame parent) { for (int i = 0; i < classLoaders.size(); i++) { JARClassLoader classLoader = (JARClassLoader) classLoaders.get(i); ZipFile zipFile = classLoader.getZipFile(); Enumeration entries = zipFile.entries();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -