KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > store > jdbc > adapter > BlobJDBCAdapter


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

18 package org.apache.activemq.store.jdbc.adapter;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.sql.Blob JavaDoc;
24 import java.sql.Connection JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 import javax.jms.JMSException JavaDoc;
30
31 import org.apache.activemq.store.jdbc.TransactionContext;
32 import org.apache.activemq.util.ByteArrayOutputStream;
33
34
35 /**
36  * This JDBCAdapter inserts and extracts BLOB data using the
37  * getBlob()/setBlob() operations. This is a little more involved
38  * since to insert a blob you have to:
39  *
40  * 1: insert empty blob.
41  * 2: select the blob
42  * 3: finally update the blob with data value.
43  *
44  * The databases/JDBC drivers that use this adapter are:
45  * <ul>
46  * <li></li>
47  * </ul>
48  *
49  * @org.apache.xbean.XBean element="blobJDBCAdapter"
50  *
51  * @version $Revision: 1.2 $
52  */

53 public class BlobJDBCAdapter extends DefaultJDBCAdapter {
54     
55     public void doAddMessage(Connection JavaDoc c, long seq, String JavaDoc messageID, String JavaDoc destinationName, byte[] data) throws SQLException JavaDoc,
56             JMSException JavaDoc {
57         PreparedStatement JavaDoc s = null;
58         ResultSet JavaDoc rs = null;
59         try {
60             
61             // Add the Blob record.
62
s = c.prepareStatement(statements.getAddMessageStatement());
63             s.setLong(1, seq);
64             s.setString(2, destinationName);
65             s.setString(3, messageID);
66             s.setString(4, " ");
67             
68             if (s.executeUpdate() != 1)
69                 throw new JMSException JavaDoc("Failed to broker message: " + messageID
70                         + " in container.");
71             s.close();
72
73             // Select the blob record so that we can update it.
74
s = c.prepareStatement(statements.getFindMessageStatement());
75             s.setLong(1, seq);
76             rs = s.executeQuery();
77             if (!rs.next())
78                 throw new JMSException JavaDoc("Failed to broker message: " + messageID
79                         + " in container.");
80
81             // Update the blob
82
Blob JavaDoc blob = rs.getBlob(1);
83             OutputStream JavaDoc stream = blob.setBinaryStream(data.length);
84             stream.write(data);
85             stream.close();
86             s.close();
87
88             // Update the row with the updated blob
89
s = c.prepareStatement(statements.getUpdateMessageStatement());
90             s.setBlob(1, blob);
91             s.setLong(2, seq);
92
93         } catch (IOException JavaDoc e) {
94             throw (SQLException JavaDoc) new SQLException JavaDoc("BLOB could not be updated: "
95                     + e).initCause(e);
96         } finally {
97             try {
98                 rs.close();
99             } catch (Throwable JavaDoc e) {
100             }
101             try {
102                 s.close();
103             } catch (Throwable JavaDoc e) {
104             }
105         }
106     }
107     
108     public byte[] doGetMessage(TransactionContext c, long seq) throws SQLException JavaDoc {
109         PreparedStatement JavaDoc s=null; ResultSet JavaDoc rs=null;
110         try {
111             
112             s = c.getConnection().prepareStatement(statements.getFindMessageStatement());
113             s.setLong(1, seq);
114             rs = s.executeQuery();
115             
116             if( !rs.next() )
117                 return null;
118             Blob JavaDoc blob = rs.getBlob(1);
119             InputStream JavaDoc is = blob.getBinaryStream();
120             
121             ByteArrayOutputStream os = new ByteArrayOutputStream((int)blob.length());
122             int ch;
123             while( (ch=is.read())>= 0 ) {
124                 os.write(ch);
125             }
126             is.close();
127             os.close();
128             
129             return os.toByteArray();
130             
131         } catch (IOException JavaDoc e) {
132             throw (SQLException JavaDoc) new SQLException JavaDoc("BLOB could not be updated: "
133                     + e).initCause(e);
134         } finally {
135             try { rs.close(); } catch (Throwable JavaDoc e) {}
136             try { s.close(); } catch (Throwable JavaDoc e) {}
137         }
138     }
139
140 }
141
Popular Tags