From f5f13c4f6b6fe3b4d98e349dfd7db59339803436 Mon Sep 17 00:00:00 2001 From: Michael Lando Date: Sun, 19 Feb 2017 12:35:04 +0200 Subject: push addional code Change-Id: Ia427bb3460cda3a896f8faced2de69eaf3807b74 Signed-off-by: Michael Lando --- .../core/factory/AbstractContextFactory.java | 27 ++++++ .../core/factory/FactoriesConfigImpl.java | 61 ++++++++++++ .../org/openecomp/core/factory/FactoryConfig.java | 46 +++++++++ .../core/factory/api/AbstractComponentFactory.java | 105 +++++++++++++++++++++ .../core/factory/api/FactoriesConfiguration.java | 29 ++++++ 5 files changed, 268 insertions(+) create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/AbstractContextFactory.java create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoriesConfigImpl.java create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoryConfig.java create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/api/AbstractComponentFactory.java create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/api/FactoriesConfiguration.java (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src') diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/AbstractContextFactory.java b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/AbstractContextFactory.java new file mode 100644 index 0000000000..f4e1a85fee --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/AbstractContextFactory.java @@ -0,0 +1,27 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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; + +import org.openecomp.core.factory.impl.AbstractFactoryBase; + +public abstract class AbstractContextFactory extends AbstractFactoryBase { + public abstract I createInterface(C contextType); +} 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 new file mode 100644 index 0000000000..173f9b15b1 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoriesConfigImpl.java @@ -0,0 +1,61 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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; + + +import org.openecomp.core.factory.api.FactoriesConfiguration; +import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.core.utilities.json.JsonUtil; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public final class FactoriesConfigImpl implements FactoriesConfiguration { + + + private static final String FACTORY_CONFIG_FILE_NAME = "factoryConfiguration.json"; + private static Map factoryMap = new HashMap(); + private static boolean initialized = false; + + @SuppressWarnings("unchecked") + @Override + public Map getFactoriesMap() { + synchronized (this) { + if (!initialized) { + init(); + initialized = true; + } + } + return factoryMap; + } + + private void init() { + List factoryConfigIsList = FileUtils.getFileInputStreams(FACTORY_CONFIG_FILE_NAME); + for (InputStream factoryConfigIs : factoryConfigIsList) { + factoryMap.putAll(JsonUtil.json2Object(factoryConfigIs, Map.class)); + } + } + + +} + diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoryConfig.java b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoryConfig.java new file mode 100644 index 0000000000..d664cbee38 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoryConfig.java @@ -0,0 +1,46 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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; + +import org.openecomp.core.factory.api.FactoriesConfiguration; +import org.openecomp.core.utilities.CommonMethods; + +import java.util.Map; + +public final class FactoryConfig { + + private static final FactoriesConfiguration INSTANCE; + + static { + + try { + INSTANCE = CommonMethods.newInstance( + "org.openecomp.core.factory.FactoriesConfigImpl", FactoriesConfiguration.class); + } catch (Exception exception) { + exception.printStackTrace(); + throw exception; + } + } + + public static Map getFactoriesMap() { + return INSTANCE.getFactoriesMap(); + } +} 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 new file mode 100644 index 0000000000..9cd7749f55 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/api/AbstractComponentFactory.java @@ -0,0 +1,105 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.api; + +import org.openecomp.core.factory.FactoryConfig; +import org.openecomp.core.factory.impl.AbstractFactoryBase; +import org.openecomp.core.utilities.CommonMethods; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.errors.ErrorCategory; +import org.openecomp.sdc.common.errors.ErrorCode; + +import java.util.Map; + +public abstract class AbstractComponentFactory extends AbstractFactory { + + static { + Registry registry = new RegistryImpl(); + InitializationHelper.registerFactoryMapping(registry); + } + + interface Registry { + void register(String factory, String impl); + } + + private static class RegistryImpl implements Registry { + @Override + public void register(String factory, String impl) { + AbstractFactoryBase.registerFactory(factory, impl); + } + } + + static class InitializationHelper { + + + private static boolean isRegistered = false; + + private InitializationHelper() { + } + + static synchronized boolean registerFactoryMapping(Registry registry) { + + boolean done = !isRegistered; + + if (!isRegistered) { + registerFactoryMappingImpl(registry); + isRegistered = true; + } + + return done; + } + + private static void registerFactoryMappingImpl(Registry registry) { + Map factoryMap = FactoryConfig.getFactoriesMap(); + + try { + for (Map.Entry entry : factoryMap.entrySet()) { + String abstractClassName = entry.getKey(); + String concreteTypeName = entry.getValue(); + + if (CommonMethods.isEmpty(concreteTypeName)) { + throw new CoreException( + new ErrorCode.ErrorCodeBuilder().withId("E0003") + .withMessage("Missing configuration value:" + concreteTypeName + ".") + .withCategory(ErrorCategory.SYSTEM).build()); + + } + + registry.register(abstractClassName, concreteTypeName); + } + } catch (RuntimeException exception) { + throw exception; + } catch (Exception exception) { + throw new RuntimeException(exception); + } + } + + @SuppressWarnings("unchecked") + private static Class unsecureCast(Class cls) { + return (Class) cls; + } + + private static String nameOf(Class clazz) { + return (clazz != null) ? clazz.getName() : "null"; + } + } + +} diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/api/FactoriesConfiguration.java b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/api/FactoriesConfiguration.java new file mode 100644 index 0000000000..8086181204 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/api/FactoriesConfiguration.java @@ -0,0 +1,29 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.api; + +import java.util.Map; + + +public interface FactoriesConfiguration { + + Map getFactoriesMap(); +} -- cgit 1.2.3-korg