f3f4a6240d77cf1b909866b4b6dd9d427ec21d72
[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.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;
30
31 /**
32  * This class returns a {@link LockManager} for the particular type of locking mechanism that has been configured for
33  * use.
34  *
35  * @author Liam Fallon (liam.fallon@ericsson.com)
36  */
37 public class LockManagerFactory {
38     // Get a reference to the logger
39     private static final XLogger LOGGER = XLoggerFactory.getXLogger(LockManagerFactory.class);
40
41     /**
42      * Return a {@link LockManager} for the particular type of locking mechanism configured for use.
43      *
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
47      */
48     public LockManager createLockManager(final AxArtifactKey key) throws ContextException {
49         LOGGER.entry("Lock Manager factory, key=" + key);
50
51         final LockManagerParameters lockManagerParameters = ParameterService.getParameters(LockManagerParameters.class);
52
53         // Get the class for the lock manager using reflection
54         Object lockManagerObject = null;
55         final String pluginClass = lockManagerParameters.getPluginClass();
56         try {
57             lockManagerObject = Class.forName(pluginClass).newInstance();
58         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
59             LOGGER.error(
60                     "Apex context lock manager class not found for context lock manager plugin \"" + pluginClass + "\"",
61                     e);
62             throw new ContextException(
63                     "Apex context lock manager class not found for context lock manager plugin \"" + pluginClass + "\"",
64                     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 \"" + 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");
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 }