Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add YAMLConfigManager support #1446

Merged
merged 8 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions modules/siddhi-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.siddhi.core.exception;

/**
* Exception class to be used when non-existence definition is requested.
*/
public class YAMLConfigManagerException extends RuntimeException {

public YAMLConfigManagerException() {
super();
}

public YAMLConfigManagerException(String message) {
super(message);
}

public YAMLConfigManagerException(String message, Throwable throwable) {
super(message, throwable);
}

public YAMLConfigManagerException(Throwable throwable) {
super(throwable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.siddhi.core.util;

import io.siddhi.core.exception.YAMLConfigManagerException;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* Util class to read file content
*/
public class FileReader {

private static final Logger LOG = Logger.getLogger(FileReader.class);

public static String readYAMLConfigFile(Path filePath) throws YAMLConfigManagerException {
if (!filePath.toFile().exists()) {
throw new YAMLConfigManagerException("Error while initializing YAML config manager, " +
"YAML file does not exist with path '" + filePath.toAbsolutePath().toString() + "'.");
}
if (!filePath.toString().endsWith(".yaml")) {
throw new YAMLConfigManagerException("Error while initializing YAML config manager, file extension " +
"'yaml' expected");
}

if (LOG.isDebugEnabled()) {
LOG.debug("Initialize config provider instance from configuration file: " + filePath.toString());
}
try {
return readContent(filePath);
} catch (IOException e) {
throw new YAMLConfigManagerException("Unable to read config file '" + filePath.toAbsolutePath().toString()
+ "'.", e);
}
}

private static String readContent(Path filePath) throws IOException {
byte[] contentBytes = Files.readAllBytes(filePath);
return new String(contentBytes, StandardCharsets.UTF_8);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,28 @@
*/
public interface ConfigManager {

/**
* Generates Config reader for extensions with specific namespace and name
*
* @param namespace Namespace of the extension
* @param name Name of the extension
* @return ConfigReader
*/
ConfigReader generateConfigReader(String namespace, String name);

/**
* Generates hash map of properties for siddhi annotation reference
*
* @param name Reference Name
* @return Hashmap of the properties
*/
Map<String, String> extractSystemConfigs(String name);

/**
* Extracts specific siddhi property in system properties
*
* @param name Name of the property
* @return Value of the property
*/
String extractProperty(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,19 @@
*/
public interface ConfigReader {

/**
* Returns the value of the system property if set else the default value is returned
*
* @param name Name of the property
* @param defaultValue Default value for the property
* @return Value of the system property
*/
String readConfig(String name, String defaultValue);

/**
* Return all the configurations in the config reader
* @return Hashmap of properties and values
*/
Map<String, String> getAllConfigs();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.siddhi.core.util.config;

import io.siddhi.core.exception.YAMLConfigManagerException;
import io.siddhi.core.util.SiddhiConstants;
import io.siddhi.core.util.config.model.Extension;
import io.siddhi.core.util.config.model.ExtensionChildConfiguration;
import io.siddhi.core.util.config.model.Reference;
import io.siddhi.core.util.config.model.ReferenceChildConfiguration;
import io.siddhi.core.util.config.model.RootConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor;
import org.yaml.snakeyaml.introspector.BeanAccess;
import org.yaml.snakeyaml.introspector.PropertyUtils;

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

/**
* YAML file based Config Manger
*/
public class YAMLConfigManager implements ConfigManager {

private static final Logger LOG = LoggerFactory.getLogger(YAMLConfigManager.class);
private RootConfiguration rootConfiguration;

public YAMLConfigManager(String yamlContent) {
init(yamlContent);
}

/**
* Initialises YAML Config Manager by parsing the YAML file
*
* @throws YAMLConfigManagerException Exception is thrown if there are issues in processing thr yaml file
*/
private void init(String yamlContent) throws YAMLConfigManagerException {
try {
CustomClassLoaderConstructor constructor = new CustomClassLoaderConstructor(
RootConfiguration.class, RootConfiguration.class.getClassLoader());
PropertyUtils propertyUtils = new PropertyUtils();
propertyUtils.setSkipMissingProperties(true);
constructor.setPropertyUtils(propertyUtils);

Yaml yaml = new Yaml(constructor);
yaml.setBeanAccess(BeanAccess.FIELD);
this.rootConfiguration = yaml.load(yamlContent);
} catch (Exception e) {
throw new YAMLConfigManagerException("Unable to parse YAML string, '" + yamlContent + "'.", e);
}
}

@Override
public ConfigReader generateConfigReader(String namespace, String name) {
for (Extension extension : this.rootConfiguration.getExtensions()) {
ExtensionChildConfiguration childConfiguration = extension.getExtension();
if (childConfiguration.getNamespace().equals(namespace) &&
childConfiguration.getName().equals(name)) {
return new YAMLConfigReader(childConfiguration.getProperties());
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Could not find a matching configuration for name: " + name + "and namespace: " +
namespace + "!");
}
return new YAMLConfigReader(new HashMap<>());
}

@Override
public Map<String, String> extractSystemConfigs(String name) {
for (Reference reference : this.rootConfiguration.getRefs()) {
ReferenceChildConfiguration childConf = reference.getReference();
if (childConf.getName().equals(name)) {
Map<String, String> referenceConfigs = new HashMap<>();
referenceConfigs.put(SiddhiConstants.ANNOTATION_ELEMENT_TYPE, childConf.getType());
referenceConfigs.putAll(childConf.getProperties());
return referenceConfigs;
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Could not find a matching reference for name: '" + name + "'!");
}
return new HashMap<>();
}

@Override
public String extractProperty(String name) {
String property = this.rootConfiguration.getProperties().get(name);
if (LOG.isDebugEnabled()) {
LOG.debug("Could not find a matching configuration for property name: " + name + "");
}
return property;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.siddhi.core.util.config;

import java.util.Map;

/**
* Extension config readers
*/
public class YAMLConfigReader implements ConfigReader {
private final Map<String, String> configs;

public YAMLConfigReader(Map<String, String> configs) {
this.configs = configs;
}

@Override
public String readConfig(String name, String defaultValue) {
String value = configs.get(name);
if (value != null) {
return value;
} else {
return defaultValue;
}
}

@Override
public Map<String, String> getAllConfigs() {
return configs;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.siddhi.core.util.config.model;

/**
* A third level configuration bean class for siddhi extension config.
*/
public class Extension {

private ExtensionChildConfiguration extension = new ExtensionChildConfiguration();

public ExtensionChildConfiguration getExtension() {
return extension;
}
}

Loading