KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > core > support > AbstractLobStreamingResultSetExtractor


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jdbc.core.support;
18
19 import java.io.IOException JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.SQLException JavaDoc;
22
23 import org.springframework.dao.DataAccessException;
24 import org.springframework.dao.EmptyResultDataAccessException;
25 import org.springframework.dao.IncorrectResultSizeDataAccessException;
26 import org.springframework.jdbc.LobRetrievalFailureException;
27 import org.springframework.jdbc.core.ResultSetExtractor;
28
29 /**
30  * Abstract ResultSetExtractor implementation that assumes streaming of LOB data.
31  * Typically used as inner class, with access to surrounding method arguments.
32  *
33  * <p>Delegates to the <code>streamData</code> template method for streaming LOB
34  * content to some OutputStream, typically using a LobHandler. Converts an
35  * IOException thrown during streaming to a LobRetrievalFailureException.
36  *
37  * <p>A usage example with JdbcTemplate:
38  *
39  * <pre class="code">JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); // reusable object
40  * final LobHandler lobHandler = new DefaultLobHandler(); // reusable object
41  *
42  * jdbcTemplate.query(
43  * "SELECT content FROM imagedb WHERE image_name=?", new Object[] {name},
44  * new AbstractLobStreamingResultSetExtractor() {
45  * public void streamData(ResultSet rs) throws SQLException, IOException {
46  * FileCopyUtils.copy(lobHandler.getBlobAsBinaryStream(rs, 1), contentStream);
47  * }
48  * }
49  * );</pre>
50  *
51  * @author Juergen Hoeller
52  * @since 1.0.2
53  * @see org.springframework.jdbc.support.lob.LobHandler
54  * @see org.springframework.jdbc.LobRetrievalFailureException
55  */

56 public abstract class AbstractLobStreamingResultSetExtractor implements ResultSetExtractor {
57
58     /**
59      * Delegates to handleNoRowFound, handleMultipleRowsFound and streamData,
60      * according to the ResultSet state. Converts an IOException thrown by
61      * streamData to a LobRetrievalFailureException.
62      * @see #handleNoRowFound
63      * @see #handleMultipleRowsFound
64      * @see #streamData
65      * @see org.springframework.jdbc.LobRetrievalFailureException
66      */

67     public final Object JavaDoc extractData(ResultSet JavaDoc rs) throws SQLException JavaDoc, DataAccessException {
68         if (!rs.next()) {
69             handleNoRowFound();
70         }
71         else {
72             try {
73                 streamData(rs);
74                 if (rs.next()) {
75                     handleMultipleRowsFound();
76                 }
77             }
78             catch (IOException JavaDoc ex) {
79                 throw new LobRetrievalFailureException("Couldn't stream LOB content", ex);
80             }
81         }
82         return null;
83     }
84
85     /**
86      * Handle the case where the ResultSet does not contain a row.
87      * @throws DataAccessException a corresponding exception,
88      * by default an EmptyResultDataAccessException
89      * @see org.springframework.dao.EmptyResultDataAccessException
90      */

91     protected void handleNoRowFound() throws DataAccessException {
92         throw new EmptyResultDataAccessException(
93                 "LobStreamingResultSetExtractor did not find row in database", 1);
94     }
95
96     /**
97      * Handle the case where the ResultSet contains multiple rows.
98      * @throws DataAccessException a corresponding exception,
99      * by default an IncorrectResultSizeDataAccessException
100      * @see org.springframework.dao.IncorrectResultSizeDataAccessException
101      */

102     protected void handleMultipleRowsFound() throws DataAccessException {
103         throw new IncorrectResultSizeDataAccessException(
104                 "LobStreamingResultSetExtractor found multiple rows in database", 1);
105     }
106
107     /**
108      * Stream LOB content from the given ResultSet to some OutputStream.
109      * <p>Typically used as inner class, with access to surrounding method arguments
110      * and to a LobHandler instance variable of the surrounding class.
111      * @param rs the ResultSet to take the LOB content from
112      * @throws SQLException if thrown by JDBC methods
113      * @throws IOException if thrown by stream access methods
114      * @throws DataAccessException in case of custom exceptions
115      * @see org.springframework.jdbc.support.lob.LobHandler#getBlobAsBinaryStream
116      * @see org.springframework.util.FileCopyUtils
117      */

118     protected abstract void streamData(ResultSet JavaDoc rs) throws SQLException JavaDoc, IOException JavaDoc, DataAccessException;
119
120 }
121
Popular Tags