001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.language.markup.xsp;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.Reader;
022: import java.lang.reflect.InvocationTargetException;
023: import java.sql.Blob;
024: import java.sql.CallableStatement;
025: import java.sql.Clob;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028: import java.sql.Types;
029:
030: import org.apache.avalon.framework.CascadingRuntimeException;
031:
032: /**
033: * This is a helper class to remove redundant code in
034: * esql pages.
035: *
036: * Based on the orginal esql.xsl.
037: *
038: * @author <a href="mailto:tcurdt@dff.st">Torsten Curdt</a>
039: * @version CVS $Id: EsqlHelper.java 452425 2006-10-03 11:18:47Z vgritsenko $
040: */
041: public class EsqlHelper {
042:
043: private static void close(Object lob) {
044: if (lob == null) {
045: return;
046: }
047:
048: // ORACLE 'temporary lob' problem patch start
049: Class clazz = lob.getClass();
050: String name = clazz.getName();
051: if (name.equals("oracle.sql.BLOB")
052: || name.equals("oracle.sql.CLOB")) {
053: try {
054: if (clazz.getMethod("isTemporary", new Class[0])
055: .invoke(lob, new Object[0])
056: .equals(Boolean.TRUE)) {
057: clazz.getMethod("freeTemporary", new Class[0])
058: .invoke(lob, new Object[0]);
059: }
060: } catch (IllegalAccessException e) {
061: /* ignored */
062: } catch (InvocationTargetException e) {
063: /* ignored */
064: } catch (NoSuchMethodException e) {
065: /* ignored */
066: }
067: }
068: }
069:
070: private static byte[] readBytes(Blob blob, byte[] defaultValue)
071: throws SQLException, IOException {
072: if (blob == null) {
073: return defaultValue;
074: }
075:
076: InputStream is = null;
077: try {
078: int length = (int) blob.length();
079: if (length == 0) {
080: return defaultValue;
081: }
082:
083: byte[] buffer = new byte[length];
084: is = blob.getBinaryStream();
085: is.read(buffer);
086:
087: return buffer;
088: } finally {
089: try {
090: if (is != null) {
091: is.close();
092: }
093: } catch (IOException e) { /* ignored */
094: }
095: close(blob);
096: }
097: }
098:
099: private static String readAscii(Clob clob, String defaultValue)
100: throws SQLException, IOException {
101: if (clob == null) {
102: return defaultValue;
103: }
104:
105: InputStream is = null;
106: try {
107: int length = (int) clob.length();
108: if (length == 0) {
109: return defaultValue;
110: }
111:
112: byte[] buffer = new byte[length];
113: is = clob.getAsciiStream();
114: is.read(buffer);
115:
116: return new String(buffer, 0, length);
117: } finally {
118: try {
119: if (is != null) {
120: is.close();
121: }
122: } catch (IOException e) { /* ignored */
123: }
124: close(clob);
125: }
126: }
127:
128: private static String read(Clob clob, String defaultValue)
129: throws SQLException, IOException {
130: if (clob == null) {
131: return defaultValue;
132: }
133:
134: Reader r = null;
135: try {
136: int length = (int) clob.length();
137: if (length == 0) {
138: return defaultValue;
139: }
140:
141: char[] buffer = new char[length];
142: r = clob.getCharacterStream();
143: r.read(buffer);
144:
145: return new String(buffer, 0, length);
146: } finally {
147: try {
148: if (r != null) {
149: r.close();
150: }
151: } catch (IOException e) { /* ignored */
152: }
153: close(clob);
154: }
155: }
156:
157: /** returns byte array from BLOB */
158: public static byte[] getBlob(ResultSet set, String column)
159: throws RuntimeException {
160: try {
161: return EsqlHelper.getBlob(set, set.findColumn(column));
162: } catch (Exception e) {
163: throw new CascadingRuntimeException(
164: "Error getting blob data for column " + column, e);
165: }
166: }
167:
168: /** returns byte array from BLOB */
169: public static byte[] getBlob(ResultSet set, int column)
170: throws Exception {
171: try {
172: if (set.getMetaData().getColumnType(column) == java.sql.Types.BLOB) {
173: return readBytes(set.getBlob(column), null);
174: } else {
175: String value = set.getString(column);
176: if (value == null) {
177: return null;
178: }
179: return value.getBytes();
180: }
181: } catch (Exception e) {
182: throw new CascadingRuntimeException(
183: "Error getting blob data for column " + column, e);
184: }
185: }
186:
187: /** returns byte array from BLOB */
188: public static byte[] getBlob(CallableStatement cs, int column,
189: String defaultString) throws Exception {
190:
191: byte[] defaultValue = null;
192: if (defaultString != null && !defaultString.equals("_null_")) {
193: defaultValue = defaultString.getBytes();
194: }
195:
196: try {
197: if (cs.getMetaData().getColumnType(column) == java.sql.Types.BLOB) {
198: return readBytes(cs.getBlob(column), defaultValue);
199: } else {
200: String value = cs.getString(column);
201: if (value == null) {
202: return defaultValue;
203: }
204: return value.getBytes();
205: }
206: } catch (Exception e) {
207: throw new CascadingRuntimeException(
208: "Error getting blob data for column " + column, e);
209: }
210: }
211:
212: /** returns Unicode encoded string from CLOB or String column */
213: public static String getStringOrClob(ResultSet set, String column,
214: String defaultString) throws RuntimeException {
215: try {
216: return EsqlHelper.getStringOrClob(set, set
217: .findColumn(column), defaultString);
218: } catch (Exception e) {
219: throw new CascadingRuntimeException(
220: "Error getting text from column " + column, e);
221: }
222: }
223:
224: /** returns Unicode encoded string from CLOB or String column */
225: public static String getStringOrClob(ResultSet set, int column,
226: String defaultString) throws Exception {
227: if (defaultString != null && defaultString.equals("_null_")) {
228: defaultString = null;
229: }
230:
231: try {
232: if (set.getMetaData().getColumnType(column) == java.sql.Types.CLOB) {
233: return read(set.getClob(column), defaultString);
234: } else {
235: String result = set.getString(column);
236: if (result == null) {
237: result = defaultString;
238: }
239: return result;
240: }
241: } catch (Exception e) {
242: throw new CascadingRuntimeException(
243: "Error getting text from column " + column, e);
244: }
245: }
246:
247: /** returns Unicode encoded string from CLOB or String column */
248: public static String getStringOrClob(CallableStatement cs,
249: int column, String defaultString) throws Exception {
250: if (defaultString != null && defaultString.equals("_null_")) {
251: defaultString = null;
252: }
253:
254: try {
255: return read(cs.getClob(column), defaultString);
256: } catch (Exception e) {
257: throw new CascadingRuntimeException(
258: "Error getting text from column " + column, e);
259: }
260: }
261:
262: /** returns ascii string from CLOB or String column */
263: public static String getAscii(ResultSet set, String column,
264: String defaultString) throws RuntimeException {
265: try {
266: int colIndex = set.findColumn(column);
267: return EsqlHelper.getAscii(set, colIndex, defaultString);
268: } catch (Exception e) {
269: throw new CascadingRuntimeException(
270: "Error getting ascii data for column " + column, e);
271: }
272: }
273:
274: /** returns ascii string from CLOB or String column */
275: public static String getAscii(ResultSet set, int column,
276: String defaultString) {
277: if (defaultString != null && defaultString.equals("_null_")) {
278: defaultString = null;
279: }
280:
281: try {
282: if (set.getMetaData().getColumnType(column) == Types.CLOB) {
283: return readAscii(set.getClob(column), defaultString);
284: } else {
285: String result = set.getString(column);
286: if (result == null) {
287: result = defaultString;
288: }
289: return result;
290: }
291: } catch (Exception e) {
292: throw new CascadingRuntimeException(
293: "Error getting ascii data from column " + column, e);
294: }
295: }
296:
297: /** returns ascii string from CLOB or String column */
298: public static String getAscii(CallableStatement cs, int column,
299: String defaultString) {
300: try {
301: return readAscii(cs.getClob(column), defaultString);
302: } catch (Exception e) {
303: throw new CascadingRuntimeException(
304: "Error getting ascii data for column " + column, e);
305: }
306: }
307:
308: public static String getStringFromByteArray(byte[] bytes,
309: String encoding, String defaultString) {
310: if (bytes != null) {
311: try {
312: return new String(bytes, encoding);
313: } catch (java.io.UnsupportedEncodingException uee) {
314: throw new CascadingRuntimeException(
315: "Unsupported Encoding Exception", uee);
316: }
317: } else {
318: if (defaultString != null
319: && !defaultString.equals("_null_")) {
320: return defaultString;
321: } else {
322: return null; /* before was "" but null is more consequent */
323: }
324: }
325: }
326: }
|