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;
|
2015-04-21 16:47:04 +02:00
|
|
|
import java.util.Map.Entry;
|
2013-12-18 16:29:45 +01:00
|
|
|
|
2015-04-21 15:04:51 +02:00
|
|
|
import javax.sql.DataSource;
|
|
|
|
|
2015-11-20 15:20:00 +01:00
|
|
|
import ch.eitchnet.db.DbMigrationState;
|
|
|
|
import ch.eitchnet.db.DbSchemaVersionCheck;
|
|
|
|
import ch.eitchnet.privilege.model.Certificate;
|
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;
|
2015-06-29 08:34:14 +02:00
|
|
|
import li.strolch.persistence.api.ActivityDao;
|
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;
|
2015-04-21 16:47:04 +02:00
|
|
|
import li.strolch.persistence.postgresql.PostgreSqlDbConnectionBuilder.StrolchPostgreDataSource;
|
2013-12-18 16:29:45 +01:00
|
|
|
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;
|
|
|
|
import li.strolch.runtime.privilege.PrivilegeHandler;
|
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 15:05:16 +01:00
|
|
|
public static final String SCRIPT_PREFIX = "strolch"; //$NON-NLS-1$
|
2015-04-21 15:04:51 +02:00
|
|
|
private Map<String, DataSource> dsMap;
|
2013-12-18 16:29:45 +01:00
|
|
|
|
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
|
2015-06-11 19:06:52 +02:00
|
|
|
public void initialize(ComponentConfiguration componentConfiguration) throws Exception {
|
2013-12-18 16:29:45 +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();
|
|
|
|
|
2015-04-21 15:04:51 +02:00
|
|
|
DbConnectionBuilder connectionBuilder = new PostgreSqlDbConnectionBuilder(getContainer(),
|
|
|
|
componentConfiguration);
|
|
|
|
this.dsMap = connectionBuilder.build();
|
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
|
|
|
|
*
|
2015-04-21 15:04:51 +02:00
|
|
|
* @return the dsMap
|
2014-09-23 23:57:11 +02:00
|
|
|
*/
|
2015-04-21 15:04:51 +02:00
|
|
|
public Map<String, DataSource> getDataSources() {
|
|
|
|
return this.dsMap;
|
2013-12-18 16:29:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-06-11 19:06:52 +02:00
|
|
|
public void start() throws Exception {
|
2013-12-18 16:29:45 +01:00
|
|
|
|
2015-07-10 10:48:17 +02:00
|
|
|
ComponentConfiguration configuration = getConfiguration();
|
|
|
|
boolean allowSchemaCreation = configuration.getBoolean(PROP_ALLOW_SCHEMA_CREATION, Boolean.FALSE);
|
|
|
|
boolean allowSchemaMigration = configuration.getBoolean(PROP_ALLOW_SCHEMA_MIGRATION, Boolean.FALSE);
|
|
|
|
boolean allowSchemaDrop = configuration.getBoolean(PROP_ALLOW_SCHEMA_DROP, Boolean.FALSE);
|
|
|
|
boolean allowDataInitOnSchemaCreate = configuration.getBoolean(PROP_ALLOW_DATA_INIT_ON_SCHEMA_CREATE,
|
2014-09-23 23:57:11 +02:00
|
|
|
Boolean.FALSE);
|
|
|
|
|
2014-11-14 00:07:53 +01:00
|
|
|
DbSchemaVersionCheck schemaVersionCheck = new DbSchemaVersionCheck(SCRIPT_PREFIX, this.getClass(),
|
|
|
|
allowSchemaCreation, allowSchemaMigration, allowSchemaDrop);
|
2015-06-11 19:12:09 +02:00
|
|
|
schemaVersionCheck.checkSchemaVersion(this.dsMap);
|
2014-09-23 23:57:11 +02:00
|
|
|
|
|
|
|
// if allowed, perform DB initialization
|
2014-11-14 15:05:16 +01:00
|
|
|
if (!allowSchemaCreation || !allowDataInitOnSchemaCreate) {
|
2015-11-20 15:20:00 +01:00
|
|
|
logger.info(
|
|
|
|
"Data Initialization not enabled as either 'allowSchemaCreation or 'allowDataInitOnSchemaCreate' is false!, so not checking if needed."); //$NON-NLS-1$
|
2014-09-23 23:57:11 +02:00
|
|
|
} else {
|
|
|
|
Map<String, DbMigrationState> dbMigrationStates = schemaVersionCheck.getDbMigrationStates();
|
2014-11-14 15:05:16 +01:00
|
|
|
String msg = "Data Initialization is enabled, checking for {0} realms if DB initialization is required..."; //$NON-NLS-1$
|
2014-09-23 23:57:11 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-04-21 16:47:04 +02:00
|
|
|
@Override
|
2015-06-11 19:06:52 +02:00
|
|
|
public void destroy() throws Exception {
|
2015-11-20 15:20:00 +01:00
|
|
|
if (this.dsMap != null) {
|
|
|
|
for (Entry<String, DataSource> entry : this.dsMap.entrySet()) {
|
|
|
|
StrolchPostgreDataSource ds = (StrolchPostgreDataSource) entry.getValue();
|
|
|
|
ds.shutdown();
|
|
|
|
}
|
2015-04-21 16:47:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
super.destroy();
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-04-21 15:04:51 +02:00
|
|
|
DataSource ds = this.dsMap.get(realm);
|
|
|
|
if (ds == null) {
|
|
|
|
String msg = MessageFormat.format("There is no DataSource registered for the realm {0}", realm); //$NON-NLS-1$
|
2013-12-23 01:55:34 +01:00
|
|
|
throw new StrolchPersistenceException(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2015-04-21 15:04:51 +02:00
|
|
|
return ds.getConnection();
|
|
|
|
} catch (Exception e) {
|
2014-11-14 15:05:16 +01:00
|
|
|
String msg = "Failed to open a connection to {0} due to {1}"; //$NON-NLS-1$
|
2015-04-21 15:04:51 +02:00
|
|
|
throw new StrolchPersistenceException(MessageFormat.format(msg, ds, 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
|
|
|
|
2015-06-29 08:34:14 +02:00
|
|
|
@Override
|
|
|
|
public ActivityDao getActivityDao(StrolchTransaction tx) {
|
|
|
|
return ((PostgreSqlStrolchTransaction) tx).getActivityDao();
|
|
|
|
}
|
2015-07-10 10:48:17 +02:00
|
|
|
|
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();
|
2014-11-14 15:05:16 +01:00
|
|
|
StrolchConfiguration strolchConfiguration = getContainer().getAgent().getStrolchConfiguration();
|
2014-09-23 23:57:11 +02:00
|
|
|
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
|
|
|
}
|