OSDN Git Service

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