diff options
author | kjaniak <kornel.janiak@nokia.com> | 2018-02-26 14:52:50 +0100 |
---|---|---|
committer | Patrick Brady <pb071s@att.com> | 2018-02-28 23:08:22 +0000 |
commit | f5efd33ea7f230c52135b55ecd4ac254ea7de692 (patch) | |
tree | c775a4ead693ba191c90f7fcb02f588b1dbef394 /appc-common | |
parent | b45d2ce251d5e93b26298dbe352ea01c171aad92 (diff) |
Extraction of try catch in ConfigurationFactory
Try catch block placed in new private method getClonedDefaultConfiguration,
minor changes added, UT for class added.
Change-Id: Ic908e6d3a8fe179a2a38d922d10e24cff76a21da
Issue-ID: APPC-674
Signed-off-by: kjaniak <kornel.janiak@nokia.com>
Diffstat (limited to 'appc-common')
-rw-r--r-- | appc-common/src/main/java/org/onap/appc/configuration/ConfigurationFactory.java | 54 | ||||
-rw-r--r-- | appc-common/src/test/java/org/onap/appc/configuration/ConfigurationFactoryTest.java | 15 |
2 files changed, 44 insertions, 25 deletions
diff --git a/appc-common/src/main/java/org/onap/appc/configuration/ConfigurationFactory.java b/appc-common/src/main/java/org/onap/appc/configuration/ConfigurationFactory.java index a0fda4033..06d58a05a 100644 --- a/appc-common/src/main/java/org/onap/appc/configuration/ConfigurationFactory.java +++ b/appc-common/src/main/java/org/onap/appc/configuration/ConfigurationFactory.java @@ -32,6 +32,7 @@ import java.io.InputStream; import java.text.DateFormat; import java.util.Date; import java.util.HashMap; +import java.util.Optional; import java.util.Properties; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock; @@ -104,7 +105,7 @@ import com.att.eelf.i18n.EELFResourceManager; */ public final class ConfigurationFactory { - private static final EELFLogger logger = EELFManager.getInstance().getApplicationLogger(); + private static final EELFLogger logger = EELFManager.getInstance().getLogger(ConfigurationFactory.class); /** * This is a string constant for the comma character. It's intended to be used a common string @@ -177,17 +178,17 @@ public final class ConfigurationFactory { config = new DefaultConfiguration(); initialize(null); } - } catch (Exception t) { - logger.error("getConfiguration", t); + } catch (Exception e){ + logger.error("getConfiguration", e); } finally { writeLock.unlock(); } readLock.lock(); } - return config; } finally { readLock.unlock(); } + return config; } /** @@ -201,34 +202,26 @@ public final class ConfigurationFactory { * can be altered if needed. */ public static Configuration getConfiguration(final Object owner) { + DefaultConfiguration local; ReadLock readLock = lock.readLock(); readLock.lock(); try { - DefaultConfiguration local = (DefaultConfiguration) localConfigs.get(owner); + local = (DefaultConfiguration) localConfigs.get(owner); if (local == null) { readLock.unlock(); WriteLock writeLock = lock.writeLock(); writeLock.lock(); - try { - local = (DefaultConfiguration) localConfigs.get(owner); - if (local == null) { - DefaultConfiguration global = (DefaultConfiguration) getConfiguration(); - try { - local = (DefaultConfiguration) global.clone(); - } catch (CloneNotSupportedException e) { - logger.error("getConfiguration", e); - } - localConfigs.put(owner, local); - } - } finally { - writeLock.unlock(); + local = (DefaultConfiguration) localConfigs.get(owner); + if (local == null) { + local = getClonedDefaultConfiguration(owner, local); } - readLock.lock(); + writeLock.unlock(); } - return local; + readLock.lock(); } finally { readLock.unlock(); } + return local; } /** @@ -260,6 +253,20 @@ public final class ConfigurationFactory { } } + private static DefaultConfiguration getClonedDefaultConfiguration(Object owner, DefaultConfiguration local) { + Optional<DefaultConfiguration> global = + Optional.ofNullable((DefaultConfiguration) getConfiguration()); + try { + if (global.isPresent()) { + local = (DefaultConfiguration) global.get().clone(); + } + } catch (CloneNotSupportedException e) { + logger.error("getClonedDefaultConfiguration", e); + } + localConfigs.put(owner, local); + return local; + } + /** * This method will clear the current configuration and then re-initialize it with the default * values, application-specific configuration file, user-supplied properties (if any), and then @@ -302,8 +309,7 @@ public final class ConfigurationFactory { try { in.close(); } catch (IOException e) { - // not much we can do since logger may not be configured yet - e.printStackTrace(System.out); + logger.error("Cannot close inputStream", e); } } for (String key : config.getProperties().stringPropertyNames()) { @@ -367,9 +373,7 @@ public final class ConfigurationFactory { stream.close(); } } catch (IOException e) { - // not much we can do since logger may not be configured - // yet - e.printStackTrace(System.out); + logger.error("Unable to close stream", e); } } } diff --git a/appc-common/src/test/java/org/onap/appc/configuration/ConfigurationFactoryTest.java b/appc-common/src/test/java/org/onap/appc/configuration/ConfigurationFactoryTest.java new file mode 100644 index 000000000..7d0206467 --- /dev/null +++ b/appc-common/src/test/java/org/onap/appc/configuration/ConfigurationFactoryTest.java @@ -0,0 +1,15 @@ +package org.onap.appc.configuration; + +import org.junit.Assert; +import org.junit.Test; + +import static org.onap.appc.configuration.ConfigurationFactory.getConfiguration; + +public class ConfigurationFactoryTest { + @Test + public void should_returnDefaultConfiguration(){ + Configuration conf = null; + + Assert.assertTrue(getConfiguration() instanceof DefaultConfiguration); + } +}
\ No newline at end of file |