2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
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.models.dao;
24 import java.lang.reflect.InvocationTargetException;
25 import javax.ws.rs.core.Response;
27 import org.onap.policy.common.utils.validation.Assertions;
28 import org.onap.policy.models.base.PfModelException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * This factory class returns a Policy Framework DAO for the configured persistence mechanism. The
34 * factory uses the plugin class specified in {@link DaoParameters} to instantiate a DAO instance.
36 * @author Liam Fallon (liam.fallon@est.tech)
38 public class PfDaoFactory {
39 // Get a reference to the logger
40 private static final Logger LOGGER = LoggerFactory.getLogger(PfDaoFactory.class);
43 * Return a Policy Framework DAO for the required Policy Framework DAO plugin class.
45 * @param daoParameters parameters to use to read the database configuration information
46 * @return the Policy Framework DAO
47 * @throws PfModelException on invalid JPA plugins
49 public PfDao createPfDao(final DaoParameters daoParameters) throws PfModelException {
50 Assertions.argumentOfClassNotNull(daoParameters, PfModelException.class,
51 "Parameter \"daoParameters\" may not be null");
53 // Get the class for the DAO using reflection
54 Object pfDaoObject = null;
56 pfDaoObject = Class.forName(daoParameters.getPluginClass()).getDeclaredConstructor().newInstance();
57 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IllegalArgumentException
58 | InvocationTargetException | NoSuchMethodException | SecurityException e) {
60 "Policy Framework DAO class not found for DAO plugin \"" + daoParameters.getPluginClass() + "\"";
61 LOGGER.error(errorMessage);
62 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, e);
65 // Check the class is a Policy Framework DAO
66 if (!(pfDaoObject instanceof PfDao)) {
67 String errorMessage = "Specified DAO plugin class \"" + daoParameters.getPluginClass()
68 + "\" does not implement the PfDao interface";
69 LOGGER.error(errorMessage);
70 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage);
73 return (PfDao) pfDaoObject;