OSDN Git Service

update install process. add stigmata plugin check
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / command / InstallCommand.java
1 package jp.sourceforge.stigmata.command;
2
3 /*
4  * $Id$
5  */
6
7 import java.io.File;
8 import java.io.FileInputStream;
9 import java.io.FileOutputStream;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.OutputStream;
13
14 import jp.sourceforge.stigmata.BirthmarkContext;
15 import jp.sourceforge.stigmata.BirthmarkEnvironment;
16 import jp.sourceforge.stigmata.Stigmata;
17 import jp.sourceforge.stigmata.utils.Utility;
18
19 /**
20  * 
21  * @author Haruaki Tamada
22  * @version $Revision$
23  */
24 public class InstallCommand extends AbstractStigmataCommand{
25     public boolean isAvailableArguments(String[] args){
26         return args.length > 0;
27     }
28
29     @Override
30     public String getCommandString(){
31         return "install";
32     }
33
34     public void perform(Stigmata stigmata, BirthmarkContext context, String[] args){
35         File pluginsDir = new File(BirthmarkEnvironment.getStigmataHome(), "plugins");
36         BirthmarkEnvironment env = context.getEnvironment();
37
38         for(int i = 0; i < args.length; i++){
39             File pluginSource = new File(args[i]);
40             File pluginDest = new File(pluginsDir, pluginSource.getName());
41
42             if(!Utility.isStigmataPluginJarFile(pluginSource)){
43                 throw new IllegalArgumentException(pluginSource + ": not stigmata plugin file.");
44             }
45             if(pluginDest.exists()){
46                 String override = env.getProperty("override.exists.plugin");
47                 if(override != null &&
48                    (override.equalsIgnoreCase("true") || override.equalsIgnoreCase("yes"))){
49                     pluginDest.delete();
50                 }
51                 else{
52                     File backupFile = new File(pluginDest.getParent(), pluginDest.getName() + ".back");
53                     if(backupFile.exists()) backupFile.delete();
54                     pluginDest.renameTo(backupFile);
55                 }
56             }
57
58             byte[] data = new byte[256];
59             int read;
60
61             try{
62                 InputStream in = new FileInputStream(pluginSource);
63                 OutputStream out = new FileOutputStream(pluginDest);
64
65                 while((read = in.read(data)) != -1){
66                     out.write(data, 0, read);
67                 }
68                 in.close();
69                 out.close();
70             } catch(IOException e){
71             }
72         }
73     }
74 }