View Javadoc

1   package fr.in2p3.jsaga.command;
2   
3   import org.ogf.saga.error.DoesNotExistException;
4   
5   import java.io.*;
6   import java.net.URL;
7   import java.util.*;
8   
9   /* ***************************************************
10  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
11  * ***             http://cc.in2p3.fr/             ***
12  * ***************************************************
13  * File:   PostInstall
14  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
15  * Date:   10 oct. 2008
16  * ***************************************************
17  * Description:                                      */
18  /**
19   *
20   */
21  public class PostInstall {
22      private static enum PostInstallType {security, data, job}
23      private String m_scheme;                private static final String SCHEME = "scheme";
24      private String m_osName;                private static final String OS_NAME = "os.name";
25      private File[] m_preInstalledFiles;     private static final String PREINSTALLED_FILES = "pre-installed.files";
26      private String m_preInstalledMessage;   private static final String PREINSTALLED_MESSAGE = "pre-installed.message";
27      private boolean m_superUser;            private static final String SUPER_USER = "super-user";
28      private URL m_script;                   private static final String SCRIPT = "script";
29      private File[] m_postInstalledFiles;    private static final String POSTINSTALLED_FILES = "post-installed.files";
30      private DoesNotExistException m_exception;
31  
32      public static void main(String[] args) throws Exception {
33          if (args.length > 1) {
34              System.err.println("usage: jsaga-post-install [<plug-in>]");
35              System.exit(1);
36          }
37          Map<String,PostInstall> map = new HashMap<String,PostInstall>();
38          for (Enumeration e=PostInstall.class.getClassLoader().getResources("META-INF/post-install.properties"); e.hasMoreElements(); ) {
39              PostInstall pi = new PostInstall((URL) e.nextElement());
40              map.put(pi.m_scheme, pi);
41          }
42          switch(args.length) {
43              case 0:
44                  for (PostInstall pi : map.values()) {
45                      String message;
46                      try {
47                          if (pi.isPostInstalled()) {
48                              message = "OK";
49                          } else {
50                              message = "To be post-installed";
51                          }
52                      } catch(Exception e) {
53                          message = "ERROR ["+e.getMessage()+"]";
54                      }
55                      System.out.println(pi.m_scheme+"\t\t"+message);
56                  }
57                  break;
58              case 1:
59                  PostInstall pi = map.get(args[0]);
60                  if (pi != null) {
61                      pi.dumpScript();
62                  } else {
63                      System.err.println("Adaptor not found: "+args[0]);
64                      System.exit(1);
65                  }
66                  break;
67          }
68      }
69  
70      public PostInstall(URL resource) throws Exception {
71          Properties prop = new Properties();
72          InputStream stream = resource.openStream();
73          prop.load(stream);
74          stream.close();
75          m_scheme = prop.getProperty(SCHEME);
76          m_osName = prop.getProperty(OS_NAME);
77          m_preInstalledFiles = toFilesArray(prop.getProperty(PREINSTALLED_FILES));
78          m_preInstalledMessage = prop.getProperty(PREINSTALLED_MESSAGE);
79          m_superUser = "true".equalsIgnoreCase(prop.getProperty(SUPER_USER));
80          m_script = PostInstall.class.getClassLoader().getResource(prop.getProperty(SCRIPT));
81      }
82  
83      ///////////////////////////////////////////// public ////////////////////////////////////////////
84  
85      /**
86       * @return true if is post-installed, else false
87       * @throws Exception if can not be post-installed
88       */
89      public boolean isPostInstalled() throws Exception {
90          if (m_exception != null) {
91              throw m_exception;
92          } else if (exist(m_postInstalledFiles)) {
93              return true;
94          } else {
95              // check if can be post-installed
96              String os = System.getProperty("os.name");
97              if (m_osName!=null && !os.startsWith(m_osName)) {
98                  throw new Exception("This plug-in only supports OS: "+m_osName);
99              }
100             if (! exist(m_preInstalledFiles)) {
101                 throw new Exception(m_preInstalledMessage);
102             }
103             if (m_superUser) {
104                 if ("Linux".equalsIgnoreCase(os) && !new File("/proc/kmsg").canRead()) {
105                     throw new Exception("You must be a super-user (root) to post-install this plug-in");
106                 } else if ("Windows".equalsIgnoreCase(os) && !new File("C:\\MSDOS.SYS").canRead()) {
107                     throw new Exception("You must be a super-user (Administrator) to post-install this plug-in");
108                 }
109             }
110             return false;
111         }
112     }
113 
114     public void dumpScript() throws DoesNotExistException, IOException {
115         if (m_exception != null) {
116             throw m_exception;
117         }
118         String line;
119         BufferedReader reader = new BufferedReader(new InputStreamReader(m_script.openStream()));
120         while( (line=reader.readLine()) != null ) {
121             System.out.println(line);
122         }
123         reader.close();
124     }
125 
126     //////////////////////////////////////////// private ////////////////////////////////////////////
127 
128     private static File[] toFilesArray(String paths) {
129         String[] pathsArray = paths.split(" ");
130         File[] filesArray = new File[pathsArray.length];
131         for (int i=0; i<filesArray.length; i++) {
132             filesArray[i] = new File(pathsArray[i]);
133         }
134         return filesArray;
135     }
136 
137     private static boolean exist(File[] files) {
138         for (int i=0; files!=null && i<files.length; i++) {
139             if (! files[i].exists()) {
140                 return false;
141             }
142         }
143         return true;
144     }
145 }