1 package jp.sourceforge.stigmata.command;
8 import java.io.FileWriter;
9 import java.io.IOException;
10 import java.io.PrintWriter;
11 import java.net.MalformedURLException;
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
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;
25 * @author Haruaki Tamada
29 public class UninstallCommand extends AbstractStigmataCommand{
31 public boolean isAvailableArguments(String[] args){
32 return args.length > 0;
36 public String getCommandString(){
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;
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;
52 String fileName = getPluginFileNameOfService(context, service);
54 File pluginFile = new File(pluginsDir, fileName);
55 pluginFile.renameTo(new File(pluginFile.getParentFile(), pluginFile.getName() + ".back"));
59 if(removeServiceInConfigFile){
60 updateConfigFile(env);
64 private void updateConfigFile(BirthmarkEnvironment env){
65 File configFile = new File(BirthmarkEnvironment.getStigmataHome(), "stigmata.xml");
67 new ConfigFileExporter(env).export(new PrintWriter(new FileWriter(configFile)));
68 } catch(IOException e){
73 private String getPluginFileNameOfService(BirthmarkContext context, BirthmarkSpi service){
74 Class<?> serviceClass = service.getClass();
75 URL location = serviceClass.getResource("/" + serviceClass.getName().replace('.', '/') + ".class");
78 Pattern pattern = Pattern.compile("jar:(.*)/plugins/(.*.jar)!([a-zA-Z0-9$/.]+.class)");
79 Matcher matcher = pattern.matcher(location.toString());
81 if(matcher.matches()){
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);
88 } catch(MalformedURLException e){