KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > pluginmgr > PluginList


1 /*
2  * PluginList.java - Plugin list downloaded from server
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2001, 2003 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.pluginmgr;
24
25 //{{{ Imports
26
import java.io.*;
27 import java.net.URL JavaDoc;
28 import java.util.*;
29 import java.util.zip.GZIPInputStream JavaDoc;
30 import org.xml.sax.XMLReader JavaDoc;
31 import org.xml.sax.InputSource JavaDoc;
32 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
33 import org.gjt.sp.util.Log;
34 import org.gjt.sp.util.StandardUtilities;
35 import org.gjt.sp.util.WorkRequest;
36 import org.gjt.sp.util.IOUtilities;
37 import org.gjt.sp.jedit.*;
38 //}}}
39

40 /**
41  * Plugin list downloaded from server.
42  * @since jEdit 3.2pre2
43  * @version $Id: PluginList.java 8303 2007-01-02 21:50:43Z kpouer $
44  */

45 class PluginList
46 {
47     /**
48      * Magic numbers used for auto-detecting GZIP files.
49      */

50     public static final int GZIP_MAGIC_1 = 0x1f;
51     public static final int GZIP_MAGIC_2 = 0x8b;
52
53     final List<Plugin> plugins = new ArrayList<Plugin>();
54     final Map<String JavaDoc, Plugin> pluginHash = new HashMap<String JavaDoc, Plugin>();
55     final List<PluginSet> pluginSets = new ArrayList<PluginSet>();
56
57     /**
58      * The mirror id.
59      * @since jEdit 4.3pre3
60      */

61     private final String JavaDoc id;
62
63     //{{{ PluginList constructor
64
PluginList(WorkRequest workRequest) throws Exception JavaDoc
65     {
66         String JavaDoc path = jEdit.getProperty("plugin-manager.export-url");
67         id = jEdit.getProperty("plugin-manager.mirror.id");
68         if (!id.equals(MirrorList.Mirror.NONE))
69             path += "?mirror="+id;
70         PluginListHandler handler = new PluginListHandler(this,path);
71         XMLReader JavaDoc parser = XMLReaderFactory.createXMLReader();
72         InputStream JavaDoc in = null;
73         InputStream JavaDoc inputStream = null;
74         try
75         {
76             inputStream = new URL JavaDoc(path).openStream();
77             workRequest.setStatus(jEdit.getProperty("plugin-manager.list-download"));
78             in = new BufferedInputStream(inputStream);
79             if(in.markSupported())
80             {
81                 in.mark(2);
82                 int b1 = in.read();
83                 int b2 = in.read();
84                 in.reset();
85
86                 if(b1 == GZIP_MAGIC_1 && b2 == GZIP_MAGIC_2)
87                     in = new GZIPInputStream JavaDoc(in);
88             }
89
90             InputSource JavaDoc isrc = new InputSource JavaDoc(new InputStreamReader(in,"UTF8"));
91             isrc.setSystemId("jedit.jar");
92             parser.setContentHandler(handler);
93             parser.setDTDHandler(handler);
94             parser.setEntityResolver(handler);
95             parser.setErrorHandler(handler);
96             parser.parse(isrc);
97         }
98         finally
99         {
100             IOUtilities.closeQuietly(in);
101             IOUtilities.closeQuietly(inputStream);
102         }
103     } //}}}
104

105     //{{{ addPlugin() method
106
void addPlugin(Plugin plugin)
107     {
108         plugin.checkIfInstalled();
109         plugins.add(plugin);
110         pluginHash.put(plugin.name,plugin);
111     } //}}}
112

113     //{{{ addPluginSet() method
114
void addPluginSet(PluginSet set)
115     {
116         pluginSets.add(set);
117     } //}}}
118

119     //{{{ finished() method
120
void finished()
121     {
122         // after the entire list is loaded, fill out plugin field
123
// in dependencies
124
for(int i = 0; i < plugins.size(); i++)
125         {
126             Plugin plugin = plugins.get(i);
127             for(int j = 0; j < plugin.branches.size(); j++)
128             {
129                 Branch branch = plugin.branches.get(j);
130                 for(int k = 0; k < branch.deps.size(); k++)
131                 {
132                     Dependency dep = branch.deps.get(k);
133                     if(dep.what.equals("plugin"))
134                         dep.plugin = pluginHash.get(dep.pluginName);
135                 }
136             }
137         }
138     } //}}}
139

140     //{{{ dump() method
141
void dump()
142     {
143         for(int i = 0; i < plugins.size(); i++)
144         {
145             System.err.println(plugins.get(i));
146             System.err.println();
147         }
148     } //}}}
149

150     //{{{ getMirrorId() method
151
/**
152      * Returns the mirror ID.
153      *
154      * @return the mirror ID
155      * @since jEdit 4.3pre3
156      */

157     String JavaDoc getMirrorId()
158     {
159         return id;
160     } //}}}
161

162     //{{{ PluginSet class
163
static class PluginSet
164     {
165         String JavaDoc name;
166         String JavaDoc description;
167         final List<String JavaDoc> plugins = new ArrayList<String JavaDoc>();
168
169         public String JavaDoc toString()
170         {
171             return plugins.toString();
172         }
173     } //}}}
174

175     //{{{ Plugin class
176
public static class Plugin
177     {
178         String JavaDoc jar;
179         String JavaDoc name;
180         String JavaDoc description;
181         String JavaDoc author;
182         final List<Branch> branches = new ArrayList<Branch>();
183         //String installed;
184
//String installedVersion;
185

186         void checkIfInstalled()
187         {
188             /* // check if the plugin is already installed.
189             // this is a bit of hack
190             PluginJAR[] jars = jEdit.getPluginJARs();
191             for(int i = 0; i < jars.length; i++)
192             {
193                 String path = jars[i].getPath();
194                 if(!new File(path).exists())
195                     continue;
196
197                 if(MiscUtilities.getFileName(path).equals(jar))
198                 {
199                     installed = path;
200
201                     EditPlugin plugin = jars[i].getPlugin();
202                     if(plugin != null)
203                     {
204                         installedVersion = jEdit.getProperty(
205                             "plugin." + plugin.getClassName()
206                             + ".version");
207                     }
208                     break;
209                 }
210             }
211
212             String[] notLoaded = jEdit.getNotLoadedPluginJARs();
213             for(int i = 0; i < notLoaded.length; i++)
214             {
215                 String path = notLoaded[i];
216
217                 if(MiscUtilities.getFileName(path).equals(jar))
218                 {
219                     installed = path;
220                     break;
221                 }
222             } */

223         }
224
225         String JavaDoc getInstalledVersion()
226         {
227             PluginJAR[] jars = jEdit.getPluginJARs();
228             for(int i = 0; i < jars.length; i++)
229             {
230                 String JavaDoc path = jars[i].getPath();
231
232                 if(MiscUtilities.getFileName(path).equals(jar))
233                 {
234                     EditPlugin plugin = jars[i].getPlugin();
235                     if(plugin != null)
236                     {
237                         return jEdit.getProperty(
238                             "plugin." + plugin.getClassName()
239                             + ".version");
240                     }
241                     else
242                         return null;
243                 }
244             }
245
246             return null;
247         }
248
249         String JavaDoc getInstalledPath()
250         {
251             PluginJAR[] jars = jEdit.getPluginJARs();
252             for(int i = 0; i < jars.length; i++)
253             {
254                 String JavaDoc path = jars[i].getPath();
255
256                 if(MiscUtilities.getFileName(path).equals(jar))
257                     return path;
258             }
259
260             return null;
261         }
262
263         /**
264          * Find the first branch compatible with the running jEdit release.
265          */

266         Branch getCompatibleBranch()
267         {
268             for(int i = 0; i < branches.size(); i++)
269             {
270                 Branch branch = branches.get(i);
271                 if(branch.canSatisfyDependencies())
272                     return branch;
273             }
274
275             return null;
276         }
277
278         boolean canBeInstalled()
279         {
280             Branch branch = getCompatibleBranch();
281             return branch != null && !branch.obsolete
282                 && branch.canSatisfyDependencies();
283         }
284
285         void install(Roster roster, String JavaDoc installDirectory, boolean downloadSource)
286         {
287             String JavaDoc installed = getInstalledPath();
288
289             Branch branch = getCompatibleBranch();
290             if(branch.obsolete)
291             {
292                 if(installed != null)
293                     roster.addRemove(installed);
294                 return;
295             }
296
297             //branch.satisfyDependencies(roster,installDirectory,
298
// downloadSource);
299

300             if(installed != null)
301             {
302                 installDirectory = MiscUtilities.getParentOfPath(
303                     installed);
304             }
305
306             roster.addInstall(
307                 installed,
308                 downloadSource ? branch.downloadSource : branch.download,
309                 installDirectory,
310                 downloadSource ? branch.downloadSourceSize : branch.downloadSize);
311
312         }
313
314         public String JavaDoc toString()
315         {
316             return name;
317         }
318     } //}}}
319

320     //{{{ Branch class
321
static class Branch
322     {
323         String JavaDoc version;
324         String JavaDoc date;
325         int downloadSize;
326         String JavaDoc download;
327         int downloadSourceSize;
328         String JavaDoc downloadSource;
329         boolean obsolete;
330         final List<Dependency> deps = new ArrayList<Dependency>();
331
332         boolean canSatisfyDependencies()
333         {
334             for(int i = 0; i < deps.size(); i++)
335             {
336                 Dependency dep = deps.get(i);
337                 if(!dep.canSatisfy())
338                     return false;
339             }
340
341             return true;
342         }
343
344         void satisfyDependencies(Roster roster, String JavaDoc installDirectory,
345             boolean downloadSource)
346         {
347             for(int i = 0; i < deps.size(); i++)
348             {
349                 Dependency dep = deps.get(i);
350                 dep.satisfy(roster,installDirectory,downloadSource);
351             }
352         }
353
354         public String JavaDoc toString()
355         {
356             return "[version=" + version + ",download=" + download
357                 + ",obsolete=" + obsolete + ",deps=" + deps + ']';
358         }
359     } //}}}
360

361     //{{{ Dependency class
362
static class Dependency
363     {
364         final String JavaDoc what;
365         final String JavaDoc from;
366         final String JavaDoc to;
367         // only used if what is "plugin"
368
final String JavaDoc pluginName;
369         Plugin plugin;
370
371         Dependency(String JavaDoc what, String JavaDoc from, String JavaDoc to, String JavaDoc pluginName)
372         {
373             this.what = what;
374             this.from = from;
375             this.to = to;
376             this.pluginName = pluginName;
377         }
378
379         boolean isSatisfied()
380         {
381             if(what.equals("plugin"))
382             {
383                 for(int i = 0; i < plugin.branches.size(); i++)
384                 {
385                     String JavaDoc installedVersion = plugin.getInstalledVersion();
386                     if(installedVersion != null
387                         &&
388                     (from == null || StandardUtilities.compareStrings(
389                         installedVersion,from,false) >= 0)
390                         &&
391                         (to == null || StandardUtilities.compareStrings(
392                               installedVersion,to,false) <= 0))
393                     {
394                         return true;
395                     }
396                 }
397
398                 return false;
399             }
400             else if(what.equals("jdk"))
401             {
402                 String JavaDoc javaVersion = System.getProperty("java.version").substring(0,3);
403
404                 if((from == null || StandardUtilities.compareStrings(
405                     javaVersion,from,false) >= 0)
406                     &&
407                     (to == null || StandardUtilities.compareStrings(
408                              javaVersion,to,false) <= 0))
409                     return true;
410                 else
411                     return false;
412             }
413             else if(what.equals("jedit"))
414             {
415                 String JavaDoc build = jEdit.getBuild();
416
417                 if((from == null || StandardUtilities.compareStrings(
418                     build,from,false) >= 0)
419                     &&
420                     (to == null || StandardUtilities.compareStrings(
421                              build,to,false) <= 0))
422                     return true;
423                 else
424                     return false;
425             }
426             else
427             {
428                 Log.log(Log.ERROR,this,"Invalid dependency: " + what);
429                 return false;
430             }
431         }
432
433         boolean canSatisfy()
434         {
435             if(isSatisfied())
436                 return true;
437             if (what.equals("plugin"))
438                 return plugin.canBeInstalled();
439             return false;
440         }
441
442         void satisfy(Roster roster, String JavaDoc installDirectory,
443             boolean downloadSource)
444         {
445             if(what.equals("plugin"))
446             {
447                 String JavaDoc installedVersion = plugin.getInstalledVersion();
448                 for(int i = 0; i < plugin.branches.size(); i++)
449                 {
450                     Branch branch = plugin.branches.get(i);
451                     if((installedVersion == null
452                         ||
453                     StandardUtilities.compareStrings(
454                         installedVersion,branch.version,false) < 0)
455                         &&
456                     (from == null || StandardUtilities.compareStrings(
457                         branch.version,from,false) >= 0)
458                         &&
459                         (to == null || StandardUtilities.compareStrings(
460                               branch.version,to,false) <= 0))
461                     {
462                         plugin.install(roster,installDirectory,
463                             downloadSource);
464                         return;
465                     }
466                 }
467             }
468         }
469
470         public String JavaDoc toString()
471         {
472             return "[what=" + what + ",from=" + from
473                 + ",to=" + to + ",plugin=" + plugin + ']';
474         }
475     } //}}}
476
}
477
Popular Tags