/*
* Copyright (C) 2003-2007 eXo Platform SAS.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see.
*/
package org.exoplatform.services.database.impl;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.sql.DataSource;
import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
import org.enhydra.jdbc.standard.StandardXADataSource;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.container.xml.PropertiesParam;
import org.exoplatform.services.database.DatabaseService;
import org.exoplatform.services.database.ExoDatasource;
import org.exoplatform.services.transaction.TransactionService;
/**
* Created by The eXo Platform SAS Author : Tuan Nguyen
* tuan08@users.sourceforge.net Apr 4, 2006
*/
public class XAPoolTxSupportDatabaseService implements DatabaseService {
private HashMap datasources_;
private ExoDatasource defaultDS_;
private TransactionService txService_;
public XAPoolTxSupportDatabaseService(InitParams params, TransactionService txService) throws Exception {
datasources_ = new HashMap(5);
txService_ = txService;
Iterator i = params.getPropertiesParamIterator();
while (i.hasNext()) {
PropertiesParam param = (PropertiesParam) i.next();
String name = param.getName();
ExoDatasource ds = new ExoDatasource(createDatasource(param.getProperties()));
datasources_.put(name, ds);
if (defaultDS_ == null)
defaultDS_ = ds;
}
}
public ExoDatasource getDatasource() throws Exception {
return defaultDS_;
}
public ExoDatasource getDatasource(String dsName) throws Exception {
return datasources_.get(dsName);
}
public Connection getConnection() throws Exception {
return defaultDS_.getConnection();
}
public Connection getConnection(String dsName) throws Exception {
ExoDatasource ds = datasources_.get(dsName);
return ds.getConnection();
}
public void closeConnection(Connection conn) throws Exception {
defaultDS_.closeConnection(conn);
}
public TransactionService getTransactionService() throws Exception {
return txService_;
}
private DataSource createDatasource(Map props) throws Exception {
StandardXADataSource ds = new StandardXADataSource();
ds.setDriverName(props.get("connection.driver"));
ds.setUrl(props.get("connection.url"));
ds.setUser(props.get("connection.login"));
ds.setPassword(props.get("connection.password"));
// ds.setMinCon(Integer.parseInt(props.get("connection.min-size"))) ;
// ds.setMaxCon(Integer.parseInt(props.get("connection.max-size"))) ;
ds.setTransactionManager(txService_.getTransactionManager());
StandardXAPoolDataSource pool = new StandardXAPoolDataSource(3);
pool.setMinSize(Integer.parseInt(props.get("connection.min-size")));
pool.setMaxSize(Integer.parseInt(props.get("connection.max-size")));
pool.setUser(props.get("connection.login"));
pool.setPassword(props.get("connection.password"));
pool.setDataSource(ds);
return pool;
}
}