PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/pluginmgr/Roster.java

#
Java | 319 lines | 239 code | 53 blank | 27 comment | 32 complexity | 5052103d4ffa52e1da42b6b4b5b50b4f MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * Roster.java - A list of things to do, used in various places
  3. * Copyright (C) 2001 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package org.gjt.sp.jedit.pluginmgr;
  20. import javax.swing.JOptionPane;
  21. import java.awt.Component;
  22. import java.io.*;
  23. import java.net.*;
  24. import java.util.zip.*;
  25. import java.util.*;
  26. import org.gjt.sp.jedit.io.VFSManager; // we use VFSManager.error() method
  27. import org.gjt.sp.jedit.*;
  28. import org.gjt.sp.util.Log;
  29. class Roster
  30. {
  31. Roster()
  32. {
  33. operations = new Vector();
  34. }
  35. void addOperation(Operation op)
  36. {
  37. for(int i = 0; i < operations.size(); i++)
  38. {
  39. if(operations.elementAt(i).equals(op))
  40. return;
  41. }
  42. operations.addElement(op);
  43. }
  44. int getOperationCount()
  45. {
  46. return operations.size();
  47. }
  48. boolean isEmpty()
  49. {
  50. return operations.size() == 0;
  51. }
  52. boolean performOperations(PluginManagerProgress progress)
  53. {
  54. for(int i = 0; i < operations.size(); i++)
  55. {
  56. Operation op = (Operation)operations.elementAt(i);
  57. if(op.perform(progress))
  58. progress.done(true);
  59. else
  60. {
  61. progress.done(false);
  62. return false;
  63. }
  64. if(Thread.interrupted())
  65. return false;
  66. }
  67. return true;
  68. }
  69. // private members
  70. private Vector operations;
  71. static interface Operation
  72. {
  73. boolean perform(PluginManagerProgress progress);
  74. boolean equals(Object o);
  75. }
  76. static class Remove implements Operation
  77. {
  78. Remove(String plugin)
  79. {
  80. this.plugin = plugin;
  81. }
  82. public boolean perform(PluginManagerProgress progress)
  83. {
  84. progress.removing(MiscUtilities.getFileName(plugin));
  85. // close JAR file
  86. EditPlugin.JAR jar = jEdit.getPluginJAR(plugin);
  87. if(jar != null)
  88. jar.getClassLoader().closeZipFile();
  89. // move JAR first
  90. File jarFile = new File(plugin);
  91. File srcFile = new File(plugin.substring(0,plugin.length() - 4));
  92. boolean ok = true;
  93. ok &= deleteRecursively(jarFile);
  94. if(srcFile.exists())
  95. ok &= deleteRecursively(srcFile);
  96. String[] args = { plugin };
  97. if(!ok)
  98. GUIUtilities.error(progress,"plugin-manager.remove-failed",args);
  99. return ok;
  100. }
  101. public boolean equals(Object o)
  102. {
  103. if(o instanceof Remove
  104. && ((Remove)o).plugin.equals(plugin))
  105. return true;
  106. else
  107. return false;
  108. }
  109. // private members
  110. private String plugin;
  111. private boolean deleteRecursively(File file)
  112. {
  113. Log.log(Log.NOTICE,this,"Deleting " + file + " recursively");
  114. boolean ok = true;
  115. if(file.isDirectory())
  116. {
  117. String path = file.getPath();
  118. String[] children = file.list();
  119. for(int i = 0; i < children.length; i++)
  120. {
  121. ok &= deleteRecursively(new File(path,children[i]));
  122. }
  123. }
  124. ok &= file.delete();
  125. return ok;
  126. }
  127. }
  128. static class Install implements Operation
  129. {
  130. Install(String url, String installDirectory)
  131. {
  132. // catch those hooligans passing null urls
  133. if(url == null)
  134. throw new NullPointerException();
  135. this.url = url;
  136. this.installDirectory = installDirectory;
  137. }
  138. public boolean perform(PluginManagerProgress progress)
  139. {
  140. try
  141. {
  142. String fileName = MiscUtilities.getFileName(url);
  143. progress.downloading(fileName);
  144. String path = download(progress,fileName,url);
  145. if(path == null)
  146. {
  147. // interrupted download
  148. return false;
  149. }
  150. progress.installing(fileName);
  151. install(progress,path,installDirectory);
  152. return true;
  153. }
  154. catch(InterruptedIOException iio)
  155. {
  156. // do nothing, user clicked 'Stop'
  157. return false;
  158. }
  159. catch(IOException io)
  160. {
  161. Log.log(Log.ERROR,this,io);
  162. String[] args = { io.getMessage() };
  163. VFSManager.error(progress,"ioerror",args);
  164. return false;
  165. }
  166. catch(Exception e)
  167. {
  168. Log.log(Log.ERROR,this,e);
  169. return false;
  170. }
  171. }
  172. public boolean equals(Object o)
  173. {
  174. if(o instanceof Install
  175. && ((Install)o).url.equals(url))
  176. {
  177. /* even if installDirectory is different */
  178. return true;
  179. }
  180. else
  181. return false;
  182. }
  183. // private members
  184. private String url;
  185. private String installDirectory;
  186. private String download(PluginManagerProgress progress,
  187. String fileName, String url) throws Exception
  188. {
  189. URLConnection conn = new URL(url).openConnection();
  190. progress.setMaximum(Math.max(0,conn.getContentLength()));
  191. String path = MiscUtilities.constructPath(getDownloadDir(),fileName);
  192. if(!copy(progress,conn.getInputStream(),
  193. new FileOutputStream(path),true,true))
  194. return null;
  195. return path;
  196. }
  197. private boolean install(PluginManagerProgress progress,
  198. String path, String dir) throws Exception
  199. {
  200. progress.setMaximum(1);
  201. ZipFile zipFile = new ZipFile(path);
  202. Enumeration enum = zipFile.entries();
  203. while(enum.hasMoreElements())
  204. {
  205. ZipEntry entry = (ZipEntry)enum.nextElement();
  206. String name = entry.getName().replace('/',File.separatorChar);
  207. File file = new File(dir,name);
  208. if(entry.isDirectory())
  209. file.mkdirs();
  210. else
  211. {
  212. new File(file.getParent()).mkdirs();
  213. copy(progress,zipFile.getInputStream(entry),
  214. new FileOutputStream(file),false,false);
  215. }
  216. }
  217. new File(path).delete();
  218. progress.setValue(1);
  219. return true;
  220. }
  221. private boolean copy(PluginManagerProgress progress,
  222. InputStream in, OutputStream out, boolean canStop,
  223. boolean doProgress) throws Exception
  224. {
  225. in = new BufferedInputStream(in);
  226. out = new BufferedOutputStream(out);
  227. byte[] buf = new byte[4096];
  228. int copied = 0;
  229. loop: for(;;)
  230. {
  231. int count = in.read(buf,0,buf.length);
  232. if(count == -1)
  233. break loop;
  234. if(doProgress)
  235. {
  236. copied += count;
  237. progress.setValue(copied);
  238. }
  239. out.write(buf,0,count);
  240. if(canStop && Thread.interrupted())
  241. {
  242. in.close();
  243. out.close();
  244. return false;
  245. }
  246. }
  247. in.close();
  248. out.close();
  249. return true;
  250. }
  251. static File downloadDir;
  252. static String getDownloadDir()
  253. {
  254. if(downloadDir == null)
  255. {
  256. String settings = jEdit.getSettingsDirectory();
  257. if(settings == null)
  258. settings = System.getProperty("user.home");
  259. downloadDir = new File(MiscUtilities.constructPath(
  260. settings,"PluginManager.download"));
  261. downloadDir.mkdirs();
  262. }
  263. return downloadDir.getPath();
  264. }
  265. }
  266. }