1 /*===========================================================================
2 *
3 *                            PUBLIC DOMAIN NOTICE
4 *               National Center for Biotechnology Information
5 *
6 *  This software/database is a "United States Government Work" under the
7 *  terms of the United States Copyright Act.  It was written as part of
8 *  the author's official duties as a United States Government employee and
9 *  thus cannot be copyrighted.  This software/database is freely available
10 *  to the public for use. The National Library of Medicine and the U.S.
11 *  Government have not placed any restriction on its use or reproduction.
12 *
13 *  Although all reasonable efforts have been taken to ensure the accuracy
14 *  and reliability of the software and data, the NLM and the U.S.
15 *  Government do not and cannot warrant the performance or results that
16 *  may be obtained by using this software or data. The NLM and the U.S.
17 *  Government disclaim all warranties, express or implied, including
18 *  warranties of performance, merchantability or fitness for any particular
19 *  purpose.
20 *
21 *  Please cite the author in any work or product based on this material.
22 *
23 * ==============================================================================
24 *
25 */
26 
27 
28 package gov.nih.nlm.ncbi.ngs;
29 
30 
31 import java.io.BufferedReader;
32 import java.io.InputStreamReader;
33 import java.util.Vector;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36 
37 class LibVersionChecker {
getVersion(String libname, String libpath, boolean useLoadLibrary)38     static Version getVersion(String libname, String libpath, boolean useLoadLibrary) {
39         Vector<String> cmdarray = new Vector<String>();
40         String property = System.getProperty("java.home");
41         if (property != null) {
42             cmdarray.add(property + LibPathIterator.fileSeparator()
43                     + "bin" + LibPathIterator.fileSeparator() + "java");
44             if (!tryJava(cmdarray)) {
45                 cmdarray.remove(0);
46             }
47         }
48         if (cmdarray.size() == 0) {
49             cmdarray.add("java");
50             if (!tryJava(cmdarray)) {
51                 // come up with exception class
52                 throw new RuntimeException("Failed to check library " + libpath + " version: failed to execute java");
53             }
54         }
55 
56         String classpath = System.getProperty("java.class.path");
57         if (classpath != null) {
58             cmdarray.add("-cp");
59             cmdarray.add(classpath);
60         }
61         cmdarray.add(createPropertyString("java.library.path"));
62         if (System.getProperty("vdb.log") != null) {
63             cmdarray.add(createPropertyString("vdb.log"));
64         }
65         cmdarray.add("gov.nih.nlm.ncbi.ngs.LibVersionChecker");
66         cmdarray.add(libname);
67         cmdarray.add(libpath);
68         if (useLoadLibrary) {
69             cmdarray.add("true");
70         }
71 
72         Logger.finer(">>> RUNNING CHILD ...");
73         String version = null;
74         try {
75             String cmd[] = new String[cmdarray.size()];
76             for (int i = 0; i < cmdarray.size(); ++i) {
77                 cmd[i] = cmdarray.elementAt(i);
78             }
79             Logger.finest(cmd);
80             Process p = Runtime.getRuntime().exec(cmd);
81             BufferedReader bri =
82                     new BufferedReader(new InputStreamReader(p.getInputStream()));
83             BufferedReader bre =
84                     new BufferedReader(new InputStreamReader(p.getErrorStream()));
85             String line = null;
86             while ((line = bre.readLine()) != null) {
87                 System.err.println(line);
88             }
89             bre.close();
90             while ((line = bri.readLine()) != null) {
91                 Pattern pattern = Pattern.compile("^LibManager: version='(.*)'$");
92                 Matcher matcher = pattern.matcher(line);
93                 while (matcher.find()) {
94                     version = matcher.group(1);
95                     if (version != null) {
96                         break;
97                     }
98                 }
99                 if (version == null) {
100                     System.out.println(line);
101                 }
102             }
103             bri.close();
104             p.waitFor();
105         } catch (Exception e) { Logger.finest(e); }
106         Logger.finer("<<< Done CHILD");
107         if (version != null) {
108             return new Version(version);
109         }
110         return null;
111     }
112 
113     /** Call checkLib for every argument to the version of local dll,
114      compare it with the latest available
115      and download the latest if it is more recent */
main(String[] args)116     public static void main(String[] args) {
117         LibVersionChecker checker = new LibVersionChecker();
118 
119         if (args.length != 2 && args.length != 3) {
120             throw new RuntimeException("Not enough arguments: should be 2 or 3");
121         }
122 
123         String libname = args[0];
124         String libpath = args[1];
125         boolean useLoadLibrary = args.length == 3 && args[2].equals("true");
126         String version = checker.checkLib(libname, libpath, useLoadLibrary);
127         if (version != null) {
128             System.out.println("LibManager: version='" + version + "'");
129         }
130 
131     }
132 
133     /** Check the version of local dll,
134      compare it with the latest available;
135      download the latest if it is more recent */
checkLib(String libname, String path, boolean useLoadLibrary)136     private String checkLib(String libname, String path, boolean useLoadLibrary) {
137         Logger.finest("> Checking the version of " + path  + " library...");
138 
139         Logger.finest(">> Loading the library...");
140         boolean loaded = false;
141         try {
142             if (useLoadLibrary) {
143                 System.loadLibrary(path);
144             } else {
145                 System.load(path);
146             }
147             loaded = true;
148         } catch (UnsatisfiedLinkError e) {
149             Logger.finest("<< Failed to load library " + path);
150             Logger.finest(e);
151         }
152 
153         String version = null;
154         if (loaded) {
155             Logger.finest(">> Checking current version of the library...");
156             version = getLoadedVersion(libname);
157 
158             Logger.finest("<< The current version of " + path + " = " + version);
159         }
160 
161         Logger.finest("< Done checking version of the library");
162 
163         return version;
164     }
165 
getLoadedVersion(String libname)166     static String getLoadedVersion(String libname) {
167         try {
168             if (libname.equals("ncbi-vdb")) {
169                 return Manager.getPackageVersion();
170             } else if (libname.equals("ngs-sdk")) {
171                 return ngs.Package.getPackageVersion();
172             } else {
173                 Logger.warning("It is not known how to check "
174                         + "the version of " + libname + " library");
175                 return null;
176             }
177         } catch (ngs.ErrorMsg e) {
178             Logger.finest(e);
179         } catch (UnsatisfiedLinkError e) {
180             Logger.finest(e);
181         }
182         return null;
183     }
184 
185     /** Make sure we can execute java */
tryJava(Vector<String> cmdarray)186     private static boolean tryJava(Vector<String> cmdarray) {
187         try {
188             Process p
189                     = Runtime.getRuntime().exec(cmdarray.elementAt(0) + " -version");
190             if (p.waitFor() == 0) {
191                 return true;
192             }
193         } catch (Exception e) {}
194         return false;
195     }
196 
197     /** Create java property option */
createPropertyString(String key)198     private static String createPropertyString(String key) {
199         String property = System.getProperty(key);
200         if (property == null) {
201             throw new RuntimeException("Property " + key + " is not defined");
202         }
203         return "-D" + key + "=" + property + "";
204     }
205 
206 }
207