*/
public static void updateFromInternet() {
long startTime = new Date().getTime();
boolean isErrorOccured = false;
try {
final Config conf = Config.getConfig();
// Check if auto update is wished at all.
final String autoUpdateActivated = conf.get("AutoUpdate.activate", "false");
if (!autoUpdateActivated.equalsIgnoreCase("true")) {
return;
}
// Make proxy settings.
final String httpProxy = conf.get("AutoUpdate.proxy", null);
if (null != httpProxy) {
int dot = httpProxy.indexOf(":");
if (-1 == dot) {
System.setProperty("http.proxyHost", httpProxy);
}
else {
System.setProperty("http.proxyHost", httpProxy.substring(0, dot));
System.setProperty("http.proxyPort", httpProxy.substring(dot + 1));
}
}
// Access old jar file.
String classPath = System.getProperty("java.class.path");
String separator = System.getProperty("path.separator").toString().substring(0, 1);
StringTokenizer classPathTokenizer = new StringTokenizer(classPath, separator);
String jarFileName = null;
while (null == jarFileName && classPathTokenizer.hasMoreTokens()) {
jarFileName = classPathTokenizer.nextToken();
if (-1 == jarFileName.indexOf("agentopia.jar")) {
jarFileName = null;
}
}
if (null == jarFileName) {
Logger.getLogger().warn("Could not find agentopia.jar in classpath. Update failed.");
return;
}
DataInputStream jarIn = new DataInputStream(new FileInputStream(jarFileName));
int jarByteCount = jarIn.available();
if (jarByteCount <= 0) {
Logger.getLogger().warn("Could not open agentopia.jar. Aborting.");
return;
}
jarIn.close();
// Determine internet location.
String baseUrl = conf.get("AutoUpdate.download", DEFAULT_URL);
if (!baseUrl.endsWith("/")) {
baseUrl += "/";
}
// Read version information.
URL versionUrl = new URL(baseUrl + "version");
BufferedReader versionIn = new BufferedReader(new InputStreamReader(versionUrl.openStream()));
String versionInfo = versionIn.readLine();
String fileSizeInfo = versionIn.readLine();
versionIn.close();
final int currentVersion = conf.getInt("AutoUpdate.version", 0);
final int remoteVersion = Integer.parseInt(versionInfo);
final int remoteFileSize = Integer.parseInt(fileSizeInfo);
if (currentVersion >= remoteVersion) {
if (AgentopiaConstants.PERFORMANCE_DEBUG) {
Logger.getLogger().info("Internet auto update check took " + (System.currentTimeMillis() - startTime) + " millies.");
}
return;
}
// Read new jar file.
URL jarUrl = new URL(baseUrl + "agentopia.jar");
byte[] jarBytes = new byte[remoteFileSize];
jarIn = new DataInputStream(new BufferedInputStream(jarUrl.openStream()));
jarIn.readFully(jarBytes);
jarIn.close();
DataOutputStream jarOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(jarFileName)));
jarOut.write(jarBytes);
jarOut.flush();
jarOut.close();
conf.putInt("AutoUpdate.version", remoteVersion);
// Say you gotta restart.
if (GUI.isGraphicAvailable()) {
javax.swing.JOptionPane.showMessageDialog(null, "Update complete (from version " + currentVersion + " to version " + remoteVersion + "). Please restart. Sorry for the inconvenience.");
}
else {
Logger.getLogger().info("Update complete. Version is now " + remoteVersion + ".\nPlease restart. Sorry for the inconvenience.");
}
// Return.
conf.save();
System.exit(0);
}
catch (Exception exc) {
isErrorOccured = true;
Logger.getLogger().warn(exc, "Internet update had an exception.");