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 23, 2015
1 parent
a06c825
commit 55d36e5
Showing
3 changed files
with
102 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
8 changes: 8 additions & 0 deletions
8
server/src/main/java/com/kritsit/casetracker/server/domain/services/IUpdateService.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,8 @@ | ||
package com.kritsit.casetracker.server.domain.services; | ||
|
||
import java.io.IOException; | ||
|
||
public interface IUpdateService { | ||
boolean isUpdateRequired(String currentVersion) throws IOException; | ||
byte[] getUpdate() throws IOException; | ||
} |
65 changes: 65 additions & 0 deletions
65
server/src/main/java/com/kritsit/casetracker/server/domain/services/Updater.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,65 @@ | ||
package com.kritsit.casetracker.server.domain.services; | ||
|
||
import com.kritsit.casetracker.shared.domain.FileSerializer; | ||
|
||
import org.slf4j.LoggerFactory; | ||
import org.slf4j.Logger; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.InputStreamReader; | ||
import java.io.IOException; | ||
|
||
public class Updater implements IUpdateService { | ||
private final Logger logger = LoggerFactory.getLogger(Updater.class); | ||
private File root; | ||
|
||
public Updater() { | ||
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); | ||
return !currentVersion.equals(requiredVersion); | ||
} | ||
|
||
public byte[] getUpdate() throws IOException { | ||
File client = findClientArchive(); | ||
FileSerializer serializer = new FileSerializer(); | ||
return serializer.serialize(client); | ||
} | ||
|
||
private File findClientArchive() throws FileNotFoundException { | ||
logger.debug("Finding client archive..."); | ||
File[] files = root.listFiles(); | ||
for (File file : files) { | ||
String fileName = file.getName(); | ||
if (fileName.contains("client") && fileName.endsWith(".jar")) { | ||
return file; | ||
} | ||
} | ||
throw new FileNotFoundException("Unable to find client archive in " + | ||
root.getAbsolutePath()); | ||
} | ||
|
||
private String getLatestVersion(File client) throws IOException { | ||
logger.debug("Running client to get version..."); | ||
Process clientProcess = new ProcessBuilder("java", "-jar", | ||
client.getAbsolutePath(), "--version").start(); | ||
InputStreamReader inReader = | ||
new InputStreamReader(clientProcess.getInputStream()); | ||
BufferedReader in = new BufferedReader(inReader); | ||
String line; | ||
while ((line = in.readLine()) != null) { | ||
logger.debug("Output: {}", line); | ||
if (line.contains("Version: ")) { | ||
return line.replace("Version: ", ""); | ||
} | ||
} | ||
throw new IOException("Unable to find version of client archive " + | ||
client.getAbsolutePath()); | ||
} | ||
} |