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.util.*; 018 import java.util.jar.JarFile; 019 import java.util.jar.JarEntry; 020 import java.io.*; 021 import java.net.*; 022 023 /** 024 * Represents a plugin in a Jar File 025 * 026 * @author Adam Olsen 027 * @created October 31, 2004 028 * @version 1.0 029 */ 030 public class PluginJAR 031 { 032 private Properties props = new Properties(); 033 private ArrayList contents = new ArrayList(); 034 private JarFile jar; 035 private boolean loaded = false; 036 private String location; 037 private Plugin plugin = null; 038 039 /** 040 * Constructs the Plugin Jar 041 * 042 * @param location The location of the plugin 043 * @exception IOException if there is an exception while opening the plugin 044 */ 045 public PluginJAR( String location ) 046 throws IOException 047 { 048 this.location = location; 049 050 jar = new JarFile( location ); 051 052 loadContents(); 053 } 054 055 /** 056 * Gets the jarEntry with a specific name 057 * 058 * @param name the name of the file you want to get the entry for 059 * @return The jarEntry value 060 */ 061 public JarEntry getJarEntry( String name ) 062 { 063 JarEntry entry = null; 064 try 065 { 066 entry = jar.getJarEntry( name ); 067 } 068 catch( IllegalStateException ex ) 069 { 070 try 071 { 072 jar = new JarFile( location ); 073 entry = jar.getJarEntry( name ); 074 } 075 catch( IllegalStateException e ) 076 { 077 } 078 catch( IOException e ) 079 { 080 } 081 } 082 083 return entry; 084 } 085 086 /** 087 * Closes the JarFile 088 */ 089 public void close() 090 { 091 try 092 { 093 jar.close(); 094 } 095 catch( Exception e ) 096 { 097 } 098 } 099 100 /** 101 * Gets the InputStream from an entry 102 * 103 * @param entry which entry to get the input stream for 104 * @return The InputStream 105 */ 106 public InputStream getInputStream( JarEntry entry ) 107 { 108 InputStream stream = null; 109 try 110 { 111 stream = jar.getInputStream( entry ); 112 } 113 catch( IOException ex ) 114 { 115 } 116 117 return stream; 118 } 119 120 /** 121 * @return the location of this Jar 122 */ 123 public String getLocation() 124 { 125 return location; 126 } 127 128 /** 129 * Sets whether or not the jar has been loaded 130 * 131 * @param loaded true if boolean 132 */ 133 public void setLoaded( boolean loaded ) 134 { 135 this.loaded = loaded; 136 } 137 138 /** 139 * @return true if this jar is loaded 140 */ 141 public boolean getLoaded() 142 { 143 return loaded; 144 } 145 146 /** 147 * Loads the contents of the Jar into the Properties 148 * 149 * @exception IOException if there is an error reading the jar file 150 */ 151 public void loadContents() 152 throws IOException 153 { 154 contents.removeAll( contents ); 155 Enumeration e = jar.entries(); 156 while( e.hasMoreElements() ) 157 { 158 JarEntry entry = (JarEntry)e.nextElement(); 159 160 // if it contains the file "plugin.properties", it's a plugin 161 // so read in the properties file 162 if( entry.getName().equals( "plugin.properties" ) ) 163 { 164 InputStream stream = jar.getInputStream( entry ); 165 props.load( stream ); 166 167 File file = new File( location ); 168 169 props.setProperty( "size", "" + file.length() ); 170 props.setProperty( "fileName", file.getPath() ); 171 stream.close(); 172 } 173 174 contents.add( entry.getName() ); 175 } 176 } 177 178 /** 179 * Loads the specified plugin 180 * 181 * @return the Plugin 182 */ 183 public Plugin loadPlugin() 184 { 185 PluginLoader loader = PluginLoader.getInstance(); 186 187 try 188 { 189 Class c = loader.loadClass( props.getProperty( "mainClass" ) ); 190 if( c == null ) return null; 191 192 plugin = (Plugin)c.newInstance(); 193 this.loaded = plugin.init(); 194 } 195 catch( Exception ex ) 196 { 197 com.valhalla.Logger.debug( "Could not load the main class from the jar file." ); 198 } 199 200 if( loaded ) com.valhalla.Logger.debug( getName() + " Plugin Loaded" ); 201 else com.valhalla.Logger.debug( "Error loading " + getName() ); 202 203 return plugin; 204 } 205 206 /** 207 * Unloads the specified plugin 208 */ 209 public void unloadPlugin() 210 { 211 com.valhalla.Logger.debug( "Unloading Plugin " + getName() ); 212 plugin.unload(); 213 this.loaded = false; 214 } 215 216 /** 217 * Returns the jar information( 218 * 219 * @return a Properties with information about the jar 220 */ 221 public Properties getProperties() 222 { 223 return props; 224 } 225 226 /** 227 * @return the name of this plugin 228 */ 229 public String getName() 230 { 231 return props.getProperty( "name" ); 232 } 233 234 /** 235 * @param file the file to check 236 * @return true if the jar contains a file 237 */ 238 public boolean contains( String file ) 239 { 240 for( int i = 0; i < contents.size(); i++ ) 241 { 242 String name = (String)contents.get( i ); 243 if( file.equals( name ) ) 244 { 245 return true; 246 } 247 } 248 249 return false; 250 } 251 } 252