pluginjar.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 211 行

JAVA
211
字号
/*
 * $Id: PluginJar.java,v 1.5 2004/02/26 10:33:50 epr Exp $
 */
package org.jnode.plugin.model;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.Hashtable;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import nanoxml.XMLElement;

import org.jnode.plugin.PluginDescriptor;
import org.jnode.plugin.PluginException;
import org.jnode.util.FileUtils;
import org.jnode.util.OsUtils;
import org.jnode.vm.BootableObject;

/**
 * @author Ewout Prangsma (epr@users.sourceforge.net)
 */
public class PluginJar implements BootableObject {

	/** The descriptor of this file */
	private final PluginDescriptor descriptor;
	/** The bytes of the plugin jar file */
	private final byte[] pluginJarData;
	/** The cached JarFile instance */
	private transient JarFile pluginJar;
	private transient File pluginTmpFile;

	/**
	 * Initialize this instance
	 * 
	 * @param registry
	 * @param pluginUrl
	 */
	public PluginJar(PluginRegistryModel registry, URL pluginUrl) throws PluginException, IOException {
		this(registry, pluginUrl.openStream(), null);
	}

	/**
	 * Initialize this instance
	 * 
	 * @param registry
	 * @param pluginIs
	 */
	public PluginJar(PluginRegistryModel registry, InputStream pluginIs, URL pluginUrl) throws PluginException {

		// Load the plugin into memory
		final byte[] pluginJarData;
		try {
			final byte[] buf = new byte[4096];
			final ByteArrayOutputStream pluginOs = new ByteArrayOutputStream();
			FileUtils.copy(pluginIs, pluginOs, buf, true);
			pluginJarData = pluginOs.toByteArray();
		} catch (IOException ex) {
			throw new PluginException("Error loading jarfile", ex);
		}

		final XMLElement root;
		try {
			// Not find the plugin.xml
			final JarFile jarFile = getJar(pluginUrl, pluginJarData);
			JarEntry entry = jarFile.getJarEntry("plugin.xml");
			if (entry == null) {
				throw new PluginException("plugin.xml not found in jar file");
			}

			// Now parse plugin.xml
			root = new XMLElement(new Hashtable(), true, false);
			final Reader r = new InputStreamReader(jarFile.getInputStream(entry));
			try {
				root.parseFromReader(r);
			} finally {
				r.close();
			}
		} catch (IOException ex) {
			throw new PluginException("Plugin " + pluginUrl, ex);
		}
		if (!root.getName().equals("plugin")) {
			throw new PluginException("plugin element expected");
		}
		this.descriptor = new PluginDescriptorModel(registry, this, root);
		if (this.descriptor.isSystemPlugin()) {
			this.pluginJarData = null;
		} else {
			this.pluginJarData = pluginJarData;
		}
	}

	/**
	 * Does this jar-file contain the resource with the given name.
	 * 
	 * @param resourceName
	 * @return boolean
	 */
	public final InputStream getResourceAsStream(String resourceName) {
		try {
			final JarFile jarFile = getJar(null, pluginJarData);
			final JarEntry entry = jarFile.getJarEntry(resourceName);
			if (entry == null) {
				return null;
			} else {
				return jarFile.getInputStream(entry);
			}
		} catch (IOException ex) {
			return null;
		}
	}

	/**
	 * Does this jar-file contain the resource with the given name.
	 * 
	 * @param resourceName
	 * @return boolean
	 */
	public final boolean containsResource(String resourceName) {
		try {
			final JarFile jarFile = getJar(null, pluginJarData);
			final JarEntry entry = jarFile.getJarEntry(resourceName);
			return (entry != null);
		} catch (IOException ex) {
			return false;
		}
	}

	/**
	 * Does this jar-file contain the resource with the given name.
	 * 
	 * @param resourceName
	 * @return boolean
	 */
	public final URL getResource(String resourceName) {
		if (resourceName.startsWith("/")) {
			resourceName = resourceName.substring(1);
		}
		try {
			final JarFile jarFile = getJar(null, pluginJarData);
			final JarEntry entry = jarFile.getJarEntry(resourceName);
			if (entry == null) {
				return null;
			} else {
				final String id = descriptor.getId();
				return new URL("plugin:" + id + "!/" + resourceName);
			}
		} catch (IOException ex) {
			System.out.println("ioex: " + ex.getMessage());
			return null;
		}
	}

	/**
	 * Gets the descriptor of this plugin-jar file.
	 * 
	 * @return Returns the descriptor.
	 */
	public final PluginDescriptor getDescriptor() {
		return this.descriptor;
	}

	/**
	 * Gets the JarFile of this pluginjar.
	 * 
	 * @param pluginUrl
	 *            Can be null
	 * @return The plugin jarfile.
	 * @throws IOException
	 */
	private JarFile getJar(URL pluginUrl, byte[] pluginJarData) throws IOException {
		if (pluginJar == null) {
			final String protocol = (pluginUrl != null) ? pluginUrl.getProtocol() : "";
			if (protocol.equals("file")) {
				pluginJar = new JarFile(pluginUrl.getFile());
			} else if (OsUtils.isJNode()) {
				pluginJar = new JarFile(pluginJarData);
			} else {
				pluginTmpFile = File.createTempFile("jnode", "jartmp");
				final FileOutputStream fos = new FileOutputStream(pluginTmpFile);
				fos.write(pluginJarData);
				fos.close();
				pluginJar = new JarFile(pluginTmpFile);
				pluginTmpFile.deleteOnExit();
			}
		}
		return pluginJar;
	}
	
	public void finalize() {
	    if (pluginJar != null) {
	        try {
                pluginJar.close();
            } catch (IOException ex) {
                // Ignore
            }
            pluginJar = null;
	    }
	    if (pluginTmpFile != null) {
	        pluginTmpFile.delete();
	        pluginTmpFile = null;
	    }
	}
}

⌨️ 快捷键说明

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