aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api/src/main/java/org/openecomp/core/factory/impl/FactoryManager.java
blob: ec2d93f35b4f2572e47fa083be2c16c48f34ae50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation
 *  ================================================================================
 *  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.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.openecomp.core.factory.impl;

import com.amdocs.zusammen.utils.facade.impl.FactoryConfig;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.StringUtils;
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;

public class FactoryManager {

    private static final FactoryManager instance = new FactoryManager();
    private static final String ERROR_CODE_E0001 = "E0001";
    /**
     * Temporary registry of default implementations. The map keeps class names rather then class
     * types to allow unloading of those classes from memory by garbage collector if factory is not
     * actually used.
     */
    private final Map<String, String> factoryRegistry = new ConcurrentHashMap<>();
    /**
     * Cached factory instances.
     */
    private final Map<String, AbstractFactoryBase> factoryInstanceMap = new ConcurrentHashMap<>();


    private FactoryManager() {
        initializeFactoryRegistry();
    }

    public static synchronized FactoryManager getInstance() {
        return instance;
    }

    private void initializeFactoryRegistry() {
        final Map<String, String> factoryMap = FactoryConfig.getFactoriesMap();

        for (final Map.Entry<String, String> entry : factoryMap.entrySet()) {
            final String abstractClassName = entry.getKey();
            final String concreteTypeName = entry.getValue();

            if (StringUtils.isEmpty(concreteTypeName)) {
                throw new CoreException(
                    new ErrorCode.ErrorCodeBuilder().withId("E0003")
                        .withMessage("Missing configuration value:" + concreteTypeName + ".")
                        .withCategory(ErrorCategory.SYSTEM).build());

            }
            registerFactory(abstractClassName, concreteTypeName);
        }
    }

    /**
     * Instantiates the configured implementation of an abstract factory.
     *
     * @param <F>         Type specific abstract factory for concrete Java interface
     * @param factoryType Java class of type specific abstract factory
     * @return Instance of implementation class
     */
    @SuppressWarnings("unchecked")
    public <F extends AbstractFactoryBase> F getFactoryInstance(Class<F> factoryType) {
        if (factoryType == null) {
            throw new CoreException(
                new ErrorCode.ErrorCodeBuilder().withId(ERROR_CODE_E0001)
                    .withMessage("Mandatory input factory type.").withCategory(ErrorCategory.SYSTEM)
                    .build());

        }
        final String factoryTypeName = factoryType.getName();
        // Check if the factory is already cached
        if (factoryInstanceMap.get(factoryTypeName) == null) {
            //if not, create a new one and cache it
            // Get the implementation class name
            final String implName = factoryRegistry.get(factoryTypeName);

            if (StringUtils.isEmpty(implName)) {
                throw new CoreException(
                    new ErrorCode.ErrorCodeBuilder().withId(ERROR_CODE_E0001)
                        .withMessage("Mandatory input factory implementation.")
                        .withCategory(ErrorCategory.SYSTEM).build());
            }

            F factory = CommonMethods.newInstance(implName, factoryType);
            factory.init();
            // Cache the instantiated singleton
            factoryInstanceMap.putIfAbsent(factoryTypeName, factory);
        }

        return (F) factoryInstanceMap.get(factoryTypeName);
    }

    public void registerFactory(final String factoryName, final String implName) {
        factoryRegistry.put(factoryName, implName);
    }

    /**
     * Unregister factory and removes the cached instance if any.
     * @param factoryName the factory name to unregister
     */
    public void unregisterFactory(final String factoryName) {
        final String factoryClass = factoryRegistry.get(factoryName);
        if (StringUtils.isNotEmpty(factoryClass)) {
            factoryInstanceMap.remove(factoryClass);
            factoryRegistry.remove(factoryName);
        }
    }

    /**
     * Removes the cached factory instance if any.
     *
     * @param <F>     the type parameter
     * @param factory the factory
     */
    public <F extends AbstractFactoryBase> void removeFactoryInstance(Class<F> factory) {
        if (factory == null) {
            throw new CoreException(
                new ErrorCode.ErrorCodeBuilder().withId(ERROR_CODE_E0001).withMessage("Mandatory input factory.")
                    .withCategory(ErrorCategory.SYSTEM).build());
        }

        factoryInstanceMap.remove(factory.getName());
    }

    /**
     * Stop all.
     */
    public void stopAll() {
        factoryInstanceMap.values().forEach(AbstractFactoryBase::stop);
    }
}