/*-
* ============LICENSE_START=======================================================
* ONAP : APPC
* ================================================================================
* Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Copyright (C) 2017 Amdocs
* =============================================================================
* 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.
*
* ============LICENSE_END=========================================================
*/
package org.onap.appc.listener;
import java.util.Properties;
/**
* A class for instantiating Listener objects. It is primarily used to hold properties that start with the given prefix.
* It also holds a class that implements {@see Listener} and will be used by the controller to spawn a new listener
* object.
*
* @since Apr 25, 2016
* @version $Id$
*/
public class ListenerProperties {
private String prefix;
private Class extends Listener> listenerClass;
private Properties props;
/**
* Creates a new listener object with the given prefix and properties. Any property starting with the prefix is
* added to the internal properties object with the prefix removed. All other properties are ignored.
* ListenerProperties constructor
*
* @param prefix
* The prefix of the properties to load
* @param allProps
* The properties object to load from.
*/
public ListenerProperties(String prefix, Properties allProps) {
this.prefix = prefix;
props = new Properties();
String dottedPrefix = String.format("%s.", prefix);
for (String key : allProps.stringPropertyNames()) {
if (key.startsWith(dottedPrefix) && key.length() > dottedPrefix.length()) {
props.put(key.substring(dottedPrefix.length()), allProps.get(key));
}
}
}
/**
* @return The prefix of these properties
*/
public String getPrefix() {
return prefix;
}
/**
* Sets the listener class. Will be used by {@see Controller} to instantiate the Listener thread for this object
*
* @param cls
* The class to be created. Implements {@see Listener}
*/
public void setListenerClass(Class extends Listener> cls) {
this.listenerClass = cls;
}
/**
* @return The class that will be used by {@see Controller} to instantiate the Listener thread for this object
*/
public Class extends Listener> getListenerClass() {
return listenerClass;
}
/**
* Returns a property matching a given KEYS
*
* @param key
* The KEYS object who's value to return.
* @return The value of the property or null if none exists
*/
public String getProperty(KEYS key) {
return getProperty(key, null);
}
/**
* Returns a property matching a given string.
*
* @param key
* The key who's value to return.
* @return The value of the property or null if none exists
*/
public String getProperty(String key) {
return getProperty(key, null);
}
/**
* Returns a property matching a given KEYS
*
* @param key
* The KEYS object who's value to return.
* @param defaultValue
* The value to return if the property is not found
* @return The value of the property or null if none exists
*/
public String getProperty(KEYS key, String defaultValue) {
return getProperty(key.getPropertySuffix(), defaultValue);
}
/**
* Returns a property matching a given string.
*
* @param key
* The key who's value to return.
* @param defaultValue
* The value to return if the property is not found
* @return The value of the property or null if none exists
*/
public String getProperty(String key, String defaultValue) {
return props.getProperty(key, defaultValue);
}
/**
* @return The properties object containing all properties
*/
public Properties getProperties() {
return props;
}
/**
* Reads the prefix.disabled property to determine if the listener is disabled and should not be run by the
* controller. Defaults to false if property not set or value cannot be parsed.
*
* @return true if the listener is disabled and should not be started. false if the listener should be start
* normally (default).
*/
public boolean isDisabled() {
return Boolean.valueOf(getProperty(KEYS.DISABLED, "false"));
}
@Override
public String toString() {
return String.format("%s", prefix);
}
/**
* Set of common properties that will be used by most systems. Primarily relating to DMaaP and ThreadPools
*
* @since Apr 25, 2016
* @version $Id$
*/
public enum KEYS {
/**
* Property to determine if the listener should be disabled. If not set, defaults to false
*/
DISABLED("disabled"),
/**
* Property for the message service type. Should be a lower case string. See MessageService.
*/
MESSAGE_SERVICE("service"),
/**
* A hostname or comma separated list (no spaces) of hostnames of servers in a cluster. Can have ports included
* as well.
* Examples:
*