2013-12-18 16:29:45 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package li.strolch.persistence.postgresql;
|
|
|
|
|
2014-11-14 00:07:53 +01:00
|
|
|
import static ch.eitchnet.db.DbConstants.PROP_ALLOW_DATA_INIT_ON_SCHEMA_CREATE;
|
|
|
|
import static ch.eitchnet.db.DbConstants.PROP_ALLOW_SCHEMA_CREATION;
|
|
|
|
import static ch.eitchnet.db.DbConstants.PROP_ALLOW_SCHEMA_DROP;
|
|
|
|
import static ch.eitchnet.db.DbConstants.PROP_ALLOW_SCHEMA_MIGRATION;
|
2014-09-23 23:57:11 +02:00
|
|
|
import static li.strolch.agent.api.RealmHandler.SYSTEM_USER_DB_INITIALIZER;
|
2014-02-19 00:11:14 +01:00
|
|
|
|
2013-12-23 01:55:34 +01:00
|
|
|
import java.sql.Connection;
|
2013-12-18 16:29:45 +01:00
|
|
|
import java.text.MessageFormat;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2014-01-25 22:14:41 +01:00
|
|
|
import li.strolch.agent.api.ComponentContainer;
|
2014-09-23 23:57:11 +02:00
|
|
|
import li.strolch.agent.api.RealmHandler;
|
|
|
|
import li.strolch.agent.api.StrolchAgent;
|
2014-01-10 23:23:17 +01:00
|
|
|
import li.strolch.agent.api.StrolchComponent;
|
2014-02-24 21:48:59 +01:00
|
|
|
import li.strolch.agent.api.StrolchRealm;
|
2014-08-23 20:49:52 +02:00
|
|
|
import li.strolch.persistence.api.AuditDao;
|
2014-01-11 17:55:56 +01:00
|
|
|
import li.strolch.persistence.api.OrderDao;
|
2013-12-24 00:18:00 +01:00
|
|
|
import li.strolch.persistence.api.PersistenceHandler;
|
2014-01-11 17:55:56 +01:00
|
|
|
import li.strolch.persistence.api.ResourceDao;
|
2013-12-24 00:36:26 +01:00
|
|
|
import li.strolch.persistence.api.StrolchPersistenceException;
|
2013-12-18 16:29:45 +01:00
|
|
|
import li.strolch.persistence.api.StrolchTransaction;
|
|
|
|
import li.strolch.runtime.configuration.ComponentConfiguration;
|
2014-11-14 00:07:53 +01:00
|
|
|
import li.strolch.runtime.configuration.DbConnectionBuilder;
|
2014-09-23 23:57:11 +02:00
|
|
|
import li.strolch.runtime.configuration.StrolchConfiguration;
|
2013-12-18 16:29:45 +01:00
|
|
|
import li.strolch.runtime.configuration.StrolchConfigurationException;
|
2014-09-23 23:57:11 +02:00
|
|
|
import li.strolch.runtime.privilege.PrivilegeHandler;
|
2014-11-14 00:07:53 +01:00
|
|
|
import ch.eitchnet.db.DbConnectionCheck;
|
|
|
|
import ch.eitchnet.db.DbConnectionInfo;
|
|
|
|
import ch.eitchnet.db.DbDriverLoader;
|
|
|
|
import ch.eitchnet.db.DbException;
|
|
|
|
import ch.eitchnet.db.DbMigrationState;
|
|
|
|
import ch.eitchnet.db.DbSchemaVersionCheck;
|
2014-08-23 20:49:52 +02:00
|
|
|
import ch.eitchnet.privilege.model.Certificate;
|
2013-12-18 16:29:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
|
|
|
*/
|
2013-12-24 00:18:00 +01:00
|
|
|
public class PostgreSqlPersistenceHandler extends StrolchComponent implements PersistenceHandler {
|
2013-12-18 16:29:45 +01:00
|
|
|
|
2014-11-14 00:07:53 +01:00
|
|
|
public static final String SCRIPT_PREFIX = "strolch";
|
2013-12-18 16:29:45 +01:00
|
|
|
private ComponentConfiguration componentConfiguration;
|
|
|
|
private Map<String, DbConnectionInfo> connetionInfoMap;
|
|
|
|
|
2014-01-25 22:14:41 +01:00
|
|
|
public PostgreSqlPersistenceHandler(ComponentContainer container, String componentName) {
|
2013-12-18 16:29:45 +01:00
|
|
|
super(container, componentName);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void initialize(ComponentConfiguration componentConfiguration) {
|
|
|
|
|
|
|
|
this.componentConfiguration = componentConfiguration;
|
2014-02-19 00:11:14 +01:00
|
|
|
|
2014-11-14 00:07:53 +01:00
|
|
|
// server loader does not seem to work in all contexts, thus:
|
|
|
|
org.postgresql.Driver.getLogLevel();
|
|
|
|
|
|
|
|
DbConnectionBuilder connectionBuilder = new DbConnectionBuilder(getContainer(), componentConfiguration);
|
|
|
|
Map<String, DbConnectionInfo> connectionInfoMap = connectionBuilder.build();
|
|
|
|
for (DbConnectionInfo connectionInfo : connectionInfoMap.values()) {
|
|
|
|
try {
|
|
|
|
DbDriverLoader.loadDriverForConnection(connectionInfo);
|
|
|
|
} catch (DbException e) {
|
|
|
|
throw new StrolchConfigurationException("Could not load driver for connection "
|
|
|
|
+ connectionInfo.getUrl(), e);
|
|
|
|
}
|
2014-02-19 00:11:14 +01:00
|
|
|
}
|
2013-12-18 16:29:45 +01:00
|
|
|
|
2014-11-14 00:07:53 +01:00
|
|
|
this.connetionInfoMap = connectionInfoMap;
|
2014-02-19 00:11:14 +01:00
|
|
|
super.initialize(componentConfiguration);
|
|
|
|
}
|
|
|
|
|
2014-09-23 23:57:11 +02:00
|
|
|
/**
|
|
|
|
* Returns the map of {@link DbConnectionInfo} which can be used in maintenance mode
|
|
|
|
*
|
|
|
|
* @return the connetionInfoMap
|
|
|
|
*/
|
|
|
|
public Map<String, DbConnectionInfo> getConnetionInfoMap() {
|
|
|
|
return this.connetionInfoMap;
|
2013-12-18 16:29:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void start() {
|
|
|
|
|
|
|
|
// test all connections
|
|
|
|
DbConnectionCheck connectionCheck = new DbConnectionCheck(this.connetionInfoMap);
|
2014-11-14 00:07:53 +01:00
|
|
|
try {
|
|
|
|
connectionCheck.checkConnections();
|
|
|
|
} catch (DbException e) {
|
|
|
|
throw new StrolchConfigurationException("At least one connection failed: " + e.getMessage(), e);
|
|
|
|
}
|
2013-12-18 16:29:45 +01:00
|
|
|
|
2014-09-23 23:57:11 +02:00
|
|
|
boolean allowSchemaCreation = componentConfiguration.getBoolean(PROP_ALLOW_SCHEMA_CREATION, Boolean.FALSE);
|
2014-11-14 00:07:53 +01:00
|
|
|
boolean allowSchemaMigration = componentConfiguration.getBoolean(PROP_ALLOW_SCHEMA_MIGRATION, Boolean.FALSE);
|
2014-09-23 23:57:11 +02:00
|
|
|
boolean allowSchemaDrop = componentConfiguration.getBoolean(PROP_ALLOW_SCHEMA_DROP, Boolean.FALSE);
|
|
|
|
boolean allowDataInitOnSchemaCreate = componentConfiguration.getBoolean(PROP_ALLOW_DATA_INIT_ON_SCHEMA_CREATE,
|
|
|
|
Boolean.FALSE);
|
|
|
|
|
2014-11-14 00:07:53 +01:00
|
|
|
DbSchemaVersionCheck schemaVersionCheck = new DbSchemaVersionCheck(SCRIPT_PREFIX, this.getClass(),
|
|
|
|
allowSchemaCreation, allowSchemaMigration, allowSchemaDrop);
|
|
|
|
try {
|
|
|
|
schemaVersionCheck.checkSchemaVersion(this.connetionInfoMap);
|
|
|
|
} catch (DbException e) {
|
|
|
|
throw new StrolchConfigurationException(
|
|
|
|
"Failed to validate the schema for a connection: " + e.getMessage(), e);
|
|
|
|
}
|
2014-09-23 23:57:11 +02:00
|
|
|
|
|
|
|
// if allowed, perform DB initialization
|
|
|
|
if (!allowSchemaCreation || !allowSchemaDrop || !allowDataInitOnSchemaCreate) {
|
|
|
|
logger.info("Data Initialization not enabled, so not checking if needed.");
|
|
|
|
} else {
|
|
|
|
Map<String, DbMigrationState> dbMigrationStates = schemaVersionCheck.getDbMigrationStates();
|
|
|
|
String msg = "Data Initialization is enabled, checking for {0} realms if DB initialization is required...";
|
|
|
|
logger.info(MessageFormat.format(msg, dbMigrationStates.size()));
|
|
|
|
PrivilegeHandler privilegeHandler = getContainer().getPrivilegeHandler();
|
|
|
|
StrolchAgent agent = getContainer().getAgent();
|
|
|
|
PostgreSqlSchemaInitializer schemaInitializer = new PostgreSqlSchemaInitializer(agent, this,
|
|
|
|
dbMigrationStates);
|
|
|
|
privilegeHandler.runAsSystem(SYSTEM_USER_DB_INITIALIZER, schemaInitializer);
|
|
|
|
}
|
2013-12-18 16:29:45 +01:00
|
|
|
|
|
|
|
super.start();
|
|
|
|
}
|
|
|
|
|
2014-08-23 20:49:52 +02:00
|
|
|
@Override
|
|
|
|
public StrolchTransaction openTx(StrolchRealm realm, Certificate certificate, String action) {
|
2014-09-08 13:35:57 +02:00
|
|
|
return new PostgreSqlStrolchTransaction(getContainer().getPrivilegeHandler(), realm, certificate, action, this);
|
2013-12-18 16:29:45 +01:00
|
|
|
}
|
|
|
|
|
2013-12-23 01:55:34 +01:00
|
|
|
Connection getConnection(String realm) {
|
|
|
|
DbConnectionInfo dbInfo = this.connetionInfoMap.get(realm);
|
|
|
|
if (dbInfo == null) {
|
|
|
|
String msg = MessageFormat.format("There is no connection registered for the realm {0}", realm); //$NON-NLS-1$
|
|
|
|
throw new StrolchPersistenceException(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2014-11-14 00:07:53 +01:00
|
|
|
return dbInfo.openConnection();
|
|
|
|
} catch (DbException e) {
|
|
|
|
throw new StrolchPersistenceException("Failed to open a connection to " + dbInfo + " due to "
|
|
|
|
+ e.getMessage(), e);
|
2013-12-23 01:55:34 +01:00
|
|
|
}
|
2013-12-18 16:29:45 +01:00
|
|
|
}
|
2014-01-11 17:55:56 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public OrderDao getOrderDao(StrolchTransaction tx) {
|
|
|
|
return ((PostgreSqlStrolchTransaction) tx).getOrderDao();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ResourceDao getResourceDao(StrolchTransaction tx) {
|
|
|
|
return ((PostgreSqlStrolchTransaction) tx).getResourceDao();
|
|
|
|
}
|
2014-08-23 20:49:52 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public AuditDao getAuditDao(StrolchTransaction tx) {
|
|
|
|
return ((PostgreSqlStrolchTransaction) tx).getAuditDao();
|
|
|
|
}
|
2014-09-23 23:57:11 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void performDbInitialization() {
|
|
|
|
ComponentContainer container = getContainer();
|
|
|
|
StrolchAgent agent = container.getAgent();
|
|
|
|
PrivilegeHandler privilegeHandler = container.getPrivilegeHandler();
|
|
|
|
StrolchConfiguration strolchConfiguration = this.getContainer().getAgent().getStrolchConfiguration();
|
|
|
|
PostgreSqlDbInitializer sqlDbInitializer = new PostgreSqlDbInitializer(agent, this,
|
|
|
|
strolchConfiguration.getComponentConfiguration(getName()));
|
|
|
|
privilegeHandler.runAsSystem(RealmHandler.SYSTEM_USER_DB_INITIALIZER, sqlDbInitializer);
|
|
|
|
}
|
2013-12-18 16:29:45 +01:00
|
|
|
}
|