2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.context.impl.persistence;
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;
35 * This class returns a persistor for the particular type of persistor mechanism that has been configured for use.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class PersistorFactory {
40 // Get a reference to the logger
41 private static final XLogger LOGGER = XLoggerFactory.getXLogger(PersistorFactory.class);
44 * Return a persistor for the persistence mechanism configured for use.
46 * @param key The key for the persistor
48 * @throws ContextException on invalid persistor types
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");
54 final PersistorParameters persistorParameters = ParameterService
55 .get(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
57 // Get the class for the persistor using reflection
58 Object persistorObject = null;
59 final String pluginClass = persistorParameters.getPluginClass();
61 persistorObject = Class.forName(pluginClass).newInstance();
62 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
63 LOGGER.error("Apex context persistor class not found for context persistor plugin \"" + pluginClass + "\"",
65 throw new ContextException("Apex context persistor class not found for context persistor plugin \""
66 + pluginClass + "\"", e);
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");
77 // The persistor to return
78 final Persistor persistor = (Persistor) persistorObject;
80 // Lock and load the persistor
83 LOGGER.exit("Persistor factory, key=" + key + ", selected persistor of class " + persistor.getClass());