OSDN Git Service

refs #344 インストールのとき,依存ライブラリも一緒にプラグインディレクトリへコピーするように変更した.
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / utils / Utility.java
1 package jp.sourceforge.stigmata.utils;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.jar.JarEntry;
8 import java.util.jar.JarFile;
9 import java.util.jar.Manifest;
10
11 public class Utility{
12     /**
13      * no instance is created
14      */
15     private Utility(){
16     }
17
18     public static void deleteDirectory(File dir){
19         File[] files = dir.listFiles();
20         for(File file: files){
21             if(file.isDirectory()){
22                 deleteDirectory(file);
23             }
24             else{
25                 file.delete();
26             }
27         }
28         dir.delete();
29     }
30
31     public static String array2String(String[] values){
32         StringBuilder builder = new StringBuilder();
33         for(int i = 0; i < values.length; i++){
34             if(i != 0)
35                 builder.append(", ");
36             builder.append(values[i]);
37         }
38         return new String(builder);
39     }
40
41     public static String[] getDependencies(File source){
42         JarFile jarfile = null;
43         try{
44             jarfile = new JarFile(source);
45             Manifest manifest = jarfile.getManifest();
46             String classPath = manifest.getMainAttributes().getValue("Class-Path");
47             if(classPath != null && !classPath.equals("")){
48                 return classPath.split("[ \t]");
49             }
50         } catch(IOException e){
51         } finally{
52             if(jarfile != null){
53                 try{
54                     jarfile.close();
55                 } catch(IOException e){
56                 }
57             }
58         }
59         return new String[0];
60     }
61
62     public static boolean isStigmataPluginJarFile(File pluginFile, List<String> messages){
63         boolean flag = true;
64         if(pluginFile == null){
65             flag = false;
66         }
67         if(!pluginFile.getName().endsWith(".jar")){
68             messages.add("install.error.notjarfile");
69             flag = false;
70         }
71         if(!pluginFile.exists()){
72             messages.add("install.error.file.missing");
73             flag = false;
74         }
75
76         // check service descriptor.
77         if(flag){
78             try{
79                 JarFile jarfile = new JarFile(pluginFile);
80                 JarEntry entry = jarfile.getJarEntry("META-INF/services/jp.sourceforge.stigmata.spi.BirthmarkService");
81                 if(entry == null){
82                     messages.add("install.error.servicedescriptor.missing");
83                     flag = false;
84                 }
85                 jarfile.close();
86             } catch(IOException e){
87                 e.printStackTrace();
88             }
89         }
90         return flag;
91     }
92 }