|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| DatabaseCommandProcessor.java | 0% | 7,7% | 27,3% | 9,7% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* Replica is published under the terms
|
|
| 3 |
* of the Apache Software License.
|
|
| 4 |
*/
|
|
| 5 |
package replica.database.commands;
|
|
| 6 |
|
|
| 7 |
import java.sql.Connection;
|
|
| 8 |
import java.sql.DriverManager;
|
|
| 9 |
import java.sql.SQLException;
|
|
| 10 |
import java.sql.Statement;
|
|
| 11 |
|
|
| 12 |
import javax.sql.DataSource;
|
|
| 13 |
|
|
| 14 |
import replica.command.Command;
|
|
| 15 |
import replica.core.ApplicationCommandProcessor;
|
|
| 16 |
import replica.core.ApplicationMessage;
|
|
| 17 |
import replica.session.Session;
|
|
| 18 |
import replica.session.SessionManager;
|
|
| 19 |
|
|
| 20 |
/**
|
|
| 21 |
* It extends the application standart processor, to provide some
|
|
| 22 |
* database specific services.
|
|
| 23 |
*
|
|
| 24 |
* @author Pedro Costa
|
|
| 25 |
* @author Helder Silva
|
|
| 26 |
* @since 20/Jan/2004
|
|
| 27 |
*/
|
|
| 28 |
public class DatabaseCommandProcessor extends ApplicationCommandProcessor { |
|
| 29 |
|
|
| 30 |
private DataSource dataSource;
|
|
| 31 |
private SessionManager sessionManager;
|
|
| 32 |
|
|
| 33 |
final static String CONNECTION = "CONNECTION"; |
|
| 34 |
|
|
| 35 |
/**
|
|
| 36 |
*
|
|
| 37 |
*/
|
|
| 38 | 3 |
public DatabaseCommandProcessor() {
|
| 39 | 3 |
super();
|
| 40 |
} |
|
| 41 |
|
|
| 42 | 0 |
protected Object executeCommand(Command command, ApplicationMessage appMsg){
|
| 43 |
|
|
| 44 | 0 |
if( command instanceof DatabaseCommand ) |
| 45 | 0 |
return executeCommand( (DatabaseCommand)command, appMsg);
|
| 46 |
|
|
| 47 | 0 |
return super.executeCommand( command, appMsg); |
| 48 |
} |
|
| 49 |
|
|
| 50 | 0 |
protected Object executeCommand(DatabaseCommand command, ApplicationMessage appMsg){
|
| 51 |
|
|
| 52 | 0 |
Statement st = null;
|
| 53 | 0 |
Connection connection = null;
|
| 54 |
|
|
| 55 | 0 |
try{
|
| 56 |
|
|
| 57 | 0 |
connection = getConnection(appMsg.getSessionID()); |
| 58 |
|
|
| 59 | 0 |
return command.execute(
|
| 60 |
new DatabaseCommandVO(connection,appMsg.getBody().toString()) );
|
|
| 61 |
} |
|
| 62 |
catch(SQLException e){
|
|
| 63 | 0 |
if( connection != null ) |
| 64 | 0 |
destroyConnection( connection , appMsg.getSessionID() ); |
| 65 | 0 |
throw new RuntimeException(e); |
| 66 |
} |
|
| 67 |
} |
|
| 68 |
|
|
| 69 | 0 |
private void destroyConnection( Connection connection, String userSessionID ){ |
| 70 |
|
|
| 71 | 0 |
sessionManager.getSession( userSessionID ).removeObject( connection ); |
| 72 |
|
|
| 73 | 0 |
try{
|
| 74 | 0 |
connection.close(); |
| 75 |
} |
|
| 76 |
catch(SQLException e){
|
|
| 77 | 0 |
e.printStackTrace(); |
| 78 |
} |
|
| 79 |
} |
|
| 80 |
|
|
| 81 |
/**
|
|
| 82 |
* It tries to get a connection from the user session. If it can find one, a
|
|
| 83 |
* new one is created and put on session.
|
|
| 84 |
*
|
|
| 85 |
* @param sessionId
|
|
| 86 |
* @return a connection for the user.
|
|
| 87 |
*/
|
|
| 88 | 0 |
Connection getConnection(String sessionId) throws SQLException{
|
| 89 |
|
|
| 90 | 0 |
Session session = sessionManager.getSession(sessionId); |
| 91 |
|
|
| 92 | 0 |
Connection connection = (Connection)session.getObject(CONNECTION); |
| 93 |
|
|
| 94 | 0 |
if( connection == null ){ |
| 95 |
|
|
| 96 | 0 |
connection = getConnection(session.getUserId(), session.getUserPassword()); |
| 97 |
|
|
| 98 | 0 |
session.putObject(CONNECTION, connection); |
| 99 |
} |
|
| 100 |
|
|
| 101 | 0 |
return connection;
|
| 102 |
} |
|
| 103 |
|
|
| 104 |
/**
|
|
| 105 |
* Returns a connection from the data source for the given user
|
|
| 106 |
* and password.
|
|
| 107 |
*
|
|
| 108 |
* If user is null or empty, DataSource.getConnection is used,
|
|
| 109 |
* otherwise, DataSource.getConnection(String user, String password)
|
|
| 110 |
* is called.
|
|
| 111 |
*
|
|
| 112 |
* IMPLEMENTATION NOTE: the jakarta commons dbcp BasicDataSource does
|
|
| 113 |
* note support the getConnection(String user, String password).
|
|
| 114 |
* Use the Spring provided DriverManagerDataSource
|
|
| 115 |
* witch does not provide a connection pool service.
|
|
| 116 |
*
|
|
| 117 |
* @param userId
|
|
| 118 |
* @param password
|
|
| 119 |
* @return
|
|
| 120 |
* @throws SQLException if something goes wrong!
|
|
| 121 |
*/
|
|
| 122 | 0 |
Connection getConnection(String userId, String password) throws SQLException{
|
| 123 |
|
|
| 124 | 0 |
Connection c = null;
|
| 125 |
|
|
| 126 |
// TODO re-think this to provide a connection pool.
|
|
| 127 |
// the jakarta commons dbcp does not support getConnection(user,pwd).
|
|
| 128 |
|
|
| 129 | 0 |
if( userId == null || userId.trim().length() == 0 ) |
| 130 | 0 |
c = getDataSource().getConnection(); |
| 131 |
else
|
|
| 132 | 0 |
c = getDataSource().getConnection(userId, password); |
| 133 |
|
|
| 134 | 0 |
if( !c.getAutoCommit() )
|
| 135 | 0 |
c.setAutoCommit(true);
|
| 136 |
|
|
| 137 | 0 |
return c;
|
| 138 |
} |
|
| 139 |
|
|
| 140 | 0 |
Connection getNewConnectionFromDriverManager(String driverName, |
| 141 |
String url, String user, String pwd) throws SQLException{
|
|
| 142 |
|
|
| 143 | 0 |
try{
|
| 144 | 0 |
Class.forName ( driverName ); |
| 145 |
} |
|
| 146 |
catch(ClassNotFoundException e){
|
|
| 147 | 0 |
throw new RuntimeException(e); |
| 148 |
} |
|
| 149 |
|
|
| 150 | 0 |
if( pwd == null ) |
| 151 | 0 |
pwd = "";
|
| 152 |
|
|
| 153 | 0 |
return DriverManager.getConnection (url, user, pwd);
|
| 154 |
} |
|
| 155 |
|
|
| 156 |
/**
|
|
| 157 |
* @return
|
|
| 158 |
*/
|
|
| 159 | 0 |
public DataSource getDataSource() {
|
| 160 | 0 |
return dataSource;
|
| 161 |
} |
|
| 162 |
|
|
| 163 |
/**
|
|
| 164 |
* @return
|
|
| 165 |
*/
|
|
| 166 | 0 |
public SessionManager getSessionManager() {
|
| 167 | 0 |
return sessionManager;
|
| 168 |
} |
|
| 169 |
|
|
| 170 |
/**
|
|
| 171 |
* @param source
|
|
| 172 |
*/
|
|
| 173 | 3 |
public void setDataSource(DataSource source) { |
| 174 | 3 |
dataSource = source; |
| 175 |
} |
|
| 176 |
|
|
| 177 |
/**
|
|
| 178 |
* @param manager
|
|
| 179 |
*/
|
|
| 180 | 3 |
public void setSessionManager(SessionManager manager) { |
| 181 | 3 |
sessionManager = manager; |
| 182 |
} |
|
| 183 |
|
|
| 184 |
} |
|
| 185 |
|
|
||||||||||