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.util.Date; 32 import java.io.File; 33 import java.io.FileInputStream; 34 import java.io.FileOutputStream; 35 import java.io.IOException; 36 import java.util.HashMap; 37 import java.util.Map; 38 39 class LMProperties extends java.util.Properties { LMProperties(String bitsStr, Map<String, String> libraryVersions)40 LMProperties(String bitsStr, Map<String, String> libraryVersions) { 41 bits = bitsStr; 42 path = LibPathIterator.ncbiHome(); 43 44 if (path != null) { 45 path += LibPathIterator.fileSeparator() + "LibManager.properties"; 46 47 try { 48 FileInputStream inStream = new FileInputStream(path); 49 load(inStream); 50 inStream.close(); 51 } catch (IOException e) {} 52 } 53 } 54 setProperty(String key, String value)55 public Object setProperty(String key, String value) { 56 String saved = getProperty(key); 57 if (saved != null && saved.equals(value)) { 58 return saved; 59 } else { 60 dirty = true; 61 return super.setProperty(key, value); 62 } 63 } 64 get(String libname)65 String get(String libname) { 66 return get(libname, Logger.Level.FINE); 67 } 68 cfgFilePath()69 String cfgFilePath() { 70 return path; 71 } 72 setLastSearch(String libname)73 void setLastSearch(String libname) { 74 setProperty(getLibRoot(libname) + "last-search", Long.toString(new Date().getTime())); 75 } 76 getLastSeach(String libname)77 Date getLastSeach(String libname) { 78 String dateLong = getProperty(getLibRoot(libname) + "last-search"); 79 if (dateLong == null) { 80 return null; 81 } 82 83 return new Date(Long.valueOf(dateLong)); 84 } 85 setLatestVersion(String libname, String version)86 void setLatestVersion(String libname, String version) { 87 String node = getLibRoot(libname); 88 setProperty(node + "latest-version/value", version); 89 setProperty(node + "latest-version/updated", Long.toString(new Date().getTime())); 90 } 91 getLatestVersion(String libname, long cacheTrustInterval)92 String getLatestVersion(String libname, long cacheTrustInterval) { 93 String node = getLibRoot(libname); 94 String version = getProperty(node + "latest-version/value"); 95 String dateLong = getProperty(node + "latest-version/updated"); 96 if (dateLong == null || version == null) { 97 return null; 98 } 99 100 if (new Date().getTime() - Long.valueOf(dateLong) > cacheTrustInterval) { 101 remove(node + "latest-version/value"); 102 remove(node + "latest-version/updated"); 103 return null; 104 } 105 106 return version; 107 } 108 notLoaded(String libname)109 void notLoaded(String libname) { 110 String node = getLibRoot(libname); 111 remove(node + "loaded/path"); 112 remove(node + "loaded/version"); 113 remove(node + "last-search"); 114 dirty = true; 115 } 116 loaded(String libname, String version, String path)117 void loaded(String libname, String version, String path) 118 { set(libname, "loaded", version, path); } 119 saved(String libname, String version, String path)120 void saved(String libname, String version, String path) 121 { set(libname, "saved", version, path); } 122 store()123 void store() { 124 try { 125 if (!dirty) { 126 return; 127 } 128 129 File file = new File(cfgFilePath()); 130 File parent = file.getParentFile(); 131 if (parent == null) { 132 Logger.finest 133 ("Cannot find parent directory to store properties"); 134 return; 135 } else if (!parent.exists()) { 136 if (!parent.mkdir()) { 137 Logger.finest("Cannot create " + parent.getName()); 138 return; 139 } 140 parent.setExecutable(false, false); 141 parent.setReadable(false, false); 142 parent.setWritable(false, false); 143 parent.setExecutable(true, true); 144 parent.setReadable(true, true); 145 parent.setWritable(true, true); 146 } 147 FileOutputStream fileOut = new FileOutputStream(file); 148 store(fileOut, null); 149 fileOut.close(); 150 151 dirty = false; 152 } catch (IOException e) { 153 Logger.finest(e); 154 } 155 } 156 157 //////////////////////////////////////////////////////////////////////////////// 158 set(String libname, String name, String version, String path)159 private void set(String libname, String name, String version, 160 String path) 161 { 162 String node = getLibRoot(libname) + name +"/"; 163 164 setProperty(node + "path" , path); 165 setProperty(node + "version", version); 166 } 167 get(String libname, Logger.Level level)168 private String get(String libname, Logger.Level level) { 169 String path = get(libname, "loaded", level); 170 if (path == null) { 171 path = get(libname, "saved", level); 172 } 173 return path; 174 } 175 get(String libname, String name, Logger.Level level)176 private String get 177 (String libname, String name, Logger.Level level) 178 { 179 String node = getLibRoot(libname) + name +"/"; 180 String version = getProperty(node + "version"); 181 if (version != null) { 182 String path = getProperty(node + "path"); 183 if (path != null) { 184 Logger.log(level, "The version of the most recently" 185 + " loaded " + libname + " = " + version); 186 return path; 187 } else { 188 remove(node + "version"); 189 dirty = true; 190 } 191 } 192 return null; 193 } 194 getLibRoot(String libname)195 private String getLibRoot(String libname) { 196 return "/dll/" + libname + "/" + bits +"/"; 197 } 198 199 private String path; 200 private String bits; 201 private boolean dirty; 202 } 203