001 /*
002 Copyright (C) 2003 Adam Olsen
003 This program is free software; you can redistribute it and/or modify
004 it under the terms of the GNU General Public License as published by
005 the Free Software Foundation; either version 1, or (at your option)
006 any later version.
007 This program is distributed in the hope that it will be useful,
008 but WITHOUT ANY WARRANTY; without even the implied warranty of
009 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010 GNU General Public License for more details.
011 You should have received a copy of the GNU General Public License
012 along with this program; if not, write to the Free Software
013 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
014 */
015 package com.valhalla.pluginmanager;
016
017 import java.io.File;
018 import java.io.IOException;
019 import java.io.InputStream;
020 import java.util.ArrayList;
021 import java.util.Enumeration;
022 import java.util.Properties;
023 import java.util.jar.JarEntry;
024 import java.util.jar.JarFile;
025
026 /**
027 * Represents a plugin in a Jar File
028 *
029 * @author Adam Olsen
030 * @created October 31, 2004
031 * @version 1.0
032 */
033 public class PluginJAR {
034 private Properties props = new Properties();
035
036 private ArrayList contents = new ArrayList();
037
038 private JarFile jar;
039
040 private boolean loaded = false;
041
042 private String location;
043
044 private Plugin plugin = null;
045
046 /**
047 * Constructs the Plugin Jar
048 *
049 * @param location
050 * The location of the plugin
051 * @exception IOException
052 * if there is an exception while opening the plugin
053 */
054 public PluginJAR(String location) throws IOException {
055 this.location = location;
056
057 jar = new JarFile(location);
058
059 loadContents();
060 }
061
062 /**
063 * Gets the jarEntry with a specific name
064 *
065 * @param name
066 * the name of the file you want to get the entry for
067 * @return The jarEntry value
068 */
069 public JarEntry getJarEntry(String name) {
070 JarEntry entry = null;
071 try {
072 entry = jar.getJarEntry(name);
073 } catch (IllegalStateException ex) {
074 try {
075 jar = new JarFile(location);
076 entry = jar.getJarEntry(name);
077 } catch (IllegalStateException e) {
078 } catch (IOException e) {
079 }
080 }
081
082 return entry;
083 }
084
085 /**
086 * Closes the JarFile
087 */
088 public void close() {
089 try {
090 jar.close();
091 } catch (Exception e) {
092 }
093 }
094
095 /**
096 * Gets the InputStream from an entry
097 *
098 * @param entry
099 * which entry to get the input stream for
100 * @return The InputStream
101 */
102 public InputStream getInputStream(JarEntry entry) {
103 InputStream stream = null;
104 try {
105 stream = jar.getInputStream(entry);
106 } catch (IOException ex) {
107 }
108
109 return stream;
110 }
111
112 /**
113 * @return the location of this Jar
114 */
115 public String getLocation() {
116 return location;
117 }
118
119 /**
120 * Sets whether or not the jar has been loaded
121 *
122 * @param loaded
123 * true if boolean
124 */
125 public void setLoaded(boolean loaded) {
126 this.loaded = loaded;
127 }
128
129 /**
130 * @return true if this jar is loaded
131 */
132 public boolean getLoaded() {
133 return loaded;
134 }
135
136 /**
137 * Loads the contents of the Jar into the Properties
138 *
139 * @exception IOException
140 * if there is an error reading the jar file
141 */
142 public void loadContents() throws IOException {
143 contents.removeAll(contents);
144 Enumeration e = jar.entries();
145 while (e.hasMoreElements()) {
146 JarEntry entry = (JarEntry) e.nextElement();
147
148 // if it contains the file "plugin.properties", it's a plugin
149 // so read in the properties file
150 if (entry.getName().equals("plugin.properties")) {
151 InputStream stream = jar.getInputStream(entry);
152 props.load(stream);
153
154 File file = new File(location);
155
156 props.setProperty("size", "" + file.length());
157 props.setProperty("fileName", file.getPath());
158 stream.close();
159 }
160
161 contents.add(entry.getName());
162 }
163 }
164
165 /**
166 * Loads the specified plugin
167 *
168 * @return the Plugin
169 */
170 public Plugin loadPlugin() {
171 PluginLoader loader = PluginLoader.getInstance();
172
173 try {
174 Class c = loader.loadClass(props.getProperty("mainClass"));
175 if (c == null)
176 return null;
177
178 plugin = (Plugin) c.newInstance();
179 this.loaded = plugin.init();
180 } catch (Exception ex) {
181 System.out.println(ex.getCause().getMessage());
182 com.valhalla.Logger
183 .debug("Could not load the main class from the jar file.");
184 }
185
186 if (loaded)
187 com.valhalla.Logger.debug(getName() + " Plugin Loaded");
188 else
189 com.valhalla.Logger.debug("Error loading " + getName());
190
191 return plugin;
192 }
193
194 /**
195 * Unloads the specified plugin
196 */
197 public void unloadPlugin() {
198 com.valhalla.Logger.debug("Unloading Plugin " + getName());
199 plugin.unload();
200 this.loaded = false;
201 }
202
203 /**
204 * Returns the jar information(
205 *
206 * @return a Properties with information about the jar
207 */
208 public Properties getProperties() {
209 return props;
210 }
211
212 /**
213 * @return the name of this plugin
214 */
215 public String getName() {
216 return props.getProperty("name");
217 }
218
219 /**
220 * @param file
221 * the file to check
222 * @return true if the jar contains a file
223 */
224 public boolean contains(String file) {
225 for (int i = 0; i < contents.size(); i++) {
226 String name = (String) contents.get(i);
227 if (file.equals(name)) {
228 return true;
229 }
230 }
231
232 return false;
233 }
234 }
235