This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kyle Robertze
committed
Oct 22, 2015
1 parent
6f20734
commit 793994b
Showing
5 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
client/src/main/java/com/kritsit/casetracker/client/domain/services/IUpdate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
client/src/main/java/com/kritsit/casetracker/client/domain/services/Update.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
client/src/test/java/com/kritsit/casetracker/client/domain/services/UpdateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |