OSDN Git Service

00169d8338f542355e3e7fd888f73b6b94a5975c
[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.birthmarks.BirthmarkService;
16 import jp.sourceforge.stigmata.spi.BirthmarkSpi;
17 import jp.sourceforge.stigmata.utils.ConfigFileExporter;
18
19 /**
20  * 
21  * @author Haruaki Tamada
22  */
23 @Deprecated
24 public class UninstallCommand extends AbstractStigmataCommand{
25
26     @Override
27     public boolean isAvailableArguments(String[] args){
28         return args.length > 0;
29     }
30
31     @Override
32     public String getCommandString(){
33         return "uninstall";
34     }
35
36     @Override
37     public void perform(Stigmata stigmata, BirthmarkContext context, String[] args){
38         File pluginsDir = new File(BirthmarkEnvironment.getStigmataHome(), "plugins");
39         BirthmarkEnvironment env = context.getEnvironment();
40         boolean removeServiceInConfigFile = false;
41
42         for(int i = 0; i < args.length; i++){
43             BirthmarkSpi service = env.getService(args[i]);
44             if(service instanceof BirthmarkService){
45                 env.removeService(args[i]);
46                 removeServiceInConfigFile = true;
47             }
48             else{
49                 String fileName = getPluginFileNameOfService(context, service);
50                 if(fileName != null){
51                     File pluginFile = new File(pluginsDir, fileName);
52                     pluginFile.renameTo(new File(pluginFile.getParentFile(), pluginFile.getName() + ".back"));
53                 }
54             }
55         }
56         if(removeServiceInConfigFile){
57             updateConfigFile(env);
58         }
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, BirthmarkSpi 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 }