001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package com.noelios.restlet.ext.jdbc;
020:
021: import java.io.IOException;
022: import java.io.OutputStream;
023: import java.sql.ResultSet;
024: import java.sql.SQLException;
025:
026: import javax.sql.rowset.WebRowSet;
027:
028: import org.restlet.data.MediaType;
029: import org.restlet.resource.OutputRepresentation;
030:
031: import com.sun.rowset.WebRowSetImpl;
032:
033: /**
034: * XML Representation of a ResultSet instance wrapped either in a JdbcResult
035: * instance or in a WebRowSet. Leverage the WebRowSet API to create the Response
036: * entity.<br/> Give access to the JdbcResult instance and to the WebRowSet for
037: * retrieval of the connected ResultSet in the same JVM (for advanced use
038: * cases).<br/>
039: *
040: * @see <a
041: * href="http://java.sun.com/j2se/1.5.0/docs/api/javax/sql/rowset/WebRowSet.html">WebRowSet
042: * Interface</a>
043: * @author Thierry Boileau
044: * @author Jerome Louvel (contact@noelios.com)
045: */
046: public class RowSetRepresentation extends OutputRepresentation {
047: /**
048: * Creates a WebRowSet from a ResultSet.
049: *
050: * @param resultSet
051: * The result set to use to populate the Web row set.
052: * @return A WebRowSet from a ResultSet.
053: * @throws SQLException
054: */
055: private static WebRowSet create(ResultSet resultSet)
056: throws SQLException {
057: WebRowSet result = new WebRowSetImpl();
058:
059: if (resultSet != null) {
060: result.populate(resultSet);
061: }
062:
063: return result;
064: }
065:
066: /** Inner WebRowSet Instance. */
067: private WebRowSet webRowSet;
068:
069: /** JdbcResult instance that gives access to the resultSet. */
070: private JdbcResult jdbcResult;
071:
072: /**
073: * Constructor.
074: *
075: * @param jdbcResult
076: * The inner JdbcResult.
077: * @throws SQLException
078: */
079: public RowSetRepresentation(JdbcResult jdbcResult)
080: throws SQLException {
081: this (create((jdbcResult == null) ? null : jdbcResult
082: .getResultSet()));
083: this .jdbcResult = jdbcResult;
084: }
085:
086: /**
087: * Constructor.
088: *
089: * @param resultSet
090: * The result set to use to populate the Web row set.
091: * @throws SQLException
092: */
093: public RowSetRepresentation(ResultSet resultSet)
094: throws SQLException {
095: this (create(resultSet));
096: }
097:
098: /**
099: * Constructor.
100: *
101: * @param webRowSet
102: * The inner WebRowSet.
103: */
104: public RowSetRepresentation(WebRowSet webRowSet) {
105: super (MediaType.TEXT_XML);
106: this .webRowSet = webRowSet;
107: }
108:
109: /**
110: * Returns the inner JdbcResult instance or null.
111: *
112: * @return The inner JdbcResult instance or null.
113: */
114: public JdbcResult getJdbcResult() {
115: return jdbcResult;
116: }
117:
118: /**
119: * Returns the inner WebRowSet instance.
120: *
121: * @return The inner WebRowSet instance.
122: */
123: public WebRowSet getWebRowSet() {
124: return this .webRowSet;
125: }
126:
127: @Override
128: public void write(OutputStream outputStream) throws IOException {
129: try {
130: webRowSet.writeXml(outputStream);
131: } catch (SQLException se) {
132: throw new IOException(se.getMessage());
133: }
134:
135: try {
136: if (this .jdbcResult != null) {
137: this .jdbcResult.release();
138: }
139: } catch (SQLException se) {
140: throw new IOException(
141: "SQL exception while releasing the JdbcResult instance after writing the representation. "
142: + se.getMessage());
143: }
144:
145: try {
146: if (this .webRowSet != null) {
147: this .webRowSet.release();
148: this .webRowSet.close();
149: }
150: } catch (SQLException se) {
151: throw new IOException(
152: "Error while releasing the WebRowSet instance after writing the representation. "
153: + se.getMessage());
154: }
155: }
156: }
|