[New] Added StrolchAgent.toJson()

This commit is contained in:
Robert von Burg 2023-07-28 11:24:41 +02:00
parent 2de45d2c36
commit a85e1e44a1
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
5 changed files with 104 additions and 8 deletions

View File

@ -485,4 +485,17 @@ public class StrolchAgent {
// this.strolchConfiguration = new StrolchConfiguration(newStrolchConfig.getRuntimeConfiguration(),
// configurationsByComponent);
}
public JsonObject toJson() {
JsonObject agentJ = getStrolchConfiguration().getRuntimeConfiguration().toJson();
JsonArray componentsJ = new JsonArray();
List<StrolchComponent> components = getComponentsOrderedByRoot();
for (StrolchComponent component : components) {
componentsJ.add(component.toJson());
}
agentJ.add(COMPONENTS, componentsJ);
return agentJ;
}
}

View File

@ -125,6 +125,17 @@ public class StrolchComponent {
return this.configuration;
}
/**
* Returns a JSON representation of this component's configuration and state
*
* @return a JSON representation of this component
*/
public JsonObject toJson() {
JsonObject componentJ = this.configuration.toJson();
componentJ.addProperty(Tags.Json.STATE, this.state.name());
return componentJ;
}
/**
* Return the {@link ExecutorService} instantiated for this agent
*

View File

@ -239,4 +239,41 @@ public abstract class AbstractionConfiguration {
throw new StrolchConfigurationException(msg);
}
}
public JsonObject toJson() {
JsonObject componentJ = new JsonObject();
componentJ.addProperty(Tags.Json.NAME, this.name);
Map<String, JsonObject> propertiesMap = new HashMap<>();
for (String key : this.configurationValues.keySet()) {
JsonObject propertyJ = new JsonObject();
propertyJ.addProperty(Tags.Json.KEY, key);
propertyJ.addProperty(Tags.Json.VALUE, this.configurationValues.get(key));
propertyJ.addProperty(Tags.Json.UNUSED, true);
propertiesMap.put(key, propertyJ);
}
for (String key : this.defaultValues.keySet()) {
JsonObject propertyJ = propertiesMap.computeIfAbsent(key, s -> {
JsonObject p = new JsonObject();
p.addProperty(Tags.Json.KEY, key);
return p;
});
propertyJ.addProperty(Tags.Json.UNUSED, false);
propertyJ.addProperty(Tags.Json.DEFAULT_VALUE, this.defaultValues.get(key));
String type = this.valueTypes.get(key);
if (type.equals(SECRET))
propertyJ.addProperty(Tags.Json.VALUE, "***");
propertyJ.addProperty(Tags.Json.TYPE, type);
}
JsonArray propertiesJ = propertiesMap.values().stream()
.sorted(comparing(e -> e.get(Tags.Json.KEY).getAsString()))
.collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
componentJ.add(Tags.Json.PROPERTIES, propertiesJ);
return componentJ;
}
}

View File

@ -15,6 +15,10 @@
*/
package li.strolch.runtime.configuration;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import li.strolch.model.Tags;
import java.io.File;
import java.util.Map;
import java.util.Set;
@ -74,11 +78,22 @@ public class ComponentConfiguration extends AbstractionConfiguration {
return getDataDir(key, defValue, true);
if (!pathF.exists() || !pathF.isDirectory() || (writeable ? !pathF.canWrite() : !pathF.canRead()))
throw new IllegalStateException("The path " + path + " for key " + key + " is not a directory or " + (
writeable ?
"writeable" :
"readable") + "!");
throw new IllegalStateException("The path " + path + " for key " + key + " is not a directory or " +
(writeable ? "writeable" : "readable") + "!");
return pathF;
}
@Override
public JsonObject toJson() {
JsonObject componentJ = super.toJson();
componentJ.addProperty(ConfigurationTags.API, this.api);
componentJ.addProperty(ConfigurationTags.IMPL, this.impl);
componentJ.add(Tags.Json.DEPENDENCIES,
this.dependencies.stream().collect(JsonArray::new, JsonArray::add, JsonArray::addAll));
return componentJ;
}
}

View File

@ -15,6 +15,10 @@
*/
package li.strolch.runtime.configuration;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import li.strolch.model.Tags;
import java.io.File;
import java.text.MessageFormat;
import java.util.Locale;
@ -68,7 +72,7 @@ public class RuntimeConfiguration extends AbstractionConfiguration {
this.dataPath = dataPathF;
this.tempPath = tempPathF;
this.locale = new Locale(getString(PROP_LOCALE, Locale.getDefault().toLanguageTag()));
this.locale = Locale.forLanguageTag(getString(PROP_LOCALE, Locale.getDefault().toLanguageTag()));
this.supportedLanguages = supportedLanguages;
}
@ -117,7 +121,7 @@ public class RuntimeConfiguration extends AbstractionConfiguration {
public File getConfigFile(String context, String fileName, boolean checkExists) {
File configFile = new File(getConfigPath(), fileName);
if (checkExists && (!configFile.isFile() || !configFile.canRead())) {
String msg = "[{0}] requires config file which does not exist with name: {1}";
String msg = "[{0}] requires config file from component {1} which does not exist with name: {2}";
msg = MessageFormat.format(msg, getName(), context, fileName);
throw new StrolchConfigurationException(msg);
}
@ -137,7 +141,7 @@ public class RuntimeConfiguration extends AbstractionConfiguration {
public File getDataFile(String context, String fileName, boolean checkExists) {
File dataFile = new File(getDataPath(), fileName);
if (checkExists && (!dataFile.isFile() || !dataFile.canRead())) {
String msg = "[{0}] requires data file which does not exist with name: {1}";
String msg = "[{0}] requires data file from component {1} which does not exist with name: {2}";
msg = MessageFormat.format(msg, getName(), context, fileName);
throw new StrolchConfigurationException(msg);
}
@ -157,10 +161,26 @@ public class RuntimeConfiguration extends AbstractionConfiguration {
public File getDataDir(String context, String dirName, boolean checkExists) {
File dataDir = new File(getDataPath(), dirName);
if (checkExists && (!dataDir.isDirectory() || !dataDir.canRead())) {
String msg = "[{0}] requires data directory which does not exist with name: {1}";
String msg = "[{0}] requires data directory from component {1} which does not exist with name: {2}";
msg = MessageFormat.format(msg, getName(), context, dirName);
throw new StrolchConfigurationException(msg);
}
return dataDir;
}
@Override
public JsonObject toJson() {
JsonObject runtimeJ = super.toJson();
runtimeJ.addProperty(Tags.Json.APPLICATION_NAME, applicationName);
runtimeJ.addProperty(Tags.Json.ENVIRONMENT, environment);
runtimeJ.addProperty(Tags.Json.CONFIG_PATH, configPath.getAbsolutePath());
runtimeJ.addProperty(Tags.Json.DATA_PATH, dataPath.getAbsolutePath());
runtimeJ.addProperty(Tags.Json.TEMP_PATH, tempPath.getAbsolutePath());
runtimeJ.addProperty(Tags.Json.LOCALE, locale.toLanguageTag());
runtimeJ.add(Tags.Json.SUPPORTED_LANGUAGES, supportedLanguages.stream().map(SupportedLanguage::name)
.collect(JsonArray::new, JsonArray::add, JsonArray::addAll));
return runtimeJ;
}
}