From fe4aff78dd7efe4feb092ba8025077ba1b419905 Mon Sep 17 00:00:00 2001 From: siddharth0905 Date: Tue, 26 Dec 2017 12:37:18 +0530 Subject: Fix sonar issues Added static inner class for initializing system prop Change-Id: I63c23640058a68c7f8d228a2a509d1b440fd20e6 Issue-ID: SDC-343 Signed-off-by: siddharth0905 --- .../impl/CassandraConnectionInitializer.java | 132 +++++++++++---------- 1 file changed, 67 insertions(+), 65 deletions(-) (limited to 'openecomp-be/lib') diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/CassandraConnectionInitializer.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/CassandraConnectionInitializer.java index 0115ab6d5b..8eeeb1e27b 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/CassandraConnectionInitializer.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/CassandraConnectionInitializer.java @@ -1,33 +1,46 @@ -package org.openecomp.core.zusammen.impl; +/* + * Copyright © 2016-2017 European Support Limited + * + * 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. + */ +package org.openecomp.core.zusammen.impl; import org.apache.commons.lang3.StringUtils; + import org.openecomp.core.nosqldb.util.CassandraUtils; +import java.util.function.Supplier; + import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; -import java.util.Objects; - -/** - * @author Avrahamg - * @since April 25, 2017 - */ public class CassandraConnectionInitializer implements ServletContextListener { - private static String DATA_CENTER_PROPERTY_NAME = "cassandra.datacenter"; - private static String CONSISTENCY_LEVEL_PROPERTY_NAME = "cassandra.consistency.level"; - private static String NODES_PROPERTY_NAME = "cassandra.nodes"; - private static String AUTHENTICATE_PROPERTY_NAME = "cassandra.authenticate"; - private static String TRUE = "true"; - private static String FALSE = "false"; - private static String SSL_PROPERTY_NAME = "cassandra.ssl"; - private static String TRUSTSTORE_PROPERTY_NAME = "cassandra.truststore"; - private static String TRUSTSTORE_PASSWORD_PROPERTY_NAME = "cassandra.truststore.password"; - private static String USER_PROPERTY_NAME = "cassandra.user"; - private static String PASSWORD_PROPERTY_NAME = "cassandra.password"; - private static String KEYSPACE_PROPERTY_NAME = "cassandra.keyspace"; - private static String ZUSAMMEN = "zusammen"; + private static final String CASSANDRA_PREFIX = "cassandra."; + private static final String DATA_CENTER_PROPERTY_NAME = CASSANDRA_PREFIX + "datacenter"; + private static final String CONSISTENCY_LEVEL_PROPERTY_NAME = + CASSANDRA_PREFIX + "consistency.level"; + private static final String NODES_PROPERTY_NAME = CASSANDRA_PREFIX + "nodes"; + private static final String AUTHENTICATE_PROPERTY_NAME = CASSANDRA_PREFIX + "authenticate"; + private static final String SSL_PROPERTY_NAME = CASSANDRA_PREFIX + "ssl"; + private static final String TRUSTSTORE_PROPERTY_NAME = CASSANDRA_PREFIX + "truststore"; + private static final String TRUSTSTORE_PASSWORD_PROPERTY_NAME = + CASSANDRA_PREFIX + "truststore.password"; + private static final String USER_PROPERTY_NAME = CASSANDRA_PREFIX + "user"; + private static final String PASSWORD_PROPERTY_NAME = CASSANDRA_PREFIX + "password"; + private static final String KEYSPACE_PROPERTY_NAME = CASSANDRA_PREFIX + "keyspace"; + private static final String ZUSAMMEN = "zusammen"; @Override public void contextInitialized(ServletContextEvent servletContextEvent) { @@ -35,64 +48,53 @@ public class CassandraConnectionInitializer implements ServletContextListener { } public static void setCassandraConnectionPropertiesToSystem() { + DeferredInitializer.init(); + } - if (!System.getProperties().containsKey(NODES_PROPERTY_NAME)) { - System.setProperty(NODES_PROPERTY_NAME, StringUtils.join(CassandraUtils.getAddresses(), ',')); - } - - if (!System.getProperties().containsKey(AUTHENTICATE_PROPERTY_NAME)) { - System.setProperty(AUTHENTICATE_PROPERTY_NAME, - CassandraUtils.isAuthenticate() ? TRUE : FALSE); - } - if (!System.getProperties().containsKey(SSL_PROPERTY_NAME)) { - System.setProperty(SSL_PROPERTY_NAME, - CassandraUtils.isSsl() ? TRUE : FALSE); - } - - if (!System.getProperties().containsKey(TRUSTSTORE_PROPERTY_NAME)) { - System.setProperty(TRUSTSTORE_PROPERTY_NAME, CassandraUtils.getTruststore()); - } + @Override + public void contextDestroyed(ServletContextEvent servletContextEvent) { + // no-op, required by the interface + } - if (!System.getProperties().containsKey(TRUSTSTORE_PASSWORD_PROPERTY_NAME)) { - System.setProperty(TRUSTSTORE_PASSWORD_PROPERTY_NAME, CassandraUtils.getTruststorePassword()); + private static class DeferredInitializer { + + static { + setSystemProperty(NODES_PROPERTY_NAME, () -> + StringUtils.join(CassandraUtils.getAddresses(), ',')); + setBooleanSystemProperty(AUTHENTICATE_PROPERTY_NAME, CassandraUtils::isAuthenticate); + setBooleanSystemProperty(SSL_PROPERTY_NAME, CassandraUtils::isSsl); + setSystemProperty(TRUSTSTORE_PROPERTY_NAME, CassandraUtils::getTruststore); + setSystemProperty(TRUSTSTORE_PASSWORD_PROPERTY_NAME, CassandraUtils::getTruststorePassword); + setSystemProperty(USER_PROPERTY_NAME, CassandraUtils::getUser); + setSystemProperty(PASSWORD_PROPERTY_NAME, CassandraUtils::getPassword); + setSystemProperty(KEYSPACE_PROPERTY_NAME, () -> ZUSAMMEN); + setNullableSystemProperty(DATA_CENTER_PROPERTY_NAME, CassandraUtils::getLocalDataCenter); + setNullableSystemProperty(CONSISTENCY_LEVEL_PROPERTY_NAME, + CassandraUtils::getConsistencyLevel); } - if (!System.getProperties().containsKey(USER_PROPERTY_NAME)) { - System.setProperty(USER_PROPERTY_NAME, CassandraUtils.getUser()); - } + private static void setSystemProperty(String name, Supplier valueSupplier) { - if (!System.getProperties().containsKey(PASSWORD_PROPERTY_NAME)) { - System.setProperty(PASSWORD_PROPERTY_NAME, CassandraUtils.getPassword()); + if (System.getProperty(name) == null) { + System.setProperty(name, valueSupplier.get()); + } } - if (!System.getProperties().containsKey(KEYSPACE_PROPERTY_NAME)) { - System.setProperty(KEYSPACE_PROPERTY_NAME, ZUSAMMEN); + private static void setBooleanSystemProperty(String name, Supplier valueSupplier) { + setSystemProperty(name, () -> Boolean.toString(valueSupplier.get())); } + private static void setNullableSystemProperty(String name, Supplier valueSupplier) { - if (!System.getProperties().containsKey(DATA_CENTER_PROPERTY_NAME)) { - String dataCenter = CassandraUtils.getLocalDataCenter(); - if (Objects.nonNull(dataCenter)) { - System.setProperty(DATA_CENTER_PROPERTY_NAME, dataCenter); - } - } - - if (!System.getProperties().containsKey(CONSISTENCY_LEVEL_PROPERTY_NAME)) { - String consistencyLevel = CassandraUtils.getConsistencyLevel(); - if (Objects.nonNull(consistencyLevel)) { - System.setProperty(CONSISTENCY_LEVEL_PROPERTY_NAME, consistencyLevel); + if ((System.getProperty(name) == null) && (valueSupplier.get() != null)) { + System.setProperty(name, valueSupplier.get()); } } + private DeferredInitializer() { } - } - - // -Dcassandra.nodes=10.147.97.145 -Dcassandra.keyspace=zusammen -Dcassandra.authenticate=true -Dcassandra.ssl=true - // -Dcassandra.truststore=/apps/jetty/base/be/config/.truststore -Dcassandra.truststore.password=Aa123456 - // -Dcassandra.user=asdc_user -Dcassandra.password=Aa1234%^! - - @Override - public void contextDestroyed(ServletContextEvent servletContextEvent) { - + public static void init() { + // just to ensure static initialization + } } } -- cgit 1.2.3-korg