3e91206727ac949de7464d303c421f60c95806da
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.context.impl.locking;
22
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;
31
32 /**
33  * This class returns a {@link LockManager} for the particular type of locking mechanism that has been configured for
34  * use.
35  *
36  * @author Liam Fallon (liam.fallon@ericsson.com)
37  */
38 public class LockManagerFactory {
39     // Get a reference to the logger
40     private static final XLogger LOGGER = XLoggerFactory.getXLogger(LockManagerFactory.class);
41
42     /**
43      * Return a {@link LockManager} for the particular type of locking mechanism configured for use.
44      *
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
48      */
49     public LockManager createLockManager(final AxArtifactKey key) throws ContextException {
50         LOGGER.entry("Lock Manager factory, key=" + key);
51
52         final LockManagerParameters lockManagerParameters = ParameterService
53                         .get(ContextParameterConstants.LOCKING_GROUP_NAME);
54
55         // Get the class for the lock manager using reflection
56         Object lockManagerObject = null;
57         final String pluginClass = lockManagerParameters.getPluginClass();
58         try {
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
62                             + "\"", e);
63             throw new ContextException("Apex context lock manager class not found for context lock manager plugin \""
64                             + pluginClass + "\"", e);
65         }
66
67         // Check the class is a lock manager
68         if (!(lockManagerObject instanceof LockManager)) {
69             LOGGER.error("Specified Apex context lock manager plugin class \"{}\" "
70                             + "does not implement the LockManager interface", pluginClass);
71             throw new ContextException("Specified Apex context lock manager plugin class \"" + pluginClass
72                             + "\" does not implement the LockManager interface");
73         }
74
75         // The context lock manager to return
76         final LockManager lockManager = (LockManager) lockManagerObject;
77
78         // Lock and load (OK sorry!!!) the lock manager
79         lockManager.init(key);
80
81         LOGGER.exit("Lock manager factory, key=" + key + ", selected lock manager of class " + lockManager.getClass());
82         return lockManager;
83     }
84 }