Skip to content

Commit 563d224

Browse files
author
Zhihang Yao
committed
add new config module
1 parent f720b07 commit 563d224

File tree

11 files changed

+394
-1
lines changed

11 files changed

+394
-1
lines changed

citydb-cli/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dependencies {
1111
implementation project(':citydb-database')
1212
implementation project(':citydb-operation')
1313
implementation project(':citydb-database-postgres')
14+
api project(':citydb-config')
1415
api project(':citydb-plugin')
1516
api 'info.picocli:picocli:4.7.5'
1617
}

citydb-cli/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
requires org.citydb.operation;
88
requires transitive org.citydb.plugin;
99
requires transitive info.picocli;
10+
requires org.citydb.config;
1011

1112
exports org.citydb.cli;
1213
exports org.citydb.cli.command;

citydb-cli/src/main/java/org/citydb/cli/Launcher.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.citydb.cli.util.CliConstants;
3434
import org.citydb.cli.util.CommandHelper;
3535
import org.citydb.cli.util.PidFile;
36+
import org.citydb.config.ConfigManager;
3637
import org.citydb.core.CoreConstants;
3738
import org.citydb.logging.LoggerManager;
3839
import org.citydb.plugin.Extension;
@@ -88,8 +89,13 @@ public class Launcher implements Command, CommandLine.IVersionProvider {
8889
"(default: ${MAP-FALLBACK-VALUE}).")
8990
private Map<String, Boolean> usePlugins;
9091

92+
@CommandLine.Option(names = "--config-file", scope = CommandLine.ScopeType.INHERIT, paramLabel = "<file>",
93+
description = "Load configuration from this file.")
94+
private Path configFile;
95+
9196
private final Logger logger = LoggerManager.getInstance().getLogger();
9297
private final PluginManager pluginManager = PluginManager.getInstance();
98+
private final ConfigManager configManager = ConfigManager.getInstance();
9399
private final CommandHelper helper = CommandHelper.of(logger);
94100
private String commandLine;
95101
private String subCommandName;
@@ -140,6 +146,10 @@ private int execute(String[] args) throws Exception {
140146
throw new CommandLine.ParameterException(cmd, "Missing required subcommand.");
141147
}
142148

