KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > util > SqlUtil


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/util/SqlUtil.java,v 1.33 2004/10/24 13:47:30 hkollmann Exp $
3  * $Revision: 1.33 $
4  * $Date: 2004/10/24 13:47:30 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.util;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.FileNotFoundException JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.io.ObjectInputStream JavaDoc;
35
36 import java.sql.Blob JavaDoc;
37 import java.sql.Connection JavaDoc;
38 import java.sql.ResultSet JavaDoc;
39 import java.sql.SQLException JavaDoc;
40
41 /**
42  * <p>
43  * this utility-class provides convenience methods for SQL related tasks
44  * </p>
45  *
46  * @author Joe Peer
47  * @author Eric Pugh
48  */

49 public class SqlUtil {
50     // logging category for this class
51
private static Log logCat = LogFactory.getLog(SqlUtil.class.getName());
52
53     /**
54      * Close the input connection
55      *
56      * @param con
57      * the connection to close
58      */

59     public static final void closeConnection(Connection JavaDoc con) {
60         if (con != null) {
61             try {
62                 SqlUtil.logCat.debug("About to close connection - " + con);
63                 con.close();
64                 SqlUtil.logCat.debug("Connection closed");
65             } catch (SQLException JavaDoc e) {
66                 SqlUtil
67                         .logSqlException(e,
68                                 "::closeConnection - cannot close the input connection");
69             }
70         }
71     }
72
73     /**
74      * Log the SQLException stacktrace (adding the input description to the
75      * first log statement) and do the same for all the nested exceptions.
76      *
77      * @param e
78      * the SQL exception to log
79      * @param desc
80      * the exception description
81      */

82     public static final void logSqlException(SQLException JavaDoc e, String JavaDoc desc) {
83         int i = 0;
84         String JavaDoc excDesc = "::logSqlExceptionSQL - main SQL exception";
85
86         // adding the input description to the main log statement;
87
if (!Util.isNull(desc)) {
88             excDesc += (" [" + desc + "]");
89         }
90
91         SqlUtil.logCat.error(excDesc, e);
92
93         while ((e = e.getNextException()) != null)
94             SqlUtil.logCat.error("::logSqlException - nested SQLException ("
95                     + (i++) + ")", e);
96     }
97
98     /**
99      * Log the SQLException stacktrace and do the same for all the nested
100      * exceptions.
101      *
102      * @param e
103      * the SQL exception to log
104      */

105     public static final void logSqlException(SQLException JavaDoc e) {
106         SqlUtil.logSqlException(e, null);
107     }
108
109     /**
110      * Read the database field and write to the client its content
111      *
112      * @param rs
113      * Description of the Parameter
114      * @param fileName
115      * is the filename or NULL in the classic (Fileholder-based) BLOB
116      * handling
117      * @exception IOException
118      * Description of the Exception
119      * @exception SQLException
120      * Description of the Exception
121      */

122     public static FileHolder readFileHolderBlob(ResultSet JavaDoc rs)
123             throws IOException JavaDoc, SQLException JavaDoc {
124         logCat.info("READING FILEHOLDER");
125         Object JavaDoc o = rs.getObject(1);
126         if (o == null) {
127             logCat.warn("::readDbFieldBlob - blob null, no response sent");
128             return null;
129         }
130         logCat.info("o instanceof ..." + o.getClass().getName());
131         // if the object the JDBC driver returns to us implements
132
// the java.sql.Blob interface, then we use the BLOB object
133
// which wraps the binary stream of our FileHolder:
134
try {
135             if (o instanceof java.sql.Blob JavaDoc) {
136                 Blob JavaDoc blob = rs.getBlob(1);
137                 ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(blob
138                         .getBinaryStream());
139                 FileHolder fh = (FileHolder) ois.readObject();
140                 return fh;
141             }
142             // otherwise we are aquiring the stream directly:
143
else {
144                 // old ("classic") mode
145
InputStream JavaDoc blobIS = rs.getBinaryStream(1);
146                 ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(blobIS);
147                 FileHolder fh = (FileHolder) ois.readObject();
148                 return fh;
149             }
150         } catch (ClassNotFoundException JavaDoc cnfe) {
151             logCat.error("::readDbFieldBlob - class not found", cnfe);
152             throw new IOException JavaDoc("error:" + cnfe.toString());
153         }
154
155     }
156
157     /**
158      * Read the database field and write to the client its content
159      *
160      * @param rs
161      * Description of the Parameter
162      * @param fileName
163      * is the filename or NULL in the classic (Fileholder-based) BLOB
164      * handling
165      * @exception IOException
166      * Description of the Exception
167      * @exception SQLException
168      * Description of the Exception
169      */

170     public static InputStream JavaDoc readDbFieldBlob(ResultSet JavaDoc rs) throws IOException JavaDoc,
171             SQLException JavaDoc {
172         logCat.info("READING BLOB");
173         Object JavaDoc o = rs.getObject(1);
174         if (o == null) {
175             logCat.warn("::readDbFieldBlob - blob null, no response sent");
176             return null;
177         }
178         logCat.info("o instanceof ..." + o.getClass().getName());
179         // if the object the JDBC driver returns to us implements
180
// the java.sql.Blob interface, then we use the BLOB object
181
// which wraps the binary stream of our FileHolder:
182
if (o instanceof java.sql.Blob JavaDoc) {
183             return ((Blob JavaDoc) o).getBinaryStream();
184         }
185         // otherwise we are aquiring the stream directly:
186
else {
187             // new mode
188
return rs.getBinaryStream(1);
189         }
190     }
191
192     /**
193      * Read the blob field from the filesystem and write to the client its
194      * content.
195      *
196      * @param fileName
197      * Description of the Parameter
198      * @param directory
199      * Description of the Parameter
200      * @param defVal
201      * Default value of the file tag
202      * @exception FileNotFoundException
203      * Description of the Exception
204      * @exception IOException
205      * Description of the Exception
206      */

207     public static FileInputStream JavaDoc readDiskBlob(String JavaDoc fileName,
208             String JavaDoc directory, String JavaDoc defVal) throws FileNotFoundException JavaDoc,
209             IOException JavaDoc {
210         logCat.info(new StringBuffer JavaDoc("READING DISKBLOB\n directory = [")
211                 .append(directory).append("]\n").append(" fileName = [")
212                 .append(fileName).append("]\n").append(" defaultValue = [")
213                 .append(defVal).append("]\n").toString());
214
215         if (Util.isNull(fileName)) {
216             if ((fileName = defVal) != null) {
217                 logCat
218                         .info("::readDiskBlob - database data is null; use the default value ["
219                                 + fileName + "]");
220             }
221         }
222
223         // directory or fileName can be null!
224
//if ((directory != null) && (fileName != null))
225
if (fileName != null) {
226             fileName = fileName.trim();
227             File JavaDoc file = new File JavaDoc(directory, fileName);
228             if (file.exists()) {
229                 logCat.info("::readDiskBlob - file found ["
230                         + file.getAbsoluteFile() + "]");
231                 return new FileInputStream JavaDoc(file);
232             } else {
233                 logCat.error("::readDiskBlob - file ["
234                         + (directory + "/" + fileName) + "] not found");
235
236                 return null;
237             }
238         } else {
239             logCat
240                     .warn("::readDiskBlob - file name or directory value is null");
241
242             return null;
243         }
244     }
245 }
246
Popular Tags