2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.configuration;
28 import java.io.IOException;
29 import java.io.InputStream;
31 import java.security.CodeSource;
32 import java.security.ProtectionDomain;
33 import java.security.Provider;
34 import java.security.Provider.Service;
35 import java.security.Security;
36 import java.util.Map.Entry;
37 import java.util.Properties;
38 import java.util.jar.JarFile;
39 import java.util.jar.Manifest;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
43 import org.openecomp.appc.encryption.EncryptionTool;
44 import org.openecomp.appc.util.UnmodifiableProperties;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * This class provides the implementation of the <code>Configuration</code> interface. It is created by the
50 * ConfigurationFactory and initialized with the configuration values for the process.
55 public final class DefaultConfiguration implements Configuration, Cloneable {
57 private static final Logger logger = LoggerFactory.getLogger(DefaultConfiguration.class);
60 * The framework configuration properties.
62 private Properties properties = new Properties();
65 * Construct the configuration object.
67 public DefaultConfiguration() {
72 * Clears all properties
79 * @see java.lang.Object#clone()
82 protected Object clone() throws CloneNotSupportedException {
83 DefaultConfiguration clone = (DefaultConfiguration) super.clone();
85 clone.properties = new Properties(this.properties);
86 clone.properties.putAll(this.properties);
92 * Decrypts an encrypted value, if it is encrypted, and returns the clear text. Performs no operation on the string
93 * if it is not encrypted.
95 * @param value The value to (optionally) be decrypted
96 * @return The clear text
98 @SuppressWarnings("nls")
99 private static String decrypt(String value) {
101 if (value.startsWith(EncryptionTool.ENCRYPTED_VALUE_PREFIX)) {
103 return EncryptionTool.getInstance().decrypt(value);
104 } catch (Exception e) {
105 StringBuilder out = new StringBuilder();
106 for (Provider p : Security.getProviders()) {
107 for (Service s : p.getServices()) {
108 String algo = s.getAlgorithm();
109 out.append(String.format("\n==Found Algorithm [ %s ] in provider [ %s ] and service [ %s ]",
110 algo, p.getName(), s.getClassName()));
113 logger.debug(out.toString());
114 logger.warn(String.format("Could not decrypt the configuration value [%s]", value), e);
122 * Decrypts all elements in the properties object
124 private void decryptAllProperties() {
125 if (properties != null) {
126 for (Entry<Object, Object> e : properties.entrySet()) {
127 if (e.getValue() != null) {
128 e.setValue(decrypt(e.getValue().toString()));
135 * @see java.lang.Object#equals(java.lang.Object)
138 public boolean equals(Object obj) {
144 if (this.getClass() != obj.getClass()) {
148 DefaultConfiguration other = (DefaultConfiguration) obj;
150 return (this.properties.size() == other.properties.size())
151 && (this.properties.entrySet().containsAll(other.properties.entrySet()))
152 && (other.properties.entrySet().containsAll(this.properties.entrySet()));
157 * This method will use the properties object to expand any variables that may be present in the template provided.
158 * Variables are represented by the string "${name}", where "name" is the name of a property defined in either the
159 * current configuration object, or system properties if undefined. If the value cannot be found, the variable is
160 * removed and an empty string is used to replace the variable.
162 * @param template The template to be expanded
163 * @return The expanded template where each variable is replaced with its value
165 @SuppressWarnings("nls")
166 private String expandVariables(String template) {
167 if (template == null) {
171 // Decrypt the template if needed
172 // template = decrypt(template); DH: Do not assign values to parameters, bad form! Also, Sonar complains
175 StringBuffer buffer = new StringBuffer(decrypt(template));
176 Pattern pattern = Pattern.compile("\\$\\{([^\\}]+)\\}");
177 Matcher matcher = pattern.matcher(buffer);
178 while (matcher.find()) {
179 String variable = matcher.group(1);
180 String value = properties.getProperty(variable);
182 value = System.getProperty(variable);
187 buffer.replace(matcher.start(), matcher.end(), value);
191 return buffer.toString().trim();
195 * This method is called to obtain a property expressed as a boolean value (true or false). The standard rules for
196 * Boolean.parseBoolean() are used.
198 * @param key The property key
199 * @return The value of the property expressed as a boolean, or false if it does not exist.
201 @SuppressWarnings("nls")
203 public boolean getBooleanProperty(String key) {
204 return Boolean.valueOf(getProperty(key, "false"));
208 * This method is called to obtain a property expressed as a boolean value (true or false). The standard rules for
209 * Boolean.valueOf(String) are used.
211 * @param key The property key
212 * @param defaultValue The default value to be returned if the property does not exist
213 * @return The value of the property expressed as a boolean, or false if it does not exist.
214 * @see org.openecomp.appc.configuration.Configuration#getBooleanProperty(java.lang.String, boolean)
217 public boolean getBooleanProperty(String key, boolean defaultValue) {
218 if (isPropertyDefined(key)) {
219 return getBooleanProperty(key);
225 * Returns the indicated property value expressed as a floating point double-precision value (double).
227 * @param key The property to retrieve
228 * @return The value of the property, or 0.0 if not found
229 * @see org.openecomp.appc.configuration.Configuration#getDoubleProperty(java.lang.String)
231 @SuppressWarnings("nls")
233 public double getDoubleProperty(String key) {
235 return Double.valueOf(getProperty(key, "0.0"));
236 } catch (NumberFormatException e) {
242 * This method is called to obtain a property as a string value
244 * @param key The key of the property
245 * @param defaultValue The default value to be returned if the property does not exist
246 * @return The string value, or null if it does not exist.
247 * @see org.openecomp.appc.configuration.Configuration#getDoubleProperty(java.lang.String, double)
250 public double getDoubleProperty(String key, double defaultValue) {
251 if (isPropertyDefined(key)) {
252 return getDoubleProperty(key);
258 * Returns the property indicated expressed as an integer. The standard rules for
259 * {@link Integer#parseInt(String, int)} using a radix of 10 are used.
261 * @param key The property name to retrieve.
262 * @return The value of the property, or 0 if it does not exist or is invalid.
263 * @see org.openecomp.appc.configuration.Configuration#getIntegerProperty(java.lang.String)
265 @SuppressWarnings("nls")
267 public int getIntegerProperty(String key) {
269 return Integer.parseInt(getProperty(key, "0"), 10);
270 } catch (NumberFormatException e) {
276 * Returns the property indicated expressed as an integer. The standard rules for Integer.parseInt(String, int)
277 * using a radix of 10 are used.
279 * @param key The property name to retrieve.
280 * @param defaultValue The default value to be returned if the property does not exist
281 * @return The value of the property, or 0 if it does not exist or is invalid.
282 * @see org.openecomp.appc.configuration.Configuration#getIntegerProperty(java.lang.String, int)
285 public int getIntegerProperty(String key, int defaultValue) {
286 if (isPropertyDefined(key)) {
287 return getIntegerProperty(key);
293 * Returns the specified property as a long integer value, if it exists, or zero if it does not.
295 * @param key The key of the property desired.
296 * @return The value of the property expressed as an integer long value, or zero if the property does not exist or
297 * is not a valid integer long.
298 * @see org.openecomp.appc.configuration.Configuration#getLongProperty(java.lang.String)
300 @SuppressWarnings("nls")
302 public long getLongProperty(String key) {
304 return Long.parseLong(getProperty(key, "0"), 10);
305 } catch (NumberFormatException e) {
311 * Returns the specified property as a long integer value, if it exists, or the default value if it does not exist
314 * @param key The key of the property desired.
315 * @param defaultValue the value to be returned if the property is not valid or does not exist.
316 * @return The value of the property expressed as an integer long value, or the default value if the property does
317 * not exist or is not a valid integer long.
318 * @see org.openecomp.appc.configuration.Configuration#getLongProperty(java.lang.String, long)
321 public long getLongProperty(String key, long defaultValue) {
322 if (isPropertyDefined(key)) {
323 return getLongProperty(key);
329 * This method can be called to retrieve a properties object that is immutable. Any attempt to modify the properties
330 * object returned will result in an exception. This allows a caller to view the current configuration as a set of
333 * @return An unmodifiable properties object.
334 * @see org.openecomp.appc.configuration.Configuration#getProperties()
337 public Properties getProperties() {
338 return new UnmodifiableProperties(properties);
342 * This method is called to obtain a property as a string value
344 * @param key The key of the property
345 * @return The string value, or null if it does not exist.
348 public String getProperty(String key) {
349 String value = properties.getProperty(key);
353 return expandVariables(value.trim());
357 * This method is called to obtain a property as a string value
359 * @param key The key of the property
360 * @param defaultValue The default value to be returned if the property does not exist
361 * @return The string value, or null if it does not exist.
362 * @see org.openecomp.appc.configuration.Configuration#getProperty(java.lang.String, java.lang.String)
365 public String getProperty(String key, String defaultValue) {
366 if (isPropertyDefined(key)) {
367 return getProperty(key);
370 if (defaultValue == null) {
374 return expandVariables(defaultValue.trim());
378 * @see java.lang.Object#hashCode()
381 public int hashCode() {
382 return (properties == null ? 0 : properties.hashCode());
386 * Returns true if the named property is defined, false otherwise.
388 * @param key The key of the property we are interested in
389 * @return True if the property exists.
392 public boolean isPropertyDefined(String key) {
393 return properties.containsKey(key);
397 * Returns an indication of the validity of the boolean property. A boolean property is considered to be valid only
398 * if it has the value "true" or "false" (ignoring case).
400 * @param key The property to be checked
401 * @return True if the value is a boolean constant, or false if it does not exist or is not a correct string
402 * @see org.openecomp.appc.configuration.Configuration#isValidBoolean(java.lang.String)
404 @SuppressWarnings("nls")
406 public boolean isValidBoolean(String key) {
407 String value = getProperty(key);
409 value = value.toLowerCase();
410 return value.matches("true|false");
416 * Returns an indication if the indicated property represents a valid double-precision floating point number.
418 * @param key The property to be examined
419 * @return True if the property is a valid representation of a double, or false if it does not exist or contains
420 * illegal characters.
421 * @see org.openecomp.appc.configuration.Configuration#isValidDouble(java.lang.String)
424 public boolean isValidDouble(String key) {
425 String value = getProperty(key);
428 Double.valueOf(value);
430 } catch (NumberFormatException e) {
438 * Returns an indication if the property is a valid integer value or not.
440 * @param key The key of the property to check
441 * @return True if the value is a valid integer string, or false if it does not exist or contains illegal
443 * @see org.openecomp.appc.configuration.Configuration#isValidInteger(java.lang.String)
446 public boolean isValidInteger(String key) {
447 String value = getProperty(key);
450 Integer.parseInt(value.trim(), 10);
452 } catch (NumberFormatException e) {
460 * Determines is the specified property exists and is a valid representation of an integer long value.
462 * @param key The property to be checked
463 * @return True if the property is a valid representation of an integer long value, and false if it either does not
464 * exist or is not valid.
465 * @see org.openecomp.appc.configuration.Configuration#isValidLong(java.lang.String)
468 public boolean isValidLong(String key) {
469 String value = getProperty(key);
472 Long.parseLong(value.trim(), 10);
474 } catch (NumberFormatException e) {
482 * This method allows an implementation to load configuration properties that may override default values.
484 * @param is An input stream that contains the properties to be loaded
486 public void setProperties(InputStream is) {
489 } catch (IOException e) {
495 * This method allows an implementation to load configuration properties that may override default values.
497 * @param props An optional Properties object to be merged into the configuration, replacing any same-named
499 * @see org.openecomp.appc.configuration.Configuration#setProperties(java.util.Properties)
502 public void setProperties(Properties props) {
503 properties.putAll(props);
504 decryptAllProperties();
508 * This method allows a caller to insert a new property definition into the configuration object. This allows the
509 * application to adjust or add to the current configuration. If the property already exists, it is replaced with
512 * @param key The key of the property to be defined
513 * @param value The value of the property to be defined
514 * @see org.openecomp.appc.configuration.Configuration#setProperty(java.lang.String, java.lang.String)
517 public void setProperty(String key, String value) {
518 properties.setProperty(key, decrypt(value));
522 * @see java.lang.Object#toString()
524 @SuppressWarnings("nls")
526 public String toString() {
527 return String.format("Configuration: %d properties, keys:[%s]", properties.size(), properties.keySet()
532 * This is a helper method to read the manifest of the jar file that this class was loaded from. Note that this will
533 * only work if the code is packaged in a jar file. If it is an open deployment, such as under eclipse, this will
534 * not work and there is code added to detect that case.
536 * @return The manifest object from the jar file, or null if the code is not packaged in a jar file.
541 private Manifest getManifest() {
542 ProtectionDomain domain = getClass().getProtectionDomain();
543 CodeSource source = domain.getCodeSource();
544 URL location = source.getLocation();
545 String path = location.getPath();
546 int index = path.indexOf('!');
548 path = path.substring(0, index);
550 if (path.endsWith(".jar")) {
551 try (JarFile jar = new JarFile(location.getFile())) {
552 return jar.getManifest();
553 } catch (IOException e) {
554 logger.error("getManifest", e);