KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > ScenarioUtility


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.tools;
26
27 import java.io.DataInputStream JavaDoc;
28 import java.io.DataOutputStream JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.FileNotFoundException JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.sql.Connection JavaDoc;
35 import java.sql.ResultSet JavaDoc;
36 import java.util.ArrayList JavaDoc;
37
38 import org.objectweb.cjdbc.driver.Blob;
39
40 /**
41  * This class defines a ScenarioUtility
42  *
43  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
44  * @version 1.0
45  */

46 public class ScenarioUtility
47 {
48
49   /**
50    * Get the result as an arraylist of executing a sql query on a connection
51    *
52    * @param query sql query
53    * @param con connection to use to execute the query
54    * @param prepare if set to true, use preparedStatement instead of statement
55    * @return <code>ArrayList</code> of the converted <code>ResultSet</code>
56    * @throws Exception if fails
57    */

58   public static ArrayList JavaDoc getSingleQueryResult(String JavaDoc query, Connection JavaDoc con,
59       boolean prepare) throws Exception JavaDoc
60   {
61     if (prepare)
62       return convertResultSet(con.prepareStatement(query).executeQuery());
63     else
64       return convertResultSet(con.createStatement().executeQuery(query));
65   }
66
67   /**
68    * Get the result as an arraylist of executing a sql query on a connection
69    *
70    * @param query sql query
71    * @param con connection to use to execute the query
72    * @return <code>ArrayList</code> of the converted <code>ResultSet</code>
73    * @throws Exception if fails
74    */

75   public static ArrayList JavaDoc getSingleQueryResult(String JavaDoc query, Connection JavaDoc con)
76       throws Exception JavaDoc
77   {
78     return getSingleQueryResult(query, con, false);
79   }
80
81   /**
82    * @see #getSingleQueryResult(String, Connection)
83    */

84   public static void displaySingleQueryResult(String JavaDoc query, Connection JavaDoc con)
85       throws Exception JavaDoc
86   {
87     displayResultOnScreen(getSingleQueryResult(query, con));
88   }
89
90   /**
91    * @see #getSingleQueryResult(String, Connection)
92    */

93   public static void displaySingleQueryResult(String JavaDoc query, Connection JavaDoc con,
94       boolean prepare) throws Exception JavaDoc
95   {
96     displayResultOnScreen(getSingleQueryResult(query, con, prepare));
97   }
98
99   /**
100    * Format a result and display it on the screen
101    *
102    * @param result a converted resultset
103    */

104   public static void displayResultOnScreen(ArrayList JavaDoc result)
105   {
106     int size = result.size();
107     ArrayList JavaDoc list;
108     for (int i = 0; i < size; i++)
109     {
110       list = (ArrayList JavaDoc) result.get(i);
111       System.out.println("row[" + i + "]:" + list);
112     }
113   }
114
115   /**
116    * Format a result and display it on the screen
117    *
118    * @param set a ResultSet
119    * @throws Exception if an error occurs
120    */

121   public static void displayResultOnScreen(ResultSet JavaDoc set) throws Exception JavaDoc
122   {
123     displayResultOnScreen(convertResultSet(set));
124   }
125
126   /**
127    * Converts the result set to an array list so can be display
128    *
129    * @param set the result set with data
130    * @return <code>ArrayList</code> of data
131    * @throws Exception if result set is not valid
132    */

133   public static final ArrayList JavaDoc convertResultSet(ResultSet JavaDoc set)
134       throws Exception JavaDoc
135   {
136     ArrayList JavaDoc list = new ArrayList JavaDoc();
137     int colCount = set.getMetaData().getColumnCount();
138     while (set.next())
139     {
140       ArrayList JavaDoc row = new ArrayList JavaDoc();
141       for (int i = 1; i <= colCount; i++)
142       {
143         row.add(set.getString(i));
144       }
145       list.add(row);
146     }
147     //System.out.println(list);
148
return list;
149   }
150
151   /**
152    * ReadBinary data from the file. Tested ok with writeBinary param file
153    * destination target
154    *
155    * @param file file to read from
156    * @return file content as an array of byte
157    * @exception IOException if read fails from target
158    */

159   public static byte[] readBinary(File JavaDoc file) throws IOException JavaDoc
160   {
161     long len = file.length();
162     FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
163     byte[] data = new byte[(int) len];
164     DataInputStream JavaDoc bais = new DataInputStream JavaDoc(fis);
165     bais.read(data);
166     return data;
167   }
168
169   /**
170    * Creates a new blob from a given file
171    *
172    * @param storeFile the path to the file to use
173    * @return <code>Blob</code> which content is that of the file
174    * @throws Exception if fails or if cannot find file
175    */

176   public static Blob createBlob(String JavaDoc storeFile) throws Exception JavaDoc
177   {
178     File JavaDoc fis = new File JavaDoc(storeFile);
179     if (!fis.exists())
180       fis = new File JavaDoc(new ScenarioUtility().getClass().getResource(storeFile)
181           .getFile());
182     if (!fis.exists())
183       throw new FileNotFoundException JavaDoc();
184     Blob bob = new org.objectweb.cjdbc.driver.Blob(ScenarioUtility
185         .readBinary(fis));
186     return bob;
187   }
188
189   /**
190    * WriteBinary data to the file. Tested ok with readBinary
191    *
192    * @param data to be written to the file
193    * @param file destination target
194    * @exception IOException if write fails on target
195    */

196   public static void writeBinary(byte[] data, File JavaDoc file) throws IOException JavaDoc
197   {
198     FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(file);
199     DataOutputStream JavaDoc baos = new DataOutputStream JavaDoc(fos);
200     baos.write(data);
201   }
202
203   /**
204    * Checks if two result sets have the same content
205    *
206    * @param rs1 an open result set
207    * @param rs2 an open result set
208    * @return <tt>true</tt> if the two results sets have the same data(does not
209    * checks for metadata)
210    */

211   public static boolean checkEquals(ResultSet JavaDoc rs1, ResultSet JavaDoc rs2)
212   {
213     try
214     {
215       ArrayList JavaDoc list1 = ScenarioUtility.convertResultSet(rs1);
216       //System.out.println(list1);
217
ArrayList JavaDoc list2 = ScenarioUtility.convertResultSet(rs2);
218       //System.out.println(list2);
219
if (list1.size() != list2.size())
220         return false;
221
222       Object JavaDoc o1;
223       Object JavaDoc o2;
224       for (int i = 0; i < list1.size(); i++)
225       {
226         ArrayList JavaDoc list11 = (ArrayList JavaDoc) list1.get(i);
227         ArrayList JavaDoc list22 = (ArrayList JavaDoc) list2.get(i);
228         if (list11.size() != list22.size())
229           return false;
230         for (int j = 0; j < list11.size(); j++)
231         {
232           o1 = list11.get(j);
233           o2 = list22.get(j);
234           if (o1 == null && o2 == null)
235             continue;
236           else if (o1 == null || o2 == null)
237             return false;
238           if (o1.equals(o2))
239             continue;
240           else
241             return false;
242         }
243       }
244       return true;
245     }
246     catch (Exception JavaDoc e)
247     {
248       e.printStackTrace();
249       return false;
250     }
251   }
252
253   /**
254    * Completely deletes a directory
255    *
256    * @param dir to delete
257    * @return true if it was successfully deleted
258    */

259   public static boolean deleteDir(File JavaDoc dir)
260   {
261     // to see if this directory is actually a symbolic link to a directory,
262
// we want to get its canonical path - that is, we follow the link to
263
// the file it's actually linked to
264
File JavaDoc candir;
265     try
266     {
267       candir = dir.getCanonicalFile();
268     }
269     catch (IOException JavaDoc e)
270     {
271       return false;
272     }
273
274     // a symbolic link has a different canonical path than its actual path,
275
// unless it's a link to itself
276
if (!candir.equals(dir.getAbsoluteFile()))
277     {
278       // this file is a symbolic link, and there's no reason for us to
279
// follow it, because then we might be deleting something outside of
280
// the directory we were told to delete
281
return false;
282     }
283
284     // now we go through all of the files and subdirectories in the
285
// directory and delete them one by one
286
File JavaDoc[] files = candir.listFiles();
287     if (files != null)
288     {
289       for (int i = 0; i < files.length; i++)
290       {
291         File JavaDoc file = files[i];
292
293         // in case this directory is actually a symbolic link, or it's
294
// empty, we want to try to delete the link before we try
295
// anything
296
boolean deleted = file.delete();
297         if (!deleted)
298         {
299           // deleting the file failed, so maybe it's a non-empty
300
// directory
301
if (file.isDirectory())
302             deleteDir(file);
303
304           // otherwise, there's nothing else we can do
305
}
306       }
307     }
308
309     // now that we tried to clear the directory out, we can try to delete it
310
// again
311
return dir.delete();
312   }
313 }
Popular Tags