Skip to content

Commit da2598e

Browse files
committed
Merge branch 'dev'
2 parents a01cdf9 + f7596f2 commit da2598e

11 files changed

Lines changed: 127 additions & 34 deletions

File tree

src/application/RuntimeVariables.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class RuntimeVariables {
77
private static Map<Variable ,Object> variables = new HashMap<>();
88

99
public enum Variable{
10-
INTERFACE_NAME, MAIN_CONTROLLER, NETWORK_CONTROLLER, RECEIVING_IP_ADDRESS, RECEIVED_MESSAGE, CONFIGURATION_CONTROLLER;
10+
INTERFACE_NAME, MAIN_CONTROLLER, NETWORK_CONTROLLER, RECEIVING_IP_ADDRESS, RECEIVED_MESSAGE, CONFIGURATION_CONTROLLER, SCRIPTS_FOLDER;
1111
}
1212

1313
public static void saveVariable(Variable variable, Object value) {
@@ -17,4 +17,8 @@ public static void saveVariable(Variable variable, Object value) {
1717
public static Object getVariable(Variable variable){
1818
return variables.get(variable);
1919
}
20+
21+
public static boolean isRunningFromJar(){
22+
return RuntimeVariables.class.getResource("/icon.png").toString().contains("rsrc:");
23+
}
2024
}

src/controller/MainController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private void setSystemLookAndFeel() throws Exception {
2626
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
2727
}
2828

29-
public void run() throws SocketException {
29+
public void run() throws IOException {
3030
rootWindow = new RootWindow();
3131
interfaceOverview();
3232
}

src/domain/JarExtractor.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package domain;
2+
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.io.IOException;
6+
import java.net.URISyntaxException;
7+
import java.nio.file.Files;
8+
import java.util.Enumeration;
9+
import java.util.jar.JarEntry;
10+
import java.util.jar.JarFile;
11+
12+
import application.RuntimeVariables;
13+
import application.RuntimeVariables.Variable;
14+
15+
/**
16+
* Extracts files from the jar files and
17+
* puts them in the system temp folder
18+
* @author JValck
19+
*
20+
*/
21+
public class JarExtractor {
22+
23+
/**
24+
* Extracts all the files under the path specified by the
25+
* first parameter.
26+
* It will extract them in %TEMP%/pathInJar/
27+
* @param pathInJar The path in the jar file to extract
28+
* @throws IOException
29+
* @throws URISyntaxException
30+
*/
31+
public void extract(String pathInJar) throws IOException{
32+
if(pathInJar.startsWith("/")) pathInJar = pathInJar.substring(1);//remove leading pathSeparator
33+
String path = Files.createTempDirectory(pathInJar).toString();
34+
System.out.println("CREATED:\t"+path);
35+
RuntimeVariables.saveVariable(Variable.SCRIPTS_FOLDER, path);
36+
extractJar(pathInJar, path);
37+
}
38+
39+
private void extractJar(String pathInJar, String path) throws IOException, FileNotFoundException {
40+
JarFile jar = getCurrentJarFile();
41+
Enumeration<JarEntry> enumEntries = jar.entries();
42+
while (enumEntries.hasMoreElements()) {
43+
java.util.jar.JarEntry entry = (java.util.jar.JarEntry) enumEntries.nextElement();
44+
if(!entry.getName().contains(pathInJar)) continue;
45+
java.io.File f = new java.io.File(path + java.io.File.separator + entry.getName());
46+
System.out.println("File: "+entry.getName());
47+
if (entry.isDirectory()) { // if its a directory, create it
48+
f.mkdir();
49+
continue;
50+
}
51+
java.io.InputStream is = jar.getInputStream(entry); // get the input stream
52+
java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
53+
while (is.available() > 0) { // write contents of 'is' to 'fos'
54+
fos.write(is.read());
55+
}
56+
fos.close();
57+
is.close();
58+
}
59+
jar.close();
60+
}
61+
62+
private JarFile getCurrentJarFile() throws IOException {
63+
File classPath = new File(System.getProperty("java.class.path"));
64+
File dir = classPath.getAbsoluteFile();
65+
String jarFile = dir.toString();
66+
System.out.println("Jar file:\t"+jarFile);
67+
return new java.util.jar.JarFile(jarFile);
68+
}
69+
}

src/network/configuration/EthernetInterfaceIPSetter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
public interface EthernetInterfaceIPSetter {
66
void setStaticIP() throws IOException, InterruptedException;
77
void setDynamicIP() throws IOException, InterruptedException;
8+
boolean runningFromJar();
89
}

src/network/configuration/LinuxEthenetInterfaceSetter.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
package network.configuration;
22

3+
import java.io.File;
34
import java.io.IOException;
45

56
import javax.swing.JOptionPane;
67

8+
import application.Launcher;
9+
import application.RuntimeVariables;
10+
import application.RuntimeVariables.Variable;
11+
import domain.JarExtractor;
12+
713
public class LinuxEthenetInterfaceSetter implements EthernetInterfaceIPSetter {
814

915
@Override
1016
public void setStaticIP() throws IOException, InterruptedException {
11-
String path = getClass().getResource("/setIpAddress.pl").toString().replace("file:/", "");
17+
String path = Launcher.class.getResource("/scripts/setIpAddress.pl").toString().replace("file:/", "");
18+
if(runningFromJar()){
19+
JarExtractor extractor = new JarExtractor();
20+
extractor.extract("/scripts");
21+
path = RuntimeVariables.getVariable(Variable.SCRIPTS_FOLDER)+File.separator+"scripts"+File.separator+"setIpAddress.pl";
22+
}
1223
Process p = Runtime.getRuntime().exec(new String[] {"perl", path});
1324
p.waitFor();
1425
}
@@ -19,4 +30,9 @@ public void setDynamicIP() throws IOException, InterruptedException {
1930
+ "Ga naar de instellingen van uw besturingssysteem om DHCP in te schakelen.");
2031
}
2132

33+
@Override
34+
public boolean runningFromJar() {
35+
return RuntimeVariables.isRunningFromJar();
36+
}
37+
2238
}
Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
11
package network.configuration;
22

3+
import java.io.File;
34
import java.io.IOException;
45

6+
import application.Launcher;
7+
import application.RuntimeVariables;
8+
import application.RuntimeVariables.Variable;
9+
import domain.JarExtractor;
10+
511
public class WindowsEthernetInterfaceIPSetter implements
612
EthernetInterfaceIPSetter {
713

814
@Override
915
public void setStaticIP() throws IOException, InterruptedException {
10-
//remove the /scripts prefix, as this part will be ignored in the jar
11-
String path = getClass().getResource("/toStatic.bat").toString().replace("file:/", "");
16+
String path = Launcher.class.getResource("/scripts/toStatic.bat").toString().replace("file:/", "");;
17+
if(runningFromJar()){
18+
JarExtractor extractor = new JarExtractor();
19+
extractor.extract("/scripts");
20+
path = RuntimeVariables.getVariable(Variable.SCRIPTS_FOLDER)+File.separator+"scripts"+File.separator+"toStatic.bat";
21+
}
1222
Process p = Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", path});
1323
p.waitFor();
14-
/*BufferedReader stdInput = new BufferedReader(new
15-
InputStreamReader(p.getInputStream()));
16-
17-
BufferedReader stdError = new BufferedReader(new
18-
InputStreamReader(p.getErrorStream()));
19-
20-
// read the output from the command
21-
System.out.println("Here is the standard output of the command:\n");
22-
String s = null;
23-
while ((s = stdInput.readLine()) != null) {
24-
System.out.println(s);
25-
}
26-
27-
// read any errors from the attempted command
28-
System.out.println("Here is the standard error of the command (if any):\n");
29-
while ((s = stdError.readLine()) != null) {
30-
System.out.println(s);
31-
}*/
32-
33-
3424
}
3525

3626
@Override
37-
public void setDynamicIP() throws IOException, InterruptedException {
38-
//remove the /scripts prefix, as this part will be ignored in the jar
39-
String path = getClass().getResource("/toDHCP.bat").toString().replace("file:/", "");
40-
Process p = Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", path});
41-
p.waitFor();
27+
public void setDynamicIP() throws IOException, InterruptedException {
28+
String path = getClass().getResource("/scripts/toDHCP.bat").toString().replace("file:/", "");
29+
if(runningFromJar()){
30+
JarExtractor extractor = new JarExtractor();
31+
extractor.extract("/scripts");
32+
path = RuntimeVariables.getVariable(Variable.SCRIPTS_FOLDER)+File.separator+"scripts"+File.separator+"toDHCP.bat";
33+
}
34+
System.out.println(path);
35+
Process p = Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", path});
36+
p.waitFor();
4237
}
4338

39+
@Override
40+
public boolean runningFromJar(){
41+
return RuntimeVariables.isRunningFromJar();
42+
}
4443
}

0 commit comments

Comments
 (0)