1 package org.jgap.distr.grid.util; 2 3 import org.jgap.util.*; 4 import java.io.*; 5 import java.net.*; 6 import org.jgap.distr.grid.request.*; 7 8 public class GridKit { 9 public static String ensureDirectory(String a_currentDir, String a_subDir, 10 String a_descr) 11 throws IOException { 12 if (a_currentDir == null || a_currentDir.length() < 1) { 13 String currentDir = FileKit.getCurrentDir(); 14 String workDir = FileKit.addSubDir(currentDir, a_subDir, true); 17 File f = new File(workDir); 18 if (!f.exists()) { 19 if (!f.mkdirs()) { 20 throw new RuntimeException ("Creation of " 21 + a_descr + " " 22 + workDir 23 + " failed!"); 24 } 25 } 26 return workDir; 27 } 28 else { 29 return null; 30 } 31 } 32 33 public static URLConnection getConnection(String a_url) 34 throws Exception { 35 URL url1 = new URL(a_url); 36 URL url = new URL(url1.toExternalForm()); 37 URLConnection con = url.openConnection(); 38 con.setUseCaches(false); 39 con.setDoInput(true); 40 con.setDoOutput(true); 41 return con; 42 } 43 44 public static VersionInfo isUpdateAvailable(String BASE_URL, 45 String a_moduleName, 46 String currentVersion) 47 throws Exception { 48 VersionInfo result; 49 String url = BASE_URL + "getVersion=" + a_moduleName; 50 addURLParameter(url, "version", currentVersion); 51 URLConnection con = GridKit.getConnection(url); 52 ObjectInputStream ois = new ObjectInputStream(con.getInputStream()); 53 result = (VersionInfo) ois.readObject(); 54 return result; 55 } 56 57 public static String addURLParameter(String a_requestURL, String a_key, 58 long a_value) { 59 String result = a_requestURL + "&" + a_key + "=" + a_value; 60 return result; 61 } 62 63 public static String addURLParameter(String a_requestURL, String a_key, 64 String a_value) { 65 String result = a_requestURL + "&" + a_key + "=" + a_value; 66 return result; 67 } 68 69 public static String retrieveModule(String BASE_URL, 70 VersionInfo a_versionInfo, 71 String a_destDir) 72 throws Exception { 73 String filename = a_versionInfo.filenameOfLib; 74 if (!getFile(BASE_URL, filename, a_destDir)) { 75 return null; 76 } 77 return filename; 78 } 79 80 public static void updateModule(String a_filename, String a_workDir, 81 String a_libDir) 82 throws Exception { 83 String libDir = a_libDir; 84 String sourceFileName = a_workDir + a_filename; 87 FileKit.copyFile(sourceFileName, libDir); 88 if (!FileKit.deleteFile(sourceFileName)) { 91 92 } 93 } 94 95 public static boolean getFile(String BASE_URL, String a_sourceFilename, 96 String a_targetDir) 97 throws Exception { 98 String filename = FileKit.getFilename(a_sourceFilename); 99 String destFilename = a_targetDir + filename; 100 long offset; 101 File f = new File(destFilename); 103 if (f.exists()) { 104 offset = f.length(); 105 } 106 else { 107 offset = 0; 108 } 109 String a_url = BASE_URL + "download="; 110 String requestURL = a_url + a_sourceFilename; 111 requestURL = GridKit.addURLParameter(requestURL, "offset", offset); 113 URL url1 = new URL(requestURL); 114 URL url = new URL(url1.toExternalForm()); 115 HttpURLConnection con = (HttpURLConnection) url.openConnection(); 117 con.setUseCaches(false); 119 con.setDoOutput(true); 121 boolean append; 123 if (offset == 0) { 124 append = false; 125 } 126 else { 127 append = true; 128 } 129 InputStream in = con.getInputStream(); 130 ObjectInputStream sis = new ObjectInputStream(new BufferedInputStream(in, 131 1024 * 5)); 132 Status s; 133 FileOutputStream fos = new FileOutputStream(destFilename, append); 135 long currentOffset = 0; 136 try { 137 int loopIndex = 0; 138 do { 139 s = (Status) sis.readObject(); 140 if (s.code == 0) { 141 if (loopIndex == 0) { 142 if (s.buffer == null || s.buffer.length < 1) { 143 System.out.println("File already exists"); 144 return true; 145 } 146 } 147 } 148 else if (s.code < 0) { 149 throw new IOException(s.description); 150 } 151 loopIndex++; 152 fos.write(s.buffer); 153 currentOffset += s.buffer.length; 154 if (s.code == 0) { 155 break; 156 } 157 } while (true); 159 } catch (SocketException sex) { 160 System.err.println("Connection to server lost" 161 + " - file transfer interrupted (resum possible)"); 162 sex.printStackTrace(); 163 fos.close(); 165 return false; 166 } 167 fos.close(); 168 System.out.println("File received: " + destFilename); 169 return true; 170 } 171 172 public static void updateModuleLibrary(String BASE_URL, String a_moduleName, 173 String a_libDir, 174 String a_workDir) 175 throws Exception { 176 String currentVersion; 177 String JGAPVersionNeeded; 178 String filename; 179 boolean isCoreModule; 180 if (a_moduleName.equals("evolutionDistributed")) { 181 182 filename = "evdistr.jar"; 183 isCoreModule = false; 184 } 185 else if (a_moduleName.equals("jgap")) { 186 187 filename = "jgap.jar"; 188 isCoreModule = true; 189 } 190 else { 191 throw new IllegalArgumentException ("Unknown module " + a_moduleName); 192 } 193 filename = a_libDir + filename; 194 if (!FileKit.existsFile(filename)) { 196 currentVersion = "none"; 197 System.out.println(" File not found: " + filename); 198 } 199 else { 200 if (isCoreModule) { 202 currentVersion = FileKit.getVersionOfJGAP(filename); 203 } 204 else { 205 currentVersion = FileKit.getVersionOfModule(filename); 206 JGAPVersionNeeded = FileKit.getVersionOfJGAP(filename); 210 } 211 } 212 VersionInfo versionInfo = GridKit.isUpdateAvailable(BASE_URL, a_moduleName, 214 currentVersion); 215 if (versionInfo.currentVersion == null) { 216 System.out.println("Module " + a_moduleName + " is unknown"); 217 return; 218 } 219 if (!currentVersion.equals(versionInfo.currentVersion)) { 220 System.out.println("Newer module " + a_moduleName + " with version " 221 + versionInfo.currentVersion 222 + " available"); 223 String workDir = a_workDir; 224 filename = GridKit.retrieveModule(BASE_URL, versionInfo, workDir); 226 if (filename != null) { 227 GridKit.updateModule(filename, workDir, a_libDir); 229 } 230 } 231 else { 232 System.out.println("Module " + a_moduleName + " with version " + 233 currentVersion + " is up-to-date"); 234 } 235 } 236 237 public static void updateJGAPLibrary(String BASE_URL, String a_libDir, 238 String a_workDir) 239 throws Exception { 240 updateModuleLibrary(BASE_URL, "jgap", a_libDir, a_workDir); 241 } 242 } 243 | Popular Tags |