OSDN Git Service

update command line interface, and introduce command pattern in Main class
[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
18 /**
19  * 
20  * @author Haruaki Tamada
21  * @version $Revision$
22  */
23 public class InstallCommand extends AbstractStigmataCommand{
24     public boolean isAvailableArguments(String[] args){
25         return args.length > 0;
26     }
27
28     @Override
29     public String getCommandString(){
30         return "install";
31     }
32
33     public void perform(Stigmata stigmata, BirthmarkContext context, String[] args){
34         File pluginsDir = new File(BirthmarkEnvironment.getStigmataHome(), "plugins");
35         BirthmarkEnvironment env = context.getEnvironment();
36
37         for(int i = 0; i < args.length; i++){
38             File pluginSource = new File(args[i]);
39             File pluginDest = new File(pluginsDir, pluginSource.getName());
40
41             if(!pluginSource.getName().endsWith(".jar")){
42                 throw new IllegalArgumentException("plugin is allowed only jar archive: " + args[i]);
43             }
44             if(pluginDest.exists()){
45                 String override = env.getProperty("override.exists.plugin");
46                 if(override != null && (override.equalsIgnoreCase("true") || override.equalsIgnoreCase("yes"))){
47                     pluginDest.delete();
48                 }
49                 else{
50                     File backupFile = new File(pluginDest.getParent(), pluginDest.getName() + ".back");
51                     if(backupFile.exists()) backupFile.delete();
52                     pluginDest.renameTo(backupFile);
53                 }
54             }
55
56             byte[] data = new byte[256];
57             int read;
58
59             try{
60                 InputStream in = new FileInputStream(pluginSource);
61                 OutputStream out = new FileOutputStream(pluginDest);
62
63                 while((read = in.read(data)) != -1){
64                     out.write(data, 0, read);
65                 }
66                 in.close();
67                 out.close();
68             } catch(IOException e){
69             }
70         }
71     }
72 }