149+
if (configFile != null) {
150+
configManager.load(configFile);
151+
}
152+
143153
if (usePlugins != null) {
144154
for (Plugin plugin : pluginManager.getPlugins()) {
145155
plugin.setEnabled(usePlugins.getOrDefault(plugin.getClass().getName(), plugin.isEnabled()));

citydb-config/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies {
2+
api project(':citydb-core')
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module org.citydb.config {
2+
requires com.alibaba.fastjson2;
3+
4+
exports org.citydb.config;
5+
exports org.citydb.config.configs;
6+
7+
opens org.citydb.config to com.alibaba.fastjson2;
8+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* citydb-tool - Command-line tool for the 3D City Database
3+
* https://www.3dcitydb.org/
4+
*
5+
* Copyright 2022-2023
6+
* virtualcitysystems GmbH, Germany
7+
* https://vc.systems/
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*/
21+
22+
package org.citydb.config;
23+
24+
import com.alibaba.fastjson2.JSON;
25+
import com.alibaba.fastjson2.JSONObject;
26+
import com.alibaba.fastjson2.JSONWriter;
27+
import com.alibaba.fastjson2.annotation.JSONField;
28+
import org.citydb.config.configs.ExportConfig;
29+
import org.citydb.config.configs.ImportConfig;
30+
31+
import java.io.IOException;
32+
import java.nio.file.Files;
33+
import java.nio.file.Path;
34+
import java.util.HashMap;
35+
import java.util.Map;
36+
import java.util.Objects;
37+
38+
public class Config {
39+
@JSONField(name = "import")
40+
private ImportConfig importConfig;
41+
@JSONField(name = "export")
42+
private ExportConfig exportConfig;
43+
@JSONField(name = "plugins")
44+
private Map<String, JSONObject> pluginsConfig;
45+
46+
public ImportConfig getImportConfig() {
47+
if (importConfig == null) {
48+
importConfig = new ImportConfig();
49+
}
50+
51+
return importConfig;
52+
}
53+
54+
public Config setImportConfig(ImportConfig importConfig) {
55+
this.importConfig = importConfig;
56+
return this;
57+
}
58+
59+
public ExportConfig getExportConfig() {
60+
if (exportConfig == null) {
61+
exportConfig = new ExportConfig();
62+
}
63+
64+
return exportConfig;
65+
}
66+
67+
public Config setExportConfig(ExportConfig exportConfig) {
68+
this.exportConfig = exportConfig;
69+
return this;
70+
}
71+
72+
public Map<String, JSONObject> getPluginsConfig() {
73+
if (pluginsConfig == null) {
74+
pluginsConfig = new HashMap<>();
75+
}
76+
77+
return pluginsConfig;
78+
}
79+
80+
public Config setPluginsConfig(Map<String, JSONObject> pluginsConfig) {
81+
this.pluginsConfig = pluginsConfig;
82+
return this;
83+
}
84+
85+
public void writeTo(Path target) throws ConfigException {
86+
Objects.requireNonNull(target, "The target path must not be null.");
87+
try {
88+
Files.write(target, JSON.toJSONString(this, JSONWriter.Feature.PrettyFormat).getBytes());
89+
} catch (IOException e) {
90+
throw new ConfigException("Failed to write config file to " + target, e);
91+
}
92+
}
93+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* citydb-tool - Command-line tool for the 3D City Database
3+
* https://www.3dcitydb.org/
4+
*
5+
* Copyright 2022-2023
6+
* virtualcitysystems GmbH, Germany
7+
* https://vc.systems/
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*/
21+
22+
package org.citydb.config;
23+
24+
public class ConfigException extends Exception {
25+
26+
public ConfigException() {
27+
super();
28+
}
29+
30+
public ConfigException(String message) {
31+
super(message);
32+
}
33+
34+
public ConfigException(Throwable cause) {
35+
super(cause);
36+
}
37+
38+
public ConfigException(String message, Throwable cause) {
39+
super(message, cause);
40+
}
41+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* citydb-tool - Command-line tool for the 3D City Database
3+
* https://www.3dcitydb.org/
4+
*
5+
* Copyright 2022-2023
6+
* virtualcitysystems GmbH, Germany
7+
* https://vc.systems/
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*/
21+
22+
package org.citydb.config;
23+
24+
import com.alibaba.fastjson2.JSON;
25+
import com.alibaba.fastjson2.JSONException;
26+
import com.alibaba.fastjson2.JSONObject;
27+
28+
import java.io.BufferedInputStream;
29+
import java.io.IOException;
30+
import java.io.InputStream;
31+
import java.nio.file.Files;
32+
import java.nio.file.Path;
33+
import java.util.Objects;
34+
35+
public class ConfigManager {
36+
private static final ConfigManager instance = new ConfigManager();
37+
38+
private Config config;
39+
40+
private ConfigManager() {
41+
}
42+
43+
public static ConfigManager getInstance() {
44+
return instance;
45+
}
46+
47+
public Config getConfig() {
48+
return config;
49+
}
50+
51+
public void load(Path path) throws ConfigException {
52+
Objects.requireNonNull(path, "The path must not be null.");
53+
54+
try (InputStream inStream = new BufferedInputStream(Files.newInputStream(path))) {
55+
config = JSON.parseObject(new String(inStream.readAllBytes()), Config.class);
56+
} catch (IOException e) {
57+
throw new ConfigException("Failed to read the config file " + path + ".", e);
58+
} catch (JSONException e) {
59+
throw new ConfigException("Failed to parse the config file " + path + ".", e);
60+
}
61+
}
62+
63+
public <T> T getConfig(Class<T> type) throws ConfigException {
64+
JSONObject options = null;
65+
66+
if (type.getName().equals("org.citydb.operation.importer.ImportOptions")) {
67+
options = config.getImportConfig().getOperationOptions();
68+
} else if (type.getName().equals("org.citydb.io.reader.ReadOptions")) {
69+
options = config.getImportConfig().getReadOptions();
70+
} else if (type.getName().equals("org.citydb.io.citygml.reader.CityGMLFormatOptions")) {
71+
options = config.getImportConfig().getFormatOptions().get("CityGML");
72+
} else if (type.getName().equals("org.citydb.io.citygml.reader.CityJSONFormatOptions")) {
73+
options = config.getImportConfig().getFormatOptions().get("CityJSON");
74+
} else if (type.getName().equals("org.citydb.operation.exporter.ExportOptions")) {
75+
options = config.getExportConfig().getOperationOptions();
76+
} else if (type.getName().equals("org.citydb.io.writer.WriteOptions")) {
77+
options = config.getExportConfig().getWriteOptions();
78+
} else if (type.getName().equals("org.citydb.io.citygml.writer.CityGMLFormatOptions")) {
79+
options = config.getExportConfig().getFormatOptions().get("CityGML");
80+
} else if (type.getName().equals("org.citydb.io.citygml.writer.CityJSONFormatOptions")) {
81+
options = config.getExportConfig().getFormatOptions().get("CityJSON");
82+
}
83+
84+
if (options != null) {
85+
return JSON.parseObject(options.toString(), type);
86+
} else {
87+
throw new ConfigException("Unsupported configuration type '" + type.getName() + "'.");
88+
}
89+
}
90+
91+
public <T> T getPluginConfig(String pluginName, Class<T> type) {
92+
JSONObject pluginConfig = config.getPluginsConfig().get(pluginName);
93+
if (pluginConfig != null) {
94+
return JSON.parseObject(pluginConfig.toString(), type);
95+
}
96+
97+
return null;
98+
}
99+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* citydb-tool - Command-line tool for the 3D City Database
3+
* https://www.3dcitydb.org/
4+
*
5+
* Copyright 2022-2023
6+
* virtualcitysystems GmbH, Germany
7+
* https://vc.systems/
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*/
21+
22+
package org.citydb.config.configs;
23+
24+
import com.alibaba.fastjson2.JSONObject;
25+
import com.alibaba.fastjson2.annotation.JSONField;
26+
27+
import java.util.HashMap;
28+
import java.util.Map;
29+
30+
public class ExportConfig {
31+
@JSONField(name = "writeOptions")
32+
private JSONObject writeOptions;
33+
@JSONField(name = "formatOptions")
34+
private Map<String, JSONObject> formatOptions;
35+
@JSONField(name = "operationOptions")
36+
private JSONObject operationOptions;
37+
38+
public JSONObject getWriteOptions() {
39+
return writeOptions;
40+
}
41+
42+
public ExportConfig setWriteOptions(JSONObject options) {
43+
this.writeOptions = options;
44+
return this;
45+
}
46+
47+
public Map<String, JSONObject> getFormatOptions() {
48+
if (formatOptions == null) {
49+
formatOptions = new HashMap<>();
50+
}
51+
52+
return formatOptions;
53+
}
54+
55+
public ExportConfig setFormatOptions(Map<String, JSONObject> options) {
56+
this.formatOptions = options;
57+
return this;
58+
}
59+
60+
public JSONObject getOperationOptions() {
61+
return operationOptions;
62+
}
63+
64+
public ExportConfig setOperationOptions(JSONObject options) {
65+
this.operationOptions = options;
66+
return this;
67+
}
68+
}

0 commit comments

Comments
 (0)