2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 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.locking;
24 import org.onap.policy.apex.context.ContextException;
25 import org.onap.policy.apex.context.LockManager;
26 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
27 import org.onap.policy.apex.context.parameters.LockManagerParameters;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
29 import org.onap.policy.common.parameters.ParameterService;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
34 * This class returns a {@link LockManager} for the particular type of locking mechanism that has been configured for
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class LockManagerFactory {
40 // Get a reference to the logger
41 private static final XLogger LOGGER = XLoggerFactory.getXLogger(LockManagerFactory.class);
44 * Return a {@link LockManager} for the particular type of locking mechanism configured for use.
46 * @param key The key for the lock manager
47 * @return a lock manager that can generate locks using some underlying mechanism
48 * @throws ContextException on errors in getting a lock manager
50 public LockManager createLockManager(final AxArtifactKey key) throws ContextException {
51 LOGGER.entry("Lock Manager factory, key=" + key);
53 final LockManagerParameters lockManagerParameters =
54 ParameterService.get(ContextParameterConstants.LOCKING_GROUP_NAME);
56 // Get the class for the lock manager using reflection
57 Object lockManagerObject = null;
58 final String pluginClass = lockManagerParameters.getPluginClass();
60 lockManagerObject = Class.forName(pluginClass).getDeclaredConstructor().newInstance();
61 } catch (Exception e) {
63 "Apex context lock manager class not found for context lock manager plugin \"" + pluginClass + "\"",
65 throw new ContextException(
66 "Apex context lock manager class not found for context lock manager plugin \"" + pluginClass + "\"",
70 // Check the class is a lock manager
71 if (!(lockManagerObject instanceof LockManager)) {
72 LOGGER.error("Specified Apex context lock manager plugin class \"{}\" "
73 + "does not implement the LockManager interface", pluginClass);
74 throw new ContextException("Specified Apex context lock manager plugin class \"" + pluginClass
75 + "\" does not implement the LockManager interface");
78 // The context lock manager to return
79 final LockManager lockManager = (LockManager) lockManagerObject;
81 // Lock and load (OK sorry!!!) the lock manager
82 lockManager.init(key);
84 LOGGER.exit("Lock manager factory, key=" + key + ", selected lock manager of class " + lockManager.getClass());