Thinking in Java
Java开发技巧与实践
posts - 9,comments - 6,trackbacks - 0
Eclipse更新时会对Features和Plugins的文件进行一定的清理,经过观察发现Eclipse的清理动作如下:
  1. Features目录会自动更新无需手工清理。
  2. Plugins目录中的source jar会自动清理,而运行jar包会保留之前的一个版本。
出于冗余的考虑,一般来讲不需要手工清理Plugins目录,而手工清理通常需要借助TC等工具逐项对比后删除,需仔细且难保出现错误。因此,写了一个可自动清理Plugins中旧Jar包的辅助程序。
该程序通过对比Plugins中相同包名的Jar包文件,自动删除较旧的Jar包,用了一些小技巧:
  1. 比较版本时直接使用了文件的修改时间比较,当然这是一种比较懒的方法,有可能会出错。
  2. 删除时使用了JNA提供的删除到回收站的方法,以防出错后可以恢复。
源代码见下。

  1 package org.tsinghua.benjamin;
  2 
  3 import java.io.File;
  4 import java.io.FileFilter;
  5 import java.io.IOException;
  6 import java.util.ArrayList;
  7 import java.util.HashMap;
  8 import java.util.Iterator;
  9 import java.util.List;
 10 
 11 import com.sun.jna.platform.FileUtils;
 12 
 13 public class EclipseUpdateCleaner {
 14 
 15 //    private static final String pluginPath = "d:\\Eclipse\\x86\\Luna\\plugins\\";
 16     private static final String pluginPath = "d:\\Eclipse\\x64\\Luna\\plugins\\";
 17     private static final HashMap<String, List<String>> mapDir = new HashMap<>();
 18     private static final HashMap<String, List<String>> mapJar = new HashMap<>();
 19 
 20     private static final class PluginFilter implements FileFilter {
 21 
 22         @Override
 23         public boolean accept(File pathname) {
 24             if (pathname == null || !pathname.exists())
 25                 return false;
 26             if (pathname.isDirectory())
 27                 return true;
 28 
 29             if (pathname.getName().toLowerCase().endsWith(".jar"))
 30                 return true;
 31             return false;
 32         }
 33 
 34     }
 35 
 36     public static void main(String[] args) {
 37         File file = new File(pluginPath);
 38         if(!file.exists() || !file.isDirectory())
 39             return;
 40         
 41         File[] files = file.listFiles(new PluginFilter());
 42         for (File file2 : files) {
 43             String name = file2.getName();
 44             int index = name.indexOf('_');
 45             assert(index > 0);
 46             String key = name.substring(0, index);
 47             String value = name.substring(index + 1);
 48             if(file2.isDirectory()) {
 49                 addToMap(mapDir, key, value);
 50             } else {
 51                 addToMap(mapJar, key, value);
 52             }
 53         }
 54         
 55         System.out.println("---Dir---");
 56         recycleOldJars(mapDir);
 57         
 58         System.out.println("---Jar---");
 59         recycleOldJars(mapJar);
 60     }
 61 
 62     /**
 63      * @param map 
 64      * @param key
 65      * @param value
 66      */
 67     protected static void addToMap(HashMap<String,List<String>> map, String key, String value) {
 68         if(mapDir.containsKey(key)) {
 69             List<String> list = map.get(key);
 70             list.add(value);
 71         } else {
 72             map.put(key, new ArrayList<String>(){
 73                 private static final long serialVersionUID = 7647872335210732416L;
 74 
 75             {
 76                 add(value);
 77             }});
 78         }
 79     }
 80 
 81     /**
 82      * @param fileUtils
 83      * @param iterator
 84      */
 85     protected static void recycleOldJars(HashMap<String, List<String>> map) {
 86         Iterator<String> iterator = map.keySet().iterator();
 87         FileUtils fileUtils = FileUtils.getInstance();
 88         while(iterator.hasNext()) {
 89             String next = iterator.next();
 90             List<String> list = map.get(next);
 91             if(list.size() < 2)
 92                 continue;
 93             else {
 94                 File curr = new File(pluginPath, next + "_" + list.get(0));
 95                 List<File> files = new ArrayList<File>();
 96                 for(int i = 1; i < list.size(); i++) {
 97                     File temp = new File(pluginPath, next + "_" + list.get(i));
 98                     if(temp.lastModified() > curr.lastModified()) {
 99                         files.add(curr);
100                         curr = temp;
101                     }
102                 }
103                 
104                 System.out.println(String.format("[new]%s", curr.getName()));
105                 File[] rs = new File[files.size()];
106                 for (int i = 0; i < rs.length; i++) {
107                     rs[i] = files.get(i);
108                     System.out.println(String.format("\t[delete]%s", rs[i].getName()));
109                 }
110                 
111                 if(fileUtils.hasTrash()) {
112                     try {
113                         fileUtils.moveToTrash(rs);
114                     } catch (IOException e) {
115                         e.printStackTrace();
116                     }
117                 }
118             }
119         }
120     }
121 
122 }
123 


无人分享的快乐不是真快乐,没人分担的痛苦是真痛苦。
posted on 2015-06-11 14:12 Feenn 阅读(438) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: