From 563d22443f09ceaa5de80cf4dec1231f2aac658e Mon Sep 17 00:00:00 2001 From: Zhihang Yao Date: Wed, 20 Dec 2023 09:53:07 +0100 Subject: [PATCH 1/5] add new config module --- citydb-cli/build.gradle | 1 + citydb-cli/src/main/java/module-info.java | 1 + .../main/java/org/citydb/cli/Launcher.java | 10 ++ citydb-config/build.gradle | 3 + citydb-config/src/main/java/module-info.java | 8 ++ .../main/java/org/citydb/config/Config.java | 93 +++++++++++++++++ .../org/citydb/config/ConfigException.java | 41 ++++++++ .../java/org/citydb/config/ConfigManager.java | 99 +++++++++++++++++++ .../citydb/config/configs/ExportConfig.java | 68 +++++++++++++ .../citydb/config/configs/ImportConfig.java | 68 +++++++++++++ settings.gradle | 3 +- 11 files changed, 394 insertions(+), 1 deletion(-) create mode 100644 citydb-config/build.gradle create mode 100644 citydb-config/src/main/java/module-info.java create mode 100644 citydb-config/src/main/java/org/citydb/config/Config.java create mode 100644 citydb-config/src/main/java/org/citydb/config/ConfigException.java create mode 100644 citydb-config/src/main/java/org/citydb/config/ConfigManager.java create mode 100644 citydb-config/src/main/java/org/citydb/config/configs/ExportConfig.java create mode 100644 citydb-config/src/main/java/org/citydb/config/configs/ImportConfig.java diff --git a/citydb-cli/build.gradle b/citydb-cli/build.gradle index bf62e464..b4e50d02 100644 --- a/citydb-cli/build.gradle +++ b/citydb-cli/build.gradle @@ -11,6 +11,7 @@ dependencies { implementation project(':citydb-database') implementation project(':citydb-operation') implementation project(':citydb-database-postgres') + api project(':citydb-config') api project(':citydb-plugin') api 'info.picocli:picocli:4.7.5' } diff --git a/citydb-cli/src/main/java/module-info.java b/citydb-cli/src/main/java/module-info.java index cf49a9f8..9f28bf45 100644 --- a/citydb-cli/src/main/java/module-info.java +++ b/citydb-cli/src/main/java/module-info.java @@ -7,6 +7,7 @@ requires org.citydb.operation; requires transitive org.citydb.plugin; requires transitive info.picocli; + requires org.citydb.config; exports org.citydb.cli; exports org.citydb.cli.command; diff --git a/citydb-cli/src/main/java/org/citydb/cli/Launcher.java b/citydb-cli/src/main/java/org/citydb/cli/Launcher.java index 78455883..064e5cc4 100644 --- a/citydb-cli/src/main/java/org/citydb/cli/Launcher.java +++ b/citydb-cli/src/main/java/org/citydb/cli/Launcher.java @@ -33,6 +33,7 @@ import org.citydb.cli.util.CliConstants; import org.citydb.cli.util.CommandHelper; import org.citydb.cli.util.PidFile; +import org.citydb.config.ConfigManager; import org.citydb.core.CoreConstants; import org.citydb.logging.LoggerManager; import org.citydb.plugin.Extension; @@ -88,8 +89,13 @@ public class Launcher implements Command, CommandLine.IVersionProvider { "(default: ${MAP-FALLBACK-VALUE}).") private Map usePlugins; + @CommandLine.Option(names = "--config-file", scope = CommandLine.ScopeType.INHERIT, paramLabel = "", + description = "Load configuration from this file.") + private Path configFile; + private final Logger logger = LoggerManager.getInstance().getLogger(); private final PluginManager pluginManager = PluginManager.getInstance(); + private final ConfigManager configManager = ConfigManager.getInstance(); private final CommandHelper helper = CommandHelper.of(logger); private String commandLine; private String subCommandName; @@ -140,6 +146,10 @@ private int execute(String[] args) throws Exception { throw new CommandLine.ParameterException(cmd, "Missing required subcommand."); } + if (configFile != null) { + configManager.load(configFile); + } + if (usePlugins != null) { for (Plugin plugin : pluginManager.getPlugins()) { plugin.setEnabled(usePlugins.getOrDefault(plugin.getClass().getName(), plugin.isEnabled())); diff --git a/citydb-config/build.gradle b/citydb-config/build.gradle new file mode 100644 index 00000000..b1f38cd1 --- /dev/null +++ b/citydb-config/build.gradle @@ -0,0 +1,3 @@ +dependencies { + api project(':citydb-core') +} \ No newline at end of file diff --git a/citydb-config/src/main/java/module-info.java b/citydb-config/src/main/java/module-info.java new file mode 100644 index 00000000..225cd249 --- /dev/null +++ b/citydb-config/src/main/java/module-info.java @@ -0,0 +1,8 @@ +module org.citydb.config { + requires com.alibaba.fastjson2; + + exports org.citydb.config; + exports org.citydb.config.configs; + + opens org.citydb.config to com.alibaba.fastjson2; +} \ No newline at end of file diff --git a/citydb-config/src/main/java/org/citydb/config/Config.java b/citydb-config/src/main/java/org/citydb/config/Config.java new file mode 100644 index 00000000..38537fb3 --- /dev/null +++ b/citydb-config/src/main/java/org/citydb/config/Config.java @@ -0,0 +1,93 @@ +/* + * citydb-tool - Command-line tool for the 3D City Database + * https://www.3dcitydb.org/ + * + * Copyright 2022-2023 + * virtualcitysystems GmbH, Germany + * https://vc.systems/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citydb.config; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.annotation.JSONField; +import org.citydb.config.configs.ExportConfig; +import org.citydb.config.configs.ImportConfig; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +public class Config { + @JSONField(name = "import") + private ImportConfig importConfig; + @JSONField(name = "export") + private ExportConfig exportConfig; + @JSONField(name = "plugins") + private Map pluginsConfig; + + public ImportConfig getImportConfig() { + if (importConfig == null) { + importConfig = new ImportConfig(); + } + + return importConfig; + } + + public Config setImportConfig(ImportConfig importConfig) { + this.importConfig = importConfig; + return this; + } + + public ExportConfig getExportConfig() { + if (exportConfig == null) { + exportConfig = new ExportConfig(); + } + + return exportConfig; + } + + public Config setExportConfig(ExportConfig exportConfig) { + this.exportConfig = exportConfig; + return this; + } + + public Map getPluginsConfig() { + if (pluginsConfig == null) { + pluginsConfig = new HashMap<>(); + } + + return pluginsConfig; + } + + public Config setPluginsConfig(Map pluginsConfig) { + this.pluginsConfig = pluginsConfig; + return this; + } + + public void writeTo(Path target) throws ConfigException { + Objects.requireNonNull(target, "The target path must not be null."); + try { + Files.write(target, JSON.toJSONString(this, JSONWriter.Feature.PrettyFormat).getBytes()); + } catch (IOException e) { + throw new ConfigException("Failed to write config file to " + target, e); + } + } +} diff --git a/citydb-config/src/main/java/org/citydb/config/ConfigException.java b/citydb-config/src/main/java/org/citydb/config/ConfigException.java new file mode 100644 index 00000000..95dda0db --- /dev/null +++ b/citydb-config/src/main/java/org/citydb/config/ConfigException.java @@ -0,0 +1,41 @@ +/* + * citydb-tool - Command-line tool for the 3D City Database + * https://www.3dcitydb.org/ + * + * Copyright 2022-2023 + * virtualcitysystems GmbH, Germany + * https://vc.systems/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citydb.config; + +public class ConfigException extends Exception { + + public ConfigException() { + super(); + } + + public ConfigException(String message) { + super(message); + } + + public ConfigException(Throwable cause) { + super(cause); + } + + public ConfigException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/citydb-config/src/main/java/org/citydb/config/ConfigManager.java b/citydb-config/src/main/java/org/citydb/config/ConfigManager.java new file mode 100644 index 00000000..e3ecf175 --- /dev/null +++ b/citydb-config/src/main/java/org/citydb/config/ConfigManager.java @@ -0,0 +1,99 @@ +/* + * citydb-tool - Command-line tool for the 3D City Database + * https://www.3dcitydb.org/ + * + * Copyright 2022-2023 + * virtualcitysystems GmbH, Germany + * https://vc.systems/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citydb.config; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONException; +import com.alibaba.fastjson2.JSONObject; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Objects; + +public class ConfigManager { + private static final ConfigManager instance = new ConfigManager(); + + private Config config; + + private ConfigManager() { + } + + public static ConfigManager getInstance() { + return instance; + } + + public Config getConfig() { + return config; + } + + public void load(Path path) throws ConfigException { + Objects.requireNonNull(path, "The path must not be null."); + + try (InputStream inStream = new BufferedInputStream(Files.newInputStream(path))) { + config = JSON.parseObject(new String(inStream.readAllBytes()), Config.class); + } catch (IOException e) { + throw new ConfigException("Failed to read the config file " + path + ".", e); + } catch (JSONException e) { + throw new ConfigException("Failed to parse the config file " + path + ".", e); + } + } + + public T getConfig(Class type) throws ConfigException { + JSONObject options = null; + + if (type.getName().equals("org.citydb.operation.importer.ImportOptions")) { + options = config.getImportConfig().getOperationOptions(); + } else if (type.getName().equals("org.citydb.io.reader.ReadOptions")) { + options = config.getImportConfig().getReadOptions(); + } else if (type.getName().equals("org.citydb.io.citygml.reader.CityGMLFormatOptions")) { + options = config.getImportConfig().getFormatOptions().get("CityGML"); + } else if (type.getName().equals("org.citydb.io.citygml.reader.CityJSONFormatOptions")) { + options = config.getImportConfig().getFormatOptions().get("CityJSON"); + } else if (type.getName().equals("org.citydb.operation.exporter.ExportOptions")) { + options = config.getExportConfig().getOperationOptions(); + } else if (type.getName().equals("org.citydb.io.writer.WriteOptions")) { + options = config.getExportConfig().getWriteOptions(); + } else if (type.getName().equals("org.citydb.io.citygml.writer.CityGMLFormatOptions")) { + options = config.getExportConfig().getFormatOptions().get("CityGML"); + } else if (type.getName().equals("org.citydb.io.citygml.writer.CityJSONFormatOptions")) { + options = config.getExportConfig().getFormatOptions().get("CityJSON"); + } + + if (options != null) { + return JSON.parseObject(options.toString(), type); + } else { + throw new ConfigException("Unsupported configuration type '" + type.getName() + "'."); + } + } + + public T getPluginConfig(String pluginName, Class type) { + JSONObject pluginConfig = config.getPluginsConfig().get(pluginName); + if (pluginConfig != null) { + return JSON.parseObject(pluginConfig.toString(), type); + } + + return null; + } +} diff --git a/citydb-config/src/main/java/org/citydb/config/configs/ExportConfig.java b/citydb-config/src/main/java/org/citydb/config/configs/ExportConfig.java new file mode 100644 index 00000000..2e769297 --- /dev/null +++ b/citydb-config/src/main/java/org/citydb/config/configs/ExportConfig.java @@ -0,0 +1,68 @@ +/* + * citydb-tool - Command-line tool for the 3D City Database + * https://www.3dcitydb.org/ + * + * Copyright 2022-2023 + * virtualcitysystems GmbH, Germany + * https://vc.systems/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citydb.config.configs; + +import com.alibaba.fastjson2.JSONObject; +import com.alibaba.fastjson2.annotation.JSONField; + +import java.util.HashMap; +import java.util.Map; + +public class ExportConfig { + @JSONField(name = "writeOptions") + private JSONObject writeOptions; + @JSONField(name = "formatOptions") + private Map formatOptions; + @JSONField(name = "operationOptions") + private JSONObject operationOptions; + + public JSONObject getWriteOptions() { + return writeOptions; + } + + public ExportConfig setWriteOptions(JSONObject options) { + this.writeOptions = options; + return this; + } + + public Map getFormatOptions() { + if (formatOptions == null) { + formatOptions = new HashMap<>(); + } + + return formatOptions; + } + + public ExportConfig setFormatOptions(Map options) { + this.formatOptions = options; + return this; + } + + public JSONObject getOperationOptions() { + return operationOptions; + } + + public ExportConfig setOperationOptions(JSONObject options) { + this.operationOptions = options; + return this; + } +} \ No newline at end of file diff --git a/citydb-config/src/main/java/org/citydb/config/configs/ImportConfig.java b/citydb-config/src/main/java/org/citydb/config/configs/ImportConfig.java new file mode 100644 index 00000000..1ba14ec7 --- /dev/null +++ b/citydb-config/src/main/java/org/citydb/config/configs/ImportConfig.java @@ -0,0 +1,68 @@ +/* + * citydb-tool - Command-line tool for the 3D City Database + * https://www.3dcitydb.org/ + * + * Copyright 2022-2023 + * virtualcitysystems GmbH, Germany + * https://vc.systems/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citydb.config.configs; + +import com.alibaba.fastjson2.JSONObject; +import com.alibaba.fastjson2.annotation.JSONField; + +import java.util.HashMap; +import java.util.Map; + +public class ImportConfig { + @JSONField(name = "readOptions") + private JSONObject readOptions; + @JSONField(name = "formatOptions") + private Map formatOptions; + @JSONField(name = "operationOptions") + private JSONObject operationOptions; + + public JSONObject getReadOptions() { + return readOptions; + } + + public ImportConfig setReadOptions(JSONObject options) { + this.readOptions = options; + return this; + } + + public Map getFormatOptions() { + if (formatOptions == null) { + formatOptions = new HashMap<>(); + } + + return formatOptions; + } + + public ImportConfig setFormatOptions(Map options) { + this.formatOptions = options; + return this; + } + + public JSONObject getOperationOptions() { + return operationOptions; + } + + public ImportConfig setOperationOptions(JSONObject options) { + this.operationOptions = options; + return this; + } +} diff --git a/settings.gradle b/settings.gradle index 46bac54a..cf637580 100644 --- a/settings.gradle +++ b/settings.gradle @@ -11,4 +11,5 @@ include 'citydb-io-citygml' include 'citydb-logging' include 'citydb-model' include 'citydb-operation' -include 'citydb-plugin' \ No newline at end of file +include 'citydb-plugin' +include 'citydb-config' \ No newline at end of file From 34a8a8186364743d9d3fbe9ffec5fb0ea405cd28 Mon Sep 17 00:00:00 2001 From: Zhihang Yao Date: Wed, 20 Dec 2023 14:32:09 +0100 Subject: [PATCH 2/5] remove unnecessary json fields annotation --- .../src/main/java/org/citydb/config/configs/ExportConfig.java | 4 ---- .../src/main/java/org/citydb/config/configs/ImportConfig.java | 4 ---- 2 files changed, 8 deletions(-) diff --git a/citydb-config/src/main/java/org/citydb/config/configs/ExportConfig.java b/citydb-config/src/main/java/org/citydb/config/configs/ExportConfig.java index 2e769297..7ea2bb3f 100644 --- a/citydb-config/src/main/java/org/citydb/config/configs/ExportConfig.java +++ b/citydb-config/src/main/java/org/citydb/config/configs/ExportConfig.java @@ -22,17 +22,13 @@ package org.citydb.config.configs; import com.alibaba.fastjson2.JSONObject; -import com.alibaba.fastjson2.annotation.JSONField; import java.util.HashMap; import java.util.Map; public class ExportConfig { - @JSONField(name = "writeOptions") private JSONObject writeOptions; - @JSONField(name = "formatOptions") private Map formatOptions; - @JSONField(name = "operationOptions") private JSONObject operationOptions; public JSONObject getWriteOptions() { diff --git a/citydb-config/src/main/java/org/citydb/config/configs/ImportConfig.java b/citydb-config/src/main/java/org/citydb/config/configs/ImportConfig.java index 1ba14ec7..7d6e4426 100644 --- a/citydb-config/src/main/java/org/citydb/config/configs/ImportConfig.java +++ b/citydb-config/src/main/java/org/citydb/config/configs/ImportConfig.java @@ -22,17 +22,13 @@ package org.citydb.config.configs; import com.alibaba.fastjson2.JSONObject; -import com.alibaba.fastjson2.annotation.JSONField; import java.util.HashMap; import java.util.Map; public class ImportConfig { - @JSONField(name = "readOptions") private JSONObject readOptions; - @JSONField(name = "formatOptions") private Map formatOptions; - @JSONField(name = "operationOptions") private JSONObject operationOptions; public JSONObject getReadOptions() { From 89932838b0beba3f3e22098cd43ebe75b4be344e Mon Sep 17 00:00:00 2001 From: Zhihang Yao Date: Wed, 20 Dec 2023 15:48:37 +0100 Subject: [PATCH 3/5] review updates --- .../main/java/org/citydb/cli/Launcher.java | 8 ++- citydb-config/src/main/java/module-info.java | 1 - .../main/java/org/citydb/config/Config.java | 66 +++---------------- .../java/org/citydb/config/ConfigManager.java | 52 +++------------ .../citydb/config/configs/ExportConfig.java | 64 ------------------ .../citydb/config/configs/ImportConfig.java | 64 ------------------ 6 files changed, 26 insertions(+), 229 deletions(-) delete mode 100644 citydb-config/src/main/java/org/citydb/config/configs/ExportConfig.java delete mode 100644 citydb-config/src/main/java/org/citydb/config/configs/ImportConfig.java diff --git a/citydb-cli/src/main/java/org/citydb/cli/Launcher.java b/citydb-cli/src/main/java/org/citydb/cli/Launcher.java index 064e5cc4..6cbcb91a 100644 --- a/citydb-cli/src/main/java/org/citydb/cli/Launcher.java +++ b/citydb-cli/src/main/java/org/citydb/cli/Launcher.java @@ -33,6 +33,7 @@ import org.citydb.cli.util.CliConstants; import org.citydb.cli.util.CommandHelper; import org.citydb.cli.util.PidFile; +import org.citydb.config.Config; import org.citydb.config.ConfigManager; import org.citydb.core.CoreConstants; import org.citydb.logging.LoggerManager; @@ -99,6 +100,7 @@ public class Launcher implements Command, CommandLine.IVersionProvider { private final CommandHelper helper = CommandHelper.of(logger); private String commandLine; private String subCommandName; + private Config config; public static void main(String[] args) { Launcher launcher = new Launcher(); @@ -147,7 +149,7 @@ private int execute(String[] args) throws Exception { } if (configFile != null) { - configManager.load(configFile); + config = configManager.load(configFile); } if (usePlugins != null) { @@ -361,6 +363,10 @@ enum LogLevel { } } + public Config getConfig() { + return config; + } + @Override public String[] getVersion() { return new String[]{CliConstants.APP_NAME + ", version " + CliConstants.APP_VERSION, diff --git a/citydb-config/src/main/java/module-info.java b/citydb-config/src/main/java/module-info.java index 225cd249..0ad45dac 100644 --- a/citydb-config/src/main/java/module-info.java +++ b/citydb-config/src/main/java/module-info.java @@ -2,7 +2,6 @@ requires com.alibaba.fastjson2; exports org.citydb.config; - exports org.citydb.config.configs; opens org.citydb.config to com.alibaba.fastjson2; } \ No newline at end of file diff --git a/citydb-config/src/main/java/org/citydb/config/Config.java b/citydb-config/src/main/java/org/citydb/config/Config.java index 38537fb3..4c00dbaa 100644 --- a/citydb-config/src/main/java/org/citydb/config/Config.java +++ b/citydb-config/src/main/java/org/citydb/config/Config.java @@ -23,71 +23,25 @@ import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; -import com.alibaba.fastjson2.JSONWriter; -import com.alibaba.fastjson2.annotation.JSONField; -import org.citydb.config.configs.ExportConfig; -import org.citydb.config.configs.ImportConfig; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.HashMap; import java.util.Map; -import java.util.Objects; public class Config { - @JSONField(name = "import") - private ImportConfig importConfig; - @JSONField(name = "export") - private ExportConfig exportConfig; - @JSONField(name = "plugins") - private Map pluginsConfig; + private Map configs = new HashMap<>(); - public ImportConfig getImportConfig() { - if (importConfig == null) { - importConfig = new ImportConfig(); + public void setConfigs(Map configs) { + if (configs != null) { + this.configs = configs; } - - return importConfig; - } - - public Config setImportConfig(ImportConfig importConfig) { - this.importConfig = importConfig; - return this; - } - - public ExportConfig getExportConfig() { - if (exportConfig == null) { - exportConfig = new ExportConfig(); - } - - return exportConfig; - } - - public Config setExportConfig(ExportConfig exportConfig) { - this.exportConfig = exportConfig; - return this; - } - - public Map getPluginsConfig() { - if (pluginsConfig == null) { - pluginsConfig = new HashMap<>(); - } - - return pluginsConfig; - } - - public Config setPluginsConfig(Map pluginsConfig) { - this.pluginsConfig = pluginsConfig; - return this; } - public void writeTo(Path target) throws ConfigException { - Objects.requireNonNull(target, "The target path must not be null."); - try { - Files.write(target, JSON.toJSONString(this, JSONWriter.Feature.PrettyFormat).getBytes()); - } catch (IOException e) { - throw new ConfigException("Failed to write config file to " + target, e); + public T getConfig(String name, Class type) throws ConfigException { + JSONObject options = configs.get(name); + if (options != null) { + return JSON.parseObject(options.toString(), type); + } else { + throw new ConfigException("Unsupported configuration type '" + type.getName() + "'."); } } } diff --git a/citydb-config/src/main/java/org/citydb/config/ConfigManager.java b/citydb-config/src/main/java/org/citydb/config/ConfigManager.java index e3ecf175..997d18fd 100644 --- a/citydb-config/src/main/java/org/citydb/config/ConfigManager.java +++ b/citydb-config/src/main/java/org/citydb/config/ConfigManager.java @@ -23,7 +23,7 @@ import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONException; -import com.alibaba.fastjson2.JSONObject; +import com.alibaba.fastjson2.JSONWriter; import java.io.BufferedInputStream; import java.io.IOException; @@ -35,8 +35,6 @@ public class ConfigManager { private static final ConfigManager instance = new ConfigManager(); - private Config config; - private ConfigManager() { } @@ -44,15 +42,11 @@ public static ConfigManager getInstance() { return instance; } - public Config getConfig() { - return config; - } - - public void load(Path path) throws ConfigException { + public Config load(Path path) throws ConfigException { Objects.requireNonNull(path, "The path must not be null."); try (InputStream inStream = new BufferedInputStream(Files.newInputStream(path))) { - config = JSON.parseObject(new String(inStream.readAllBytes()), Config.class); + return JSON.parseObject(new String(inStream.readAllBytes()), Config.class); } catch (IOException e) { throw new ConfigException("Failed to read the config file " + path + ".", e); } catch (JSONException e) { @@ -60,40 +54,12 @@ public void load(Path path) throws ConfigException { } } - public T getConfig(Class type) throws ConfigException { - JSONObject options = null; - - if (type.getName().equals("org.citydb.operation.importer.ImportOptions")) { - options = config.getImportConfig().getOperationOptions(); - } else if (type.getName().equals("org.citydb.io.reader.ReadOptions")) { - options = config.getImportConfig().getReadOptions(); - } else if (type.getName().equals("org.citydb.io.citygml.reader.CityGMLFormatOptions")) { - options = config.getImportConfig().getFormatOptions().get("CityGML"); - } else if (type.getName().equals("org.citydb.io.citygml.reader.CityJSONFormatOptions")) { - options = config.getImportConfig().getFormatOptions().get("CityJSON"); - } else if (type.getName().equals("org.citydb.operation.exporter.ExportOptions")) { - options = config.getExportConfig().getOperationOptions(); - } else if (type.getName().equals("org.citydb.io.writer.WriteOptions")) { - options = config.getExportConfig().getWriteOptions(); - } else if (type.getName().equals("org.citydb.io.citygml.writer.CityGMLFormatOptions")) { - options = config.getExportConfig().getFormatOptions().get("CityGML"); - } else if (type.getName().equals("org.citydb.io.citygml.writer.CityJSONFormatOptions")) { - options = config.getExportConfig().getFormatOptions().get("CityJSON"); - } - - if (options != null) { - return JSON.parseObject(options.toString(), type); - } else { - throw new ConfigException("Unsupported configuration type '" + type.getName() + "'."); - } - } - - public T getPluginConfig(String pluginName, Class type) { - JSONObject pluginConfig = config.getPluginsConfig().get(pluginName); - if (pluginConfig != null) { - return JSON.parseObject(pluginConfig.toString(), type); + public void writeTo(Config config, Path target) throws ConfigException { + Objects.requireNonNull(target, "The target path must not be null."); + try { + Files.write(target, JSON.toJSONString(config, JSONWriter.Feature.PrettyFormat).getBytes()); + } catch (IOException e) { + throw new ConfigException("Failed to write config file to " + target, e); } - - return null; } } diff --git a/citydb-config/src/main/java/org/citydb/config/configs/ExportConfig.java b/citydb-config/src/main/java/org/citydb/config/configs/ExportConfig.java deleted file mode 100644 index 7ea2bb3f..00000000 --- a/citydb-config/src/main/java/org/citydb/config/configs/ExportConfig.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * citydb-tool - Command-line tool for the 3D City Database - * https://www.3dcitydb.org/ - * - * Copyright 2022-2023 - * virtualcitysystems GmbH, Germany - * https://vc.systems/ - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.citydb.config.configs; - -import com.alibaba.fastjson2.JSONObject; - -import java.util.HashMap; -import java.util.Map; - -public class ExportConfig { - private JSONObject writeOptions; - private Map formatOptions; - private JSONObject operationOptions; - - public JSONObject getWriteOptions() { - return writeOptions; - } - - public ExportConfig setWriteOptions(JSONObject options) { - this.writeOptions = options; - return this; - } - - public Map getFormatOptions() { - if (formatOptions == null) { - formatOptions = new HashMap<>(); - } - - return formatOptions; - } - - public ExportConfig setFormatOptions(Map options) { - this.formatOptions = options; - return this; - } - - public JSONObject getOperationOptions() { - return operationOptions; - } - - public ExportConfig setOperationOptions(JSONObject options) { - this.operationOptions = options; - return this; - } -} \ No newline at end of file diff --git a/citydb-config/src/main/java/org/citydb/config/configs/ImportConfig.java b/citydb-config/src/main/java/org/citydb/config/configs/ImportConfig.java deleted file mode 100644 index 7d6e4426..00000000 --- a/citydb-config/src/main/java/org/citydb/config/configs/ImportConfig.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * citydb-tool - Command-line tool for the 3D City Database - * https://www.3dcitydb.org/ - * - * Copyright 2022-2023 - * virtualcitysystems GmbH, Germany - * https://vc.systems/ - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.citydb.config.configs; - -import com.alibaba.fastjson2.JSONObject; - -import java.util.HashMap; -import java.util.Map; - -public class ImportConfig { - private JSONObject readOptions; - private Map formatOptions; - private JSONObject operationOptions; - - public JSONObject getReadOptions() { - return readOptions; - } - - public ImportConfig setReadOptions(JSONObject options) { - this.readOptions = options; - return this; - } - - public Map getFormatOptions() { - if (formatOptions == null) { - formatOptions = new HashMap<>(); - } - - return formatOptions; - } - - public ImportConfig setFormatOptions(Map options) { - this.formatOptions = options; - return this; - } - - public JSONObject getOperationOptions() { - return operationOptions; - } - - public ImportConfig setOperationOptions(JSONObject options) { - this.operationOptions = options; - return this; - } -} From 7ea6b47e7fd4b26f2394378b9b359d73cdccb840 Mon Sep 17 00:00:00 2001 From: Zhihang Yao Date: Wed, 20 Dec 2023 16:09:40 +0100 Subject: [PATCH 4/5] added more get/set to config class --- .../main/java/org/citydb/config/Config.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/citydb-config/src/main/java/org/citydb/config/Config.java b/citydb-config/src/main/java/org/citydb/config/Config.java index 4c00dbaa..e1d4ae42 100644 --- a/citydb-config/src/main/java/org/citydb/config/Config.java +++ b/citydb-config/src/main/java/org/citydb/config/Config.java @@ -30,10 +30,13 @@ public class Config { private Map configs = new HashMap<>(); - public void setConfigs(Map configs) { - if (configs != null) { - this.configs = configs; - } + public Map getConfigs() { + return configs; + } + + public Config setConfig(String name, JSONObject config) { + configs.put(name, config); + return this; } public T getConfig(String name, Class type) throws ConfigException { @@ -44,4 +47,21 @@ public T getConfig(String name, Class type) throws ConfigException { throw new ConfigException("Unsupported configuration type '" + type.getName() + "'."); } } + + public Config setConfig(String name, Object config) { + Object object = JSON.toJSON(config); + if (object instanceof JSONObject) { + configs.put(name, (JSONObject) JSON.toJSON(config)); + } + + return this; + } + + public Config setConfigs(Map configs) { + if (configs != null) { + this.configs = configs; + } + + return this; + } } From c1259ff340cb70904c9f9d588cad5190f9697df0 Mon Sep 17 00:00:00 2001 From: Zhihang Yao Date: Wed, 20 Dec 2023 18:56:09 +0100 Subject: [PATCH 5/5] minor change --- citydb-config/src/main/java/org/citydb/config/Config.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/citydb-config/src/main/java/org/citydb/config/Config.java b/citydb-config/src/main/java/org/citydb/config/Config.java index e1d4ae42..3d5aecf9 100644 --- a/citydb-config/src/main/java/org/citydb/config/Config.java +++ b/citydb-config/src/main/java/org/citydb/config/Config.java @@ -23,11 +23,13 @@ import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; +import com.alibaba.fastjson2.annotation.JSONField; import java.util.HashMap; import java.util.Map; public class Config { + @JSONField(name = "config") private Map configs = new HashMap<>(); public Map getConfigs() {