OSDN Git Service

refs #344 インストールのとき,依存ライブラリも一緒にプラグインディレクトリへコピーするように変更した.
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / command / UninstallCommand.java
1 package jp.sourceforge.stigmata.command;
2
3 import java.io.File;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.io.PrintWriter;
7 import java.net.MalformedURLException;
8 import java.net.URL;
9 import java.util.regex.Matcher;
10 import java.util.regex.Pattern;
11
12 import jp.sourceforge.stigmata.BirthmarkContext;
13 import jp.sourceforge.stigmata.BirthmarkEnvironment;
14 import jp.sourceforge.stigmata.Stigmata;
15 import jp.sourceforge.stigmata.spi.BirthmarkService;
16 import jp.sourceforge.stigmata.utils.ConfigFileExporter;
17
18 /**
19  * 
20  * @author Haruaki Tamada
21  */
22 @Deprecated
23 public class UninstallCommand extends AbstractStigmataCommand{
24
25     @Override
26     public boolean isAvailableArguments(String[] args){
27         return args.length > 0;
28     }
29
30     @Override
31     public String getCommandString(){
32         return "uninstall";
33     }
34
35     @Override
36     public boolean perform(Stigmata stigmata, BirthmarkContext context, String[] args){
37         File pluginsDir = new File(BirthmarkEnvironment.getStigmataHome(), "plugins");
38         BirthmarkEnvironment env = context.getEnvironment();
39         boolean removeServiceInConfigFile = false;
40
41         for(int i = 0; i < args.length; i++){
42             BirthmarkService service = env.getService(args[i]);
43             if(service instanceof BirthmarkService){
44                 env.removeService(args[i]);
45                 removeServiceInConfigFile = true;
46             }
47             else{
48                 String fileName = getPluginFileNameOfService(context, service);
49                 if(fileName != null){
50                     File pluginFile = new File(pluginsDir, fileName);
51                     pluginFile.renameTo(new File(pluginFile.getParentFile(), pluginFile.getName() + ".back"));
52                 }
53             }
54         }
55         if(removeServiceInConfigFile){
56             updateConfigFile(env);
57         }
58         return true;
59     }
60
61     private void updateConfigFile(BirthmarkEnvironment env){
62         File configFile = new File(BirthmarkEnvironment.getStigmataHome(), "stigmata.xml");
63         try{
64             new ConfigFileExporter(env).export(new PrintWriter(new FileWriter(configFile)));
65         } catch(IOException e){
66             e.printStackTrace();
67         }
68     }
69
70     private String getPluginFileNameOfService(BirthmarkContext context, BirthmarkService service){
71         Class<?> serviceClass = service.getClass();
72         URL location = serviceClass.getResource("/" + serviceClass.getName().replace('.', '/') + ".class");
73
74         if(location != null){
75             Pattern pattern = Pattern.compile("jar:(.*)/plugins/(.*.jar)!([a-zA-Z0-9$/.]+.class)");
76             Matcher matcher = pattern.matcher(location.toString());
77
78             if(matcher.matches()){
79                 try{
80                     URL homeLocation = new File(BirthmarkEnvironment.getStigmataHome()).toURI().toURL();
81                     String matchedLocation = matcher.group(1) + "/";
82                     if(matchedLocation.equals(homeLocation.toString())){
83                         return matcher.group(2);
84                     }
85                 } catch(MalformedURLException e){
86                     e.printStackTrace();
87                 }
88             }
89         }
90         return null;
91     }
92 }