// Mandatory db options
dbOptions.put(WGDatabase.COPTION_DBREFERENCE, strKey.toLowerCase());
// get the database object
WGDatabase db = null;
try {
if (config.isLazyConnecting()) {
db = server.prepareDatabase(typeClass, dbOptions);
}
else {
db = server.openDatabase(typeClass, dbOptions);
}
}
catch (Throwable e1) {
String message = "Could not open database for key " + strKey.toLowerCase();
log.error(message, e1);
dbConnectionFailures.put(strKey, e1);
return null;
}
if (db == null || (!config.isLazyConnecting() && !db.isSessionOpen())) {
log.error("Could not open database for key " + strKey.toLowerCase() + " - Check logged messages above for error details");
dbConnectionFailures.put(strKey, "Could not open database for key " + strKey.toLowerCase() + " - Check wga log for details");
return null;
}
if (!config.isLazyConnecting()) {
db.getSessionContext().setTask("Initializing database in WGA");
log.info("Mapping " + dbType + " on path \"" + db.getPath() + "\" (" + db.getTypeName() + ") to database key \"" + strKey.toLowerCase() + "\"");
try {
if (db.getContentStoreVersion() != WGDatabase.CSVERSION_NO_CONTENTSTORE) {
log.info("Database for " + strKey.toLowerCase() + " is a WGA Content Store of version " + db.getContentStoreVersion());
}
}
catch (WGAPIException e) {
log.error("Exception determining content store version of database " + strKey.toLowerCase(), e);
}
}
else {
log.info("Preparing " + dbType + " on path \"" + db.getPath() + "\" (" + db.getTypeName() + ") as database for key \"" + strKey.toLowerCase() + "\"");
}
if (title != null) {
db.setTitle(title);
}
// Inject the default language if configured, else trigger determination
if (config instanceof ContentStore) {
ContentStore csConfig = (ContentStore) config;
if (!WGUtils.isEmpty(csConfig.getDefaultLanguage())) {
db.setDefaultLanguage(csConfig.getDefaultLanguage());
}
else {
try {
db.determineDefaultLanguage();
}
catch (WGAPIException e) {
getLog().error("Exception determining default language for " + dbType + " " + db.getDbReference(), e);
}
}
}
// Set mandatory database attributes
initializeDBAttributes(db, strKey, domainConfig.getName(), firstLevelDBOptions);
// Inject domain authentication module
if (domainConfig.getAuthModule() != null) {
try {
db.setAuthenticationModule(new DomainRedirectionAuthModule(this, domainConfig.getName()));
}
catch (WGIllegalArgumentException e) {
String message = "Exception setting authentication module of " + dbType + " '" + strKey + "'";
getLog().error(message, e);
getLog().error("Cancelling connection of " + dbType + " '" + strKey + "' because the configured authentication could not be set");
dbConnectionFailures.put(strKey, message);
try {
db.close();
}
catch (Exception e2) {
}
return null;
}
}
// Configure design provider, if neccessary
if (config instanceof ContentStore) {
ContentStore csConfig = (ContentStore) config;
if (csConfig.getDesign() != null) {
getDesignManager().applyDesign(db, csConfig);
}
}
// Determine if ACL is empty
// If so, eventually add domain default manager
boolean aclEmpty = false;
try {
if (db.isConnected() && db.isSessionOpen() && db.hasFeature(WGDatabase.FEATURE_ACL_MANAGEABLE) && db.getACL().getAllEntries().size() == 0) {
aclEmpty = true;
}
}
catch (WGBackendException e1) {
getLog().error("Error retrieving ACL state of " + dbType + " '" + db.getDbReference() + "'", e1);
}
if (aclEmpty && domainConfig.getDefaultManager() != null) {
try {
getLog().info("Adding default manager '" + domainConfig.getDefaultManager() + "' to ACL of '" + strKey + "'");
db.getACL().createUserEntry(domainConfig.getDefaultManager(), WGDatabase.ACCESSLEVEL_MANAGER);
}
catch (WGAPIException e) {
getLog().error("Exception on adding default manager to ACL of '" + strKey + "'", e);
}
}
// Process system container
SystemContainerManager.SystemContainerContext scContext = null;
try {
scContext = _systemContainerManager.addDatabase(db, aclEmpty);
}
catch (InvalidCSConfigVersionException e) {
this.log.error("Unable to process system file container. The design of " + dbType + " '" + db.getDbReference() + "' was developed for a higher WGA version: " + e.getTargetVersion());
}
catch (Exception e) {
this.log.error("Exception processing system file container for " + dbType + " '" + strKey.toLowerCase() + "'", e);
}
// Merge publisher options from global, server, design, database
Map<String, String> publisherOptions = new HashMap<String, String>();
publisherOptions.putAll(_globalPublisherOptions);
if (scContext != null) {
scContext.putPublisherOptions(publisherOptions);
}
publisherOptions.putAll(config.getPublisherOptions());
// We collect those options that were added on database level in here
// and add this set as database attribute
// so we can tell them later from lower priority options
Set<String> firstLevelPublisherOptions = new HashSet<String>();
firstLevelPublisherOptions.addAll(config.getPublisherOptions().keySet());
db.setAttribute(DBATTRIB_FIRSTLEVELPUBLISHEROPTIONS, firstLevelPublisherOptions);
// Publisher options initialisation, which is equal for content dbs and plugins
processPublisherOptions(db, publisherOptions);
// check if db is empty before hdb script runs
boolean isEmptyDB = false;
try {
isEmptyDB = (db.isConnected() && db.hasFeature(WGDatabase.FEATURE_FULLCONTENTFEATURES) && db.isContentEmpty());
} catch (WGAPIException e) {
this.log.error("Unable to check if database '" + db.getDbReference() + "' is empty.", e);
}
// Validate default language definition
if (!isEmptyDB && db.hasFeature(WGDatabase.FEATURE_FULLCONTENTFEATURES)) {
db.onConnect(new ValidateDefaultLanguageAction());
}
// Eventually prepare "external self-personalisation"
if (db.hasFeature(WGDatabase.FEATURE_FULLCONTENTFEATURES) && domainConfig.getPersonalisation() == null && !db.hasFeature(WGDatabase.FEATURE_SELF_PERSONALIZABLE)) {
try {
createExternalSelfPersonalisation(db);
}
catch (WGAPIException e) {
this.log.error("Exception creating external personalisation database for db '" + db.getDbReference() + "'. Profiles will not be available.", e);
}
}
// Do system container initialisations
if (scContext != null) {
scContext.performInitialisation(new Boolean(isEmptyDB));
// Revalidate the default language if the database was empty before initialisation, might have new definitions now
if (isEmptyDB) {
db.onConnect(new ValidateDefaultLanguageAction());
}
}
// Mark this database as fully connected
db.setAttribute(DBATTRIB_FULLY_CONNECTED, "true");
if (_externalFileServingConfig.isEnabled() && db.getBooleanAttribute(DBATTRIB_EXTERNAL_FILE_SERVING_ENABLED, false)) {
getLog().info("External file serving enabled for database '" + db.getDbReference() + "'.");
}
return db;
}