net.sf.mavenhist.persistency
Class AbstractDatabasePersistency

java.lang.Object
  extended by net.sf.mavenhist.persistency.AbstractDatabasePersistency
All Implemented Interfaces:
IConfigurablePersistency, IPersistency

public abstract class AbstractDatabasePersistency
extends Object
implements IPersistency, IConfigurablePersistency

Supports an easy implementation of a DB-persistency Layer. It supports connection handling and execution of queries with exception handling.


Constructor Summary
AbstractDatabasePersistency(String dbUser, String dbPassword, String dbURL)
          Creates a new db persistency object.
 
Method Summary
protected  void closeConnection(Connection conn)
          Closes a db connection opened by getConnection().
protected  void commit(Connection conn)
          Commits on connection and rethrows eventually generated Exceptions.
protected  boolean executeCreateStatement(Connection conn, String sql)
          Executes a create operation on the database.
 boolean executeCreateStatement(String sql)
          Executes a create operation on the database.
protected  Object executeQueryStatement(Connection conn, String sql, Object[] args, IResultSetProcessor resultSetProcessor)
          Executes a query operation on the database.
 Object executeQueryStatement(String sql, Object[] args, IResultSetProcessor resultSetProcessor)
          Executes a query operation on the database.
protected  int executeUpdateStatement(Connection conn, String sql, Object[] args)
          Executes an update statement (queries won't work, only insert/update calls allowed).
 int executeUpdateStatement(String sql, Object[] args)
          Executes an update statement (queries wont work, only insert/update calls allowed).
protected  Connection getConnection()
          Gets a db connection, must be released by calling closeConnection(Connection).
abstract  String getDbDriverClass()
          Gets dbDriverName.
 String getPassword()
          Gets dbPassword.
 String getURL()
          Gets dbURL.
 String getUser()
          Gets dbUser.
 void setPassword(String dbPassword)
          Sets dbPassword.
 void setURL(String dbURL)
          Sets dbURL.
 void setUser(String dbUser)
          Sets dbUser.
 void storeMetric(Metric metric)
          Stores the metric by calling the method storeMetric.
protected abstract  void storeMetric(Metric metric, Connection conn)
          Hook implementation to store a metric value in a customized matter.
 void storeMetrics(Metric[] metrics)
          Stores the metrics by calling the method storeMetric.
 String toString()
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface net.sf.mavenhist.persistency.IPersistency
initializeMetricsDataBase
 

Constructor Detail

AbstractDatabasePersistency

public AbstractDatabasePersistency(String dbUser,
                                   String dbPassword,
                                   String dbURL)
Creates a new db persistency object.

Parameters:
dbUser - the username for the db.
dbPassword - the password for the db.
dbURL - the server URL of the db.
Method Detail

getConnection

protected Connection getConnection()
                            throws PersistencyException
Gets a db connection, must be released by calling closeConnection(Connection).

Returns:
db connection
Throws:
PersistencyException - in case of error

commit

protected void commit(Connection conn)
               throws PersistencyException
Commits on connection and rethrows eventually generated Exceptions.

Parameters:
conn - to commit on.
Throws:
PersistencyException - thrown when commit fails.

closeConnection

protected void closeConnection(Connection conn)
                        throws PersistencyException
Closes a db connection opened by getConnection().

Parameters:
conn - to be closed.
Throws:
PersistencyException - in case of error

executeQueryStatement

public Object executeQueryStatement(String sql,
                                    Object[] args,
                                    IResultSetProcessor resultSetProcessor)
                             throws PersistencyException
Executes a query operation on the database.

Parameters:
sql - to be queried.
args - to be filled in for all '?'
resultSetProcessor - to process the result set.
Returns:
the result of the resultSetProcessor that was given to the method.
Throws:
PersistencyException - in case of error.

executeQueryStatement

protected Object executeQueryStatement(Connection conn,
                                       String sql,
                                       Object[] args,
                                       IResultSetProcessor resultSetProcessor)
                                throws PersistencyException
Executes a query operation on the database.

Parameters:
conn - to be used.
sql - to be queried.
args - to be filled in for all '?'
resultSetProcessor - to process the result set.
Returns:
the result of the resultSetProcessor that was given to the method.
Throws:
PersistencyException - in case of error.

executeCreateStatement

public boolean executeCreateStatement(String sql)
                               throws PersistencyException
Executes a create operation on the database.

Parameters:
sql - to be queried.
Returns:
true if the first result is a ResultSet object; false if the first result is an update count or there is no result.
Throws:
PersistencyException - in case of error.

executeCreateStatement

protected boolean executeCreateStatement(Connection conn,
                                         String sql)
                                  throws PersistencyException
Executes a create operation on the database.

Parameters:
conn - to be used.
sql - to be queried.
Returns:
true if the first result is a ResultSet object; false if the first result is an update count or there is no result
Throws:
PersistencyException - in case of error.

executeUpdateStatement

public int executeUpdateStatement(String sql,
                                  Object[] args)
                           throws PersistencyException
Executes an update statement (queries wont work, only insert/update calls allowed). Commits implicitely.

Parameters:
sql - to be executed
args - to be filled in for all '?'
Returns:
update record count
Throws:
PersistencyException - in case of error

executeUpdateStatement

protected int executeUpdateStatement(Connection conn,
                                     String sql,
                                     Object[] args)
                              throws PersistencyException
Executes an update statement (queries won't work, only insert/update calls allowed).

Parameters:
conn - connection to be used
sql - to be executed
args - to be filled in for all '?'
Returns:
update record count
Throws:
PersistencyException - in case of error

storeMetrics

public void storeMetrics(Metric[] metrics)
                  throws PersistencyException
Stores the metrics by calling the method storeMetric. Stores a collection of metrics to a persistable form.

Specified by:
storeMetrics in interface IPersistency
Parameters:
metrics - to store
Throws:
PersistencyException - in case of error

storeMetric

public void storeMetric(Metric metric)
                 throws PersistencyException
Stores the metric by calling the method storeMetric.

Parameters:
metric - metric to store in the DB.
Throws:
PersistencyException - if storing fails.

storeMetric

protected abstract void storeMetric(Metric metric,
                                    Connection conn)
                             throws PersistencyException
Hook implementation to store a metric value in a customized matter.

Parameters:
metric - to store in the DB.
conn - to access the database and store the metric.
Throws:
PersistencyException - thrown if something fails.

getPassword

public String getPassword()
Gets dbPassword.

Specified by:
getPassword in interface IConfigurablePersistency
Returns:
dbPassword

setPassword

public void setPassword(String dbPassword)
Sets dbPassword.

Specified by:
setPassword in interface IConfigurablePersistency
Parameters:
dbPassword - dbPassword to set

getURL

public String getURL()
Gets dbURL.

Specified by:
getURL in interface IConfigurablePersistency
Returns:
dbURL

setURL

public void setURL(String dbURL)
Sets dbURL.

Specified by:
setURL in interface IConfigurablePersistency
Parameters:
dbURL - dbURL to set

getUser

public String getUser()
Gets dbUser.

Specified by:
getUser in interface IConfigurablePersistency
Returns:
dbUser

setUser

public void setUser(String dbUser)
Sets dbUser.

Specified by:
setUser in interface IConfigurablePersistency
Parameters:
dbUser - dbUser to set

getDbDriverClass

public abstract String getDbDriverClass()
Gets dbDriverName.

Returns:
DB-drivername

toString

public String toString()

Overrides:
toString in class Object


Copyright © 2009. All Rights Reserved.