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
This commit is contained in:
Luca Frosini 2017-11-06 14:35:48 +00:00
parent f59b3b550f
commit f4673a0517
1 changed files with 20 additions and 23 deletions

View File

@ -55,23 +55,15 @@ public abstract class SecurityContextMapper {
factory.setConnectionStrategy(DatabaseIntializator.CONNECTION_STRATEGY_PARAMETER.toString()); factory.setConnectionStrategy(DatabaseIntializator.CONNECTION_STRATEGY_PARAMETER.toString());
for (PermissionMode p : PermissionMode.values()) { 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<UUID, OrientGraphFactory> map = new HashMap<>(); Map<UUID, OrientGraphFactory> map = new HashMap<>();
map.put(ADMIN_SECURITY_CONTEXT_UUID, f);
securityContextFactories.put(p, map); securityContextFactories.put(p, map);
getSecurityContextFactory(ADMIN_SECURITY_CONTEXT_UUID, p, false);
} }
if (created) { if (created) {
OrientGraph orientGraph = factory.getTx(); OrientGraph orientGraph = factory.getTx();
SecurityContext.createSecurityContext(orientGraph, SecurityContext.createSecurityContext(orientGraph, MANAGEMENT_SECURITY_CONTEXT_UUID);
MANAGEMENT_SECURITY_CONTEXT_UUID);
getSecurityContextFactory(MANAGEMENT_SECURITY_CONTEXT_UUID, getSecurityContextFactory(MANAGEMENT_SECURITY_CONTEXT_UUID,
PermissionMode.READER, false); PermissionMode.READER, false);
@ -124,8 +116,7 @@ public abstract class SecurityContextMapper {
UUID context, PermissionMode permissionMode, boolean recreate) { UUID context, PermissionMode permissionMode, boolean recreate) {
OrientGraphFactory factory = null; OrientGraphFactory factory = null;
Map<UUID, OrientGraphFactory> permissionSecurityContextFactories = securityContextFactories Map<UUID, OrientGraphFactory> permissionSecurityContextFactories = securityContextFactories.get(permissionMode);
.get(permissionMode);
if(recreate) { if(recreate) {
permissionSecurityContextFactories.remove(context); permissionSecurityContextFactories.remove(context);
@ -134,12 +125,18 @@ public abstract class SecurityContextMapper {
} }
if (factory == null) { if (factory == null) {
String username = getSecurityRoleOrUserName(permissionMode, String username = null;
SecurityType.USER, context); String password = null;
String password = DatabaseEnvironment.DEFAULT_PASSWORDS
.get(permissionMode); 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, factory = new OrientGraphFactory(DatabaseEnvironment.DB_URI,
username, password).setupPool(1, 10); username, password).setupPool(1, 10);
factory.setConnectionStrategy(DatabaseIntializator.CONNECTION_STRATEGY_PARAMETER.toString()); factory.setConnectionStrategy(DatabaseIntializator.CONNECTION_STRATEGY_PARAMETER.toString());
@ -152,7 +149,7 @@ public abstract class SecurityContextMapper {
public static OrientGraph getSecurityContextGraph( public static OrientGraph getSecurityContextGraph(
UUID context, PermissionMode permissionMode) { UUID context, PermissionMode permissionMode) {
OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, true); OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, false);
OrientGraph orientGraph = factory.getTx(); OrientGraph orientGraph = factory.getTx();
if(orientGraph.isClosed()) { if(orientGraph.isClosed()) {
factory = getSecurityContextFactory(context, permissionMode, true); factory = getSecurityContextFactory(context, permissionMode, true);
@ -163,7 +160,7 @@ public abstract class SecurityContextMapper {
public static OrientGraphNoTx getSecurityContextGraphNoTx( public static OrientGraphNoTx getSecurityContextGraphNoTx(
UUID context, PermissionMode permissionMode) { UUID context, PermissionMode permissionMode) {
OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, true); OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, false);
OrientGraphNoTx orientGraphNoTx = factory.getNoTx(); OrientGraphNoTx orientGraphNoTx = factory.getNoTx();
if(orientGraphNoTx.isClosed()) { if(orientGraphNoTx.isClosed()) {
factory = getSecurityContextFactory(context, permissionMode, true); factory = getSecurityContextFactory(context, permissionMode, true);
@ -174,7 +171,7 @@ public abstract class SecurityContextMapper {
public static ODatabaseDocumentTx getSecurityContextDatabaseDocumentTx( public static ODatabaseDocumentTx getSecurityContextDatabaseDocumentTx(
UUID context, PermissionMode permissionMode) { UUID context, PermissionMode permissionMode) {
OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, true); OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, false);
ODatabaseDocumentTx databaseDocumentTx = factory.getDatabase(); ODatabaseDocumentTx databaseDocumentTx = factory.getDatabase();
if(databaseDocumentTx.isClosed()) { if(databaseDocumentTx.isClosed()) {
factory = getSecurityContextFactory(context, permissionMode, true); factory = getSecurityContextFactory(context, permissionMode, true);