923c839796c9defc43029225bbe4a6b233df0d9a
[policy/apex-pdp.git] /
1 /*-
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
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.locking;
23
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;
32
33 /**
34  * This class returns a {@link LockManager} for the particular type of locking mechanism that has been configured for
35  * use.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class LockManagerFactory {
40     // Get a reference to the logger
41     private static final XLogger LOGGER = XLoggerFactory.getXLogger(LockManagerFactory.class);
42
43     /**
44      * Return a {@link LockManager} for the particular type of locking mechanism configured for use.
45      *
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
49      */
50     public LockManager createLockManager(final AxArtifactKey key) throws ContextException {
51         LOGGER.entry("Lock Manager factory, key=" + key);
52
53         final LockManagerParameters lockManagerParameters =
54                 ParameterService.get(ContextParameterConstants.LOCKING_GROUP_NAME);
55
56         // Get the class for the lock manager using reflection
57         Object lockManagerObject = null;
58         final String pluginClass = lockManagerParameters.getPluginClass();
59         try {
60             lockManagerObject = Class.forName(pluginClass).getDeclaredConstructor().newInstance();
61         } catch (Exception e) {
62             LOGGER.error(
63                     "Apex context lock manager class not found for context lock manager plugin \"" + pluginClass + "\"",
64                     e);
65             throw new ContextException(
66                     "Apex context lock manager class not found for context lock manager plugin \"" + pluginClass + "\"",
67                     e);
68         }
69
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");
76         }
77
78         // The context lock manager to return
79         final LockManager lockManager = (LockManager) lockManagerObject;
80
81         // Lock and load (OK sorry!!!) the lock manager
82         lockManager.init(key);
83
84         LOGGER.exit("Lock manager factory, key=" + key + ", selected lock manager of class " + lockManager.getClass());
85         return lockManager;
86     }
87 }