From f4673a0517c20a73bc3cf3d1759c8b12d0329a36 Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Mon, 6 Nov 2017 14:35:48 +0000 Subject: [PATCH] Refs #10221: Reopen DB connection if closed Task-Url: https://support.d4science.org/issues/10221 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@158229 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../context/SecurityContextMapper.java | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/SecurityContextMapper.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/SecurityContextMapper.java index 11e3112..6564b16 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/SecurityContextMapper.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/SecurityContextMapper.java @@ -55,23 +55,15 @@ public abstract class SecurityContextMapper { factory.setConnectionStrategy(DatabaseIntializator.CONNECTION_STRATEGY_PARAMETER.toString()); for (PermissionMode p : PermissionMode.values()) { - OrientGraphFactory f = new OrientGraphFactory( - DatabaseEnvironment.DB_URI, - DatabaseEnvironment.CHANGED_ADMIN_USERNAME, - DatabaseEnvironment.CHANGED_ADMIN_PASSWORD) - .setupPool(1, 10); - - f.setConnectionStrategy(DatabaseIntializator.CONNECTION_STRATEGY_PARAMETER.toString()); - Map map = new HashMap<>(); - map.put(ADMIN_SECURITY_CONTEXT_UUID, f); securityContextFactories.put(p, map); + + getSecurityContextFactory(ADMIN_SECURITY_CONTEXT_UUID, p, false); } - + if (created) { OrientGraph orientGraph = factory.getTx(); - SecurityContext.createSecurityContext(orientGraph, - MANAGEMENT_SECURITY_CONTEXT_UUID); + SecurityContext.createSecurityContext(orientGraph, MANAGEMENT_SECURITY_CONTEXT_UUID); getSecurityContextFactory(MANAGEMENT_SECURITY_CONTEXT_UUID, PermissionMode.READER, false); @@ -124,8 +116,7 @@ public abstract class SecurityContextMapper { UUID context, PermissionMode permissionMode, boolean recreate) { OrientGraphFactory factory = null; - Map permissionSecurityContextFactories = securityContextFactories - .get(permissionMode); + Map permissionSecurityContextFactories = securityContextFactories.get(permissionMode); if(recreate) { permissionSecurityContextFactories.remove(context); @@ -134,12 +125,18 @@ public abstract class SecurityContextMapper { } if (factory == null) { - - String username = getSecurityRoleOrUserName(permissionMode, - SecurityType.USER, context); - String password = DatabaseEnvironment.DEFAULT_PASSWORDS - .get(permissionMode); - + + String username = null; + String password = null; + + if(context.compareTo(ADMIN_SECURITY_CONTEXT_UUID)==0){ + username = DatabaseEnvironment.CHANGED_ADMIN_USERNAME; + password = DatabaseEnvironment.CHANGED_ADMIN_PASSWORD; + }else { + username = getSecurityRoleOrUserName(permissionMode, SecurityType.USER, context); + password = DatabaseEnvironment.DEFAULT_PASSWORDS.get(permissionMode); + } + factory = new OrientGraphFactory(DatabaseEnvironment.DB_URI, username, password).setupPool(1, 10); factory.setConnectionStrategy(DatabaseIntializator.CONNECTION_STRATEGY_PARAMETER.toString()); @@ -152,7 +149,7 @@ public abstract class SecurityContextMapper { public static OrientGraph getSecurityContextGraph( UUID context, PermissionMode permissionMode) { - OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, true); + OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, false); OrientGraph orientGraph = factory.getTx(); if(orientGraph.isClosed()) { factory = getSecurityContextFactory(context, permissionMode, true); @@ -163,7 +160,7 @@ public abstract class SecurityContextMapper { public static OrientGraphNoTx getSecurityContextGraphNoTx( UUID context, PermissionMode permissionMode) { - OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, true); + OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, false); OrientGraphNoTx orientGraphNoTx = factory.getNoTx(); if(orientGraphNoTx.isClosed()) { factory = getSecurityContextFactory(context, permissionMode, true); @@ -174,7 +171,7 @@ public abstract class SecurityContextMapper { public static ODatabaseDocumentTx getSecurityContextDatabaseDocumentTx( UUID context, PermissionMode permissionMode) { - OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, true); + OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, false); ODatabaseDocumentTx databaseDocumentTx = factory.getDatabase(); if(databaseDocumentTx.isClosed()) { factory = getSecurityContextFactory(context, permissionMode, true);