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.birthmarks.BirthmarkService;
16 import jp.sourceforge.stigmata.spi.BirthmarkSpi;
17 import jp.sourceforge.stigmata.utils.ConfigFileExporter;
21 * @author Haruaki Tamada
24 public class UninstallCommand extends AbstractStigmataCommand{
27 public boolean isAvailableArguments(String[] args){
28 return args.length > 0;
32 public String getCommandString(){
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;
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;
49 String fileName = getPluginFileNameOfService(context, service);
51 File pluginFile = new File(pluginsDir, fileName);
52 pluginFile.renameTo(new File(pluginFile.getParentFile(), pluginFile.getName() + ".back"));
56 if(removeServiceInConfigFile){
57 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, BirthmarkSpi 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){