2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.xacml.rest.util;
23 import java.util.List;
24 import org.onap.policy.common.logging.flexlogger.FlexLogger;
25 import org.onap.policy.common.logging.flexlogger.Logger;
26 import org.onap.policy.rest.XacmlAdminAuthorization;
27 import org.onap.policy.rest.dao.CommonClassDao;
28 import org.onap.policy.rest.jpa.GlobalRoleSettings;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Component;
33 public class JPAUtils {
34 private static final Logger LOGGER = FlexLogger.getLogger(JPAUtils.class);
36 private static CommonClassDao commonClassDao;
37 private static JPAUtils currentInstance = null;
40 public JPAUtils(CommonClassDao commonClassDao) {
41 JPAUtils.commonClassDao = commonClassDao;
46 * Get an instance of a JPAUtils. It creates one if it does not exist. Only one instance is allowed
47 * to be created per server.
49 * @param emf The EntityFactoryManager to be used for database connections
50 * @return The new instance of JPAUtils or throw exception if the given emf is null.
51 * @throws IllegalStateException if a JPAUtils has already been constructed. Call
52 * getJPAUtilsInstance() to get this.
54 public static JPAUtils getJPAUtilsInstance() {
55 if (currentInstance == null) {
56 currentInstance = new JPAUtils();
57 return currentInstance;
59 return currentInstance;
63 // Default Constructor
67 * Returns the lockdown value, in case of exception it is assumed that lockdown functionality is not
68 * supported and returns false.
71 * @throws ReadOnlyException
72 * @throws ConversionException
74 public boolean dbLockdownIgnoreErrors() {
75 if (LOGGER.isTraceEnabled())
76 LOGGER.trace("ENTER");
78 boolean lockdown = false;
80 lockdown = dbLockdown();
81 } catch (Exception e) {
82 LOGGER.warn("Cannot access DB lockdown value", e);
88 * Returns the lockdown value from the database.
90 * @throws ReadOnlyException
91 * @throws ConversionException
93 public boolean dbLockdown() throws IllegalAccessException {
94 if (LOGGER.isTraceEnabled())
95 LOGGER.trace("ENTER");
96 List<Object> data = commonClassDao.getData(GlobalRoleSettings.class);
98 GlobalRoleSettings globalRoleSettings = null;
100 if (!data.isEmpty()) {
101 globalRoleSettings = (GlobalRoleSettings) data.get(0);
104 if (globalRoleSettings == null) {
105 // this should not happen
106 String msg = "NO GlobalSetttings for " + XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString();
107 if (LOGGER.isErrorEnabled())
109 throw new IllegalAccessException(msg);
112 if (!globalRoleSettings.getRole().equals(XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString())) {
113 String msg = "NOT FOUND db data for " + XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString();
114 if (LOGGER.isErrorEnabled())
116 throw new IllegalAccessException(msg);
119 return globalRoleSettings.isLockdown();