Configuration conf = new Configuration();
try {
if (db.getCreationOptions().containsKey(DBOPTION_MAPPINGFILE)) {
File mappingFile = new File((String) db.getCreationOptions().get(DBOPTION_MAPPINGFILE));
if (!mappingFile.exists() || !mappingFile.isFile()) {
throw new WGInvalidDatabaseException("Configured mapping file '" + db.getCreationOptions().get(DBOPTION_MAPPINGFILE) + "' does not exist or is no valid file.");
}
conf.addFile(mappingFile);
}
else if (db.getCreationOptions().containsKey(DBOPTION_MAPPINGRESOURCE)) {
conf.addResource((String) db.getCreationOptions().get(DBOPTION_MAPPINGRESOURCE), this.getClass().getClassLoader());
}
else if (_ddlVersion == WGDatabase.CSVERSION_WGA5) {
if (version.getPatchLevel() >= 3) {
conf.addResource(HIBERNATE_V5_P3_MAPPING_FILE, this.getClass().getClassLoader());
}
else if (version.getPatchLevel() >= 2) {
conf.addResource(HIBERNATE_V5_P2_MAPPING_FILE, this.getClass().getClassLoader());
}
else {
conf.addResource(HIBERNATE_V5_MAPPING_FILE, this.getClass().getClassLoader());
}
}
else if (_ddlVersion == WGDatabase.CSVERSION_WGA4_1) {
conf.addResource(HIBERNATE_V41_MAPPING_FILE, this.getClass().getClassLoader());
} else {
conf.addResource(HIBERNATE_V3_MAPPING_FILE, this.getClass().getClassLoader());
}
}
catch (MappingException e) {
throw new WGInvalidDatabaseException("Exception parsing hibernate mapping", e);
}
Properties props = new Properties();
if (path.startsWith("jdbc:")) {
props.put("hibernate.connection.url", path);
putDefaultConPoolProps(props);
}
else {
props.put("hibernate.connection.datasource", path);
}
if (user != null || pwd != null) {
props.put("hibernate.connection.username", WGUtils.getValueOrDefault(user, ""));
props.put("hibernate.connection.password", WGUtils.getValueOrDefault(pwd, ""));
}
if (db.getCreationOptions().containsKey("Driver")) {
props.put("hibernate.connection.driver_class", db.getCreationOptions().get("Driver"));
}
// Move old Hibernate2 packages to Hibernate3 packages
Iterator dbOptionsIt = db.getCreationOptions().keySet().iterator();
while (dbOptionsIt.hasNext()) {
Object key = dbOptionsIt.next();
String value = (String) db.getCreationOptions().get(key);
if (value != null && value.startsWith("net.sf.hibernate.")) {
value = "org.hibernate." + value.substring(17);
db.getCreationOptions().put(key, value);
}
}
props.put(Environment.ISOLATION, String.valueOf(Connection.TRANSACTION_READ_COMMITTED));
//props.put(Environment.QUERY_SUBSTITUTIONS, "true 1, false 0");
props.putAll(db.getCreationOptions());
conf.addProperties(props);
// Create session factory
try {
_sessionFactory = conf.buildSessionFactory();
}
catch (HibernateException e) {
throw new WGInvalidDatabaseException("Error creating session factory: " + e.getMessage(), e);
}
// parse masterPersistenceTimeout
if (db.getCreationOptions().containsKey(COPTION_MASTERPERSISTENCE_TIMEOUT)) {
_masterPersistenceTimeout = Long.parseLong((String)db.getCreationOptions().get(COPTION_MASTERPERSISTENCE_TIMEOUT));
}
// parse HQL query default type
String hqlType = (String) db.getCreationOptions().get(COPTION_HQL_FETCH_TYPE);
if (hqlType != null) {
_hqlLazyByDefault = hqlType.equals(HQL_FETCHTYPE_LAZY);
}
// open session
WGUserAccess accessLevel;
try {
accessLevel = openSession(MasterLoginAuthSession.getInstance(), pwd, true);
}
catch (WGUnavailableException e) {
throw new WGInvalidDatabaseException("Error opening initial session", e);
}
catch (WGBackendException e) {
throw new WGInvalidDatabaseException("Error opening initial session", e);
}
if (accessLevel.getAccessLevel() <= WGDatabase.ACCESSLEVEL_NOACCESS) {
try {
close();
}