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.LockManagerParameters;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
27 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
32 * This class returns a {@link LockManager} for the particular type of locking mechanism that has been configured for
35 * @author Liam Fallon (liam.fallon@ericsson.com)
37 public class LockManagerFactory {
38 // Get a reference to the logger
39 private static final XLogger LOGGER = XLoggerFactory.getXLogger(LockManagerFactory.class);
42 * Return a {@link LockManager} for the particular type of locking mechanism configured for use.
44 * @param key The key for the lock manager
45 * @return a lock manager that can generate locks using some underlying mechanism
46 * @throws ContextException on errors in getting a lock manager
48 public LockManager createLockManager(final AxArtifactKey key) throws ContextException {
49 LOGGER.entry("Lock Manager factory, key=" + key);
51 final LockManagerParameters lockManagerParameters = ParameterService.getParameters(LockManagerParameters.class);
53 // Get the class for the lock manager using reflection
54 Object lockManagerObject = null;
55 final String pluginClass = lockManagerParameters.getPluginClass();
57 lockManagerObject = Class.forName(pluginClass).newInstance();
58 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
60 "Apex context lock manager class not found for context lock manager plugin \"" + pluginClass + "\"",
62 throw new ContextException(
63 "Apex context lock manager class not found for context lock manager plugin \"" + pluginClass + "\"",
67 // Check the class is a lock manager
68 if (!(lockManagerObject instanceof LockManager)) {
69 LOGGER.error("Specified Apex context lock manager plugin class \"" + pluginClass
70 + "\" 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());