PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/pluginmgr/PluginList.java

#
Java | 368 lines | 296 code | 42 blank | 30 comment | 57 complexity | 0cecc047fdb27ea3ab0011b54e7af473 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. * PluginList.java - Plugin list
  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 com.microstar.xml.*;
  21. import java.io.*;
  22. import java.net.URL;
  23. import java.util.Hashtable;
  24. import java.util.Vector;
  25. import java.util.zip.GZIPInputStream;
  26. import org.gjt.sp.util.Log;
  27. import org.gjt.sp.jedit.*;
  28. /**
  29. * Plugin list downloaded from server.
  30. * @since jEdit 3.2pre2
  31. */
  32. class PluginList
  33. {
  34. Vector plugins;
  35. Hashtable pluginHash;
  36. Vector pluginSets;
  37. PluginList() throws Exception
  38. {
  39. plugins = new Vector();
  40. pluginHash = new Hashtable();
  41. pluginSets = new Vector();
  42. String path = jEdit.getProperty("plugin-manager.url");
  43. PluginListHandler handler = new PluginListHandler(this,path);
  44. XmlParser parser = new XmlParser();
  45. parser.setHandler(handler);
  46. parser.parse(null,null,new BufferedReader(new InputStreamReader(
  47. new GZIPInputStream(new URL(path).openStream()),"UTF8")));
  48. }
  49. void addPlugin(Plugin plugin)
  50. {
  51. plugin.checkIfInstalled();
  52. plugins.addElement(plugin);
  53. pluginHash.put(plugin.name,plugin);
  54. }
  55. void addPluginSet(PluginSet set)
  56. {
  57. pluginSets.addElement(set);
  58. }
  59. void finished()
  60. {
  61. // after the entire list is loaded, fill out plugin field
  62. // in dependencies
  63. for(int i = 0; i < plugins.size(); i++)
  64. {
  65. Plugin plugin = (Plugin)plugins.elementAt(i);
  66. for(int j = 0; j < plugin.branches.size(); j++)
  67. {
  68. Branch branch = (Branch)plugin.branches.elementAt(j);
  69. for(int k = 0; k < branch.deps.size(); k++)
  70. {
  71. Dependency dep = (Dependency)branch.deps.elementAt(k);
  72. if(dep.what.equals("plugin"))
  73. dep.plugin = (Plugin)pluginHash.get(dep.pluginName);
  74. }
  75. }
  76. }
  77. }
  78. void dump()
  79. {
  80. for(int i = 0; i < plugins.size(); i++)
  81. {
  82. System.err.println((Plugin)plugins.elementAt(i));
  83. System.err.println();
  84. }
  85. }
  86. static class PluginSet
  87. {
  88. String name;
  89. String description;
  90. Vector plugins = new Vector();
  91. public String toString()
  92. {
  93. return plugins.toString();
  94. }
  95. }
  96. static class Plugin
  97. {
  98. String jar;
  99. String name;
  100. String description;
  101. String author;
  102. Vector branches = new Vector();
  103. String installed;
  104. String installedVersion;
  105. void checkIfInstalled()
  106. {
  107. // check if the plugin is already installed.
  108. // this is a bit of hack
  109. EditPlugin.JAR[] jars = jEdit.getPluginJARs();
  110. for(int i = 0; i < jars.length; i++)
  111. {
  112. String path = jars[i].getPath();
  113. if(!new File(path).exists())
  114. continue;
  115. if(MiscUtilities.getFileName(path).equals(jar))
  116. {
  117. installed = path;
  118. EditPlugin[] plugins = jars[i].getPlugins();
  119. if(plugins.length >= 1)
  120. {
  121. installedVersion = jEdit.getProperty(
  122. "plugin." + plugins[0].getClassName()
  123. + ".version");
  124. }
  125. break;
  126. }
  127. }
  128. String[] notLoaded = jEdit.getNotLoadedPluginJARs();
  129. for(int i = 0; i < notLoaded.length; i++)
  130. {
  131. String path = notLoaded[i];
  132. if(MiscUtilities.getFileName(path).equals(jar))
  133. {
  134. installed = path;
  135. break;
  136. }
  137. }
  138. }
  139. /**
  140. * Find the first branch compatible with the running jEdit release.
  141. */
  142. Branch getCompatibleBranch()
  143. {
  144. for(int i = 0; i < branches.size(); i++)
  145. {
  146. Branch branch = (Branch)branches.elementAt(i);
  147. if(branch.canSatisfyDependencies())
  148. return branch;
  149. }
  150. return null;
  151. }
  152. boolean canBeInstalled()
  153. {
  154. Branch branch = getCompatibleBranch();
  155. return branch != null && !branch.obsolete
  156. && branch.canSatisfyDependencies();
  157. }
  158. void install(Roster roster, String installDirectory, boolean downloadSource)
  159. {
  160. if(installed != null)
  161. roster.addOperation(new Roster.Remove(installed));
  162. Branch branch = getCompatibleBranch();
  163. if(branch.obsolete)
  164. return;
  165. branch.satisfyDependencies(roster,installDirectory,
  166. downloadSource);
  167. if(installed != null)
  168. {
  169. installDirectory = MiscUtilities.getParentOfPath(
  170. installed);
  171. }
  172. roster.addOperation(new Roster.Install((downloadSource
  173. ? branch.downloadSource : branch.download),
  174. installDirectory));
  175. }
  176. public String toString()
  177. {
  178. return name;
  179. }
  180. }
  181. static class Branch
  182. {
  183. String version;
  184. String date;
  185. int downloadSize;
  186. String download;
  187. int downloadSourceSize;
  188. String downloadSource;
  189. boolean obsolete;
  190. Vector deps = new Vector();
  191. boolean canSatisfyDependencies()
  192. {
  193. for(int i = 0; i < deps.size(); i++)
  194. {
  195. Dependency dep = (Dependency)deps.elementAt(i);
  196. if(!dep.canSatisfy())
  197. return false;
  198. }
  199. return true;
  200. }
  201. void satisfyDependencies(Roster roster, String installDirectory,
  202. boolean downloadSource)
  203. {
  204. for(int i = 0; i < deps.size(); i++)
  205. {
  206. Dependency dep = (Dependency)deps.elementAt(i);
  207. dep.satisfy(roster,installDirectory,downloadSource);
  208. }
  209. }
  210. public String toString()
  211. {
  212. return "[version=" + version + ",download=" + download
  213. + ",obsolete=" + obsolete + ",deps=" + deps + "]";
  214. }
  215. }
  216. static class Dependency
  217. {
  218. String what;
  219. String from;
  220. String to;
  221. // only used if what is "plugin"
  222. String pluginName;
  223. Plugin plugin;
  224. Dependency(String what, String from, String to, String pluginName)
  225. {
  226. this.what = what;
  227. this.from = from;
  228. this.to = to;
  229. this.pluginName = pluginName;
  230. }
  231. boolean isSatisfied()
  232. {
  233. if(what.equals("plugin"))
  234. {
  235. for(int i = 0; i < plugin.branches.size(); i++)
  236. {
  237. Branch branch = (Branch)plugin.branches
  238. .elementAt(i);
  239. if(plugin.installedVersion != null
  240. &&
  241. (from == null || MiscUtilities.compareStrings(
  242. plugin.installedVersion,from,false) >= 0)
  243. &&
  244. (to == null || MiscUtilities.compareStrings(
  245. plugin.installedVersion,to,false) <= 0))
  246. {
  247. return true;
  248. }
  249. }
  250. return false;
  251. }
  252. else if(what.equals("jdk"))
  253. {
  254. String javaVersion = System.getProperty("java.version").substring(0,3);
  255. if((from == null || MiscUtilities.compareStrings(
  256. javaVersion,from,false) >= 0)
  257. &&
  258. (to == null || MiscUtilities.compareStrings(
  259. javaVersion,to,false) <= 0))
  260. return true;
  261. else
  262. return false;
  263. }
  264. else if(what.equals("jedit"))
  265. {
  266. String build = jEdit.getBuild();
  267. if((from == null || MiscUtilities.compareStrings(
  268. build,from,false) >= 0)
  269. &&
  270. (to == null || MiscUtilities.compareStrings(
  271. build,to,false) <= 0))
  272. return true;
  273. else
  274. return false;
  275. }
  276. else
  277. {
  278. Log.log(Log.ERROR,this,"Invalid dependency: " + what);
  279. return false;
  280. }
  281. }
  282. boolean canSatisfy()
  283. {
  284. if(isSatisfied())
  285. return true;
  286. else if(what.equals("plugin"))
  287. {
  288. return plugin.canBeInstalled();
  289. }
  290. else
  291. return false;
  292. }
  293. void satisfy(Roster roster, String installDirectory,
  294. boolean downloadSource)
  295. {
  296. if(what.equals("plugin"))
  297. {
  298. for(int i = 0; i < plugin.branches.size(); i++)
  299. {
  300. Branch branch = (Branch)plugin.branches
  301. .elementAt(i);
  302. if((plugin.installedVersion == null
  303. ||
  304. MiscUtilities.compareStrings(
  305. plugin.installedVersion,branch.version,false) < 0)
  306. &&
  307. (from == null || MiscUtilities.compareStrings(
  308. branch.version,from,false) >= 0)
  309. &&
  310. (to == null || MiscUtilities.compareStrings(
  311. branch.version,to,false) <= 0))
  312. {
  313. plugin.install(roster,installDirectory,
  314. downloadSource);
  315. return;
  316. }
  317. }
  318. }
  319. }
  320. public String toString()
  321. {
  322. return "[what=" + what + ",from=" + from
  323. + ",to=" + to + ",plugin=" + plugin + "]";
  324. }
  325. }
  326. }