X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ONAP-PAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpap%2Fxacml%2Frest%2Futil%2FJPAUtils.java;fp=ONAP-PAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpap%2Fxacml%2Frest%2Futil%2FJPAUtils.java;h=b9880d8ac082290d5d359397be72d086ce412e21;hb=4068da123ee33b532b4b52f15545c76a978f977e;hp=22c44a8ab302bbcc70fd72dc2cdb2d9337f655fb;hpb=f0fec2a861ea0a92b27b40882ea38f9422cde0c6;p=policy%2Fengine.git diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/util/JPAUtils.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/util/JPAUtils.java index 22c44a8ab..b9880d8ac 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/util/JPAUtils.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/util/JPAUtils.java @@ -31,88 +31,88 @@ import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; public class JPAUtils { - private static final Logger LOGGER = FlexLogger.getLogger(JPAUtils.class); - - private static EntityManagerFactory emf; - private static JPAUtils currentInstance = null; - - - /** - * Get an instance of a JPAUtils. It creates one if it does not exist. - * Only one instance is allowed to be created per server. - * @param emf The EntityFactoryManager to be used for database connections - * @return The new instance of JPAUtils or throw exception if the given emf is null. - * @throws IllegalStateException if a JPAUtils has already been constructed. Call getJPAUtilsInstance() to get this. - */ - public static JPAUtils getJPAUtilsInstance(EntityManagerFactory emf){ - LOGGER.debug("getJPAUtilsInstance(EntityManagerFactory emf) as getJPAUtilsInstance("+emf+") called"); - if(currentInstance == null){ - if(emf != null){ - currentInstance = new JPAUtils(emf); - return currentInstance; - } - throw new IllegalStateException("The EntityManagerFactory is Null"); - } - return currentInstance; - } - - private JPAUtils(EntityManagerFactory emf){ - LOGGER.debug("JPAUtils(EntityManagerFactory emf) as JPAUtils("+emf+") called"); - JPAUtils.emf = emf; - } - - /** - * Returns the lockdown value, in case of exception it is assumed that lockdown functionality - * is not supported and returns false. - * - * - * @throws ReadOnlyException - * @throws ConversionException - */ - public boolean dbLockdownIgnoreErrors() { - if (LOGGER.isTraceEnabled()) - LOGGER.trace("ENTER"); - - boolean lockdown = false; - try { - lockdown = dbLockdown(); - } catch (Exception e) { - LOGGER.warn("Cannot access DB lockdown value", e); - } - return lockdown; - } - - /** - * Returns the lockdown value from the database. - * - * @throws ReadOnlyException - * @throws ConversionException - */ - public boolean dbLockdown() - throws IllegalAccessException { - if (LOGGER.isTraceEnabled()) - LOGGER.trace("ENTER"); - - EntityManager em = emf.createEntityManager(); - Query globalRoleSettingsJPA = em.createNamedQuery("GlobalRoleSettings.findAll"); - - GlobalRoleSettings globalRoleSettings = (GlobalRoleSettings) globalRoleSettingsJPA.getSingleResult(); - - if (globalRoleSettings == null) { - // this should not happen - String msg = "NO GlobalSetttings for " + XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString(); - if (LOGGER.isErrorEnabled()) - LOGGER.error(msg); - throw new IllegalAccessException(msg); - } - - if (!globalRoleSettings.getRole().equals(XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString())) { - String msg = "NOT FOUND db data for " + XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString(); - if (LOGGER.isErrorEnabled()) - LOGGER.error(msg); - throw new IllegalAccessException(msg); - } - - return globalRoleSettings.isLockdown(); - } + private static final Logger LOGGER = FlexLogger.getLogger(JPAUtils.class); + + private static EntityManagerFactory emf; + private static JPAUtils currentInstance = null; + + + /** + * Get an instance of a JPAUtils. It creates one if it does not exist. + * Only one instance is allowed to be created per server. + * @param emf The EntityFactoryManager to be used for database connections + * @return The new instance of JPAUtils or throw exception if the given emf is null. + * @throws IllegalStateException if a JPAUtils has already been constructed. Call getJPAUtilsInstance() to get this. + */ + public static JPAUtils getJPAUtilsInstance(EntityManagerFactory emf){ + LOGGER.debug("getJPAUtilsInstance(EntityManagerFactory emf) as getJPAUtilsInstance("+emf+") called"); + if(currentInstance == null){ + if(emf != null){ + currentInstance = new JPAUtils(emf); + return currentInstance; + } + throw new IllegalStateException("The EntityManagerFactory is Null"); + } + return currentInstance; + } + + private JPAUtils(EntityManagerFactory emf){ + LOGGER.debug("JPAUtils(EntityManagerFactory emf) as JPAUtils("+emf+") called"); + JPAUtils.emf = emf; + } + + /** + * Returns the lockdown value, in case of exception it is assumed that lockdown functionality + * is not supported and returns false. + * + * + * @throws ReadOnlyException + * @throws ConversionException + */ + public boolean dbLockdownIgnoreErrors() { + if (LOGGER.isTraceEnabled()) + LOGGER.trace("ENTER"); + + boolean lockdown = false; + try { + lockdown = dbLockdown(); + } catch (Exception e) { + LOGGER.warn("Cannot access DB lockdown value", e); + } + return lockdown; + } + + /** + * Returns the lockdown value from the database. + * + * @throws ReadOnlyException + * @throws ConversionException + */ + public boolean dbLockdown() + throws IllegalAccessException { + if (LOGGER.isTraceEnabled()) + LOGGER.trace("ENTER"); + + EntityManager em = emf.createEntityManager(); + Query globalRoleSettingsJPA = em.createNamedQuery("GlobalRoleSettings.findAll"); + + GlobalRoleSettings globalRoleSettings = (GlobalRoleSettings) globalRoleSettingsJPA.getSingleResult(); + + if (globalRoleSettings == null) { + // this should not happen + String msg = "NO GlobalSetttings for " + XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString(); + if (LOGGER.isErrorEnabled()) + LOGGER.error(msg); + throw new IllegalAccessException(msg); + } + + if (!globalRoleSettings.getRole().equals(XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString())) { + String msg = "NOT FOUND db data for " + XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString(); + if (LOGGER.isErrorEnabled()) + LOGGER.error(msg); + throw new IllegalAccessException(msg); + } + + return globalRoleSettings.isLockdown(); + } }