-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from 3dcitydb/feature-config-module
Add config module
- Loading branch information
Showing
9 changed files
with
205 additions
and
1 deletion.
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
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
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
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,3 @@ | ||
dependencies { | ||
api project(':citydb-core') | ||
} |
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,7 @@ | ||
module org.citydb.config { | ||
requires com.alibaba.fastjson2; | ||
|
||
exports org.citydb.config; | ||
|
||
opens org.citydb.config to com.alibaba.fastjson2; | ||
} |
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,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
41
citydb-config/src/main/java/org/citydb/config/ConfigException.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,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
65
citydb-config/src/main/java/org/citydb/config/ConfigManager.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,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); | ||
} | ||
} | ||
} |
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