PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/pluginmgr/Roster.java

#
Java | 483 lines | 346 code | 63 blank | 74 comment | 44 complexity | f7c4796f33004ba6aefdaba54064f179 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. * :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. package org.gjt.sp.jedit.pluginmgr;
  23. //{{{ Imports
  24. import javax.swing.SwingUtilities;
  25. import java.awt.Component;
  26. import java.io.*;
  27. import java.net.*;
  28. import java.util.zip.*;
  29. import java.util.*;
  30. import org.gjt.sp.jedit.*;
  31. import org.gjt.sp.util.Log;
  32. import org.gjt.sp.util.IOUtilities;
  33. import static org.gjt.sp.jedit.io.FileVFS.recursiveDelete;
  34. //}}}
  35. /**
  36. * @author $Id: Roster.java 16348 2009-10-14 10:40:15Z kpouer $
  37. */
  38. class Roster
  39. {
  40. //{{{ Roster constructor
  41. Roster()
  42. {
  43. operations = new ArrayList<Operation>();
  44. toLoad = new ArrayList<String>();
  45. } //}}}
  46. //{{{ addRemove() method
  47. /**
  48. * Add a remove operation for the given jar
  49. * @param jar the jar name
  50. */
  51. void addRemove(String jar)
  52. {
  53. addOperation(new Remove(jar));
  54. } //}}}
  55. //{{{ addInstall() method
  56. void addInstall(String installed, String url, String installDirectory,
  57. int size)
  58. {
  59. addOperation(new Install(installed,url,installDirectory,size));
  60. } //}}}
  61. //{{{ getOperation() method
  62. public Operation getOperation(int i)
  63. {
  64. return operations.get(i);
  65. } //}}}
  66. //{{{ getOperationCount() method
  67. int getOperationCount()
  68. {
  69. return operations.size();
  70. } //}}}
  71. //{{{ isEmpty() method
  72. boolean isEmpty()
  73. {
  74. return operations.isEmpty();
  75. } //}}}
  76. //{{{ performOperationsInWorkThread() method
  77. void performOperationsInWorkThread(PluginManagerProgress progress)
  78. {
  79. for(int i = 0; i < operations.size(); i++)
  80. {
  81. Operation op = operations.get(i);
  82. op.runInWorkThread(progress);
  83. progress.done();
  84. if(Thread.interrupted())
  85. return;
  86. }
  87. } //}}}
  88. //{{{ performOperationsInAWTThread() method
  89. void performOperationsInAWTThread(Component comp)
  90. {
  91. for(int i = 0; i < operations.size(); i++)
  92. {
  93. Operation op = operations.get(i);
  94. op.runInAWTThread(comp);
  95. }
  96. // add the JARs before checking deps since dep check might
  97. // require all JARs to be present
  98. for(int i = 0; i < toLoad.size(); i++)
  99. {
  100. String pluginName = toLoad.get(i);
  101. if(jEdit.getPluginJAR(pluginName) != null)
  102. {
  103. Log.log(Log.WARNING,this,"Already loaded: "
  104. + pluginName);
  105. }
  106. else
  107. jEdit.addPluginJAR(pluginName);
  108. }
  109. for(int i = 0; i < toLoad.size(); i++)
  110. {
  111. String pluginName = toLoad.get(i);
  112. PluginJAR plugin = jEdit.getPluginJAR(pluginName);
  113. if(plugin != null)
  114. plugin.checkDependencies();
  115. }
  116. // now activate the plugins
  117. for(int i = 0; i < toLoad.size(); i++)
  118. {
  119. String pluginName = toLoad.get(i);
  120. PluginJAR plugin = jEdit.getPluginJAR(pluginName);
  121. if(plugin != null)
  122. plugin.activatePluginIfNecessary();
  123. }
  124. } //}}}
  125. //{{{ Private members
  126. private static File downloadDir;
  127. private List<Operation> operations;
  128. private List<String> toLoad;
  129. //{{{ addOperation() method
  130. private void addOperation(Operation op)
  131. {
  132. for(int i = 0; i < operations.size(); i++)
  133. {
  134. if(operations.get(i).equals(op))
  135. return;
  136. }
  137. operations.add(op);
  138. } //}}}
  139. //{{{ getDownloadDir() method
  140. private static String getDownloadDir()
  141. {
  142. if(downloadDir == null)
  143. {
  144. String settings = jEdit.getSettingsDirectory();
  145. if(settings == null)
  146. settings = System.getProperty("user.home");
  147. downloadDir = new File(MiscUtilities.constructPath(
  148. settings,"PluginManager.download"));
  149. downloadDir.mkdirs();
  150. }
  151. return downloadDir.getPath();
  152. } //}}}
  153. //}}}
  154. //{{{ Operation interface
  155. abstract static class Operation
  156. {
  157. public void runInWorkThread(PluginManagerProgress progress)
  158. {
  159. }
  160. public void runInAWTThread(Component comp)
  161. {
  162. }
  163. public int getMaximum()
  164. {
  165. return 0;
  166. }
  167. } //}}}
  168. //{{{ Remove class
  169. class Remove extends Operation
  170. {
  171. //{{{ Remove constructor
  172. Remove(String jar)
  173. {
  174. this.jar = jar;
  175. } //}}}
  176. //{{{ runInAWTThread() method
  177. public void runInAWTThread(Component comp)
  178. {
  179. // close JAR file and all JARs that depend on this
  180. PluginJAR jar = jEdit.getPluginJAR(this.jar);
  181. if(jar != null)
  182. {
  183. unloadPluginJAR(jar);
  184. }
  185. toLoad.remove(this.jar);
  186. // remove cache file
  187. // move JAR first
  188. File jarFile = new File(this.jar);
  189. File srcFile = new File(this.jar.substring(0, this.jar.length() - 4));
  190. Log.log(Log.NOTICE,this,"Deleting " + jarFile);
  191. boolean ok = jarFile.delete();
  192. if(srcFile.exists())
  193. {
  194. ok &= recursiveDelete(srcFile);
  195. }
  196. if(!ok)
  197. {
  198. String[] args = {this.jar};
  199. GUIUtilities.error(comp,"plugin-manager.remove-failed",args);
  200. }
  201. } //}}}
  202. //{{{ unloadPluginJAR() method
  203. /**
  204. * This should go into a public method somewhere.
  205. * @param jar the jar of the plugin
  206. */
  207. private void unloadPluginJAR(PluginJAR jar)
  208. {
  209. String[] dependents = jar.getDependentPlugins();
  210. for (String path: dependents)
  211. {
  212. PluginJAR _jar = jEdit.getPluginJAR(path);
  213. if(_jar != null)
  214. {
  215. toLoad.add(path);
  216. unloadPluginJAR(_jar);
  217. // clear cache file
  218. String cachePath = jar.getCachePath();
  219. if(cachePath != null)
  220. new File(cachePath).delete();
  221. }
  222. }
  223. jEdit.removePluginJAR(jar,false);
  224. } //}}}
  225. //{{{ equals() method
  226. public boolean equals(Object o)
  227. {
  228. return o instanceof Remove
  229. && ((Remove) o).jar.equals(jar);
  230. } //}}}
  231. //{{{ Private members
  232. private final String jar;
  233. //}}}
  234. } //}}}
  235. //{{{ Install class
  236. class Install extends Operation
  237. {
  238. int size;
  239. //{{{ Install constructor
  240. Install(String installed, String url, String installDirectory,
  241. int size)
  242. {
  243. // catch those hooligans passing null urls
  244. if(url == null)
  245. throw new NullPointerException();
  246. this.installed = installed;
  247. this.url = url;
  248. this.installDirectory = installDirectory;
  249. this.size = size;
  250. } //}}}
  251. //{{{ getMaximum() method
  252. public int getMaximum()
  253. {
  254. return size;
  255. } //}}}
  256. //{{{ runInWorkThread() method
  257. public void runInWorkThread(PluginManagerProgress progress)
  258. {
  259. String fileName = MiscUtilities.getFileName(url);
  260. path = download(progress,fileName,url);
  261. } //}}}
  262. //{{{ runInAWTThread() method
  263. public void runInAWTThread(Component comp)
  264. {
  265. // check if download failed
  266. if(path == null)
  267. return;
  268. // if download OK, remove existing version
  269. if(installed != null)
  270. new Remove(installed).runInAWTThread(comp);
  271. ZipFile zipFile = null;
  272. try
  273. {
  274. zipFile = new ZipFile(path);
  275. Enumeration<? extends ZipEntry> e = zipFile.entries();
  276. while(e.hasMoreElements())
  277. {
  278. ZipEntry entry = e.nextElement();
  279. String name = entry.getName().replace('/',File.separatorChar);
  280. File file = new File(installDirectory,name);
  281. if(entry.isDirectory())
  282. file.mkdirs();
  283. else
  284. {
  285. new File(file.getParent()).mkdirs();
  286. InputStream in = null;
  287. FileOutputStream out = null;
  288. try
  289. {
  290. in = zipFile.getInputStream(entry);
  291. out = new FileOutputStream(file);
  292. IOUtilities.copyStream(4096,
  293. null,
  294. in,
  295. out,false);
  296. }
  297. finally
  298. {
  299. IOUtilities.closeQuietly(in);
  300. IOUtilities.closeQuietly(out);
  301. }
  302. if(file.getName().toLowerCase().endsWith(".jar"))
  303. toLoad.add(file.getPath());
  304. }
  305. }
  306. }
  307. catch(InterruptedIOException iio)
  308. {
  309. }
  310. catch(ZipException e)
  311. {
  312. Log.log(Log.ERROR,this,e);
  313. GUIUtilities.error(null,"plugin-error-download",new Object[]{""});
  314. }
  315. catch(IOException io)
  316. {
  317. Log.log(Log.ERROR,this,io);
  318. String[] args = { io.getMessage() };
  319. GUIUtilities.error(null,"ioerror",args);
  320. }
  321. catch(Exception e)
  322. {
  323. Log.log(Log.ERROR,this,e);
  324. }
  325. finally
  326. {
  327. try
  328. {
  329. if(zipFile != null)
  330. zipFile.close();
  331. }
  332. catch(IOException io)
  333. {
  334. Log.log(Log.ERROR,this,io);
  335. }
  336. if(jEdit.getBooleanProperty(
  337. "plugin-manager.deleteDownloads"))
  338. {
  339. new File(path).delete();
  340. }
  341. }
  342. } //}}}
  343. //{{{ equals() method
  344. public boolean equals(Object o)
  345. {
  346. return o instanceof Install
  347. && ((Install) o).url.equals(url);
  348. } //}}}
  349. //{{{ Private members
  350. private String installed;
  351. private final String url;
  352. private String installDirectory;
  353. private String path;
  354. //{{{ download() method
  355. private String download(PluginManagerProgress progress,
  356. String fileName, String url)
  357. {
  358. try
  359. {
  360. String host = jEdit.getProperty("plugin-manager.mirror.id");
  361. if (host == null || host.equals(MirrorList.Mirror.NONE))
  362. host = "default";
  363. String path = MiscUtilities.constructPath(getDownloadDir(),fileName);
  364. URLConnection conn = new URL(url).openConnection();
  365. progress.setStatus(jEdit.getProperty("plugin-manager.progress",new String[] {fileName, host}));
  366. InputStream in = null;
  367. FileOutputStream out = null;
  368. try
  369. {
  370. in = conn.getInputStream();
  371. out = new FileOutputStream(path);
  372. if(!IOUtilities.copyStream(progress,in,out,true))
  373. return null;
  374. }
  375. finally
  376. {
  377. IOUtilities.closeQuietly(in);
  378. IOUtilities.closeQuietly(out);
  379. }
  380. return path;
  381. }
  382. catch(InterruptedIOException iio)
  383. {
  384. // do nothing, user clicked 'Stop'
  385. return null;
  386. }
  387. catch(FileNotFoundException e)
  388. {
  389. Log.log(Log.ERROR,this,e);
  390. SwingUtilities.invokeLater(new Runnable()
  391. {
  392. public void run()
  393. {
  394. GUIUtilities.error(null,"plugin-error-download",new Object[]{""});
  395. }
  396. });
  397. return null;
  398. }
  399. catch(final IOException io)
  400. {
  401. Log.log(Log.ERROR,this,io);
  402. SwingUtilities.invokeLater(new Runnable()
  403. {
  404. public void run()
  405. {
  406. String[] args = { io.getMessage() };
  407. GUIUtilities.error(null,"plugin-error-download",args);
  408. }
  409. });
  410. return null;
  411. }
  412. catch(Exception e)
  413. {
  414. Log.log(Log.ERROR,this,e);
  415. return null;
  416. }
  417. } //}}}
  418. //}}}
  419. } //}}}
  420. }