From da5dfcd1a354807d13849c7f4ead535ccfa722fa Mon Sep 17 00:00:00 2001 From: avigaffa Date: Sun, 31 Dec 2017 15:07:39 +0200 Subject: Fixing sonar Exception Handling Change-Id: I04eb047973a3f5c07dd9dc410cb13af974e8ded1 Issue-ID: SDC-810 Signed-off-by: avigaffa Signed-off-by: vempo --- .../core/factory/impl/AbstractFactoryBase.java | 56 +++++++++++----------- .../core/factory/FactoriesConfigImpl.java | 23 ++++----- .../core/factory/api/AbstractComponentFactory.java | 3 +- 3 files changed, 39 insertions(+), 43 deletions(-) (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib') 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 registry = new ConcurrentHashMap(); + private static final Map REGISTRY = new ConcurrentHashMap<>(); /** * Cached factory instances. */ - private static Map factoryMap = - new ConcurrentHashMap(); + private static final Map 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 factorylist = factoryMap.values(); + Collection 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 extends AbstractFactory { 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 + ".") -- cgit 1.2.3-korg