diff options
author | avigaffa <avi.gaffa@amdocs.com> | 2017-12-31 15:07:39 +0200 |
---|---|---|
committer | vempo <vitaliy.emporopulo@amdocs.com> | 2018-01-02 09:41:41 +0200 |
commit | da5dfcd1a354807d13849c7f4ead535ccfa722fa (patch) | |
tree | b4ef0b97d4c25a38bf4a7a8aa5d3626d6b820a17 /openecomp-be/lib/openecomp-core-lib | |
parent | 25ca73d85757869a6d9cc33861070778503ff153 (diff) |
Fixing sonar Exception Handling
Change-Id: I04eb047973a3f5c07dd9dc410cb13af974e8ded1
Issue-ID: SDC-810
Signed-off-by: avigaffa <avi.gaffa@amdocs.com>
Signed-off-by: vempo <vitaliy.emporopulo@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-core-lib')
9 files changed, 297 insertions, 516 deletions
diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api/src/main/java/org/openecomp/core/factory/impl/AbstractFactoryBase.java b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api/src/main/java/org/openecomp/core/factory/impl/AbstractFactoryBase.java index 7b53f3416a..cff9da0d5c 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api/src/main/java/org/openecomp/core/factory/impl/AbstractFactoryBase.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api/src/main/java/org/openecomp/core/factory/impl/AbstractFactoryBase.java @@ -1,26 +1,23 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ +/* + * 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. - * ============LICENSE_END========================================================= */ package org.openecomp.core.factory.impl; +import org.apache.commons.lang3.StringUtils; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.common.errors.ErrorCategory; import org.openecomp.sdc.common.errors.ErrorCode; @@ -29,7 +26,6 @@ import java.util.Collection; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import static org.openecomp.core.utilities.CommonMethods.isEmpty; import static org.openecomp.core.utilities.CommonMethods.newInstance; public abstract class AbstractFactoryBase { @@ -39,13 +35,12 @@ public abstract class AbstractFactoryBase { * types to allow unloading of those classes from memory by garbage collector if factory is not * actually used. */ - private static Map<String, String> registry = new ConcurrentHashMap<String, String>(); + private static final Map<String, String> REGISTRY = new ConcurrentHashMap<>(); /** * Cached factory instances. */ - private static Map<String, AbstractFactoryBase> factoryMap = - new ConcurrentHashMap<String, AbstractFactoryBase>(); + private static final Map<String, AbstractFactoryBase> FACTORY_MAP = new ConcurrentHashMap<>(); /** * Registers implementor for an abstract factory. The method accepts Java classes rather then @@ -69,15 +64,15 @@ public abstract class AbstractFactoryBase { new ErrorCode.ErrorCodeBuilder().withId("E0001").withMessage("Mandatory input impl.") .withCategory(ErrorCategory.SYSTEM).build()); } - if (factoryMap != null && factoryMap.containsKey(factory.getName())) { - factoryMap.remove(factory.getName()); + if (FACTORY_MAP.containsKey(factory.getName())) { + FACTORY_MAP.remove(factory.getName()); } - registry.put(factory.getName(), impl.getName()); + REGISTRY.put(factory.getName(), impl.getName()); } // registerFactory // TODO: Remove protected static void registerFactory(String factoryName, String implName) { - registry.put(factoryName, implName); + REGISTRY.put(factoryName, implName); } // registerFactory /** @@ -92,9 +87,8 @@ public abstract class AbstractFactoryBase { new ErrorCode.ErrorCodeBuilder().withId("E0001").withMessage("Mandatory input factory.") .withCategory(ErrorCategory.SYSTEM).build()); } - if (factoryMap != null) { - factoryMap.remove(factory.getName()); - } + + FACTORY_MAP.remove(factory.getName()); } /** @@ -115,18 +109,18 @@ public abstract class AbstractFactoryBase { } // Pick up factory instance from cache - F factory = (F) factoryMap.get(factoryType.getName()); + F factory = (F) FACTORY_MAP.get(factoryType.getName()); // Check for the first time access if (factory == null) { // Synchronize factory instantiation synchronized (factoryType) { // Re-check the factory instance - factory = (F) factoryMap.get(factoryType.getName()); + factory = (F) FACTORY_MAP.get(factoryType.getName()); if (factory == null) { // Get the implementation class name - String implName = registry.get(factoryType.getName()); + String implName = REGISTRY.get(factoryType.getName()); - if (isEmpty(implName)) { + if (StringUtils.isEmpty(implName)) { throw new CoreException( new ErrorCode.ErrorCodeBuilder().withId("E0001") .withMessage("Mandatory input factory implementation.") @@ -138,7 +132,7 @@ public abstract class AbstractFactoryBase { factory.init(); // Cache the instantiated singleton - factoryMap.put(factoryType.getName(), factory); + FACTORY_MAP.put(factoryType.getName(), factory); } } } @@ -164,14 +158,14 @@ public abstract class AbstractFactoryBase { .build()); } // Pick up factory instance from cache - F factory = (F) factoryMap.get(factoryType.getName()); + F factory = (F) FACTORY_MAP.get(factoryType.getName()); // Check for the first time access if (factory != null) { isFactoryRegistered = true; } else { // Get the implementation class name - String implName = registry.get(factoryType.getName()); - if (!isEmpty(implName)) { + String implName = REGISTRY.get(factoryType.getName()); + if (StringUtils.isNotEmpty(implName)) { isFactoryRegistered = true; } } @@ -182,16 +176,20 @@ public abstract class AbstractFactoryBase { * Stop all. */ public static void stopAll() { - Collection<AbstractFactoryBase> factorylist = factoryMap.values(); + Collection<AbstractFactoryBase> factorylist = FACTORY_MAP.values(); for (AbstractFactoryBase factory : factorylist) { factory.stop(); } } protected void init() { + // allows custom initialization + // noop by default } protected void stop() { + // allows custom shutdown + // noop by default } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoriesConfigImpl.java b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoriesConfigImpl.java index b9d03f4d1a..15b9f8c1c8 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoriesConfigImpl.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoriesConfigImpl.java @@ -1,21 +1,17 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ +/* + * 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. - * ============LICENSE_END========================================================= */ package org.openecomp.core.factory; @@ -24,6 +20,7 @@ package org.openecomp.core.factory; import org.openecomp.core.factory.api.FactoriesConfiguration; import org.openecomp.core.utilities.file.FileUtils; import org.openecomp.core.utilities.json.JsonUtil; +import org.openecomp.sdc.common.errors.SdcConfigurationException; import java.io.IOException; import java.io.InputStream; @@ -36,7 +33,7 @@ public final class FactoriesConfigImpl implements FactoriesConfiguration { private static final String FACTORY_CONFIG_FILE_NAME = "factoryConfiguration.json"; - private static Map factoryMap = new HashMap(); + private static final Map FACTORY_MAP = new HashMap(); private static boolean initialized = false; @SuppressWarnings("unchecked") @@ -48,7 +45,7 @@ public final class FactoriesConfigImpl implements FactoriesConfiguration { initialized = true; } } - return factoryMap; + return FACTORY_MAP; } private void init() { @@ -57,9 +54,9 @@ public final class FactoriesConfigImpl implements FactoriesConfiguration { for (URL factoryConfigUrl : factoryConfigUrlList) { try (InputStream stream = factoryConfigUrl.openStream()) { - factoryMap.putAll(JsonUtil.json2Object(stream, Map.class)); + FACTORY_MAP.putAll(JsonUtil.json2Object(stream, Map.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new SdcConfigurationException("Failed to initialize Factory from '" + factoryConfigUrl.getPath() +"'", e); } } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/api/AbstractComponentFactory.java b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/api/AbstractComponentFactory.java index 102c6db572..24f71e7953 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/api/AbstractComponentFactory.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/api/AbstractComponentFactory.java @@ -16,6 +16,7 @@ package org.openecomp.core.factory.api; +import org.apache.commons.lang3.StringUtils; import org.openecomp.core.factory.FactoryConfig; import org.openecomp.core.factory.impl.AbstractFactoryBase; import org.openecomp.core.utilities.CommonMethods; @@ -66,7 +67,7 @@ public abstract class AbstractComponentFactory<I> extends AbstractFactory<I> { String abstractClassName = entry.getKey(); String concreteTypeName = entry.getValue(); - if (CommonMethods.isEmpty(concreteTypeName)) { + if (StringUtils.isEmpty(concreteTypeName)) { throw new CoreException( new ErrorCode.ErrorCodeBuilder().withId("E0003") .withMessage("Missing configuration value:" + concreteTypeName + ".") diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraSessionFactory.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraSessionFactory.java index 519d7b160b..c6b9160bad 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraSessionFactory.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraSessionFactory.java @@ -1,21 +1,17 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ +/* + * 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. - * ============LICENSE_END========================================================= */ package org.openecomp.core.nosqldb.impl.cassandra; @@ -28,147 +24,143 @@ import com.datastax.driver.core.Session; import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; import com.datastax.driver.core.policies.LoadBalancingPolicy; import com.datastax.driver.core.policies.TokenAwarePolicy; -import com.google.common.base.Optional; import org.openecomp.core.nosqldb.util.CassandraUtils; +import org.openecomp.sdc.common.errors.SdcConfigurationException; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManagerFactory; import java.io.FileInputStream; -import java.io.IOException; -import java.security.KeyManagementException; import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; -import java.security.UnrecoverableKeyException; -import java.security.cert.CertificateException; import java.util.Objects; +import java.util.Optional; public class CassandraSessionFactory { - private static final Logger log = (Logger) LoggerFactory.getLogger - (CassandraSessionFactory.class.getName()); - - public static Session getSession() { - return ReferenceHolder.CASSANDRA; - } - - /** - * New cassandra session session. - * - * @return the session - */ - public static Session newCassandraSession() { - Cluster.Builder builder = Cluster.builder(); - String[] addresses = CassandraUtils.getAddresses(); - for (String address : addresses) { - builder.addContactPoint(address); - } + private static final Logger LOGGER = LoggerFactory.getLogger(CassandraSessionFactory.class); - //Check if ssl - Boolean isSsl = CassandraUtils.isSsl(); - if (isSsl) { - builder.withSSL(getSslOptions().get()); + private CassandraSessionFactory() { + // static methods, cannot be instantiated } - int port = CassandraUtils.getCassandraPort(); - if (port > 0) { - builder.withPort(port); + + public static Session getSession() { + return ReferenceHolder.CASSANDRA; } - //Check if user/pass - Boolean isAuthenticate = CassandraUtils.isAuthenticate(); - if (isAuthenticate) { - builder.withCredentials(CassandraUtils.getUser(), CassandraUtils.getPassword()); + + /** + * New cassandra session session. + * + * @return the session + */ + public static Session newCassandraSession() { + Cluster.Builder builder = Cluster.builder(); + String[] addresses = CassandraUtils.getAddresses(); + for (String address : addresses) { + builder.addContactPoint(address); + } + + //Check if ssl + Boolean isSsl = CassandraUtils.isSsl(); + if (isSsl) { + builder.withSSL(getSslOptions()); + } + int port = CassandraUtils.getCassandraPort(); + if (port > 0) { + builder.withPort(port); + } + //Check if user/pass + Boolean isAuthenticate = CassandraUtils.isAuthenticate(); + if (isAuthenticate) { + builder.withCredentials(CassandraUtils.getUser(), CassandraUtils.getPassword()); + } + + setConsistencyLevel(builder, addresses); + + setLocalDataCenter(builder); + + + Cluster cluster = builder.build(); + String keyStore = CassandraUtils.getKeySpace(); + return cluster.connect(keyStore); } - setConsistencyLevel(builder, addresses); + private static void setLocalDataCenter(Cluster.Builder builder) { + String localDataCenter = CassandraUtils.getLocalDataCenter(); + if (Objects.nonNull(localDataCenter)) { + LOGGER.info("localDatacenter was provided, setting Cassndra client to use datacenter: {} as " + + "local.", localDataCenter); + + LoadBalancingPolicy tokenAwarePolicy = new TokenAwarePolicy( + DCAwareRoundRobinPolicy.builder().withLocalDc(localDataCenter).build()); + builder.withLoadBalancingPolicy(tokenAwarePolicy); + } else { + LOGGER.info( + "localDatacenter was provided, the driver will use the datacenter of the first contact " + + "point that was reached at initialization"); + } + } - setLocalDataCenter(builder); + private static void setConsistencyLevel(Cluster.Builder builder, String[] addresses) { + if (addresses != null && addresses.length > 1) { + String consistencyLevel = CassandraUtils.getConsistencyLevel(); + if (Objects.nonNull(consistencyLevel)) { + LOGGER.info( + "consistencyLevel was provided, setting Cassandra client to use consistencyLevel: {}" + + " as " + , consistencyLevel); + builder.withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf + (consistencyLevel))); + } + } + } + private static SSLOptions getSslOptions() { - Cluster cluster = builder.build(); - String keyStore = CassandraUtils.getKeySpace(); - return cluster.connect(keyStore); - } + Optional< |