-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved JSON config loading method for TopicConfigProviders to abstract…
… parent class Signed-off-by: Thomas Cooper <[email protected]>
- Loading branch information
1 parent
9881ba0
commit 44727ee
Showing
3 changed files
with
61 additions
and
62 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...ol/src/main/java/com/linkedin/kafka/cruisecontrol/config/JsonFileTopicConfigProvider.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,53 @@ | ||
/* | ||
* Copyright 2021 LinkedIn Corp. Licensed under the BSD 2-Clause License (the "License"). See License in the project root for license information. | ||
*/ | ||
|
||
package com.linkedin.kafka.cruisecontrol.config; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.stream.JsonReader; | ||
import com.linkedin.kafka.cruisecontrol.KafkaCruiseControlUtils; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
/** | ||
* Abstract implementation of {@link TopicConfigProvider} which provides a method for loading cluster configurations | ||
* from a JSON file. | ||
*/ | ||
public abstract class JsonFileTopicConfigProvider implements TopicConfigProvider { | ||
|
||
/** | ||
* Method which will find the file path from the supplied config map using the supplied cluster file config key and | ||
* load the configs contained in that JSON file into a {@link java.util.Properties} instance. | ||
* | ||
* The format of the file is JSON, with properties listed as top level key/value pairs: | ||
* | ||
* <pre> | ||
* { | ||
* "min.insync.replicas": 1, | ||
* "an.example.cluster.config": false | ||
* } | ||
* </pre> | ||
* | ||
* @param configs The map of config key/value pairs | ||
* @param clusterConfigsFileKey The key under which the config file path is stored. | ||
* @return A {@link java.util.Properties} instance containing the contents of the specified JSON config file. | ||
*/ | ||
protected static Properties loadClusterConfigs(Map<String, ?> configs, String clusterConfigsFileKey) { | ||
String configFile = KafkaCruiseControlUtils.getRequiredConfig(configs, clusterConfigsFileKey); | ||
try { | ||
try (JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(configFile), StandardCharsets.UTF_8))) { | ||
Gson gson = new Gson(); | ||
return gson.fromJson(reader, Properties.class); | ||
} | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException(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
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