-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimized and adjustments to configurations
- Loading branch information
Showing
3 changed files
with
84 additions
and
61 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
72 changes: 72 additions & 0 deletions
72
smoothcloud-node/src/main/java/eu/smoothcloud/node/configuration/JsonSerializable.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,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); | ||
} | ||
} | ||
} |
72 changes: 12 additions & 60 deletions
72
smoothcloud-node/src/main/java/eu/smoothcloud/node/configuration/LaunchConfiguration.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 |
---|---|---|
@@ -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() {} | ||
} |