1 package jp.sourceforge.stigmata.command;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.io.PrintWriter;
7 import java.net.MalformedURLException;
9 import java.util.regex.Matcher;
10 import java.util.regex.Pattern;
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;
20 * @author Haruaki Tamada
23 public class UninstallCommand extends AbstractStigmataCommand{
26 public boolean isAvailableArguments(String[] args){
27 return args.length > 0;
31 public String getCommandString(){
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;
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;
48 String fileName = getPluginFileNameOfService(context, service);
50 File pluginFile = new File(pluginsDir, fileName);
51 pluginFile.renameTo(new File(pluginFile.getParentFile(), pluginFile.getName() + ".back"));
55 if(removeServiceInConfigFile){
56 updateConfigFile(env);
61 private void updateConfigFile(BirthmarkEnvironment env){
62 File configFile = new File(BirthmarkEnvironment.getStigmataHome(), "stigmata.xml");
64 new ConfigFileExporter(env).export(new PrintWriter(new FileWriter(configFile)));
65 } catch(IOException e){
70 private String getPluginFileNameOfService(BirthmarkContext context, BirthmarkService service){
71 Class<?> serviceClass = service.getClass();
72 URL location = serviceClass.getResource("/" + serviceClass.getName().replace('.', '/') + ".class");
75 Pattern pattern = Pattern.compile("jar:(.*)/plugins/(.*.jar)!([a-zA-Z0-9$/.]+.class)");
76 Matcher matcher = pattern.matcher(location.toString());
78 if(matcher.matches()){
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);
85 } catch(MalformedURLException e){