diff options
Diffstat (limited to 'common-app-api/src/main/java/org/openecomp/sdc/common/impl')
4 files changed, 222 insertions, 225 deletions
diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/impl/ConfigFileChangeListener.java b/common-app-api/src/main/java/org/openecomp/sdc/common/impl/ConfigFileChangeListener.java index f1f1305ee9..e243bac730 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/impl/ConfigFileChangeListener.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/impl/ConfigFileChangeListener.java @@ -7,9 +7,9 @@ * 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. @@ -35,111 +35,111 @@ import java.util.Map; public class ConfigFileChangeListener extends FileChangeListener { - private static Logger log = Logger.getLogger(ConfigFileChangeListener.class.getName()); + private static Logger log = Logger.getLogger(ConfigFileChangeListener.class.getName()); - private Map<String, List<ConfigurationListener>> fileChangeToCallBack = new HashMap<>(); + private Map<String, List<ConfigurationListener>> fileChangeToCallBack = new HashMap<>(); - private Object lock = new Object(); + private Object lock = new Object(); - private YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); + private YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); - @Override - public void onFileChange(File pFile) { + @Override + public void onFileChange(File pFile) { - super.onFileChange(pFile); + super.onFileChange(pFile); - if (pFile != null) { + if (pFile != null) { - if (fileChangeToCallBack != null) { + if (fileChangeToCallBack != null) { - String id = findIdFromFileName(pFile.getName()); + String id = findIdFromFileName(pFile.getName()); - if (id != null) { + if (id != null) { - List<ConfigurationListener> listeners = fileChangeToCallBack.get(id); - if (listeners != null) { - for (ConfigurationListener configurationListener : listeners) { + List<ConfigurationListener> listeners = fileChangeToCallBack.get(id); + if (listeners != null) { + for (ConfigurationListener configurationListener : listeners) { - Class<? extends BasicConfiguration> configClass = configurationListener.getType(); + Class<? extends BasicConfiguration> configClass = configurationListener.getType(); - BasicConfiguration basicConfiguration = yamlToObjectConverter.convert(pFile.getAbsolutePath(), configClass); + BasicConfiguration basicConfiguration = yamlToObjectConverter.convert(pFile.getAbsolutePath(), configClass); - if (basicConfiguration == null) { - log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR, "", "", "Cannot update the listeners for file Change since the file content is invalid"); - continue; - } - log.debug("Loaded configuration after converting is {}", basicConfiguration); + if (basicConfiguration == null) { + log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR,"","","Cannot update the listeners for file Change since the file content is invalid"); + continue; + } + log.debug("Loaded configuration after converting is {}", basicConfiguration); - configurationListener.getCallBack().reconfigure(basicConfiguration); + configurationListener.getCallBack().reconfigure(basicConfiguration); - } - } - } else { + } + } + } else { - log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR, "", "", "Cannot calculate id from file {}", pFile.getName()); - } - } + log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR,"","","Cannot calculate id from file {}", pFile.getName()); + } + } - } + } - log.debug("File {} was changed.", pFile); - } + log.debug("File {} was changed.", pFile); + } - private String findIdFromFileName(String name) { + private String findIdFromFileName(String name) { - String result = null; - if (name != null) { - int startIndex = 0; - int endIndex = name.length(); - if (name.contains(File.separator)) { - startIndex = name.lastIndexOf(File.separator); - } - // String subNameString = name.substring(startIndex, endIndex); - // if (subNameString.contains(".")) { - // endIndex = subNameString.indexOf("."); - // } + String result = null; + if (name != null) { + int startIndex = 0; + int endIndex = name.length(); + if (name.contains(File.separator)) { + startIndex = name.lastIndexOf(File.separator); + } + // String subNameString = name.substring(startIndex, endIndex); + // if (subNameString.contains(".")) { + // endIndex = subNameString.indexOf("."); + // } - result = name.substring(startIndex, endIndex); + result = name.substring(startIndex, endIndex); - } + } - return result; - } + return result; + } - public void register(String id, ConfigurationListener configurationListener) { + public void register(String id, ConfigurationListener configurationListener) { - if (configurationListener != null) { + if (configurationListener != null) { - synchronized (lock) { + synchronized (lock) { - List<ConfigurationListener> callbacks = fileChangeToCallBack.get(id); - if (callbacks == null) { - callbacks = new ArrayList<>(); - fileChangeToCallBack.put(id, callbacks); - } - callbacks.add(configurationListener); + List<ConfigurationListener> callbacks = fileChangeToCallBack.get(id); + if (callbacks == null) { + callbacks = new ArrayList<>(); + fileChangeToCallBack.put(id, callbacks); + } + callbacks.add(configurationListener); - } + } - } + } - } + } - // public void notify(String id, BasicConfiguration object) { - // - // if (fileChangeToCallBack != null) { - // List<ConfigurationListener> listeners = fileChangeToCallBack - // .get(id); - // if (listeners != null) { - // for (ConfigurationListener configurationListener : listeners) { - // - // configurationListener.getCallBack().reconfigure(object); - // - // } - // } - // } - // - // } + // public void notify(String id, BasicConfiguration object) { + // + // if (fileChangeToCallBack != null) { + // List<ConfigurationListener> listeners = fileChangeToCallBack + // .get(id); + // if (listeners != null) { + // for (ConfigurationListener configurationListener : listeners) { + // + // configurationListener.getCallBack().reconfigure(object); + // + // } + // } + // } + // + // } } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/impl/ExternalConfiguration.java b/common-app-api/src/main/java/org/openecomp/sdc/common/impl/ExternalConfiguration.java index f1a1f490ca..79a78828e8 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/impl/ExternalConfiguration.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/impl/ExternalConfiguration.java @@ -7,9 +7,9 @@ * 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. @@ -26,80 +26,81 @@ import org.openecomp.sdc.common.api.ConfigurationSource; import java.io.File; /** + * * Save the - * + * * @author esofer + * */ public class ExternalConfiguration { - public static final int FILESYSTEM_MONITOR_INTERVAL = 1000; - private static String appName; - private static String appVersion; - private static String configDir; - private static ConfigurationSource configurationSource; - - private static FilesystemAlterationMonitor fam = null; - - private static ConfigFileChangeListener changeListener = new ConfigFileChangeListener(); - - private static boolean enableReconfigure = true; - - public static String getAppName() { - return appName; - } - - public static void setAppName(String appName) { - ExternalConfiguration.appName = appName; - } - - public static String getAppVersion() { - return appVersion; - } - - public static void setAppVersion(String appVersion) { - ExternalConfiguration.appVersion = appVersion; - } - - public static String getConfigDir() { - return configDir; - } - - public static void setConfigDir(String configDir) { - ExternalConfiguration.configDir = configDir; - } - - public static ConfigurationSource getConfigurationSource() { - return configurationSource; - } - - public static void setConfigurationSource(ConfigurationSource configurationSource) { - ExternalConfiguration.configurationSource = configurationSource; - } - - public static ConfigFileChangeListener getChangeListener() { - return changeListener; - } - - public static void listenForChanges() { - - String watchingDir = configDir + File.separator + appName; - if (enableReconfigure) { - if (fam == null) { - fam = new FilesystemAlterationMonitor(); - fam.setInterval(FILESYSTEM_MONITOR_INTERVAL); - fam.addListener(new File(watchingDir), changeListener); - fam.start(); - } - } - } - - public static void stopListenForFileChanges() { - if (enableReconfigure) { - if (fam != null) { - fam.stop(); - fam = null; - } - } - } + private static String appName; + private static String appVersion; + private static String configDir; + private static ConfigurationSource configurationSource; + + private static FilesystemAlterationMonitor fam = null; + + private static ConfigFileChangeListener changeListener = new ConfigFileChangeListener(); + + private static boolean enableReconfigure = true; + + public static String getAppName() { + return appName; + } + + public static void setAppName(String appName) { + ExternalConfiguration.appName = appName; + } + + public static String getAppVersion() { + return appVersion; + } + + public static void setAppVersion(String appVersion) { + ExternalConfiguration.appVersion = appVersion; + } + + public static String getConfigDir() { + return configDir; + } + + public static void setConfigDir(String configDir) { + ExternalConfiguration.configDir = configDir; + } + + public static ConfigurationSource getConfigurationSource() { + return configurationSource; + } + + public static void setConfigurationSource(ConfigurationSource configurationSource) { + ExternalConfiguration.configurationSource = configurationSource; + } + + public static ConfigFileChangeListener getChangeListener() { + return changeListener; + } + + public static void listenForChanges() { + + String watchingDir = configDir + File.separator + appName; + if (enableReconfigure) { + if (fam == null) { + fam = new FilesystemAlterationMonitor(); + fam.setInterval(1000); + fam.addListener(new File(watchingDir), changeListener); + fam.start(); + } + } + } + + public static void stopListenForFileChanges() { + if (enableReconfigure) { + if (fam != null) { + fam.stop(); + fam = null; + } + } + } } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/impl/FSConfigurationSource.java b/common-app-api/src/main/java/org/openecomp/sdc/common/impl/FSConfigurationSource.java index 3aa220c5bc..8cab431594 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/impl/FSConfigurationSource.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/impl/FSConfigurationSource.java @@ -7,9 +7,9 @@ * 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. @@ -31,8 +31,9 @@ import org.openecomp.sdc.common.util.YamlToObjectConverter; /** * Read configuration from file system - * + * * @author esofer + * */ public class FSConfigurationSource implements ConfigurationSource { @@ -41,50 +42,50 @@ public class FSConfigurationSource implements ConfigurationSource { private final ConfigFileChangeListener changeListener; private final String appConfigDir; - public FSConfigurationSource(ConfigFileChangeListener changeListener, String appConfigDir) { - super(); - this.changeListener = changeListener; - this.appConfigDir = appConfigDir; - } + public FSConfigurationSource(ConfigFileChangeListener changeListener, String appConfigDir) { + super(); + this.changeListener = changeListener; + this.appConfigDir = appConfigDir; + } - /* - * get and watch configuration changes. The file name we looking for is the lower case of the class name separated by "-". - * - * (non-Javadoc) - * - * @see org.openecomp.sdc.common.api.ConfigurationSource#getAndWatchConfiguration (java.lang.Class, org.openecomp.sdc.common.api.ConfigurationListener) - */ - public <T> T getAndWatchConfiguration(Class<T> className, ConfigurationListener configurationListener) { + /* + * get and watch configuration changes. The file name we looking for is the lower case of the class name separated by "-". + * + * (non-Javadoc) + * + * @see org.openecomp.sdc.common.api.ConfigurationSource#getAndWatchConfiguration (java.lang.Class, org.openecomp.sdc.common.api.ConfigurationListener) + */ + public <T> T getAndWatchConfiguration(Class<T> className, ConfigurationListener configurationListener) { - String configFileName = calculateFileName(className); + String configFileName = calculateFileName(className); - T object = yamlToObjectConverter.convert(this.appConfigDir, className, configFileName); + T object = yamlToObjectConverter.convert(this.appConfigDir, className, configFileName); - if (configurationListener != null && changeListener != null) { - if (object != null) { - changeListener.register(configFileName, configurationListener); - } - } + if (configurationListener != null && changeListener != null) { + if (object != null) { + changeListener.register(configFileName, configurationListener); + } + } - return object; - } + return object; + } - public <T> void addWatchConfiguration(Class<T> className, ConfigurationListener configurationListener) { + public <T> void addWatchConfiguration(Class<T> className, ConfigurationListener configurationListener) { - String configFileName = calculateFileName(className); + String configFileName = calculateFileName(className); - if (configurationListener != null) { - changeListener.register(configFileName, configurationListener); - } + if (configurationListener != null) { + changeListener.register(configFileName, configurationListener); + } - } + } - /** - * convert camel case string to list of words separated by "-" where each word is in lower case format. For example, MyClass will be calculated to be my-class.yaml . - * - * @param className - * @return file name based on the class name - */ + /** + * convert camel case string to list of words separated by "-" where each word is in lower case format. For example, MyClass will be calculated to be my-class.yaml . + * + * @param className + * @return file name based on the class name + */ static <T> String calculateFileName(Class<T> className) { String[] words = className.getSimpleName().split("(?=\\p{Upper})"); diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/impl/MutableHttpServletRequest.java b/common-app-api/src/main/java/org/openecomp/sdc/common/impl/MutableHttpServletRequest.java index 90ab998557..3b88ff8978 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/impl/MutableHttpServletRequest.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/impl/MutableHttpServletRequest.java @@ -7,9 +7,9 @@ * 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. @@ -22,51 +22,46 @@ package org.openecomp.sdc.common.impl; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; public final class MutableHttpServletRequest extends HttpServletRequestWrapper { - // holds custom header and value mapping - private final Map<String, String> customHeaders; + // holds custom header and value mapping + private final Map<String, String> customHeaders; - public MutableHttpServletRequest(HttpServletRequest request) { - super(request); - this.customHeaders = new HashMap<>(); - } + public MutableHttpServletRequest(HttpServletRequest request) { + super(request); + this.customHeaders = new HashMap<>(); + } - public void putHeader(String name, String value) { - this.customHeaders.put(name, value); - } + public void putHeader(String name, String value) { + this.customHeaders.put(name, value); + } - public String getHeader(String name) { - // check the custom headers first - String headerValue = customHeaders.get(name); + public String getHeader(String name) { + // check the custom headers first + String headerValue = customHeaders.get(name); - if (headerValue != null) { - return headerValue; - } - // else return from into the original wrapped object - return ((HttpServletRequest) getRequest()).getHeader(name); - } + if (headerValue != null) { + return headerValue; + } + // else return from into the original wrapped object + return ((HttpServletRequest) getRequest()).getHeader(name); + } - public Enumeration<String> getHeaderNames() { - // create a set of the custom header names - Set<String> set = new HashSet<>(customHeaders.keySet()); + public Enumeration<String> getHeaderNames() { + // create a set of the custom header names + Set<String> set = new HashSet<>(customHeaders.keySet()); - // now add the headers from the wrapped request object - @SuppressWarnings("unchecked") - Enumeration<String> e = ((HttpServletRequest) getRequest()).getHeaderNames(); - while (e.hasMoreElements()) { - // add the names of the request headers into the list - String n = e.nextElement(); - set.add(n); - } + // now add the headers from the wrapped request object + @SuppressWarnings("unchecked") + Enumeration<String> e = ((HttpServletRequest) getRequest()).getHeaderNames(); + while (e.hasMoreElements()) { + // add the names of the request headers into the list + String n = e.nextElement(); + set.add(n); + } - // create an enumeration from the set and return - return Collections.enumeration(set); - } + // create an enumeration from the set and return + return Collections.enumeration(set); + } } |