KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > tools > dblook > Logs


1 /*
2
3    Derby - Class org.apache.derby.impl.tools.dblook.Logs
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.tools.dblook;
23
24 import java.io.PrintWriter JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import java.sql.SQLException JavaDoc;
29 import org.apache.derby.tools.dblook;
30
31 public class Logs {
32
33     // Log file (for errors/warnings).
34
private static PrintWriter JavaDoc logFile = null;
35
36     // User-specified output file.
37
private static PrintWriter JavaDoc ddlFile = null;
38
39     // Statement delimiter.
40
private static String JavaDoc stmtEnd;
41
42     // Verbose mode?
43
private static boolean verbose;
44
45     // Did we write at least one message to the dblook file?
46
private static boolean atLeastOneDebug;
47
48     /* **********************************************
49      * initLogs:
50      * Prepare output streams and initialize state for
51      * handling dblook output.
52      * @param logFileName File for errors/warnings.
53      * @param ddlFileName File for generated DDL.
54      * @param appendLogs Whether or not to append to existing
55      * log and ddl files.
56      * @param doVerbose verbose mode
57      * @param endOfStmt Statement delimiter.
58      * @return true if all state is initialized successfully;
59      * false otherwise.
60      ****/

61
62     public static boolean initLogs(String JavaDoc logFileName, String JavaDoc ddlFileName,
63             boolean appendLogs, boolean doVerbose, String JavaDoc endOfStmt)
64     {
65         try {
66
67             logFile = new PrintWriter JavaDoc(new FileOutputStream JavaDoc(logFileName, appendLogs));
68             ddlFile = (ddlFileName == null) ? null
69                     : new PrintWriter JavaDoc(new FileOutputStream JavaDoc(ddlFileName, appendLogs));
70             verbose = doVerbose;
71             stmtEnd = endOfStmt;
72             atLeastOneDebug = false;
73         }
74         catch (IOException JavaDoc ioe)
75         {
76             System.out.println("Error initializing log file(s): " + ioe);
77             return false;
78         }
79
80         return true;
81
82     }
83
84
85     /* **********************************************
86      * Method to report status info to the end-user.
87      * This information will be printed as SQL script
88      * comments, which means the messages must be
89      * preceded by a "--". If the user specified a
90      * DDL file, then the message will be printed to
91      * that file; otherwise, it will be printed to
92      * the console.
93      * @param msg the information to print out.
94      ****/

95
96     public static void report(String JavaDoc msg) {
97
98         if (ddlFile == null)
99             System.out.println("-- " + msg);
100         else
101             ddlFile.println("-- " + msg);
102
103         return;
104
105     }
106
107     /* **********************************************
108      * Report a specific string to output.
109      * @param str The string to report.
110      ****/

111  
112     public static void reportString(String JavaDoc str) {
113         report(str);
114     }
115
116     /* **********************************************
117      * Report a localized message to output.
118      * @param key Key for the message to report.
119      ****/

120  
121     public static void reportMessage(String JavaDoc key) {
122         reportMessage(key, (String JavaDoc[])null);
123     }
124
125     /* **********************************************
126      * Report a localized message to output,
127      * substituting the received value where
128      * appropriate.
129      * @param key Key for the message to report.
130      * @param value Value to be inserted into the
131      * message at the {0} marker.
132      ****/

133
134     public static void reportMessage(String JavaDoc key,
135         String JavaDoc value) {
136         reportMessage(key, new String JavaDoc [] {value});
137     }
138
139     /* **********************************************
140      * Report a localized message to output,
141      * substituting the received values where
142      * appropriate.
143      * @param key Key for the message to report.
144      * @param values Array of Value to be inserted
145      * into the message at the {0}, {1}, etc markers.
146      ****/

147
148     public static void reportMessage(String JavaDoc key,
149         String JavaDoc [] values) {
150
151         String JavaDoc msg = dblook.lookupMessage(key, values);
152         report(msg);
153
154     }
155
156     /* **********************************************
157      * Prints the received exception to the log
158      * file and, if the use has specified "verbose",
159      * the screen as well.
160      * @param e The exception to be printed.
161      ****/

