/*
* #%L
* KissData
*
* $Id$
* $HeadURL$
* %%
* Copyright (C) 2012 ! BRIGHTS.COM
* %%
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* #L%
*/
package com.bryghts.kissdata.engines.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.bryghts.kissdata.DataListener;
import com.bryghts.kissdata.IStoreConfig;
import com.bryghts.kissdata.bites.ConfigBite;
import com.bryghts.kissdata.bites.DataBite;
import com.bryghts.kissdata.bites.KissBite;
import com.bryghts.kissdata.engines.IKissEngine;
import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;
public class JdbcEngine implements IKissEngine
{
private JdbcConfig config;
private BoneCP connectionPool = null;
public void init(IStoreConfig configInterface) throws ConfigBite
{
if(!(configInterface instanceof JdbcConfig))
throw new ConfigBite("Configuration instance is not a JdbcConfig");
config = (JdbcConfig) configInterface;
try
{
Class.forName(config.getDriverClassName());
}
catch (ClassNotFoundException e)
{
throw new ConfigBite("Driver not found", e);
}
BoneCPConfig bcpConfig = new BoneCPConfig();
bcpConfig.setJdbcUrl(config.getConnectionString());
bcpConfig.setUsername(config.getUsername());
bcpConfig.setPassword(config.getPassword());
bcpConfig.setMinConnectionsPerPartition(5);
bcpConfig.setMaxConnectionsPerPartition(10);
bcpConfig.setPartitionCount(1);
try
{
connectionPool = new BoneCP(bcpConfig);
}
catch (SQLException e)
{
throw new ConfigBite("Could not create the ConnectionPool");
}
}
@Override
protected void finalize() throws Throwable
{
connectionPool.shutdown();
super.finalize();
}
public void tell(String petition, Object[] parameters) throws KissBite
{
if(parameters == null || parameters.length == 0)
tellPetition(petition);
else
tellPetition(petition, parameters);
}
private void tellPetition(String petition, Object[] parameters) throws KissBite
{
Connection connection = null;
PreparedStatement stmt = null;
try
{
connection = connectionPool.getConnection();
if(connection == null)
throw new DataBite("Could not get a Connection from the pool");
stmt = connection.prepareStatement(petition);
int i = 1;
for (Object parameter : parameters)
{
stmt.setObject(i, parameter);
i++;
}
stmt.executeUpdate();
stmt.close();
stmt = null;
try
{
connection.close();
}
catch(SQLException e)
{
connection = null;
throw e;
}
}
catch (SQLException e)
{
if(stmt != null)
try
{stmt.close();}
catch (SQLException e1)
{
}
if(connection != null)
try
{connection.close();}
catch (SQLException e1)
{
}
throw new DataBite(e);
}
}
private void tellPetition(String petition) throws KissBite
{
Connection connection = null;
Statement stmt = null;
try
{
connection = connectionPool.getConnection();
if(connection == null)
throw new DataBite("Could not get a Connection from the pool");
stmt = connection.createStatement();
stmt.executeUpdate(petition);
stmt.close();
stmt = null;
try
{
connection.close();
}
catch(SQLException e)
{
connection = null;
throw e;
}
}
catch (SQLException e)
{
if(stmt != null)
try
{stmt.close();}
catch (SQLException e1)
{
}
if(connection != null)
try
{connection.close();}
catch (SQLException e1)
{
}
throw new DataBite(e);
}
}
public <T> List<T> ask(
String question,
DataListener<T> listener,
Object[] parameters) throws KissBite
{
if(parameters == null || parameters.length == 0)
return askQuestion(question, listener);
else
return askQuestion(question, listener, parameters);
}
private <T> List<T> askQuestion(
String question,
DataListener<T> listener,
Object[] parameters) throws KissBite
{
Connection connection = null;
PreparedStatement stmt = null;
try
{
connection = connectionPool.getConnection();
if(connection == null)
throw new DataBite("Could not get a Connection from the pool");
stmt = connection.prepareStatement(question);
int i = 1;
for (Object parameter : parameters)
{
stmt.setObject(i, parameter);
i++;
}
ResultSet rs = stmt.executeQuery();
JdbcLine line = new JdbcLine(rs);
List<T> r = new ArrayList<T>();
while(rs.next())
{
r.add(listener.listenLine(line));
}
stmt.close();
stmt = null;
try
{
connection.close();
}
catch(SQLException e)
{
connection = null;
throw e;
}
return r;
}
catch (SQLException e)
{
if(stmt != null)
try
{stmt.close();}
catch (SQLException e1)
{
}
if(connection != null)
try
{connection.close();}
catch (SQLException e1)
{
}
throw new DataBite(e);
}
}
private <T> List<T> askQuestion(
String question,
DataListener<T> listener) throws KissBite
{
Connection connection = null;
Statement stmt = null;
try
{
connection = connectionPool.getConnection();
if(connection == null)
throw new DataBite("Could not get a Connection from the pool");
stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(question);
JdbcLine line = new JdbcLine(rs);
List<T> r = new ArrayList<T>();
while(rs.next())
{
T tmp = listener.listenLine(line);
if(tmp != null)
r.add(tmp);
}
stmt.close();
stmt = null;
try
{
connection.close();
}
catch(SQLException e)
{
connection = null;
throw e;
}
return r;
}
catch (SQLException e)
{
if(stmt != null)
try
{stmt.close();}
catch (SQLException e1)
{
}
if(connection != null)
try
{connection.close();}
catch (SQLException e1)
{
}
throw new DataBite(e);
}
}
}