2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 javax.persistence.EntityManager;
24 import javax.persistence.EntityManagerFactory;
25 import javax.persistence.Query;
27 import org.onap.policy.rest.XacmlAdminAuthorization;
28 import org.onap.policy.rest.jpa.GlobalRoleSettings;
30 import org.onap.policy.common.logging.flexlogger.FlexLogger;
31 import org.onap.policy.common.logging.flexlogger.Logger;
33 public class JPAUtils {
34 private static final Logger LOGGER = FlexLogger.getLogger(JPAUtils.class);
36 private static EntityManagerFactory emf;
37 private static JPAUtils currentInstance = null;
41 * Get an instance of a JPAUtils. It creates one if it does not exist.
42 * Only one instance is allowed to be created per server.
43 * @param emf The EntityFactoryManager to be used for database connections
44 * @return The new instance of JPAUtils or throw exception if the given emf is null.
45 * @throws IllegalStateException if a JPAUtils has already been constructed. Call getJPAUtilsInstance() to get this.
47 public static JPAUtils getJPAUtilsInstance(EntityManagerFactory emf){
48 LOGGER.debug("getJPAUtilsInstance(EntityManagerFactory emf) as getJPAUtilsInstance("+emf+") called");
49 if(currentInstance == null){
51 currentInstance = new JPAUtils(emf);
52 return currentInstance;
54 throw new IllegalStateException("The EntityManagerFactory is Null");
56 return currentInstance;
59 private JPAUtils(EntityManagerFactory emf){
60 LOGGER.debug("JPAUtils(EntityManagerFactory emf) as JPAUtils("+emf+") called");
65 * Returns the lockdown value, in case of exception it is assumed that lockdown functionality
66 * is not supported and returns false.
69 * @throws ReadOnlyException
70 * @throws ConversionException
72 public boolean dbLockdownIgnoreErrors() {
73 if (LOGGER.isTraceEnabled())
74 LOGGER.trace("ENTER");
76 boolean lockdown = false;
78 lockdown = dbLockdown();
79 } catch (Exception e) {
80 LOGGER.warn("Cannot access DB lockdown value", e);
86 * Returns the lockdown value from the database.
88 * @throws ReadOnlyException
89 * @throws ConversionException
91 public boolean dbLockdown()
92 throws IllegalAccessException {
93 if (LOGGER.isTraceEnabled())
94 LOGGER.trace("ENTER");
96 EntityManager em = emf.createEntityManager();
97 Query globalRoleSettingsJPA = em.createNamedQuery("GlobalRoleSettings.findAll");
99 GlobalRoleSettings globalRoleSettings = (GlobalRoleSettings) globalRoleSettingsJPA.getSingleResult();
101 if (globalRoleSettings == null) {
102 // this should not happen
103 String msg = "NO GlobalSetttings for " + XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString();
104 if (LOGGER.isErrorEnabled())
106 throw new IllegalAccessException(msg);
109 if (!globalRoleSettings.getRole().equals(XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString())) {
110 String msg = "NOT FOUND db data for " + XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString();
111 if (LOGGER.isErrorEnabled())
113 throw new IllegalAccessException(msg);
116 return globalRoleSettings.isLockdown();