fe06d2fdb0a4b7f1d9e2cfae61042624adf57d6e
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-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.persistence;
23
24 import org.onap.policy.apex.context.ContextException;
25 import org.onap.policy.apex.context.Persistor;
26 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
27 import org.onap.policy.apex.context.parameters.PersistorParameters;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
29 import org.onap.policy.common.parameters.ParameterService;
30 import org.onap.policy.common.utils.validation.Assertions;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
33
34 /**
35  * This class returns a persistor for the particular type of persistor mechanism that has been configured for use.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class PersistorFactory {
40     // Get a reference to the logger
41     private static final XLogger LOGGER = XLoggerFactory.getXLogger(PersistorFactory.class);
42
43     /**
44      * Return a persistor for the persistence mechanism configured for use.
45      *
46      * @param key The key for the persistor
47      * @return a persistor
48      * @throws ContextException on invalid persistor types
49      */
50     public Persistor createPersistor(final AxArtifactKey key) throws ContextException {
51         LOGGER.entry("persistor factory, key=" + key);
52         Assertions.argumentOfClassNotNull(key, ContextException.class, "Parameter \"key\" may not be null");
53
54         final PersistorParameters persistorParameters =
55                 ParameterService.get(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
56
57         // Get the class for the persistor using reflection
58         Object persistorObject = null;
59         final String pluginClass = persistorParameters.getPluginClass();
60         try {
61             persistorObject = Class.forName(pluginClass).getDeclaredConstructor().newInstance();
62         } catch (Exception e) {
63             LOGGER.error("Apex context persistor class not found for context persistor plugin \"" + pluginClass + "\"",
64                     e);
65             throw new ContextException(
66                     "Apex context persistor class not found for context persistor plugin \"" + pluginClass + "\"", e);
67         }
68
69         // Check the class is a persistor
70         if (!(persistorObject instanceof Persistor)) {
71             LOGGER.error("Specified Apex context persistor plugin class \"{}\" "
72                     + "does not implement the ContextDistributor interface", pluginClass);
73             throw new ContextException("Specified Apex context persistor plugin class \"" + pluginClass
74                     + "\" does not implement the ContextDistributor interface");
75         }
76
77         // The persistor to return
78         final Persistor persistor = (Persistor) persistorObject;
79
80         // Lock and load the persistor
81         persistor.init(key);
82
83         LOGGER.exit("Persistor factory, key=" + key + ", selected persistor of class " + persistor.getClass());
84         return persistor;
85     }
86 }