KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Roster.java - A list of things to do, used in various places
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2001, 2004 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 javax.swing.SwingUtilities JavaDoc;
27 import java.awt.Component JavaDoc;
28 import java.io.*;
29 import java.net.*;
30 import java.util.zip.*;
31 import java.util.*;
32 import org.gjt.sp.jedit.*;
33 import org.gjt.sp.util.Log;
34 import org.gjt.sp.util.IOUtilities;
35 //}}}
36

37 /**
38  * @author $Id: Roster.java 7623 2006-10-19 13:25:36Z kpouer $
39  */

40 class Roster
41 {
42     //{{{ Roster constructor
43
Roster()
44     {
45         operations = new ArrayList<Operation>();
46         toLoad = new ArrayList<String JavaDoc>();
47     } //}}}
48

49     //{{{ addRemove() method
50
void addRemove(String JavaDoc plugin)
51     {
52         addOperation(new Remove(plugin));
53     } //}}}
54

55     //{{{ addInstall() method
56
void addInstall(String JavaDoc installed, String JavaDoc url, String JavaDoc installDirectory,
57         int size)
58     {
59         addOperation(new Install(installed,url,installDirectory,size));
60     } //}}}
61

62     //{{{ getOperation() method
63
public Operation getOperation(int i)
64     {
65         return operations.get(i);
66     } //}}}
67

68     //{{{ getOperationCount() method
69
int getOperationCount()
70     {
71         return operations.size();
72     } //}}}
73

74     //{{{ isEmpty() method
75
boolean isEmpty()
76     {
77         return operations.isEmpty();
78     } //}}}
79

80     //{{{ performOperationsInWorkThread() method
81
void performOperationsInWorkThread(PluginManagerProgress progress)
82     {
83         for(int i = 0; i < operations.size(); i++)
84         {
85             Operation op = operations.get(i);
86             op.runInWorkThread(progress);
87             progress.done();
88
89             if(Thread.interrupted())
90                 return;
91         }
92     } //}}}
93

94     //{{{ performOperationsInAWTThread() method
95
void performOperationsInAWTThread(Component JavaDoc comp)
96     {
97         for(int i = 0; i < operations.size(); i++)
98         {
99             Operation op = operations.get(i);
100             op.runInAWTThread(comp);
101         }
102
103         // add the JARs before checking deps since dep check might
104
// require all JARs to be present
105
for(int i = 0; i < toLoad.size(); i++)
106         {
107             String JavaDoc pluginName = toLoad.get(i);
108             if(jEdit.getPluginJAR(pluginName) != null)
109             {
110                 Log.log(Log.WARNING,this,"Already loaded: "
111                     + pluginName);
112             }
113             else
114                 jEdit.addPluginJAR(pluginName);
115         }
116
117         for(int i = 0; i < toLoad.size(); i++)
118         {
119             String JavaDoc pluginName = toLoad.get(i);
120             PluginJAR plugin = jEdit.getPluginJAR(pluginName);
121             if(plugin != null)
122                 plugin.checkDependencies();
123         }
124
125         // now activate the plugins
126
for(int i = 0; i < toLoad.size(); i++)
127         {
128             String JavaDoc pluginName = toLoad.get(i);
129             PluginJAR plugin = jEdit.getPluginJAR(pluginName);
130             if(plugin != null)
131                 plugin.activatePluginIfNecessary();
132         }
133     } //}}}
134

135     //{{{ Private members
136
private static File downloadDir;
137
138     private List<Operation> operations;
139     private List<String JavaDoc> toLoad;
140
141     //{{{ addOperation() method
142
private void addOperation(Operation op)
143     {
144         for(int i = 0; i < operations.size(); i++)
145         {
146             if(operations.get(i).equals(op))
147                 return;
148         }
149
150         operations.add(op);
151     } //}}}
152

153     //{{{ getDownloadDir() method
154
private static String JavaDoc getDownloadDir()
155     {
156         if(downloadDir == null)
157         {
158             String JavaDoc settings = jEdit.getSettingsDirectory();
159             if(settings == null)
160                 settings = System.getProperty("user.home");
161             downloadDir = new File(MiscUtilities.constructPath(
162                 settings,"PluginManager.download"));
163             downloadDir.mkdirs();
164         }
165
166         return downloadDir.getPath();
167     } //}}}
168

169     //}}}
170

171     //{{{ Operation interface
172
abstract static class Operation
173     {
174         public void runInWorkThread(PluginManagerProgress progress)
175         {
176         }
177
178         public void runInAWTThread(Component JavaDoc comp)
179         {
180         }
181
182         public int getMaximum()
183         {
184             return 0;
185         }
186     } //}}}
187

188     //{{{ Remove class
189
class Remove extends Operation
190     {
191         //{{{ Remove constructor
192
Remove(String JavaDoc plugin)
193         {
194             this.plugin = plugin;
195         } //}}}
196

197         //{{{ runInAWTThread() method
198
public void runInAWTThread(Component JavaDoc comp)
199         {
200             // close JAR file and all JARs that depend on this
201
PluginJAR jar = jEdit.getPluginJAR(plugin);
202             if(jar != null)
203             {
204                 unloadPluginJAR(jar);
205             }
206
207             toLoad.remove(plugin);
208
209             // remove cache file
210

211             // move JAR first
212
File jarFile = new File(plugin);
213             File srcFile = new File(plugin.substring(0,plugin.length() - 4));
214
215             Log.log(Log.NOTICE,this,"Deleting " + jarFile);
216
217             boolean ok = jarFile.delete();
218
219             if(srcFile.exists())
220                 ok &= deleteRecursively(srcFile);
221
222             if(!ok)
223             {
224                 String JavaDoc[] args = { plugin };
225                 GUIUtilities.error(comp,"plugin-manager.remove-failed",args);
226             }
227         } //}}}
228

229         //{{{ unloadPluginJAR() method
230
/**
231          * This should go into a public method somewhere.
232          * @param jar the jar of the plugin
233          */

234         private void unloadPluginJAR(PluginJAR jar)
235         {
236             String JavaDoc[] dependents = jar.getDependentPlugins();
237             for (String JavaDoc path: dependents)
238             {
239                 PluginJAR _jar = jEdit.getPluginJAR(path);
240                 if(_jar != null)
241                 {
242                     toLoad.add(path);
243                     unloadPluginJAR(_jar);
244                     // clear cache file
245
String JavaDoc cachePath = jar.getCachePath();
246                     if(cachePath != null)
247                         new File(cachePath).delete();
248
249                 }
250             }
251             jEdit.removePluginJAR(jar,false);
252             
253         } //}}}
254

255         //{{{ equals() method
256
public boolean equals(Object JavaDoc o)
257         {
258             return o instanceof Remove
259                    && ((Remove) o).plugin.equals(plugin);
260         } //}}}
261

262         //{{{ Private members
263
private final String JavaDoc plugin;
264
265         private boolean deleteRecursively(File file)
266         {
267             Log.log(Log.NOTICE,this,"Deleting " + file + " recursively");
268
269             boolean ok = true;
270
271             if(file.isDirectory())
272             {
273                 String JavaDoc path = file.getPath();
274                 String JavaDoc[] children = file.list();
275                 for(int i = 0; i < children.length; i++)
276                 {
277                     ok &= deleteRecursively(new File(path,children[i]));
278                 }
279             }
280
281             ok &= file.delete();
282
283             return ok;
284         } //}}}
285
} //}}}
286

287     //{{{ Install class
288
class Install extends Operation
289     {
290         int size;
291
292         //{{{ Install constructor
293
Install(String JavaDoc installed, String JavaDoc url, String JavaDoc installDirectory,
294             int size)
295         {
296             // catch those hooligans passing null urls
297
if(url == null)
298                 throw new NullPointerException JavaDoc();
299
300             this.installed = installed;
301             this.url = url;
302             this.installDirectory = installDirectory;
303             this.size = size;
304         } //}}}
305

306         //{{{ getMaximum() method
307
public int getMaximum()
308         {
309             return size;
310         } //}}}
311

312         //{{{ runInWorkThread() method
313
public void runInWorkThread(PluginManagerProgress progress)
314         {
315             String JavaDoc fileName = MiscUtilities.getFileName(url);
316
317             path = download(progress,fileName,url);
318         } //}}}
319

320         //{{{ runInAWTThread() method
321
public void runInAWTThread(Component JavaDoc comp)
322         {
323             // check if download failed
324
if(path == null)
325                 return;
326
327             // if download OK, remove existing version
328
if(installed != null)
329                 new Remove(installed).runInAWTThread(comp);
330
331             ZipFile zipFile = null;
332
333             try
334             {
335                 zipFile = new ZipFile(path);
336
337                 Enumeration<? extends ZipEntry> e = zipFile.entries();
338                 while(e.hasMoreElements())
339                 {
340                     ZipEntry entry = e.nextElement();
341                     String JavaDoc name = entry.getName().replace('/',File.separatorChar);
342                     File file = new File(installDirectory,name);
343                     if(entry.isDirectory())
344                         file.mkdirs();
345                     else
346                     {
347                         new File(file.getParent()).mkdirs();
348                         InputStream in = null;
349                         FileOutputStream out = null;
350                         try
351                         {
352                             in = zipFile.getInputStream(entry);
353                             out = new FileOutputStream(file);
354                             IOUtilities.copyStream(4096,
355                                 null,
356                                 in,
357                                 out,false);
358                         }
359                         finally
360                         {
361                             IOUtilities.closeQuietly(in);
362                             IOUtilities.closeQuietly(out);
363                         }
364                         if(file.getName().toLowerCase().endsWith(".jar"))
365                             toLoad.add(file.getPath());
366                     }
367                 }
368             }
369             catch(InterruptedIOException iio)
370             {
371             }
372             catch(ZipException e)
373             {
374                 Log.log(Log.ERROR,this,e);
375                 GUIUtilities.error(null,"plugin-error-download",new Object JavaDoc[]{""});
376             }
377             catch(IOException io)
378             {
379                 Log.log(Log.ERROR,this,io);
380
381                 String JavaDoc[] args = { io.getMessage() };
382                 GUIUtilities.error(null,"ioerror",args);
383             }
384             catch(Exception JavaDoc e)
385             {
386                 Log.log(Log.ERROR,this,e);
387             }
388             finally
389             {
390                 try
391                 {
392                     if(zipFile != null)
393                         zipFile.close();
394                 }
395                 catch(IOException io)
396                 {
397                     Log.log(Log.ERROR,this,io);
398                 }
399
400                 if(jEdit.getBooleanProperty(
401                     "plugin-manager.deleteDownloads"))
402                 {
403                     new File(path).delete();
404                 }
405             }
406         } //}}}
407

408         //{{{ equals() method
409
public boolean equals(Object JavaDoc o)
410         {
411             return o instanceof Install
412                    && ((Install) o).url.equals(url);
413         } //}}}
414

415         //{{{ Private members
416
private String JavaDoc installed;
417         private final String JavaDoc url;
418         private String JavaDoc installDirectory;
419         private String JavaDoc path;
420
421         //{{{ download() method
422
private String JavaDoc download(PluginManagerProgress progress,
423             String JavaDoc fileName, String JavaDoc url)
424         {
425             try
426             {
427                 String JavaDoc host = jEdit.getProperty("plugin-manager.mirror.id");
428                 if (host == null || host.equals(MirrorList.Mirror.NONE))
429                     host = "default";
430                 
431                 String JavaDoc path = MiscUtilities.constructPath(getDownloadDir(),fileName);
432                 URLConnection conn = new URL(url).openConnection();
433                 progress.setStatus(jEdit.getProperty("plugin-manager.progress",new String JavaDoc[] {fileName, host}));
434                 InputStream in = null;
435                 FileOutputStream out = null;
436                 try {
437                     in = conn.getInputStream();
438                     out = new FileOutputStream(path);
439                     if(!IOUtilities.copyStream(progress,in,out,true))
440                         return null;
441                 }
442                 finally
443                 {
444                     IOUtilities.closeQuietly(in);
445                     IOUtilities.closeQuietly(out);
446                 }
447                 
448                 return path;
449             }
450             catch(InterruptedIOException iio)
451             {
452                 // do nothing, user clicked 'Stop'
453
return null;
454             }
455             catch(FileNotFoundException e)
456             {
457                 Log.log(Log.ERROR,this,e);
458
459                 SwingUtilities.invokeLater(new Runnable JavaDoc()
460                 {
461                     public void run()
462                     {
463                         GUIUtilities.error(null,"plugin-error-download",new Object JavaDoc[]{""});
464                     }
465                 });
466
467                 return null;
468             }
469             catch(final IOException io)
470             {
471                 Log.log(Log.ERROR,this,io);
472
473                 SwingUtilities.invokeLater(new Runnable JavaDoc()
474                 {
475                     public void run()
476                     {
477                         String JavaDoc[] args = { io.getMessage() };
478                         GUIUtilities.error(null,"plugin-error-download",args);
479                     }
480                 });
481
482                 return null;
483             }
484             catch(Exception JavaDoc e)
485             {
486                 Log.log(Log.ERROR,this,e);
487
488                 return null;
489             }
490         } //}}}
491

492         //}}}
493
} //}}}
494
}
495
Popular Tags