KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > CosTransactions > ErrorLog


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28 //----------------------------------------------------------------------------
29
//
30
// Module: ErrorLog.java
31
//
32
// Description: Error logging facility.
33
//
34
// Product: com.sun.jts.CosTransactions
35
//
36
// Author: Simon Holdsworth
37
//
38
// Date: March, 1997
39
//
40
// Copyright (c): 1995-1997 IBM Corp.
41
//
42
// The source code for this program is not published or otherwise divested
43
// of its trade secrets, irrespective of what has been deposited with the
44
// U.S. Copyright Office.
45
//
46
// This software contains confidential and proprietary information of
47
// IBM Corp.
48
//----------------------------------------------------------------------------
49

50 package com.sun.jts.CosTransactions;
51
52 // Import required classes.
53

54 import java.io.*;
55 import java.util.*;
56 import java.text.DateFormat JavaDoc;
57
58 /**
59  * Provides a log of error messages.
60  *
61  * @version 0.01
62  *
63  * @author Simon Holdsworth, IBM Corporation
64  *
65  * @see
66 */

67 //----------------------------------------------------------------------------
68
// CHANGE HISTORY
69
//
70
// Version By Change Description
71
// 0.01 SAJH Initial implementation.
72
//------------------------------------------------------------------------------
73

