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

Commit

Permalink
made update user accessable
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Robertze committed Oct 23, 2015
1 parent 55d36e5 commit 11f02c9
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public interface IMenuService {
void changePasswordFrame();
int changePassword(String username, int currentHashedPass, int newHashedPass);
void updateFrame();
void closeConnection();
void restart();
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.kritsit.casetracker.client.domain.services;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.List;

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

import com.kritsit.casetracker.client.domain.ui.controller.ChangePasswordController;
import com.kritsit.casetracker.client.domain.ui.controller.UpdateController;
import com.kritsit.casetracker.shared.domain.model.Staff;

import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.List;

public class Menu implements IMenuService {

Expand All @@ -28,7 +29,6 @@ public Menu(Staff user, IConnectionService connectionService){
}

public void changePasswordFrame() {

ChangePasswordController c = new ChangePasswordController(user, this);
AnchorPane ChangePasswordPane = null;
FXMLLoader fxmlLoader = new FXMLLoader(getClass()
Expand All @@ -37,17 +37,16 @@ public void changePasswordFrame() {
fxmlLoader.setController(c);
fxmlLoader.setRoot(ChangePasswordPane);

try{
try {
ChangePasswordPane= (AnchorPane) fxmlLoader.load();
}
catch(IOException e){
logger.error("Error loading frame to change password " + e);
} catch(IOException e) {
logger.error("Error loading frame to change password.", e);
return;
}

Scene scene = new Scene(ChangePasswordPane);
Stage stage = new Stage();
stage.setTitle("Changing password for "+user.getUsername());
stage.setTitle("Changing password for " + user.getUsername());
stage.setResizable(false);
stage.setScene(scene);
stage.show();
Expand All @@ -67,6 +66,33 @@ public int changePassword(String username, int currentHashedPass, int newHashedP
}
}

public void updateFrame() {
UpdateController controller = new UpdateController();
IUpdateService updater = new Updater(connectionService, controller);
controller.setUpdateService(updater);
BorderPane updateFrame = null;
FXMLLoader fxmlLoader = new FXMLLoader(getClass()
.getResource("/ui/fxml/UpdateFrame.fxml"));

fxmlLoader.setController(controller);
fxmlLoader.setRoot(updateFrame);

try {
updateFrame = (BorderPane) fxmlLoader.load();
} catch(IOException e) {
logger.error("Error loading updater frame.", e);
return;
}

Scene scene = new Scene(updateFrame);
Stage stage = new Stage();
stage.setTitle("Update Checker");
stage.setResizable(false);
stage.setScene(scene);
controller.setStage(stage);
stage.showAndWait();
}

public void closeConnection() {
try {
connectionService.close();
Expand All @@ -77,42 +103,42 @@ public void closeConnection() {

public void restart() {
closeConnection();
try {
String java = System.getProperty("java.home") + "/bin/java";
List<String> vmArguments = ManagementFactory.getRuntimeMXBean()
.getInputArguments();
StringBuffer vmArgsOneLine = new StringBuffer();
for (String arg : vmArguments) {
if (!arg.contains("-agentlib")) {
vmArgsOneLine.append(arg);
vmArgsOneLine.append(" ");
}
}
final StringBuffer cmd = new StringBuffer(java + " " + vmArgsOneLine);
String[] mainCommand = System.getProperty("sun.java.command").split(" ");
if (mainCommand[0].endsWith(".jar")) {
cmd.append("-jar " + new File(mainCommand[0]).getPath());
} else {
cmd.append("-cp \"" + System.getProperty("java.class.path")
+ "\"" + mainCommand[0]);
}
for (int i = 1; i < mainCommand.length; i++) {
cmd.append(" ");
cmd.append(mainCommand[i]);
}
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
Runtime.getRuntime().exec(cmd.toString());
} catch (IOException e) {
logger.error("Unable to start application", e);
}
}
});
System.exit(0);
} catch (Exception e) {
logger.error("Unable to restart the application", e);
}
try {
String java = System.getProperty("java.home") + "/bin/java";
List<String> vmArguments = ManagementFactory.getRuntimeMXBean()
.getInputArguments();
StringBuffer vmArgsOneLine = new StringBuffer();
for (String arg : vmArguments) {
if (!arg.contains("-agentlib")) {
vmArgsOneLine.append(arg);
vmArgsOneLine.append(" ");
}
}
final StringBuffer cmd = new StringBuffer(java + " " + vmArgsOneLine);
String[] mainCommand = System.getProperty("sun.java.command").split(" ");
if (mainCommand[0].endsWith(".jar")) {
cmd.append("-jar " + new File(mainCommand[0]).getPath());
} else {
cmd.append("-cp \"" + System.getProperty("java.class.path")
+ "\"" + mainCommand[0]);
}
for (int i = 1; i < mainCommand.length; i++) {
cmd.append(" ");
cmd.append(mainCommand[i]);
}
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
Runtime.getRuntime().exec(cmd.toString());
} catch (IOException e) {
logger.error("Unable to start application", e);
}
}
});
System.exit(0);
} catch (Exception e) {
logger.error("Unable to restart the application", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kritsit.casetracker.client.domain.services;

import com.kritsit.casetracker.client.CaseTrackerClient;
import com.kritsit.casetracker.client.domain.ui.controller.UpdateController;
import com.kritsit.casetracker.shared.domain.FileSerializer;

import org.slf4j.LoggerFactory;
Expand All @@ -21,9 +22,11 @@ public class Updater implements IUpdateService {
private final Logger logger = LoggerFactory.getLogger(Updater.class);
private IConnectionService connection;
private File root;
private UpdateController controller;

public Updater(IConnectionService connection) {
public Updater(IConnectionService connection, UpdateController controller) {
this.connection = connection;
this.controller = controller;
root = new File("update");
}

Expand All @@ -37,6 +40,7 @@ public File update() throws IOException {
ZipFile updateZip = downloadUpdate();
unzip(updateZip);
copyDirectory(root, new File(""));
cleanUp();
return getJar(new File(""));
}

Expand All @@ -48,6 +52,7 @@ public void launch(File jar) throws IOException {

private ZipFile downloadUpdate() throws IOException {
logger.info("Downloading update...");
controller.setStatus("Downloading client from the server...");
FileSerializer serializer = new FileSerializer();
File updateZip = new File("update.zip");
byte[] updateContent = connection.getUpdate();
Expand All @@ -56,17 +61,20 @@ private ZipFile downloadUpdate() throws IOException {
}
serializer.write(updateZip, updateContent);
logger.debug("Update downloaded");
controller.setStatus("Update downloaded");
return new ZipFile(updateZip);
}

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

while (e.hasMoreElements()) {
ZipEntry entry = (ZipEntry) e.nextElement();
logger.debug("Extracting {}", entry.toString());
controller.setStatus("Extracting " + entry.toString() + "...");
if (entry.isDirectory()) {
new File(root, entry.getName()).mkdir();
} else {
Expand All @@ -90,24 +98,48 @@ private void unzip(ZipFile file) throws IOException {
}
}
logger.debug("Update unzipped");
controller.setStatus("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 {
logger.info("Copying file: {}", file.getAbsolutePath());
controller.setStatus("Copying file: " + file.getAbsolutePath());
Files.copy(file.toPath(), destFile.toPath(),
StandardCopyOption.REPLACE_EXISTING);
}
}
}

private void cleanUp() throws IOException {
logger.info("Performing clean up...");
controller.setStatus("Performing clean up...");
File update = new File("update.zip");
update.delete();
empty(root);
root.delete();
logger.debug("Clean up completed");
controller.setStatus("Clean up completed.");
}

private void empty(File directory) throws IOException {
File[] files = directory.listFiles();
for (File file : files) {
if (file.isDirectory()) {
empty(file);
} else {
file.delete();
}
}
}

private File getJar(File root) {
logger.debug("Finding jar...");
File[] files = root.listFiles();
Expand Down
Loading

0 comments on commit 11f02c9

Please sign in to comment.