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

Commit

Permalink
self update client side implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Robertze committed Oct 22, 2015
1 parent 6f20734 commit 793994b
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public interface IConnectionService {
boolean deleteUser(String username);
boolean resetPassword(String username, int hashedRandomPass);
boolean changePassword(String username, int currentHashedPass, int newHashedPass);
String getLatestVersionNumber();
byte[] getUpdate();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.kritsit.casetracker.client.domain.services;

import java.io.File;
import java.io.IOException;

public interface IUpdate {
boolean checkForUpdate();
File update() throws IOException;
void launch(File jar) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,26 @@ public boolean changePassword(String username, int currentHashedPass, int newHas
return false;
}
}

public String getLatestVersionNumber() {
try {
Request request = new Request("getLatestVersionNumber");
Response response = getResponse(request);
return (String) response.getBody();
} catch (IOException | ClassNotFoundException ex) {
logger.error("Unable to get latest version number", ex);
return null;
}
}

public byte[] getUpdate() {
try {
Request request = new Request("getUpdate");
Response response = getResponse(request);
return (byte[]) response.getBody();
} catch (IOException | ClassNotFoundException ex) {
logger.error("Unable to get latest version number", ex);
return new byte[0];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.kritsit.casetracker.client.domain.services;

import com.kritsit.casetracker.client.CaseTrackerClient;
import com.kritsit.casetracker.shared.domain.FileSerializer;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Update implements IUpdate {
private final Logger logger = LoggerFactory.getLogger(Update.class);
private IConnectionService connection;
private File root;

public Update(IConnectionService connection) {
this.connection = connection;
root = new File("update");
}

public boolean checkForUpdate() {
logger.debug("Checking if update is required");
String requiredVersion = connection.getLatestVersionNumber();
logger.info("Latest version: {}", requiredVersion);
String currentVersion = CaseTrackerClient.getVersion();
logger.info("Current version: {}", currentVersion);
return !currentVersion.equals(requiredVersion);
}

public File update() throws IOException {
logger.info("Updating client...");
ZipFile updateZip = downloadUpdate();
unzip(updateZip);
copyDirectory(root, new File(""));
return getJar(new File(""));
}

public void launch(File jar) throws IOException {
String[] run = {"java", "-jar", jar.getAbsolutePath()};
Runtime.getRuntime().exec(run);
restart();
}

private ZipFile downloadUpdate() throws IOException {
logger.info("Downloading update...");
FileSerializer serializer = new FileSerializer();
File updateZip = new File("update.zip");
byte[] updateContent = connection.getUpdate();
if (updateContent.length == 0) {
throw new IOException("Unable to download update from server");
}
serializer.write(updateZip, updateContent);
logger.debug("Update downloaded");
return new ZipFile(updateZip);
}

private void unzip(ZipFile file) throws IOException {
logger.info("Unzipping update...");
Enumeration e = file.entries();
root.mkdir();

while (e.hasMoreElements()) {
ZipEntry entry = (ZipEntry) e.nextElement();
logger.debug("Extracting {}", entry.toString());
if (entry.isDirectory()) {
new File(root, entry.getName()).mkdir();
} else {
File current = new File(root, entry.getName());
current.createNewFile();
int buffer = 2048;
try (
BufferedInputStream in =
new BufferedInputStream(file.getInputStream(entry));
FileOutputStream fos = new FileOutputStream(current);
BufferedOutputStream out = new BufferedOutputStream(fos, buffer);
) {
int count;
byte[] data = new byte[buffer];
while ((count =in.read(data, 0, buffer)) != -1) {
out.write(data, 0, count);
}
out.flush();
}
}
}
logger.debug("Update unzipped");
}

private void copyDirectory(File source, File destination) throws IOException {
logger.info("Copying files...");
File[] files = source.listFiles();
for (File file : files) {
File destFile = new File(destination, file.getName());
if (file.isDirectory()) {
destFile.mkdir();
copyDirectory(file, destFile);
} else {
Files.copy(file.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
}

private File getJar(File root) {
logger.debug("Finding jar...");
File[] files = root.listFiles();
for (File file : files) {
if (file.getName().contains("client") &&
file.getName().endsWith(".jar")) {
logger.debug("Jar found: {}", file);
return file;
}
}
logger.warn("Jar archive not found");
return null;
}

private void restart() throws IOException {
logger.info("Restarting to update");
connection.close();
System.exit(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.kritsit.casetracker.client.domain.services;

import static org.mockito.Mockito.*;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class UpdateTest extends TestCase {

public UpdateTest(String name) {
super(name);
}

public static Test suite() {
return new TestSuite(UpdateTest.class);
}

public void testGetVersion() {
IConnectionService connection = mock(IConnectionService.class);
IUpdate updater = new Update(connection);

when(connection.getLatestVersionNumber()).thenReturn("0.0.0a");

boolean needUpdate = updater.checkForUpdate();

assertTrue(needUpdate);
verify(connection).getLatestVersionNumber();
}
}

0 comments on commit 793994b

Please sign in to comment.