74 public class ErrorLog extends Object JavaDoc {
75     /**The message format strings.
76      */

77     private static Messages messages = null;
78
79     /**The default error log file name.
80      */

81     static final String JavaDoc DEFAULT_LOGFILE = "jts.log"/*#Frozen*/;
82
83     /**The path of the log file.
84      */

85     private static String JavaDoc errorLogPath = null;
86
87     /**The name of the server.
88      */

89     private static String JavaDoc serverName = null;
90
91     /**Sets up the error log path.
92      *
93      * @param
94      *
95      * @return
96      *
97      * @see
98      */

99     private static final void setup() {
100         // Get the error log file path.
101

102         int[] result = new int[1];
103         errorLogPath = Configuration.getDirectory(Configuration.TRACE_DIRECTORY,
104                                                   Configuration.JTS_SUBDIRECTORY,
105                                                   result);
106
107         // If a default was used, display a message.
108

109         if( result[0] == Configuration.DEFAULT_USED ||
110             result[0] == Configuration.DEFAULT_INVALID ) {
111
112             // In the case where the SOMBASE default is used, only display a message if
113
// an invalid value was specified in the environment value.
114

115             boolean loggingOn =
116                 Configuration.getPropertyValue(Configuration.ERR_LOGGING)
117                 != null;
118
119             if (errorLogPath != null && loggingOn) {
120                 System.err.println(
121                     ErrorLog.getMessage(Messages.INVALID_LOG_PATH,
122                                         new java.lang.Object JavaDoc[]
123                                             { errorLogPath })
124                                   );
125             }
126
127             // In the case where the SOMBASE default is invalid, the value returned is
128
// the invalid default. We then default to the current directory.
129

130             if( result[0] == Configuration.DEFAULT_INVALID ) {
131                 if (loggingOn) {
132                     System.err.println(
133                         ErrorLog.getMessage(Messages.INVALID_DEFAULT_LOG_PATH)
134                                       );
135                 }
136                 errorLogPath = "."/*#Frozen*/;
137             }
138         }
139
140         // Get the server name too.
141

142         serverName = Configuration.getServerName();
143         if( serverName == null )
144             serverName = "Anonymous transient server"/*#Frozen*/;
145
146         // Get the ResourceBundle contents for message formats.
147

148         messages = (Messages)ResourceBundle.getBundle("com.sun.jts.CosTransactions.Messages");
149     }
150
151     /**Writes an error message to the error log.
152      *
153      * @param message The error message.
154      *
155      * @return
156      *
157      * @see
158      */

159     private static final void fileWrite( String JavaDoc message ) {
160         if (Configuration.getPropertyValue(Configuration.ERR_LOGGING) == null) {
161             return;
162         }
163
164         // Open the error log file and append the message.
165

166         try {
167             File errFileHandle = new File(errorLogPath, DEFAULT_LOGFILE);
168             RandomAccessFile fileOutput = new RandomAccessFile(errFileHandle,"rw"/*#Frozen*/);
169             fileOutput.seek(fileOutput.length());
170             fileOutput.writeBytes(message);
171             fileOutput.close();
172         } catch( Throwable JavaDoc e ) {
173             System.err.println(
174                 ErrorLog.getMessage(Messages.LOG_FILE_WRITE_ERROR)
175                               );
176         }
177     }
178
179     /**
180      * Writes an error message to the error log and to the screen.
181      * <p>
182      * Note that the inserts should be Strings, Integers or Dates.
183      * Exceptions should be converted to strings before calling this method.
184      *
185      * @param message The error message number.
186      * @param inserts The error message inserts.
187      * @param fatal Indicates whether the error is fatal.
188      *
189      * @return
190      *
191      * @see
192      */

193     public static final void error(int message, Object JavaDoc[] inserts,
194                                     boolean fatal) {
195         String JavaDoc messageStr = getMessage(message, inserts);
196
197         // First display the message to the screen.
198

199         System.err.println(
200             ErrorLog.getMessage(Messages.MSG_JTS_ERROR,
201                                 new java.lang.Object JavaDoc[] { messageStr })
202                           );
203         (new Exception JavaDoc()).printStackTrace();
204
205         // Write the message to the log file.
206
/*
207         messageStr = new Date().toString() + " : " + serverName + " : JTS" +
208             messages.getMessageNumber(message)+
209             (fatal ? "F " : "E ") +
210             messageStr + "\n";
211         */

212         String JavaDoc dateString = DateFormat.getDateTimeInstance().format(new Date());
213         messageStr = ErrorLog.getMessage(Messages.LOG_MESSAGE,
214                                          new java.lang.Object JavaDoc[] {
215                                             dateString, serverName,
216                                             messages.getMessageNumber(message),
217                                             (fatal ? "F"/*#Frozen*/ : "E"/*#Frozen*/),
218                                             messageStr,
219                                          });
220
221         fileWrite(messageStr);
222
223         // If the error is fatal, then end the process.
224

225         if (fatal) {
226             // CHANGED(Ram J) - fatal errors should not cause VM crash.
227
//System.exit(1);
228

229             // throw a system exception, so that the app or the app server
230
// may catch it. Note: This may leave the tx objects in an
231
// inconsistent state, and may result in a memory leak (?).
232
throw new org.omg.CORBA.INTERNAL JavaDoc(messageStr);
233         }
234     }
235
236     /**
237      * Writes a warning message to the error log and to the screen.
238      * <p>
239      * Note that the inserts should be Strings, Integers or Dates.
240      * Exceptions should be converted to strings before calling this method.
241      *
242      * @param message The warning message number.
243      * @param inserts The warning message inserts.
244      *
245      * @return
246      *
247      * @see
248      */

249     public static final void warning(int message, Object JavaDoc[] inserts) {
250         String JavaDoc messageStr = getMessage(message, inserts);
251
252         // First display the message to the screen.
253

254         System.err.println(
255             ErrorLog.getMessage(Messages.MSG_JTS_WARNING,
256                                 new java.lang.Object JavaDoc[] { messageStr })
257                           );
258
259         // Write the message to the log file.
260
/*
261         messageStr = new Date().toString() + " : " + serverName + " : JTS" +
262             messages.getMessageNumber(message)+"W "+
263             messageStr + "\n";
264         */

265         String JavaDoc dateString = DateFormat.getDateTimeInstance().format(new Date());
266         messageStr = ErrorLog.getMessage(Messages.LOG_MESSAGE,
267                                          new java.lang.Object JavaDoc[] {
268                                             dateString, serverName,
269                                             messages.getMessageNumber(message),
270                                             "W"/*#Frozen*/, messageStr,
271                                          });
272         fileWrite(messageStr);
273     }
274
275     /**
276      * Writes an informational message to the error log and to the screen.
277      * <p>
278      * Note that the inserts should be Strings, Integers or Dates.
279      * Exceptions should be converted to strings before calling this method.
280      *
281      * @param message The informational message number.
282      * @param inserts The informational message inserts.
283      *
284      * @return
285      *
286      * @see
287      */

288     public static final void info(int message, Object JavaDoc[] inserts) {
289         String JavaDoc messageStr = getMessage(message, inserts);
290
291         // First display the message to the screen.
292

293         System.err.println(
294             ErrorLog.getMessage(Messages.MSG_JTS_INFO,
295                                 new java.lang.Object JavaDoc[] { messageStr })
296                           );
297
298         // Write the message to the log file.
299
/*
300         messageStr = new Date().toString() + " : " + serverName + " : JTS" +
301             messages.getMessageNumber(message)+"I "+
302             messageStr + "\n";
303         */

304         String JavaDoc dateString = DateFormat.getDateTimeInstance().format(new Date());
305         messageStr = ErrorLog.getMessage(Messages.LOG_MESSAGE,
306                                          new java.lang.Object JavaDoc[] {
307                                             dateString, serverName,
308                                             messages.getMessageNumber(message),
309                                             "I"/*#Frozen*/, messageStr,
310                                          });
311         fileWrite(messageStr);
312     }
313
314     /**
315      * Returns a formatted message given the message number and inserts.
316      * <p>
317      * Note that the inserts should be Strings, Integers or Dates.
318      * Exceptions should be converted to strings before calling this method.
319      *
320      * @param message The message number.
321      * @param inserts The message inserts.
322      *
323      * @return The formatted string.
324      *
325      * @see
326      */

327     static final String JavaDoc getMessage(int message, Object JavaDoc[] inserts) {
328         String JavaDoc result = null;
329
330         // Get the error log file path, and the message formats.
331

332         if (errorLogPath == null) {
333             setup();
334         }
335
336         // Format the message.
337

338         if (inserts == null) {
339             inserts = new Object JavaDoc[0];
340         }
341
342         return messages.getMessage(message, inserts);
343     }
344
345     /**
346      * Returns an unformatted message given the message number.
347      *
348      * @param messageNum The message number.
349      *
350      * @return The unformatted string.
351      *
352      * @see
353      */

354     public static final String JavaDoc getMessage(int messageNum) {
355         String JavaDoc result = null;
356
357         // Get the error log file path, and the message formats.
358

359         if (errorLogPath == null) {
360             setup();
361         }
362
363         return messages.getMessage(messageNum);
364     }
365 }
366
Popular Tags