KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jdbc > adapter > DefaultJDBCAdapter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jdbc.adapter;
18
19 import java.io.IOException JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.PreparedStatement JavaDoc;
22 import java.sql.ResultSet JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.sql.Statement JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.servicemix.jdbc.JDBCAdapter;
31 import org.apache.servicemix.jdbc.JDBCAdapterFactory;
32 import org.apache.servicemix.jdbc.Statements;
33
34 /**
35  * Implements all the default JDBC operations that are used
36  * by the JDBCPersistenceAdapter.
37  * <p/>
38  * sub-classing is encouraged to override the default
39  * implementation of methods to account for differences
40  * in JDBC Driver implementations.
41  * <p/>
42  * The JDBCAdapter inserts and extracts BLOB data using the
43  * getBytes()/setBytes() operations.
44  * <p/>
45  * The databases/JDBC drivers that use this adapter are:
46  * <ul>
47  * <li></li>
48  * </ul>
49  *
50  * @org.apache.xbean.XBean element="defaultJDBCAdapter"
51  *
52  * @version $Revision: 1.10 $
53  */

54 public class DefaultJDBCAdapter implements JDBCAdapter {
55
56     private static final Log log = LogFactory.getLog(DefaultJDBCAdapter.class);
57
58     protected Statements statements;
59
60     protected void setBinaryData(PreparedStatement JavaDoc s, int index, byte data[]) throws SQLException JavaDoc {
61         s.setBytes(index, data);
62     }
63
64     protected byte[] getBinaryData(ResultSet JavaDoc rs, int index) throws SQLException JavaDoc {
65         return rs.getBytes(index);
66     }
67
68     public void doCreateTables(Connection JavaDoc connection) throws SQLException JavaDoc, IOException JavaDoc {
69         Statement JavaDoc s = null;
70         try {
71             connection.setAutoCommit(false);
72             
73             // Check to see if the table already exists. If it does, then don't log warnings during startup.
74
// Need to run the scripts anyways since they may contain ALTER statements that upgrade a previous version of the table
75
boolean alreadyExists = false;
76             ResultSet JavaDoc rs=null;
77             try {
78                 rs= connection.getMetaData().getTables(null,null, statements.getFullStoreTableName(), new String JavaDoc[] {"TABLE"});
79                 alreadyExists = rs.next();
80             } catch (Throwable JavaDoc ignore) {
81             } finally {
82                 close(rs);
83             }
84             
85             // If the dataSource is a managed DataSource, executing a statement that throws
86
// an exception will make the connection unusable.
87
// So if the table already exists, do not try to re-create them
88
if (alreadyExists) {
89                 return;
90             }
91             
92             s = connection.createStatement();
93             String JavaDoc[] createStatments = statements.getCreateSchemaStatements();
94             for (int i = 0; i < createStatments.length; i++) {
95                 // This will fail usually since the tables will be
96
// created already.
97
try {
98                     log.debug("Executing SQL: " + createStatments[i]);
99                     s.execute(createStatments[i]);
100                 }
101                 catch (SQLException JavaDoc e) {
102                     if( alreadyExists ) {
103                         log.debug("Could not create JDBC tables; The message table already existed." +
104                                 " Failure was: " + createStatments[i] + " Message: " + e.getMessage() +
105                                 " SQLState: " + e.getSQLState() + " Vendor code: " + e.getErrorCode() );
106                     } else {
107                         log.warn("Could not create JDBC tables; they could already exist." +
108                             " Failure was: " + createStatments[i] + " Message: " + e.getMessage() +
109                             " SQLState: " + e.getSQLState() + " Vendor code: " + e.getErrorCode() );
110                         JDBCAdapterFactory.log("Failure details: ", e);
111                     }
112                 }
113             }
114             connection.commit();
115             
116         }
117         finally {
118             close(s);
119         }
120     }
121
122     public void doDropTables(Connection JavaDoc connection) throws SQLException JavaDoc, IOException JavaDoc {
123         Statement JavaDoc s = null;
124         try {
125             s = connection.createStatement();
126             String JavaDoc[] dropStatments = statements.getDropSchemaStatements();
127             for (int i = 0; i < dropStatments.length; i++) {
128                 // This will fail usually since the tables will be
129
// created already.
130
try {
131                     s.execute(dropStatments[i]);
132                 }
133                 catch (SQLException JavaDoc e) {
134                     log.warn("Could not drop JDBC tables; they may not exist." +
135                         " Failure was: " + dropStatments[i] + " Message: " + e.getMessage() +
136                         " SQLState: " + e.getSQLState() + " Vendor code: " + e.getErrorCode() );
137                     JDBCAdapterFactory.log("Failure details: ", e);
138                 }
139             }
140             connection.commit();
141         }
142         finally {
143             close(s);
144         }
145     }
146
147     public void doStoreData(Connection JavaDoc connection, String JavaDoc id, byte[] data) throws SQLException JavaDoc, IOException JavaDoc {
148         PreparedStatement JavaDoc s = null;
149         try {
150             if (s == null) {
151                 s = connection.prepareStatement(statements.getStoreDataStatement());
152             }
153             s.setString(1, id);
154             setBinaryData(s, 2, data);
155             if ( s.executeUpdate() != 1 ) {
156                 throw new SQLException JavaDoc("Failed to insert data");
157             }
158         } finally {
159             close(s);
160         }
161     }
162     
163     public byte[] doLoadData(Connection JavaDoc connection, String JavaDoc id) throws SQLException JavaDoc, IOException JavaDoc {
164         PreparedStatement JavaDoc s = null;
165         ResultSet JavaDoc rs = null;
166         try {
167             s = connection.prepareStatement(statements.getFindDataStatement());
168             s.setString(1, id);
169             rs = s.executeQuery();
170             if (!rs.next()) {
171                 return null;
172             }
173             return getBinaryData(rs, 1);
174         }
175         finally {
176             close(rs);
177             close(s);
178         }
179     }
180     
181     public void doUpdateData(Connection JavaDoc connection, String JavaDoc id, byte[] data) throws SQLException JavaDoc, IOException JavaDoc {
182         PreparedStatement JavaDoc s = null;
183         try {
184             if( s == null ) {
185                 s = connection.prepareStatement(statements.getUpdateDataStatement());
186             }
187             s.setString(2, id);
188             setBinaryData(s, 1, data);
189             if ( s.executeUpdate() != 1 ) {
190                 throw new SQLException JavaDoc("Failed to update data");
191             }
192         } finally {
193             close(s);
194         }
195     }
196     
197     public void doRemoveData(Connection JavaDoc connection, String JavaDoc id) throws SQLException JavaDoc, IOException JavaDoc {
198         PreparedStatement JavaDoc s = null;
199         try {
200             s = connection.prepareStatement(statements.getRemoveDataStatement());
201             s.setString(1, id);
202             if (s.executeUpdate() != 1) {
203                 throw new SQLException JavaDoc("Failed to remove data");
204             }
205         } finally {
206             close(s);
207         }
208     }
209     
210     static private void close(Statement JavaDoc s) {
211         try {
212             s.close();
213         } catch (Throwable JavaDoc e) {
214         }
215     }
216
217     static private void close(ResultSet JavaDoc rs) {
218         try {
219             rs.close();
220         } catch (Throwable JavaDoc e) {
221         }
222     }
223
224     public Statements getStatements() {
225         return statements;
226     }
227
228     public void setStatements(Statements statements) {
229         this.statements = statements;
230     }
231
232     public byte[][] doLoadData(Connection JavaDoc connection, String JavaDoc[] ids) throws SQLException JavaDoc, IOException JavaDoc {
233         PreparedStatement JavaDoc s = null;
234         byte[][] datas = new byte[ids.length][];
235         try {
236             s = connection.prepareStatement(statements.getFindDataStatement());
237             for (int i = 0; i < ids.length; i++) {
238                 s.setString(1, ids[i]);
239                 ResultSet JavaDoc rs = s.executeQuery();
240                 if (rs.next()) {
241                     datas[i] = getBinaryData(rs, 1);
242                 }
243                 close(rs);
244             }
245             return datas;
246         }
247         finally {
248             close(s);
249         }
250     }
251
252     public void doRemoveData(Connection JavaDoc connection, String JavaDoc[] ids) throws SQLException JavaDoc, IOException JavaDoc {
253         PreparedStatement JavaDoc s = null;
254         try {
255             s = connection.prepareStatement(statements.getRemoveDataStatement());
256             for (int i = 0; i < ids.length; i++) {
257                 s.setString(1, ids[i]);
258                 s.executeUpdate();
259             }
260         }
261         finally {
262             close(s);
263         }
264     }
265
266     public int doGetCount(Connection JavaDoc connection) throws SQLException JavaDoc, IOException JavaDoc {
267         PreparedStatement JavaDoc s = null;
268         ResultSet JavaDoc rs = null;
269         try {
270             s = connection.prepareStatement(statements.getCountStatement());
271             rs = s.executeQuery();
272             rs.next();
273             return rs.getInt(1);
274         }
275         finally {
276             close(rs);
277             close(s);
278         }
279     }
280
281     public String JavaDoc[] doGetIds(Connection JavaDoc connection) throws SQLException JavaDoc, IOException JavaDoc {
282         PreparedStatement JavaDoc s = null;
283         ResultSet JavaDoc rs = null;
284         try {
285             List JavaDoc ids = new ArrayList JavaDoc();
286             s = connection.prepareStatement(statements.getFindAllIdsStatement());
287             rs = s.executeQuery();
288             while (!rs.next()) {
289                 ids.add(rs.getString(1));
290             }
291             return (String JavaDoc[]) ids.toArray(new String JavaDoc[ids.size()]);
292         }
293         finally {
294             close(rs);
295             close(s);
296         }
297     }
298
299     public String JavaDoc[] doGetIds(Connection JavaDoc connection, int fromIndex, int toIndex) throws SQLException JavaDoc, IOException JavaDoc {
300         Statement JavaDoc s = null;
301         ResultSet JavaDoc rs = null;
302         try {
303             s = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
304             s.setFetchSize(toIndex - fromIndex);
305             rs = s.executeQuery(statements.getFindAllIdsStatement());
306             rs.absolute(fromIndex + 1);
307             String JavaDoc[] ids = new String JavaDoc[toIndex - fromIndex];
308             for (int row = 0; row < toIndex - fromIndex; row++) {
309                 ids[row] = rs.getString(1);
310                 if (!rs.next()) {
311                     break;
312                 }
313             }
314             return ids;
315         }
316         finally {
317             close(rs);
318             close(s);
319         }
320     }
321
322 }
323
Popular Tags