Skip to content

Commit

Permalink
optimized and adjustments to configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
ezTxmMC committed Dec 10, 2024
1 parent 96e9a7d commit 21b53ea
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public SmoothCloudNode() {
System.out.println("DEBUG: Before console.start()");
manager.startTask("Console", this::startConsole);
print(console);
this.launchConfiguration = new LaunchConfiguration();
}

private void startConsole() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package eu.smoothcloud.node.configuration;

import org.json.JSONObject;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Comparator;

public interface JsonSerializable {

default JSONObject toJSONObject() {
JSONObject jsonObject = new JSONObject();
var fields = this.getClass().getDeclaredFields();
Arrays.sort(fields, Comparator.comparing(Field::getName));
for (var field : fields) {
field.setAccessible(true);
try {
jsonObject.put(field.getName(), field.get(this));
} catch (IllegalAccessException e) {
throw new RuntimeException("Fehler beim Zugriff auf das Feld: " + field.getName(), e);
}
}
return jsonObject;
}

default void saveToFile(String path, String fileName) {
try {
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
FileWriter writer = new FileWriter(new File(file, fileName));
writer.write(this.toJSONObject().toString(4));
writer.close();
} catch (IOException e) {
throw new RuntimeException("Fehler beim Speichern der Datei: " + fileName, e);
}
}

static <T extends JsonSerializable> T loadFromFile(String path, String fileName, Class<T> clazz) {
try {
String filePath = Paths.get(path, fileName).toString();
String content = Files.readString(Paths.get(filePath));
JSONObject jsonObject = new JSONObject(content);
return fromJSONObject(jsonObject, clazz);
} catch (IOException e) {
throw new RuntimeException("Fehler beim Lesen der Datei: " + fileName, e);
}
}

static <T extends JsonSerializable> T fromJSONObject(JSONObject jsonObject, Class<T> clazz) {
try {
T instance = clazz.getDeclaredConstructor().newInstance();
var fields = clazz.getDeclaredFields();
for (var field : fields) {
field.setAccessible(true);
if (jsonObject.has(field.getName())) {
Object value = jsonObject.get(field.getName());
field.set(instance, value);
}
}
return instance;
} catch (Exception e) {
throw new RuntimeException("Fehler beim Deserialisieren von JSON", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,63 +1,15 @@
package eu.smoothcloud.node.configuration;

import org.json.JSONException;
import org.json.JSONObject;

public class LaunchConfiguration {
private final JSONObject jsonObject;

public LaunchConfiguration() {
this.jsonObject = new JSONObject("launcher.json");
}

public void setLanguage(String language) {
this.setValue("language", language);
}

public void setHost(String host) {
this.setValue("host", host);
}

public void setPort(int port) {
this.setValue("port", port);
}

public void setMemory(String memory) {
this.setValue("memory", memory);
}

public String getLanguage() {
return (String) this.getValue("language");
}

public String getHost() {
return (String) this.getValue("host");
}

public int getPort() {
return (int) this.getValue("port");
}

public String getMemory() {
return (String) this.getValue("memory");
}

private boolean setValue(String key, Object value) {
try {
this.jsonObject.put(key, value);
return true;
} catch (JSONException e) {
e.printStackTrace();
return false;
}
}

private Object getValue(String key) {
try {
return this.jsonObject.get(key);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class LaunchConfiguration implements JsonSerializable {
private String language;
private String host;
private int port;
private int memory;

public LaunchConfiguration() {}
}

0 comments on commit 21b53ea

Please sign in to comment.