KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > distr > grid > util > GridKit


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 JavaDoc ensureDirectory(String JavaDoc a_currentDir, String JavaDoc a_subDir,
10                                        String JavaDoc a_descr)
11       throws IOException {
12     if (a_currentDir == null || a_currentDir.length() < 1) {
13       String JavaDoc currentDir = FileKit.getCurrentDir();
14       // Set workdir, create it if it does not exist.
15
// --------------------------------------------
16
String JavaDoc 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 JavaDoc("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 JavaDoc a_url)
34       throws Exception JavaDoc {
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 JavaDoc BASE_URL,
45       String JavaDoc a_moduleName,
46       String JavaDoc currentVersion)
47       throws Exception JavaDoc {
48     VersionInfo result;
49     String JavaDoc 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 JavaDoc addURLParameter(String JavaDoc a_requestURL, String JavaDoc a_key,
58                                        long a_value) {
59     String JavaDoc result = a_requestURL + "&" + a_key + "=" + a_value;
60     return result;
61   }
62
63   public static String JavaDoc addURLParameter(String JavaDoc a_requestURL, String JavaDoc a_key,
64                                        String JavaDoc a_value) {
65     String JavaDoc result = a_requestURL + "&" + a_key + "=" + a_value;
66     return result;
67   }
68
69   public static String JavaDoc retrieveModule(String JavaDoc BASE_URL,
70                                       VersionInfo a_versionInfo,
71                                       String JavaDoc a_destDir)
72       throws Exception JavaDoc {
73     String JavaDoc 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 JavaDoc a_filename, String JavaDoc a_workDir,
81                                   String JavaDoc a_libDir)
82       throws Exception JavaDoc {
83     String JavaDoc libDir = a_libDir;
84     // Copy module to new location.
85
// ----------------------------
86
String JavaDoc sourceFileName = a_workDir + a_filename;
87     FileKit.copyFile(sourceFileName, libDir);
88     // Delete file in workdir.
89
// -----------------------
90
if (!FileKit.deleteFile(sourceFileName)) {
91       /**@todo write log: delete manually*/
92     }
93   }
94
95   public static boolean getFile(String JavaDoc BASE_URL, String JavaDoc a_sourceFilename,
96                                 String JavaDoc a_targetDir)
97       throws Exception JavaDoc {
98     String JavaDoc filename = FileKit.getFilename(a_sourceFilename);
99     String JavaDoc destFilename = a_targetDir + filename;
100     long offset;
101     // If file already exists, determine start offset
102
File f = new File(destFilename);
103     if (f.exists()) {
104       offset = f.length();
105     }
106     else {
107       offset = 0;
108     }
109     String JavaDoc a_url = BASE_URL + "download=";
110     String JavaDoc requestURL = a_url + a_sourceFilename;
111     // Include start offset into request
112
requestURL = GridKit.addURLParameter(requestURL, "offset", offset);
113     URL url1 = new URL(requestURL);
114     URL url = new URL(url1.toExternalForm());
115     // Open connection
116
HttpURLConnection con = (HttpURLConnection) url.openConnection();
117     // Configure connection
118
con.setUseCaches(false);
119 // con.setDoInput(true);
120
con.setDoOutput(true);
121     // Receive result
122
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 // BufferedInputStream ois = new BufferedInputStream(sis, 1024 * 5);
134
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 // Thread.sleep(50);
158
} while (true);
159     } catch (SocketException sex) {
160       System.err.println("Connection to server lost"
161                          + " - file transfer interrupted (resum possible)");
162       sex.printStackTrace();
163       // Close file being able to resume it later
164
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 JavaDoc BASE_URL, String JavaDoc a_moduleName,
173       String JavaDoc a_libDir,
174       String JavaDoc a_workDir)
175       throws Exception JavaDoc {
176     String JavaDoc currentVersion;
177     String JavaDoc JGAPVersionNeeded;
178     String JavaDoc filename;
179     boolean isCoreModule;
180     if (a_moduleName.equals("evolutionDistributed")) {
181       /**@todo zuordnung irgendwo abspeichern*/
182       filename = "evdistr.jar";
183       isCoreModule = false;
184     }
185     else if (a_moduleName.equals("jgap")) {
186       /**@todo zuordnung irgendwo abspeichern*/
187       filename = "jgap.jar";
188       isCoreModule = true;
189     }
190     else {
191       throw new IllegalArgumentException JavaDoc("Unknown module " + a_moduleName);
192     }
193     filename = a_libDir + filename;
194     // Determine if file available locally
195
if (!FileKit.existsFile(filename)) {
196       currentVersion = "none";
197       System.out.println(" File not found: " + filename);
198     }
199     else {
200       // If file exists: determine version of module
201
if (isCoreModule) {
202         currentVersion = FileKit.getVersionOfJGAP(filename);
203       }
204       else {
205         currentVersion = FileKit.getVersionOfModule(filename);
206         // Also determine minimal version of JGAP library that is
207
// required for the module to work.
208
// -------------------------------------------------------
209
JGAPVersionNeeded = FileKit.getVersionOfJGAP(filename);
210       }
211     }
212     // ask server if update required
213
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 JavaDoc workDir = a_workDir;
224       // Download the newest module version
225
filename = GridKit.retrieveModule(BASE_URL, versionInfo, workDir);
226       if (filename != null) {
227         // Do the update
228
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 JavaDoc BASE_URL, String JavaDoc a_libDir,
238                                        String JavaDoc a_workDir)
239       throws Exception JavaDoc {
240     updateModuleLibrary(BASE_URL, "jgap", a_libDir, a_workDir);
241   }
242 }
243
Popular Tags