Changed strategy to set the DateTimeFormat and added test for that

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@177238 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2019-02-22 13:35:56 +00:00
parent e7e7cc588e
commit 6a16d77b70
2 changed files with 36 additions and 54 deletions

View File

@ -23,6 +23,7 @@ import org.slf4j.LoggerFactory;
import com.orientechnologies.common.log.OLogManager; import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.client.remote.OStorageRemote.CONNECTION_STRATEGY; import com.orientechnologies.orient.client.remote.OStorageRemote.CONNECTION_STRATEGY;
import com.orientechnologies.orient.core.db.ODatabase.ATTRIBUTES;
import com.orientechnologies.orient.core.db.ODatabasePool; import com.orientechnologies.orient.core.db.ODatabasePool;
import com.orientechnologies.orient.core.db.ODatabaseSession; import com.orientechnologies.orient.core.db.ODatabaseSession;
import com.orientechnologies.orient.core.db.ODatabaseType; import com.orientechnologies.orient.core.db.ODatabaseType;
@ -86,21 +87,11 @@ public class DatabaseEnvironment {
private static final String SERVER_URI; private static final String SERVER_URI;
public static final String DB_URI; public static final String DB_URI;
/*
private static final String DATABASE_TYPE = "graph";
private static final String STORAGE_MODE = "plocal";
*/
public static final String O_RESTRICTED_CLASS = "ORestricted"; public static final String O_RESTRICTED_CLASS = "ORestricted";
public static final CONNECTION_STRATEGY CONNECTION_STRATEGY_PARAMETER = CONNECTION_STRATEGY.ROUND_ROBIN_CONNECT; public static final CONNECTION_STRATEGY CONNECTION_STRATEGY_PARAMETER = CONNECTION_STRATEGY.ROUND_ROBIN_CONNECT;
//private static final String ALTER_DATETIME_FORMAT_QUERY_TEMPLATE = "ALTER DATABASE DATETIMEFORMAT \"%s\"";
private static final String DATE_FORMAT_NAME = "dateformat";
private static final String ALTER_DATETIME_FORMAT_QUERY_TEMPLATE = "ALTER DATABASE DATETIMEFORMAT :" + DATE_FORMAT_NAME;
// Used to indicate virtual admin security context // Used to indicate virtual admin security context
private static final String ADMIN_SECURITY_CONTEXT; private static final String ADMIN_SECURITY_CONTEXT;
public static final UUID ADMIN_SECURITY_CONTEXT_UUID; public static final UUID ADMIN_SECURITY_CONTEXT_UUID;
@ -113,7 +104,6 @@ public class DatabaseEnvironment {
private static final String CONTEXT_SECURITY_CONTEXT; private static final String CONTEXT_SECURITY_CONTEXT;
public static final UUID CONTEXT_SECURITY_CONTEXT_UUID; public static final UUID CONTEXT_SECURITY_CONTEXT_UUID;
protected static final String DB_KEY_FILENAME_VARNAME = "DB_KEY_FILENAME"; protected static final String DB_KEY_FILENAME_VARNAME = "DB_KEY_FILENAME";
protected static final String DB_KEY_ALGORITHM_VARNAME = "DB_KEY_ALGORITHM"; protected static final String DB_KEY_ALGORITHM_VARNAME = "DB_KEY_ALGORITHM";
@ -216,14 +206,19 @@ public class DatabaseEnvironment {
ISMDiscovery.manageISM(schemaAction); ISMDiscovery.manageISM(schemaAction);
} }
logger.info("Database Connection has been properly initialized");
} catch(Throwable e) { } catch(Throwable e) {
logger.error("Error initializing database connection", e); logger.error("Error initializing database connection", e);
throw new RuntimeException("Error initializing database connection", e); throw new RuntimeException("Error initializing database connection", e);
} }
logger.info("Database Connection has been properly initialized"); KEY = initDbKey(properties);
}
protected static Key initDbKey(Properties properties) {
try { try {
logger.trace("Going to get properties required to load DB key"); logger.trace("Going to get properties required to load DB key");
String keyFileName = properties.getProperty(DB_KEY_FILENAME_VARNAME); String keyFileName = properties.getProperty(DB_KEY_FILENAME_VARNAME);
@ -232,15 +227,20 @@ public class DatabaseEnvironment {
URL keyFileURL = DatabaseEnvironment.class.getClassLoader().getResource(keyFileName); URL keyFileURL = DatabaseEnvironment.class.getClassLoader().getResource(keyFileName);
File keyFile = new File(keyFileURL.toURI()); File keyFile = new File(keyFileURL.toURI());
logger.debug("Trying to load DB key from file {} created for algorithm {}", keyFile.getAbsolutePath(), keyAlgorithm); logger.debug("Trying to load DB key from file {} created for algorithm {}", keyFile.getAbsolutePath(), keyAlgorithm);
KEY = SymmetricKey.loadKeyFromFile(keyFile, keyAlgorithm); Key key = SymmetricKey.loadKeyFromFile(keyFile, keyAlgorithm);
logger.info("DB Key has been properly initialized");
return key;
} catch(Throwable e) { } catch(Throwable e) {
logger.error("Error loading DB Key", e); logger.error("Error loading DB Key", e);
throw new RuntimeException("Error loading DB Key. Unable to continue", e); throw new RuntimeException("Error loading DB Key. Unable to continue", e);
} }
logger.info("DB Key has been properly initialized");
} }
protected static void setDateTimeFormat(ODatabaseSession oDatabaseSession) {
oDatabaseSession.set(ATTRIBUTES.DATETIMEFORMAT, ISConstants.DATETIME_PATTERN);
}
private static boolean initGraphDB() throws Exception { private static boolean initGraphDB() throws Exception {
OLogManager.instance().setWarnEnabled(false); OLogManager.instance().setWarnEnabled(false);
@ -251,45 +251,21 @@ public class DatabaseEnvironment {
logger.info("Connecting as {} to {}", ROOT_USERNAME, DB_URI); logger.info("Connecting as {} to {}", ROOT_USERNAME, DB_URI);
OrientDB orientDB = new OrientDB(SERVER_URI, ROOT_USERNAME, ROOT_PASSWORD, OrientDBConfig.defaultConfig()); OrientDB orientDB = new OrientDB(SERVER_URI, ROOT_USERNAME, ROOT_PASSWORD, OrientDBConfig.defaultConfig());
/*
OServerAdmin serverAdmin = new OServerAdmin(SERVER_URI).connect(ROOT_USERNAME, ROOT_PASSWORD);
if(!serverAdmin.existsDatabase(DB, STORAGE_MODE)) {
*/
if(!orientDB.exists(DB)) { if(!orientDB.exists(DB)) {
logger.info("The database {} does not exist. Going to create it.", DB_URI); logger.info("The database {} does not exist. Going to create it.", DB_URI);
//serverAdmin.createDatabase(DB, DATABASE_TYPE, STORAGE_MODE);
orientDB.create(DB, ODatabaseType.PLOCAL); orientDB.create(DB, ODatabaseType.PLOCAL);
logger.trace("Connecting to newly created database {} as {} with default password", DB_URI, logger.trace("Connecting to newly created database {} as {} with default password", DB_URI,
DEFAULT_ADMIN_USERNAME); DEFAULT_ADMIN_USERNAME);
/*
OrientGraphFactory factory = new OrientGraphFactory(DB_URI, DEFAULT_ADMIN_USERNAME, DEFAULT_ADMIN_PASSWORD)
.setupPool(1, 10);
OrientGraphNoTx orientGraphNoTx = factory.getNoTx();
*/
ODatabasePool pool = new ODatabasePool(orientDB,DB, DEFAULT_ADMIN_USERNAME,DEFAULT_ADMIN_PASSWORD); ODatabasePool pool = new ODatabasePool(orientDB,DB, DEFAULT_ADMIN_USERNAME,DEFAULT_ADMIN_PASSWORD);
ODatabaseSession oDatabaseSession = pool.acquire(); ODatabaseSession oDatabaseSession = pool.acquire();
DatabaseEnvironment.setDateTimeFormat(oDatabaseSession);
/* Updating DateTimeFormat to be aligned with IS model definition */
/*
String query = String.format(ALTER_DATETIME_FORMAT_QUERY_TEMPLATE, ISConstants.DATETIME_PATTERN);
OCommandSQL preparedQuery = new OCommandSQL(query);
orientGraphNoTx.getRawGraph().command(preparedQuery).execute();
*/
Map<String, Object> map = new HashMap<>();
map.put(DATE_FORMAT_NAME, ISConstants.DATETIME_PATTERN);
oDatabaseSession.command(ALTER_DATETIME_FORMAT_QUERY_TEMPLATE, map);
//OMetadata oMetadata = orientGraphNoTx.getRawGraph().getMetadata();
OMetadata oMetadata = oDatabaseSession.getMetadata(); OMetadata oMetadata = oDatabaseSession.getMetadata();
OSecurity oSecurity = oMetadata.getSecurity(); OSecurity oSecurity = oMetadata.getSecurity();
@ -315,31 +291,20 @@ public class DatabaseEnvironment {
OSchema oSchema = oMetadata.getSchema(); OSchema oSchema = oMetadata.getSchema();
OClass oRestricted = oSchema.getClass(O_RESTRICTED_CLASS); OClass oRestricted = oSchema.getClass(O_RESTRICTED_CLASS);
/*
OrientVertexType v = orientGraphNoTx.getVertexBaseType();
v.addSuperClass(oRestricted);
*/
OClass v = oSchema.getClass("V"); OClass v = oSchema.getClass("V");
v.addSuperClass(oRestricted); v.addSuperClass(oRestricted);
/*
OrientEdgeType e = orientGraphNoTx.getEdgeBaseType();
e.addSuperClass(oRestricted);
*/
OClass e = oSchema.getClass("E"); OClass e = oSchema.getClass("E");
e.addSuperClass(oRestricted); e.addSuperClass(oRestricted);
// orientGraphNoTx.commit(); oDatabaseSession.commit();
// orientGraphNoTx.shutdown();
oDatabaseSession.close(); oDatabaseSession.close();
// factory.close();
pool.close(); pool.close();
return true; return true;
} }
// serverAdmin.close();
orientDB.close(); orientDB.close();
return false; return false;

View File

@ -1,19 +1,36 @@
package org.gcube.informationsystem.resourceregistry.dbinitialization; package org.gcube.informationsystem.resourceregistry.dbinitialization;
import org.gcube.informationsystem.model.reference.ISConstants;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.context.security.SecurityContext.PermissionMode;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.orientechnologies.orient.core.db.ODatabase.ATTRIBUTES;
import com.orientechnologies.orient.core.db.ODatabaseSession;
public class DatabaseEnvironmentTest { public class DatabaseEnvironmentTest {
private static Logger logger = LoggerFactory.getLogger(DatabaseEnvironmentTest.class); private static Logger logger = LoggerFactory.getLogger(DatabaseEnvironmentTest.class);
@Test // @Test
public void createDB() throws Exception{ public void createDB() throws Exception{
String db = DatabaseEnvironment.DB_URI; String db = DatabaseEnvironment.DB_URI;
logger.trace("Created DB is {}", db); logger.trace("Created DB is {}", db);
} }
@Test
public void testAlterDateTimeFormat() throws ResourceRegistryException {
ODatabaseSession oDatabaseSession = ContextUtility.getAdminSecurityContext().getDatabaseSession(PermissionMode.WRITER);
DatabaseEnvironment.setDateTimeFormat(oDatabaseSession);
String dateTime = oDatabaseSession.get(ATTRIBUTES.DATETIMEFORMAT).toString();
Assert.assertTrue(dateTime.compareTo(ISConstants.DATETIME_PATTERN)==0);
}
/* /*
@Test @Test
public void generateDBKey() throws Exception { public void generateDBKey() throws Exception {