Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
fixed self-updating
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Robertze committed Oct 23, 2015
1 parent 11f02c9 commit 888a48a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public File update() throws IOException {
logger.info("Updating client...");
ZipFile updateZip = downloadUpdate();
unzip(updateZip);
copyDirectory(root, new File(""));
copyDirectory(root, new File("."));
cleanUp();
return getJar(new File(""));
return getJar(new File("."));
}

public void launch(File jar) throws IOException {
Expand Down Expand Up @@ -76,9 +76,9 @@ private void unzip(ZipFile file) throws IOException {
logger.debug("Extracting {}", entry.toString());
controller.setStatus("Extracting " + entry.toString() + "...");
if (entry.isDirectory()) {
new File(root, entry.getName()).mkdir();
new File(entry.getName()).mkdir();
} else {
File current = new File(root, entry.getName());
File current = new File(entry.getName());
current.createNewFile();
int buffer = 2048;
try (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,16 @@ private void updateClient() {
btnUpdate.setDisable(true);
client = updater.update();
setStatus("Client updated, restart required.");
btnUpdate.setText("Restart");
btnUpdate.setDisable(false);
} catch (IOException ex) {
logger.error("Unable to download update", ex);
setStatus("An error occurred. Unable to download update. " + ex.getMessage());
btnUpdate.setDisable(false);
btnUpdate.setText("Check for update");
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Restart");
alert.setHeaderText("Error during restart");
alert.setTitle("Update");
alert.setHeaderText("Error during update");
alert.setContentText("The update could not be downloaded: " + ex.getMessage());
alert.showAndWait();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public byte[] serialize(File f) throws IOException {

public void write(File f, byte[] bytes) throws IOException {
logger.info("Writing to file {}", f.getAbsolutePath());
boolean dirExists = f.mkdirs();
boolean fileExists = f.delete();
if (dirExists && fileExists) {
boolean dirExists = f.getAbsoluteFile().getParentFile().mkdirs();
boolean fileExists = f.createNewFile();
if (fileExists) {
try (FileOutputStream output = new FileOutputStream(f)) {
output.write(bytes);
output.flush();
}
} else {
throw new IOException("Unable to write evidence to disk");
throw new IOException("Unable to write to " + f.getAbsolutePath());
}
}
}
2 changes: 2 additions & 0 deletions server/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ db-port=3306
db-schema=CaseTracker
db-user=CaseTracker
db-password=casetracker

client-version=0.3.1-ALPHA
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
public class Configuration {
private static final Logger logger = LoggerFactory.getLogger(Configuration.class);
private static Map<String, String> database;
private static String clientVersion;

public static String getDbHost() {
return getDatabaseProperty("host");
Expand All @@ -35,6 +36,13 @@ public static String getDbPassword() {
return getDatabaseProperty("password");
}

public static String getClientVersion() {
if (clientVersion == null) {
readConfiguration();
}
return clientVersion;
}

private static String getDatabaseProperty(String key) {
logger.info("Retriving property {}", key);
if (database == null) {
Expand All @@ -56,6 +64,7 @@ private static void readConfiguration() {
database.put("schema", config.getProperty("db-schema"));
database.put("username", config.getProperty("db-user"));
database.put("password", config.getProperty("db-password"));
clientVersion = config.getProperty("client-version");
} catch (NullPointerException | IOException ex) {
logger.error("Unable to read configuration", ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kritsit.casetracker.server.domain.services;

import com.kritsit.casetracker.server.domain.Configuration;
import com.kritsit.casetracker.shared.domain.FileSerializer;

import org.slf4j.LoggerFactory;
Expand All @@ -16,13 +17,12 @@ public class Updater implements IUpdateService {
private File root;

public Updater() {
root = new File("");
root = new File(".");
}

public boolean isUpdateRequired(String currentVersion) throws IOException {
logger.debug("Checking for update for client version {}", currentVersion);
File client = findClientArchive();
String requiredVersion = getLatestVersion(client);
String requiredVersion = Configuration.getClientVersion();
return !currentVersion.equals(requiredVersion);
}

Expand All @@ -37,7 +37,7 @@ private File findClientArchive() throws FileNotFoundException {
File[] files = root.listFiles();
for (File file : files) {
String fileName = file.getName();
if (fileName.contains("client") && fileName.endsWith(".jar")) {
if (fileName.contains("client") && fileName.endsWith(".zip")) {
return file;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public byte[] serialize(File f) throws IOException {

public void write(File f, byte[] bytes) throws IOException {
logger.info("Writing to file {}", f.getAbsolutePath());
boolean dirExists = f.mkdirs();
boolean fileExists = f.delete();
if (dirExists && fileExists) {
boolean dirExists = f.getAbsoluteFile().getParentFile().mkdirs();
boolean fileExists = f.createNewFile();
if (fileExists) {
try (FileOutputStream output = new FileOutputStream(f)) {
output.write(bytes);
output.flush();
}
} else {
throw new IOException("Unable to write evidence to disk");
throw new IOException("Unable to write to " + f.getAbsolutePath());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import java.util.Map;

public class ConfigurationTest extends TestCase {
Map<String, String> database;
String clientVersion;

public ConfigurationTest(String name) {
super(name);
}
Expand All @@ -21,26 +24,29 @@ public static Test suite() {
return new TestSuite(ConfigurationTest.class);
}

public void testDatabaseProperties() throws IOException {
Map<String, String> database = readConfiguration();
assertTrue(database.get("host").equals(Configuration.getDbHost()));
assertTrue(database.get("port").equals("" + Configuration.getDbPort()));
assertTrue(database.get("schema").equals(Configuration.getDbSchema()));
assertTrue(database.get("username").equals(Configuration.getDbUsername()));
assertTrue(database.get("password").equals(Configuration.getDbPassword()));
}

private Map<String, String> readConfiguration() throws IOException {
public void setUp() throws IOException {
Properties config = new Properties();
InputStream in = new FileInputStream(new File("config.properties"));
config.load(in);
in.close();
Map<String, String> database = new HashMap<>();
database = new HashMap<>();
database.put("host", config.getProperty("db-host"));
database.put("port", config.getProperty("db-port"));
database.put("schema", config.getProperty("db-schema"));
database.put("username", config.getProperty("db-user"));
database.put("password", config.getProperty("db-password"));
return database;
clientVersion = config.getProperty("client-version");
}

public void testDatabaseProperties() {
assertTrue(database.get("host").equals(Configuration.getDbHost()));
assertTrue(database.get("port").equals("" + Configuration.getDbPort()));
assertTrue(database.get("schema").equals(Configuration.getDbSchema()));
assertTrue(database.get("username").equals(Configuration.getDbUsername()));
assertTrue(database.get("password").equals(Configuration.getDbPassword()));
}

public void testClientVersion() {
assertTrue(clientVersion.equals(Configuration.getClientVersion()));
}
}

0 comments on commit 888a48a

Please sign in to comment.