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-02-19 00:11:14 +01:00
|
|
|
import static ch.eitchnet.utils.helper.StringHelper.DOT;
|
|
|
|
|
2013-12-23 01:55:34 +01:00
|
|
|
import java.sql.Connection;
|
2013-12-18 16:29:45 +01:00
|
|
|
import java.sql.Driver;
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.text.MessageFormat;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2014-02-19 00:11:14 +01:00
|
|
|
import java.util.Set;
|
2013-12-18 16:29:45 +01:00
|
|
|
|
2014-01-25 22:14:41 +01:00
|
|
|
import li.strolch.agent.api.ComponentContainer;
|
2014-01-10 23:23:17 +01:00
|
|
|
import li.strolch.agent.api.StrolchComponent;
|
|
|
|
import li.strolch.agent.impl.StrolchRealm;
|
2013-12-18 16:29:45 +01:00
|
|
|
import li.strolch.persistence.api.DbConnectionInfo;
|
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;
|
2013-12-24 00:18:00 +01:00
|
|
|
import li.strolch.runtime.StrolchConstants;
|
2013-12-18 16:29:45 +01:00
|
|
|
import li.strolch.runtime.configuration.ComponentConfiguration;
|
|
|
|
import li.strolch.runtime.configuration.StrolchConfigurationException;
|
2013-12-23 01:55:34 +01:00
|
|
|
import li.strolch.runtime.observer.ObserverHandler;
|
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
|
|
|
|
2013-12-23 01:55:34 +01:00
|
|
|
private static final String PROP_DB_URL = "db.url"; //$NON-NLS-1$
|
|
|
|
private static final String PROP_DB_USERNAME = "db.username"; //$NON-NLS-1$
|
|
|
|
private static final String PROP_DB_PASSWORD = "db.password"; //$NON-NLS-1$
|
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;
|
|
|
|
this.connetionInfoMap = new HashMap<>();
|
|
|
|
|
2014-02-19 00:11:14 +01:00
|
|
|
Set<String> realmNames = getContainer().getRealmNames();
|
|
|
|
for (String realmName : realmNames) {
|
|
|
|
|
|
|
|
String dbUrlKey = PROP_DB_URL;
|
|
|
|
String dbUsernameKey = PROP_DB_USERNAME;
|
|
|
|
String dbPasswordKey = PROP_DB_PASSWORD;
|
|
|
|
if (!realmName.equals(StrolchConstants.DEFAULT_REALM)) {
|
|
|
|
dbUrlKey += DOT + realmName;
|
|
|
|
dbUsernameKey += DOT + realmName;
|
|
|
|
dbPasswordKey += DOT + realmName;
|
|
|
|
}
|
|
|
|
|
|
|
|
String dbUrl = componentConfiguration.getString(dbUrlKey, null);
|
|
|
|
String username = componentConfiguration.getString(dbUsernameKey, null);
|
|
|
|
String password = componentConfiguration.getString(dbPasswordKey, null);
|
|
|
|
|
|
|
|
DbConnectionInfo connectionInfo = new DbConnectionInfo(realmName, dbUrl);
|
|
|
|
connectionInfo.setUsername(username);
|
|
|
|
connectionInfo.setPassword(password);
|
|
|
|
|
|
|
|
loadDriverForConnection(connectionInfo);
|
|
|
|
this.connetionInfoMap.put(realmName, connectionInfo);
|
|
|
|
}
|
2013-12-18 16:29:45 +01:00
|
|
|
|
2014-02-19 00:11:14 +01:00
|
|
|
super.initialize(componentConfiguration);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadDriverForConnection(DbConnectionInfo connectionInfo) {
|
2013-12-18 16:29:45 +01:00
|
|
|
Driver driver;
|
|
|
|
try {
|
2013-12-27 11:02:44 +01:00
|
|
|
// server loader does not seem to work in all contexts, thus:
|
|
|
|
org.postgresql.Driver.getLogLevel();
|
|
|
|
|
2014-02-19 00:11:14 +01:00
|
|
|
driver = DriverManager.getDriver(connectionInfo.getUrl());
|
2013-12-18 16:29:45 +01:00
|
|
|
} catch (SQLException e) {
|
|
|
|
String msg = "Failed to load DB driver for URL {0} due to: {1}"; //$NON-NLS-1$
|
2014-02-19 00:11:14 +01:00
|
|
|
msg = MessageFormat.format(msg, connectionInfo.getUrl(), e.getMessage());
|
2013-12-18 16:29:45 +01:00
|
|
|
throw new StrolchConfigurationException(msg, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
String compliant = driver.jdbcCompliant() ? "" : "non"; //$NON-NLS-1$ //$NON-NLS-2$
|
|
|
|
String msg = "Using {0} JDBC compliant Driver {1}.{2}"; //$NON-NLS-1$
|
|
|
|
msg = MessageFormat.format(msg, compliant, driver.getMajorVersion(), driver.getMinorVersion());
|
|
|
|
logger.info(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void start() {
|
|
|
|
|
|
|
|
// test all connections
|
|
|
|
DbConnectionCheck connectionCheck = new DbConnectionCheck(this.connetionInfoMap);
|
|
|
|
connectionCheck.checkConnections();
|
|
|
|
|
|
|
|
DbSchemaVersionCheck schemaVersionCheck = new DbSchemaVersionCheck(this.connetionInfoMap,
|
2013-12-23 01:55:34 +01:00
|
|
|
this.componentConfiguration);
|
2013-12-18 16:29:45 +01:00
|
|
|
schemaVersionCheck.checkSchemaVersion();
|
|
|
|
|
|
|
|
super.start();
|
|
|
|
}
|
|
|
|
|
2014-01-10 19:10:42 +01:00
|
|
|
public StrolchTransaction openTx(StrolchRealm realm) {
|
2013-12-23 01:55:34 +01:00
|
|
|
PostgreSqlStrolchTransaction tx = new PostgreSqlStrolchTransaction(realm, this);
|
|
|
|
if (getContainer().hasComponent(ObserverHandler.class)) {
|
|
|
|
tx.setObserverHandler(getContainer().getComponent(ObserverHandler.class));
|
|
|
|
}
|
|
|
|
return tx;
|
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 {
|
2013-12-24 02:12:32 +01:00
|
|
|
String url = dbInfo.getUrl();
|
|
|
|
String username = dbInfo.getUsername();
|
|
|
|
String password = dbInfo.getPassword();
|
|
|
|
Connection connection = DriverManager.getConnection(url, username, password);
|
|
|
|
connection.setAutoCommit(false);
|
|
|
|
return connection;
|
2013-12-23 01:55:34 +01:00
|
|
|
} catch (SQLException e) {
|
|
|
|
String msg = MessageFormat.format("Failed to get a connection for {0} due to {1}", dbInfo, e.getMessage()); //$NON-NLS-1$
|
|
|
|
throw new StrolchPersistenceException(msg, e);
|
|
|
|
}
|
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();
|
|
|
|
}
|
2013-12-18 16:29:45 +01:00
|
|
|
}
|