1 22 23 package org.gjt.sp.jedit.pluginmgr; 24 25 import javax.swing.SwingUtilities ; 27 import java.awt.Component ; 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 37 40 class Roster 41 { 42 Roster() 44 { 45 operations = new ArrayList<Operation>(); 46 toLoad = new ArrayList<String >(); 47 } 49 void addRemove(String plugin) 51 { 52 addOperation(new Remove(plugin)); 53 } 55 void addInstall(String installed, String url, String installDirectory, 57 int size) 58 { 59 addOperation(new Install(installed,url,installDirectory,size)); 60 } 62 public Operation getOperation(int i) 64 { 65 return operations.get(i); 66 } 68 int getOperationCount() 70 { 71 return operations.size(); 72 } 74 boolean isEmpty() 76 { 77 return operations.isEmpty(); 78 } 80 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 } 94 void performOperationsInAWTThread(Component 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 for(int i = 0; i < toLoad.size(); i++) 106 { 107 String 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 pluginName = toLoad.get(i); 120 PluginJAR plugin = jEdit.getPluginJAR(pluginName); 121 if(plugin != null) 122 plugin.checkDependencies(); 123 } 124 125 for(int i = 0; i < toLoad.size(); i++) 127 { 128 String pluginName = toLoad.get(i); 129 PluginJAR plugin = jEdit.getPluginJAR(pluginName); 130 if(plugin != null) 131 plugin.activatePluginIfNecessary(); 132 } 133 } 135 private static File downloadDir; 137 138 private List<Operation> operations; 139 private List<String > toLoad; 140 141 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 } 153 private static String getDownloadDir() 155 { 156 if(downloadDir == null) 157 { 158 String 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 } 169 171 abstract static class Operation 173 { 174 public void runInWorkThread(PluginManagerProgress progress) 175 { 176 } 177 178 public void runInAWTThread(Component comp) 179 { 180 } 181 182 public int getMaximum() 183 { 184 return 0; 185 } 186 } 188 class Remove extends Operation 190 { 191 Remove(String plugin) 193 { 194 this.plugin = plugin; 195 } 197 public void runInAWTThread(Component comp) 199 { 200 PluginJAR jar = jEdit.getPluginJAR(plugin); 202 if(jar != null) 203 { 204 unloadPluginJAR(jar); 205 } 206 207 toLoad.remove(plugin); 208 209 211 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 [] args = { plugin }; 225 GUIUtilities.error(comp,"plugin-manager.remove-failed",args); 226 } 227 } 229 234 private void unloadPluginJAR(PluginJAR jar) 235 { 236 String [] dependents = jar.getDependentPlugins(); 237 for (String path: dependents) 238 { 239 PluginJAR _jar = jEdit.getPluginJAR(path); 240 if(_jar != null) 241 { 242 toLoad.add(path); 243 unloadPluginJAR(_jar); 244 String cachePath = jar.getCachePath(); 246 if(cachePath != null) 247 new File(cachePath).delete(); 248 249 } 250 } 251 jEdit.removePluginJAR(jar,false); 252 253 } 255 public boolean equals(Object o) 257 { 258 return o instanceof Remove 259 && ((Remove) o).plugin.equals(plugin); 260 } 262 private final String 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 path = file.getPath(); 274 String [] 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 } } 287 class Install extends Operation 289 { 290 int size; 291 292 Install(String installed, String url, String installDirectory, 294 int size) 295 { 296 if(url == null) 298 throw new NullPointerException (); 299 300 this.installed = installed; 301 this.url = url; 302 this.installDirectory = installDirectory; 303 this.size = size; 304 } 306 public int getMaximum() 308 { 309 return size; 310 } 312 public void runInWorkThread(PluginManagerProgress progress) 314 { 315 String fileName = MiscUtilities.getFileName(url); 316 317 path = download(progress,fileName,url); 318 } 320 public void runInAWTThread(Component comp) 322 { 323 if(path == null) 325 return; 326 327 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 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 []{""}); 376 } 377 catch(IOException io) 378 { 379 Log.log(Log.ERROR,this,io); 380 381 String [] args = { io.getMessage() }; 382 GUIUtilities.error(null,"ioerror",args); 383 } 384 catch(Exception 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 } 408 public boolean equals(Object o) 410 { 411 return o instanceof Install 412 && ((Install) o).url.equals(url); 413 } 415 private String installed; 417 private final String url; 418 private String installDirectory; 419 private String path; 420 421 private String download(PluginManagerProgress progress, 423 String fileName, String url) 424 { 425 try 426 { 427 String host = jEdit.getProperty("plugin-manager.mirror.id"); 428 if (host == null || host.equals(MirrorList.Mirror.NONE)) 429 host = "default"; 430 431 String path = MiscUtilities.constructPath(getDownloadDir(),fileName); 432 URLConnection conn = new URL(url).openConnection(); 433 progress.setStatus(jEdit.getProperty("plugin-manager.progress",new String [] {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 return null; 454 } 455 catch(FileNotFoundException e) 456 { 457 Log.log(Log.ERROR,this,e); 458 459 SwingUtilities.invokeLater(new Runnable () 460 { 461 public void run() 462 { 463 GUIUtilities.error(null,"plugin-error-download",new Object []{""}); 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 () 474 { 475 public void run() 476 { 477 String [] args = { io.getMessage() }; 478 GUIUtilities.error(null,"plugin-error-download",args); 479 } 480 }); 481 482 return null; 483 } 484 catch(Exception e) 485 { 486 Log.log(Log.ERROR,this,e); 487 488 return null; 489 } 490 } 492 } } 495 | Popular Tags |