Skip to content

Commit

Permalink
Merge pull request #13 from 3dcitydb/feature-config-module
Browse files Browse the repository at this point in the history
Add config module
  • Loading branch information
Zhihang Yao authored Dec 20, 2023
2 parents 1509cce + c1259ff commit 663f739
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 1 deletion.
1 change: 1 addition & 0 deletions citydb-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
1 change: 1 addition & 0 deletions citydb-cli/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions citydb-cli/src/main/java/org/citydb/cli/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
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;
import org.citydb.plugin.Extension;
Expand Down Expand Up @@ -88,11 +90,17 @@ public class Launcher implements Command, CommandLine.IVersionProvider {
"(default: ${MAP-FALLBACK-VALUE}).")
private Map<String, Boolean> usePlugins;

@CommandLine.Option(names = "--config-file", scope = CommandLine.ScopeType.INHERIT, paramLabel = "<file>",
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;
private Config config;

public static void main(String[] args) {
Launcher launcher = new Launcher();
Expand Down Expand Up @@ -140,6 +148,10 @@ private int execute(String[] args) throws Exception {
throw new CommandLine.ParameterException(cmd, "Missing required subcommand.");
}

if (configFile != null) {
config = configManager.load(configFile);
}

if (usePlugins != null) {
for (Plugin plugin : pluginManager.getPlugins()) {
plugin.setEnabled(usePlugins.getOrDefault(plugin.getClass().getName(), plugin.isEnabled()));
Expand Down Expand Up @@ -351,6 +363,10 @@ enum LogLevel {
}
}

public Config getConfig() {
return config;
}

@Override
public String[] getVersion() {
return new String[]{CliConstants.APP_NAME + ", version " + CliConstants.APP_VERSION,
Expand Down
3 changes: 3 additions & 0 deletions citydb-config/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies {
api project(':citydb-core')
}
7 changes: 7 additions & 0 deletions citydb-config/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module org.citydb.config {
requires com.alibaba.fastjson2;

exports org.citydb.config;

opens org.citydb.config to com.alibaba.fastjson2;
}
69 changes: 69 additions & 0 deletions citydb-config/src/main/java/org/citydb/config/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.annotation.JSONField;

import java.util.HashMap;
import java.util.Map;

public class Config {
@JSONField(name = "config")
private Map<String, JSONObject> configs = new HashMap<>();

public Map<String, JSONObject> getConfigs() {
return configs;
}

public Config setConfig(String name, JSONObject config) {
configs.put(name, config);
return this;
}

public <T> T getConfig(String name, Class<T> 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() + "'.");
}
}

public <T> 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<String, JSONObject> configs) {
if (configs != null) {
this.configs = configs;
}

return this;
}
}
41 changes: 41 additions & 0 deletions citydb-config/src/main/java/org/citydb/config/ConfigException.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
65 changes: 65 additions & 0 deletions citydb-config/src/main/java/org/citydb/config/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.JSONWriter;

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 ConfigManager() {
}

public static ConfigManager getInstance() {
return instance;
}

public Config load(Path path) throws ConfigException {
Objects.requireNonNull(path, "The path must not be null.");

try (InputStream inStream = new BufferedInputStream(Files.newInputStream(path))) {
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) {
throw new ConfigException("Failed to parse the config file " + path + ".", e);
}
}

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);
}
}
}
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ include 'citydb-io-citygml'
include 'citydb-logging'
include 'citydb-model'
include 'citydb-operation'
include 'citydb-plugin'
include 'citydb-plugin'
include 'citydb-config'

0 comments on commit 663f739

Please sign in to comment.