[3030] | 1 | /* |
---|
| 2 | * Jeti, a Java Jabber client, Copyright (C) 2002 E.S. de Boer |
---|
| 3 | * |
---|
| 4 | * This program is free software; you can redistribute it and/or modify |
---|
| 5 | * it under the terms of the GNU General Public License as published by |
---|
| 6 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | * (at your option) any later version. |
---|
| 8 | * |
---|
| 9 | * This program is distributed in the hope that it will be useful, |
---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | * GNU General Public License for more details. |
---|
| 13 | * |
---|
| 14 | * You should have received a copy of the GNU General Public License |
---|
| 15 | * along with this program; if not, write to the Free Software |
---|
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | * |
---|
| 18 | * For questions, comments etc, |
---|
| 19 | * use the website at http://jeti.jabberstudio.org |
---|
| 20 | * or mail me at eric@jeti.tk |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package nu.fw.jeti.plugins; |
---|
| 24 | |
---|
| 25 | import java.io.*; |
---|
| 26 | import java.net.MalformedURLException; |
---|
| 27 | import java.net.URL; |
---|
| 28 | import java.net.URLClassLoader; |
---|
| 29 | import java.text.MessageFormat; |
---|
| 30 | import java.util.ArrayList; |
---|
| 31 | import java.util.Iterator; |
---|
| 32 | import java.util.List; |
---|
| 33 | |
---|
| 34 | import javax.xml.parsers.SAXParser; |
---|
| 35 | |
---|
| 36 | import nu.fw.jeti.backend.Start; |
---|
| 37 | import nu.fw.jeti.backend.XMLDataFile; |
---|
| 38 | import nu.fw.jeti.util.I18N; |
---|
| 39 | import nu.fw.jeti.util.Preferences; |
---|
| 40 | |
---|
| 41 | import org.xml.sax.InputSource; |
---|
| 42 | import org.xml.sax.SAXException; |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | * @author E.S. de Boer |
---|
| 46 | * @version 1.0 |
---|
| 47 | */ |
---|
| 48 | |
---|
| 49 | public class PluginData extends XMLDataFile |
---|
| 50 | { |
---|
| 51 | private List plugins; |
---|
| 52 | //private List enabledPlugins; |
---|
| 53 | |
---|
| 54 | public PluginData(){plugins = Preferences.getPlugins();} |
---|
| 55 | |
---|
| 56 | public PluginData(SAXParser parser) |
---|
| 57 | { |
---|
| 58 | plugins = Preferences.getPlugins(); |
---|
| 59 | InputStream data = null; |
---|
| 60 | data = getClass().getResourceAsStream("/plugins.xml"); |
---|
| 61 | if(data==null) |
---|
| 62 | { |
---|
| 63 | try |
---|
| 64 | { |
---|
| 65 | //if(Start.programURL != null) |
---|
| 66 | { |
---|
| 67 | //loadremote plugins |
---|
| 68 | data = (new URL(Start.programURL + "plugins.xml")).openStream(); |
---|
| 69 | } |
---|
| 70 | //else data = new FileInputStream(Start.path +"plugins.xml"); |
---|
| 71 | //data = new URL(local + "plugins.xml").openStream(); |
---|
| 72 | } |
---|
| 73 | catch (IOException ex) |
---|
| 74 | {//plugins xml not found so make one |
---|
| 75 | scanPlugins(); |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | if(data != null) |
---|
| 79 | { |
---|
| 80 | try |
---|
| 81 | { |
---|
| 82 | parser.parse(new InputSource(new InputStreamReader(data)),new PluginsHandler(plugins)); |
---|
| 83 | } |
---|
| 84 | catch (SAXException ex) |
---|
| 85 | { |
---|
| 86 | ex.printStackTrace(); |
---|
| 87 | } |
---|
| 88 | catch (IOException ex) |
---|
| 89 | { |
---|
| 90 | ex.printStackTrace(); |
---|
| 91 | } |
---|
| 92 | for(Iterator i = plugins.iterator();i.hasNext();) |
---|
| 93 | {//remove plugins whitout description /deleted plugins still in preferences |
---|
| 94 | Object[] object = (Object[])i.next(); |
---|
| 95 | if(object[2] == null) i.remove();//was 3 |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | public void scanPlugins() |
---|
| 101 | { |
---|
| 102 | List oldPlugins = new ArrayList(plugins); |
---|
| 103 | plugins.clear(); |
---|
| 104 | searchPlugins(Start.pluginPath + "plugins"); |
---|
| 105 | try |
---|
| 106 | { |
---|
| 107 | BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( |
---|
| 108 | new FileOutputStream(Start.pluginPath +"plugins.xml"), "UTF8")); |
---|
| 109 | writer.write(this.toString()); |
---|
| 110 | writer.close(); |
---|
| 111 | } |
---|
| 112 | catch (Exception ex2) |
---|
| 113 | { |
---|
| 114 | System.err.println("Failed to save 'plugins.xml': " + ex2.getMessage()); |
---|
| 115 | } |
---|
| 116 | for(Iterator i = oldPlugins.iterator();i.hasNext();) |
---|
| 117 | { |
---|
| 118 | Object[] oldPlugin =(Object[])i.next(); |
---|
| 119 | for(Iterator j = plugins.iterator();j.hasNext();) |
---|
| 120 | { |
---|
| 121 | Object[] newPlugin =(Object[])j.next(); |
---|
| 122 | if(oldPlugin[0].equals(newPlugin[0])) |
---|
| 123 | { |
---|
| 124 | newPlugin[1] = oldPlugin[1]; |
---|
| 125 | break; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | private void searchPlugins(String dir) |
---|
| 132 | { |
---|
| 133 | try { |
---|
| 134 | File path = new File(dir); |
---|
| 135 | File file[] = path.listFiles(); |
---|
| 136 | if(file == null) |
---|
| 137 | { |
---|
| 138 | System.err.println(MessageFormat.format(I18N.gettext("main.error.{0}_contains_no_plugins"),new Object[]{dir})); |
---|
| 139 | return; |
---|
| 140 | } |
---|
| 141 | for (int tel=0;tel<file.length;tel++) |
---|
| 142 | { |
---|
| 143 | File currentFile = file[tel]; |
---|
| 144 | if(currentFile.toString().endsWith(".jar")) |
---|
| 145 | { |
---|
| 146 | getPluginInfo(currentFile); |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | } catch (Exception e) { |
---|
| 150 | // Ignore |
---|
| 151 | e.printStackTrace(); |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | private void getPluginInfo(File file) |
---|
| 156 | { |
---|
| 157 | URL url[] = new URL[1]; |
---|
| 158 | try{ |
---|
| 159 | url[0] = file.toURL(); |
---|
| 160 | }catch(MalformedURLException e){} |
---|
| 161 | //System.out.println(url[0]); |
---|
| 162 | String name =file.getName(); |
---|
| 163 | name = name.substring(0,name.length()-4); |
---|
| 164 | //System.out.println(name); |
---|
| 165 | Class pluginClass=null; |
---|
| 166 | try |
---|
| 167 | { |
---|
| 168 | URLClassLoader loader = new URLClassLoader(url,this.getClass().getClassLoader()); |
---|
| 169 | pluginClass = loader.loadClass("nu.fw.jeti.plugins." + name + ".Plugin"); |
---|
| 170 | } |
---|
| 171 | catch(Exception e) |
---|
| 172 | { |
---|
| 173 | e.printStackTrace(); |
---|
| 174 | return; |
---|
| 175 | } |
---|
| 176 | try |
---|
| 177 | { |
---|
| 178 | String name2 = pluginClass.getField("NAME").get(null).toString(); |
---|
| 179 | boolean found = false; |
---|
| 180 | for(Iterator i = plugins.iterator();i.hasNext();) |
---|
| 181 | { |
---|
| 182 | Object[] temp = (Object[])i.next(); |
---|
| 183 | if(name2.equals(temp[0])) |
---|
| 184 | { |
---|
| 185 | fillPluginsDef(temp,pluginClass ,name2); |
---|
| 186 | found = true; |
---|
| 187 | break; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | if(!found) |
---|
| 191 | {//new plugin |
---|
| 192 | Object[] temp = new Object[6]; |
---|
| 193 | fillPluginsDef(temp,pluginClass ,name2); |
---|
| 194 | temp[1] = Boolean.FALSE; |
---|
| 195 | plugins.add(temp); |
---|
| 196 | } |
---|
| 197 | //pluginClass.getMethod("getInfo",new Class[]{}).invoke(); |
---|
| 198 | } |
---|
| 199 | catch (Exception ex) |
---|
| 200 | { |
---|
| 201 | ex.printStackTrace(); |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | private void fillPluginsDef(Object[] temp,Class pluginClass,String name) throws Exception |
---|
| 206 | { |
---|
| 207 | temp[0]=name; |
---|
| 208 | temp[2]=pluginClass.getField("DESCRIPTION").get(null).toString(); |
---|
| 209 | temp[3]=pluginClass.getField("VERSION").get(null).toString(); |
---|
| 210 | temp[4]=pluginClass.getField("MIN_JETI_VERSION").get(null).toString(); |
---|
| 211 | try |
---|
| 212 | { |
---|
| 213 | temp[5]=pluginClass.getField("PARENT").get(null).toString(); |
---|
| 214 | } |
---|
| 215 | catch (NoSuchFieldException e) {} |
---|
| 216 | |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | public List getPlugins() |
---|
| 220 | { |
---|
| 221 | return plugins; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | // private static void readJAR(URL url) |
---|
| 225 | // { |
---|
| 226 | // try |
---|
| 227 | // { |
---|
| 228 | // URL urlJar = new URL("jar:"+url+"!/"); |
---|
| 229 | // JarFile jarFile = new JarFile( "" ); |
---|
| 230 | // //BufferedReader data =new BufferedReader(new InputStreamReader(jarFile.getInputStream(jarFile.getEntry("info.cfg")))); |
---|
| 231 | // |
---|
| 232 | // BufferedReader data = null; |
---|
| 233 | // try |
---|
| 234 | // { |
---|
| 235 | // data =new BufferedReader(new InputStreamReader(new URL(urlJar + "info.cfg").openStream())); |
---|
| 236 | // } |
---|
| 237 | // catch (IOException ex) |
---|
| 238 | // { |
---|
| 239 | // data =new BufferedReader(new InputStreamReader(new URL(urlJar + "icondef.xml").openStream())); |
---|
| 240 | // } |
---|
| 241 | // } |
---|
| 242 | // catch(IOException e) |
---|
| 243 | // { |
---|
| 244 | // //System.out.println(currentFile.getName() + " is not a valid jar file"); |
---|
| 245 | // System.out.println(e.toString()); |
---|
| 246 | // } |
---|
| 247 | // } |
---|
| 248 | |
---|
| 249 | public void appendToXML(StringBuffer xml) |
---|
| 250 | { |
---|
| 251 | appendHeader(xml); |
---|
| 252 | appendOpenTag(xml,"<plugins>"); |
---|
| 253 | for(Iterator i = plugins.iterator();i.hasNext();) |
---|
| 254 | { |
---|
| 255 | appendOpenTag(xml,"<plugin>"); |
---|
| 256 | Object[] temp = (Object[]) i.next(); |
---|
| 257 | appendElement(xml,"name",(String)temp[0]); |
---|
| 258 | appendElement(xml,"description",(String)temp[2]); |
---|
| 259 | appendElement(xml,"version",(String)temp[3]); |
---|
| 260 | appendElement(xml,"min_jeti_version",(String)temp[4]); |
---|
| 261 | appendElement(xml,"parent",((String)temp[5])); |
---|
| 262 | appendCloseTag(xml,"</plugin>"); |
---|
| 263 | } |
---|
| 264 | appendCloseTag(xml,"</plugins>"); |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | public static void main(String[] args) { |
---|
| 268 | if (args.length != 2) { |
---|
| 269 | System.err.println("Arguments: dir_to_scan outfile"); |
---|
| 270 | System.exit(1); |
---|
| 271 | } |
---|
| 272 | new Preferences(); |
---|
| 273 | PluginData data = new PluginData(); |
---|
| 274 | data.plugins.clear(); |
---|
| 275 | data.searchPlugins(args[0]); |
---|
| 276 | try { |
---|
| 277 | BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( |
---|
| 278 | new FileOutputStream(args[1]), "UTF8")); |
---|
| 279 | writer.write(data.toString()); |
---|
| 280 | writer.close(); |
---|
| 281 | } catch (IOException e) { |
---|
| 282 | System.err.println("Failed to write file: " + e); |
---|
| 283 | System.exit(1); |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | } |
---|
| 287 | /* |
---|
| 288 | * Overrides for emacs |
---|
| 289 | * Local variables: |
---|
| 290 | * tab-width: 4 |
---|
| 291 | * End: |
---|
| 292 | */ |
---|