2a9b8184b3f6e9bb39ddd364bdaa229b5b14b070
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.context.impl.distribution;
23
24 import org.onap.policy.apex.context.ContextException;
25 import org.onap.policy.apex.context.Distributor;
26 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
27 import org.onap.policy.apex.context.parameters.DistributorParameters;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
29 import org.onap.policy.common.parameters.ParameterService;
30 import org.onap.policy.common.utils.validation.Assertions;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
33
34 /**
35  * This class returns a context distributor for the particular type of distribution mechanism configured for use.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class DistributorFactory {
40     // Get a reference to the logger
41     private static final XLogger LOGGER = XLoggerFactory.getXLogger(DistributorFactory.class);
42
43     /**
44      * Get a context distributor for a given context set key.
45      *
46      * @param key The key for the distributor
47      * @return a context distributor
48      * @throws ContextException on context distributor creation errors
49      */
50     public Distributor getDistributor(final AxArtifactKey key) throws ContextException {
51         LOGGER.entry("Distributor factory, key=" + key);
52
53         Assertions.argumentOfClassNotNull(key, ContextException.class, "Parameter \"key\" may not be null");
54
55         // Get the class for the distributor using reflection
56         final DistributorParameters distributorParameters = ParameterService
57                         .get(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
58         final String pluginClass = distributorParameters.getPluginClass();
59         Object contextDistributorObject = null;
60         try {
61             contextDistributorObject = Class.forName(pluginClass).newInstance();
62         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
63             LOGGER.error("Apex context distributor class not found for context distributor plugin \"" + pluginClass
64                             + "\"", e);
65             throw new ContextException("Apex context distributor class not found for context distributor plugin \""
66                             + pluginClass + "\"", e);
67         }
68
69         // Check the class is a distributor
70         if (!(contextDistributorObject instanceof Distributor)) {
71             final String returnString = "Specified Apex context distributor plugin class \"" + pluginClass
72                             + "\" does not implement the ContextDistributor interface";
73             LOGGER.error(returnString);
74             throw new ContextException(returnString);
75         }
76
77         // The context Distributor to return
78         final Distributor contextDistributor = (Distributor) contextDistributorObject;
79
80         // Lock and load the context distributor
81         contextDistributor.init(key);
82
83         LOGGER.exit("Distributor factory, key=" + key + ", selected distributor of class "
84                         + contextDistributor.getClass());
85         return contextDistributor;
86     }
87 }