2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.context.impl.distribution;
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;
35 * This class returns a context distributor for the particular type of distribution mechanism configured for use.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class DistributorFactory {
40 // Get a reference to the logger
41 private static final XLogger LOGGER = XLoggerFactory.getXLogger(DistributorFactory.class);
44 * Get a context distributor for a given context set key.
46 * @param key The key for the distributor
47 * @return a context distributor
48 * @throws ContextException on context distributor creation errors
50 public Distributor getDistributor(final AxArtifactKey key) throws ContextException {
51 LOGGER.debug("Distributor factory, key={}", key);
53 Assertions.argumentOfClassNotNull(key, ContextException.class, "Parameter \"key\" may not be null");
55 // Get the class for the distributor using reflection
56 final DistributorParameters distributorParameters =
57 ParameterService.get(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
58 final String pluginClass = distributorParameters.getPluginClass();
59 Object contextDistributorObject = null;
61 contextDistributorObject = Class.forName(pluginClass).getDeclaredConstructor().newInstance();
62 } catch (Exception e) {
63 throw new ContextException(
64 "Apex context distributor class not found for context distributor plugin \"" + pluginClass + "\"",
68 // Check the class is a distributor
69 if (!(contextDistributorObject instanceof Distributor)) {
70 final String returnString = "Specified Apex context distributor plugin class \"" + pluginClass
71 + "\" does not implement the ContextDistributor interface";
72 throw new ContextException(returnString);
75 // The context Distributor to return
76 final Distributor contextDistributor = (Distributor) contextDistributorObject;
78 // Lock and load the context distributor
79 contextDistributor.init(key);
81 LOGGER.debug("Distributor factory, key={}, selected distributor of class {}", key,
82 contextDistributor.getClass());
83 return contextDistributor;