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;
26 import org.onap.policy.common.utils.validation.Assertions;
27 import org.onap.policy.models.base.PfModelException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * This factory class returns a Policy Framework DAO for the configured persistence mechanism. The
33 * factory uses the plugin class specified in {@link DaoParameters} to instantiate a DAO instance.
35 * @author Liam Fallon (liam.fallon@est.tech)
37 public class PfDaoFactory {
38 // Get a reference to the logger
39 private static final Logger LOGGER = LoggerFactory.getLogger(PfDaoFactory.class);
42 * Return a Policy Framework DAO for the required Policy Framework DAO plugin class.
44 * @param daoParameters parameters to use to read the database configuration information
45 * @return the Policy Framework DAO
46 * @throws PfModelException on invalid JPA plugins
48 public PfDao createPfDao(final DaoParameters daoParameters) throws PfModelException {
49 Assertions.argumentOfClassNotNull(daoParameters, PfModelException.class,
50 "Parameter \"daoParameters\" may not be null");
52 // Get the class for the DAO using reflection
53 Object pfDaoObject = null;
55 pfDaoObject = Class.forName(daoParameters.getPluginClass()).getDeclaredConstructor().newInstance();
56 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IllegalArgumentException
57 | InvocationTargetException | NoSuchMethodException | SecurityException e) {
59 "Policy Framework DAO class not found for DAO plugin \"" + daoParameters.getPluginClass() + "\"";
60 LOGGER.error(errorMessage);
61 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, e);
64 // Check the class is a Policy Framework DAO
65 if (!(pfDaoObject instanceof PfDao)) {
66 String errorMessage = "Specified DAO plugin class \"" + daoParameters.getPluginClass()
67 + "\" does not implement the PfDao interface";
68 LOGGER.error(errorMessage);
69 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage);
72 return (PfDao) pfDaoObject;