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 public DatabaseCommandProcessor() {
39 super();
40 }
41
42 protected Object executeCommand(Command command, ApplicationMessage appMsg){
43
44 if( command instanceof DatabaseCommand )
45 return executeCommand( (DatabaseCommand)command, appMsg);
46
47 return super.executeCommand( command, appMsg);
48 }
49
50 protected Object executeCommand(DatabaseCommand command, ApplicationMessage appMsg){
51
52 Statement st = null;
53 Connection connection = null;
54
55 try{
56
57 connection = getConnection(appMsg.getSessionID());
58
59 return command.execute(
60 new DatabaseCommandVO(connection,appMsg.getBody().toString()) );
61 }
62 catch(SQLException e){
63 if( connection != null )
64 destroyConnection( connection , appMsg.getSessionID() );
65 throw new RuntimeException(e);
66 }
67 }
68
69 private void destroyConnection( Connection connection, String userSessionID ){
70
71 sessionManager.getSession( userSessionID ).removeObject( connection );
72
73 try{
74 connection.close();
75 }
76 catch(SQLException e){
77 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 Connection getConnection(String sessionId) throws SQLException{
89
90 Session session = sessionManager.getSession(sessionId);
91
92 Connection connection = (Connection)session.getObject(CONNECTION);
93
94 if( connection == null ){
95
96 connection = getConnection(session.getUserId(), session.getUserPassword());
97
98 session.putObject(CONNECTION, connection);
99 }
100
101 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 Connection getConnection(String userId, String password) throws SQLException{
123
124 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 if( userId == null || userId.trim().length() == 0 )
130 c = getDataSource().getConnection();
131 else
132 c = getDataSource().getConnection(userId, password);
133
134 if( !c.getAutoCommit() )
135 c.setAutoCommit(true);
136
137 return c;
138 }
139
140 Connection getNewConnectionFromDriverManager(String driverName,
141 String url, String user, String pwd) throws SQLException{
142
143 try{
144 Class.forName ( driverName );
145 }
146 catch(ClassNotFoundException e){
147 throw new RuntimeException(e);
148 }
149
150 if( pwd == null )
151 pwd = "";
152
153 return DriverManager.getConnection (url, user, pwd);
154 }
155
156 /***
157 * @return
158 */
159 public DataSource getDataSource() {
160 return dataSource;
161 }
162
163 /***
164 * @return
165 */
166 public SessionManager getSessionManager() {
167 return sessionManager;
168 }
169
170 /***
171 * @param source
172 */
173 public void setDataSource(DataSource source) {
174 dataSource = source;
175 }
176
177 /***
178 * @param manager
179 */
180 public void setSessionManager(SessionManager manager) {
181 sessionManager = manager;
182 }
183
184 }
This page was automatically generated by Maven