2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.context.impl.locking;
23 import org.onap.policy.apex.context.ContextException;
24 import org.onap.policy.apex.context.LockManager;
25 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
26 import org.onap.policy.apex.context.parameters.LockManagerParameters;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
28 import org.onap.policy.common.parameters.ParameterService;
29 import org.slf4j.ext.XLogger;
30 import org.slf4j.ext.XLoggerFactory;
33 * This class returns a {@link LockManager} for the particular type of locking mechanism that has been configured for
36 * @author Liam Fallon (liam.fallon@ericsson.com)
38 public class LockManagerFactory {
39 // Get a reference to the logger
40 private static final XLogger LOGGER = XLoggerFactory.getXLogger(LockManagerFactory.class);
43 * Return a {@link LockManager} for the particular type of locking mechanism configured for use.
45 * @param key The key for the lock manager
46 * @return a lock manager that can generate locks using some underlying mechanism
47 * @throws ContextException on errors in getting a lock manager
49 public LockManager createLockManager(final AxArtifactKey key) throws ContextException {
50 LOGGER.entry("Lock Manager factory, key=" + key);
52 final LockManagerParameters lockManagerParameters = ParameterService
53 .get(ContextParameterConstants.LOCKING_GROUP_NAME);
55 // Get the class for the lock manager using reflection
56 Object lockManagerObject = null;
57 final String pluginClass = lockManagerParameters.getPluginClass();
59 lockManagerObject = Class.forName(pluginClass).newInstance();
60 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
61 LOGGER.error("Apex context lock manager class not found for context lock manager plugin \"" + pluginClass
63 throw new ContextException("Apex context lock manager class not found for context lock manager plugin \""
64 + pluginClass + "\"", e);
67 // Check the class is a lock manager
68 if (!(lockManagerObject instanceof LockManager)) {
69 LOGGER.error("Specified Apex context lock manager plugin class \"{}\" does not implement the LockManager interface",
71 throw new ContextException("Specified Apex context lock manager plugin class \"" + pluginClass
72 + "\" does not implement the LockManager interface");
75 // The context lock manager to return
76 final LockManager lockManager = (LockManager) lockManagerObject;
78 // Lock and load (OK sorry!!!) the lock manager
79 lockManager.init(key);
81 LOGGER.exit("Lock manager factory, key=" + key + ", selected lock manager of class " + lockManager.getClass());