162
163     public static void debug(Exception JavaDoc e) {
164
165         e.printStackTrace(logFile);
166         if (verbose)
167             e.printStackTrace(System.err);
168         atLeastOneDebug = true;
169
170     }
171
172     /* **********************************************
173      * Prints the message for the received key to the log
174      * log file and, if the use has specified "verbose",
175      * the screen as well.
176      * @param key Key for the message to be printed.
177      * @param value Value to be substituted into the
178      * message.
179      ****/

180     
181     public static void debug(String JavaDoc key,
182         String JavaDoc value)
183     {
184
185         String JavaDoc msg = key;
186         if (value != null) {
187             msg = dblook.lookupMessage(key,
188                 new String JavaDoc [] {value});
189         }
190
191         logFile.println("-- **--> DEBUG: " + msg);
192         if (verbose)
193             System.err.println("-- **--> DEBUG: " + msg);
194         atLeastOneDebug = true;
195
196     }
197
198     /* **********************************************
199      * Prints the message for the received key to the log
200      * log file and, if the use has specified "verbose",
201      * the screen as well.
202      * @param key Key for the message to be printed.
203      * @param value Value to be substituted into the
204      * message.
205      ****/

206
207     public static void debug(String JavaDoc key,
208         String JavaDoc [] values)
209     {
210
211         String JavaDoc msg = key;
212         if (values != null) {
213             msg = dblook.lookupMessage(key, values);
214         }
215
216         logFile.println("-- **--> DEBUG: " + msg);
217         if (verbose)
218             System.err.println("-- **--> DEBUG: " + msg);
219         atLeastOneDebug = true;
220
221     }
222
223     /* **********************************************
224      * Recursive method to unroll a chains of SQL exceptions.
225      * @param sqlE The SQL exception to unroll.
226      * @return A string representing the unrolled exception
227      * is returned.
228      ****/

229
230     public static String JavaDoc unRollExceptions(SQLException JavaDoc sqlE) {
231
232         String JavaDoc rv = sqlE.getMessage() + "\n";
233         if (sqlE.getNextException() != null)
234             return rv + unRollExceptions(sqlE.getNextException());
235         else
236             return rv;
237
238     }
239
240     /* **********************************************
241      * Write a string (piece of an SQL command) to
242      * the output DDL file.
243      * @param sql The string to write.
244      ****/

245
246     public static void writeToNewDDL(String JavaDoc sql) {
247
248         if (ddlFile == null)
249             System.out.print(sql);
250         else
251             ddlFile.print(sql);
252
253     }
254
255     /* **********************************************
256      * Write the user-given statement delimiter to
257      * the output DDL file, followed by a newline.
258      ****/

259
260     public static void writeStmtEndToNewDDL() {
261
262         if (ddlFile == null)
263             System.out.println(stmtEnd);
264         else
265             ddlFile.println(stmtEnd);
266
267     }
268
269     /* **********************************************
270      * Write a newline character to the output DDL
271      * file, followed by a newline.
272      ****/

273
274     public static void writeNewlineToNewDDL() {
275
276         if (ddlFile == null)
277             System.out.println();
278         else
279             ddlFile.println();
280     }
281
282     /* **********************************************
283      * Close output streams and, if at least one
284      * message was printed to the log file, let
285      * the user know.
286      * @return true if all streams were closed
287      * successfully; false otherwise.
288      ****/

289
290     public static boolean cleanup() {
291
292         try {
293             if (atLeastOneDebug)
294                 dblook.writeVerboseOutput(
295                     "DBLOOK_AtLeastOneDebug", null);
296             logFile.close();
297             if (ddlFile != null)
298                 ddlFile.close();
299             
300         }
301         catch (Exception JavaDoc e) {
302             System.out.println("Error releasing resources: " + e);
303             return false;
304         }
305
306         return true;
307
308     }
309
310 }
311
Popular Tags