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;
29 import java.io.IOException;
30 import java.io.InputStream;
32 import java.security.CodeSource;
33 import java.security.ProtectionDomain;
34 import java.security.Provider;
35 import java.security.Provider.Service;
36 import java.security.Security;
37 import java.util.Map.Entry;
38 import java.util.Properties;
39 import java.util.jar.JarFile;
40 import java.util.jar.Manifest;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
44 import org.openecomp.appc.encryption.EncryptionTool;
45 import org.openecomp.appc.util.UnmodifiableProperties;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * This class provides the implementation of the <code>Configuration</code> interface. It is created by the
51 * ConfigurationFactory and initialized with the configuration values for the process.
56 public final class DefaultConfiguration implements Configuration, Cloneable {
58 private static final Logger logger = LoggerFactory.getLogger(DefaultConfiguration.class);
61 * The framework configuration properties.
63 private Properties properties = new Properties();
66 * Construct the configuration object.
68 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.
96 * The value to (optionally) be decrypted
97 * @return The clear text
99 @SuppressWarnings("nls")
100 private static String decrypt(String value) {
102 if (value.startsWith(EncryptionTool.ENCRYPTED_VALUE_PREFIX)) {
104 return EncryptionTool.getInstance().decrypt(value);
105 } catch (Throwable e) {
107 for (Provider p : Security.getProviders()) {
108 for (Service s : p.getServices()) {
109 String algo = s.getAlgorithm();
111 String.format("\n==Found Algorithm [ %s ] in provider [ %s ] and service [ %s ]", algo,
112 p.getName(), s.getClassName());
116 logger.warn(String.format("Could not decrypt the configuration value [%s]", value), e);
124 * Decrypts all elements in the properties object
126 private void decryptAllProperties() {
127 if (properties != null) {
128 for (Entry<Object, Object> e : properties.entrySet()) {
129 if (e.getValue() != null) {
130 e.setValue(decrypt(e.getValue().toString()));
137 * @see java.lang.Object#equals(java.lang.Object)
140 public boolean equals(Object obj) {
146 if( this.getClass() != obj.getClass() ) {
150 DefaultConfiguration other = (DefaultConfiguration) obj;
152 if ((this.properties.size() == other.properties.size())
153 && (this.properties.entrySet().containsAll(other.properties.entrySet()))
154 && (other.properties.entrySet().containsAll(this.properties.entrySet()))) {
162 * This method will use the properties object to expand any variables that may be present in the template provided.
163 * Variables are represented by the string "${name}", where "name" is the name of a property defined in either the
164 * current configuration object, or system properties if undefined. If the value cannot be found, the variable is
165 * removed and an empty string is used to replace the variable.
168 * The template to be expanded
169 * @return The expanded template where each variable is replaced with its value
171 @SuppressWarnings("nls")
172 private String expandVariables(String template) {
173 if (template == null) {
177 // Decrypt the template if needed
178 // template = decrypt(template); DH: Do not assign values to parameters, bad form! Also, Sonar complains
181 StringBuffer buffer = new StringBuffer(decrypt(template));
182 Pattern pattern = Pattern.compile("\\$\\{([^\\}]+)\\}");
183 Matcher matcher = pattern.matcher(buffer);
184 while (matcher.find()) {
185 String variable = matcher.group(1);
186 String value = properties.getProperty(variable);
188 value = System.getProperty(variable);
193 buffer.replace(matcher.start(), matcher.end(), value);
197 return buffer.toString().trim();
201 * This method is called to obtain a property expressed as a boolean value (true or false). The standard rules for
202 * Boolean.parseBoolean() are used.
206 * @return The value of the property expressed as a boolean, or false if it does not exist.
208 @SuppressWarnings("nls")
210 public boolean getBooleanProperty(String key) {
211 return Boolean.valueOf(getProperty(key, "false")).booleanValue();
215 * This method is called to obtain a property expressed as a boolean value (true or false). The standard rules for
216 * Boolean.valueOf(String) are used.
220 * @param defaultValue
221 * The default value to be returned if the property does not exist
222 * @return The value of the property expressed as a boolean, or false if it does not exist.
223 * @see org.openecomp.appc.configuration.Configuration#getBooleanProperty(java.lang.String, boolean)
226 public boolean getBooleanProperty(String key, boolean defaultValue) {
227 if (isPropertyDefined(key)) {
228 return getBooleanProperty(key);
234 * Returns the indicated property value expressed as a floating point double-precision value (double).
237 * The property to retrieve
238 * @return The value of the property, or 0.0 if not found
239 * @see org.openecomp.appc.configuration.Configuration#getDoubleProperty(java.lang.String)
241 @SuppressWarnings("nls")
243 public double getDoubleProperty(String key) {
245 return Double.valueOf(getProperty(key, "0.0")).doubleValue();
246 } catch (NumberFormatException e) {
252 * This method is called to obtain a property as a string value
255 * The key of the property
256 * @param defaultValue
257 * The default value to be returned if the property does not exist
258 * @return The string value, or null if it does not exist.
259 * @see org.openecomp.appc.configuration.Configuration#getDoubleProperty(java.lang.String, double)
262 public double getDoubleProperty(String key, double defaultValue) {
263 if (isPropertyDefined(key)) {
264 return getDoubleProperty(key);
270 * Returns the property indicated expressed as an integer. The standard rules for
271 * {@link Integer#parseInt(String, int)} using a radix of 10 are used.
274 * The property name to retrieve.
275 * @returns The value of the property, or 0 if it does not exist or is invalid.
276 * @see org.openecomp.appc.configuration.Configuration#getIntegerProperty(java.lang.String)
278 @SuppressWarnings("nls")
280 public int getIntegerProperty(String key) {
282 return Integer.parseInt(getProperty(key, "0"), 10);
283 } catch (NumberFormatException e) {
289 * Returns the property indicated expressed as an integer. The standard rules for Integer.parseInt(String, int)
290 * using a radix of 10 are used.
293 * The property name to retrieve.
294 * @param defaultValue
295 * The default value to be returned if the property does not exist
296 * @return The value of the property, or 0 if it does not exist or is invalid.
297 * @see org.openecomp.appc.configuration.Configuration#getIntegerProperty(java.lang.String, int)
300 public int getIntegerProperty(String key, int defaultValue) {
301 if (isPropertyDefined(key)) {
302 return getIntegerProperty(key);
308 * Returns the specified property as a long integer value, if it exists, or zero if it does not.
311 * The key of the property desired.
312 * @return The value of the property expressed as an integer long value, or zero if the property does not exist or
313 * is not a valid integer long.
314 * @see org.openecomp.appc.configuration.Configuration#getLongProperty(java.lang.String)
316 @SuppressWarnings("nls")
318 public long getLongProperty(String key) {
320 return Long.parseLong(getProperty(key, "0"), 10);
321 } catch (NumberFormatException e) {
327 * Returns the specified property as a long integer value, if it exists, or the default value if it does not exist
331 * The key of the property desired.
332 * @param defaultValue
333 * the value to be returned if the property is not valid or does not exist.
334 * @return The value of the property expressed as an integer long value, or the default value if the property does
335 * not exist or is not a valid integer long.
336 * @see org.openecomp.appc.configuration.Configuration#getLongProperty(java.lang.String, long)
339 public long getLongProperty(String key, long defaultValue) {
340 if (isPropertyDefined(key)) {
341 return getLongProperty(key);
347 * This method can be called to retrieve a properties object that is immutable. Any attempt to modify the properties
348 * object returned will result in an exception. This allows a caller to view the current configuration as a set of
351 * @return An unmodifiable properties object.
352 * @see org.openecomp.appc.configuration.Configuration#getProperties()
355 public Properties getProperties() {
356 return new UnmodifiableProperties(properties);
360 * This method is called to obtain a property as a string value
363 * The key of the property
364 * @return The string value, or null if it does not exist.
367 public String getProperty(String key) {
368 String value = properties.getProperty(key);
372 return expandVariables(value.trim());
376 * This method is called to obtain a property as a string value
379 * The key of the property
380 * @param defaultValue
381 * The default value to be returned if the property does not exist
382 * @return The string value, or null if it does not exist.
383 * @see org.openecomp.appc.configuration.Configuration#getProperty(java.lang.String, java.lang.String)
386 public String getProperty(String key, String defaultValue) {
387 if (isPropertyDefined(key)) {
388 return getProperty(key);
391 if (defaultValue == null) {
395 return expandVariables(defaultValue.trim());
399 * @see java.lang.Object#hashCode()
402 public int hashCode() {
403 return (properties == null ? 0 : properties.hashCode());
407 * Returns true if the named property is defined, false otherwise.
410 * The key of the property we are interested in
411 * @return True if the property exists.
414 public boolean isPropertyDefined(String key) {
415 return properties.containsKey(key);
419 * Returns an indication of the validity of the boolean property. A boolean property is considered to be valid only
420 * if it has the value "true" or "false" (ignoring case).
423 * The property to be checked
424 * @returns True if the value is a boolean constant, or false if it does not exist or is not a correct string
425 * @see org.openecomp.appc.configuration.Configuration#isValidBoolean(java.lang.String)
427 @SuppressWarnings("nls")
429 public boolean isValidBoolean(String key) {
430 String value = getProperty(key);
432 value = value.toLowerCase();
433 return value.matches("true|false");
439 * Returns an indication if the indicated property represents a valid double-precision floating point number.
442 * The property to be examined
443 * @returns True if the property is a valid representation of a double, or false if it does not exist or contains
444 * illegal characters.
445 * @see org.openecomp.appc.configuration.Configuration#isValidDouble(java.lang.String)
448 public boolean isValidDouble(String key) {
449 String value = getProperty(key);
452 Double.valueOf(value);
454 } catch (NumberFormatException e) {
462 * Returns an indication if the property is a valid integer value or not.
465 * The key of the property to check
466 * @returns True if the value is a valid integer string, or false if it does not exist or contains illegal
468 * @see org.openecomp.appc.configuration.Configuration#isValidInteger(java.lang.String)
471 public boolean isValidInteger(String key) {
472 String value = getProperty(key);
475 Integer.parseInt(value.trim(), 10);
477 } catch (NumberFormatException e) {
485 * Determines is the specified property exists and is a valid representation of an integer long value.
488 * The property to be checked
489 * @return True if the property is a valid representation of an integer long value, and false if it either does not
490 * exist or is not valid.
491 * @see org.openecomp.appc.configuration.Configuration#isValidLong(java.lang.String)
494 public boolean isValidLong(String key) {
495 String value = getProperty(key);
498 Long.parseLong(value.trim(), 10);
500 } catch (NumberFormatException e) {
508 * This method allows an implementation to load configuration properties that may override default values.
511 * An input stream that contains the properties to be loaded
513 public void setProperties(InputStream is) {
516 } catch (IOException e) {
522 * This method allows an implementation to load configuration properties that may override default values.
525 * An optional Properties object to be merged into the configuration, replacing any same-named
527 * @see org.openecomp.appc.configuration.Configuration#setProperties(java.util.Properties)
530 public void setProperties(Properties props) {
531 properties.putAll(props);
532 decryptAllProperties();
536 * This method allows a caller to insert a new property definition into the configuration object. This allows the
537 * application to adjust or add to the current configuration. If the property already exists, it is replaced with
541 * The key of the property to be defined
543 * The value of the property to be defined
544 * @see org.openecomp.appc.configuration.Configuration#setProperty(java.lang.String, java.lang.String)
547 public void setProperty(String key, String value) {
548 properties.setProperty(key, decrypt(value));
552 * @see java.lang.Object#toString()
554 @SuppressWarnings("nls")
556 public String toString() {
557 return String.format("Configuration: %d properties, keys:[%s]", properties.size(), properties.keySet()
562 * This is a helper method to read the manifest of the jar file that this class was loaded from. Note that this will
563 * only work if the code is packaged in a jar file. If it is an open deployment, such as under eclipse, this will
564 * not work and there is code added to detect that case.
566 * @return The manifest object from the jar file, or null if the code is not packaged in a jar file.
571 private Manifest getManifest() {
572 ProtectionDomain domain = getClass().getProtectionDomain();
573 CodeSource source = domain.getCodeSource();
574 URL location = source.getLocation();
575 String path = location.getPath();
576 int index = path.indexOf('!');
578 path = path.substring(0, index);
580 if (path.endsWith(".jar")) {
581 try (JarFile jar = new JarFile(location.getFile())) {
582 return jar.getManifest();
583 } catch (IOException e) {
584 logger.error("getManifest", e);