OSDN Git Service

refs #344 インストールのとき,依存ライブラリも一緒にプラグインディレクトリへコピーするように変更した.
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / command / InstallCommand.java
1 package jp.sourceforge.stigmata.command;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9 import java.net.URI;
10 import java.net.URISyntaxException;
11 import java.net.URL;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import jp.sourceforge.stigmata.BirthmarkContext;
16 import jp.sourceforge.stigmata.BirthmarkEnvironment;
17 import jp.sourceforge.stigmata.Stigmata;
18 import jp.sourceforge.stigmata.utils.Utility;
19
20 /**
21  * 
22  * @author Haruaki Tamada
23  */
24 public class InstallCommand extends AbstractStigmataCommand{
25     @Override
26     public boolean isAvailableArguments(String[] args){
27         return args.length > 0;
28     }
29
30     @Override
31     public String getCommandString(){
32         return "install";
33     }
34
35     @Override
36     public boolean perform(Stigmata stigmata, BirthmarkContext context, String[] args){
37         File pluginsDir = new File(BirthmarkEnvironment.getStigmataHome(), "plugins");
38         BirthmarkEnvironment env = context.getEnvironment();
39         List<String> messages = new ArrayList<String>();
40
41         for(int i = 0; i < args.length; i++){
42             File pluginSource = new File(args[i]);
43             File pluginDest = new File(pluginsDir, pluginSource.getName());
44
45             if(!Utility.isStigmataPluginJarFile(pluginSource, messages)){
46                 for(String msg: messages){
47                     putMessage(msg);
48                 }
49                 throw new IllegalArgumentException(pluginSource + ": not stigmata plugin file.");
50             }
51             if(pluginDest.exists()){
52                 String override = env.getProperty("override.exists.plugin");
53                 if(override != null &&
54                    (override.equalsIgnoreCase("true") || override.equalsIgnoreCase("yes"))){
55                     pluginDest.delete();
56                 }
57                 else{
58                     File backupFile = new File(pluginDest.getParent(), pluginDest.getName() + ".back");
59                     if(backupFile.exists()) backupFile.delete();
60                     pluginDest.renameTo(backupFile);
61                 }
62             }
63             copyFile(pluginSource, pluginDest);
64             File parent = pluginSource.getParentFile();
65             File destParent = pluginDest.getParentFile();
66             String[] myDependencies = null;
67             for(String dependency: Utility.getDependencies(pluginSource)){
68                 if(myDependencies == null){
69                     myDependencies = findStigmataDependencies();
70                 }
71                 boolean include = false;
72                 for(String systemDependency: myDependencies){
73                     if(dependency.equals(systemDependency)){
74                         include = true;
75                         break;
76                     }
77                 }
78                 if(!include){
79                     File dependencyFile = new File(parent, dependency);
80                     if(dependencyFile.exists()){
81                         copyFile(dependencyFile, new File(destParent, dependency));
82                     }
83                     else{
84                         putMessage(dependency + ": not found. Install this jar file into plugin directory");
85                     }
86                 }
87             }
88         }
89         return getMessageSize() == 0;
90     }
91
92     private String[] findStigmataDependencies(){
93         URL url = getClass().getResource("/jp/sourceforge/stigmata/command/InstallCommand.class");
94         String jarfilePath = url.toString();
95         String[] deps = new String[0];
96         if(jarfilePath.startsWith("jar:")){
97             jarfilePath = jarfilePath.substring("jar:".length(), jarfilePath.lastIndexOf("!"));
98             try{
99                 deps = Utility.getDependencies(new File(new URI(jarfilePath)));
100             } catch(URISyntaxException e){
101                 e.printStackTrace();
102             }
103         }
104         return deps;
105     }
106
107     private void copyFile(File source, File dest){
108         byte[] data = new byte[256];
109         int read;
110         
111         try{
112             InputStream in = new FileInputStream(source);
113             OutputStream out = new FileOutputStream(dest);
114
115             while((read = in.read(data)) != -1){
116                 out.write(data, 0, read);
117             }
118             in.close();
119             out.close();
120         } catch(IOException e){
121         }
122     }
123 }