KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > utils > LogFormatter


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 package com.sun.jts.utils;
29
30
31 import org.omg.CosTransactions.*;
32 import javax.transaction.xa.*;
33 import java.util.*;
34 import java.util.logging.*;
35 import java.text.*;
36
37 /**
38  * This class is used to format the trace record.
39  *
40  * @author <a HREF="mailto:k.venugopal@sun.comi,kannan.srinivasan@sun.com">K Venugopal</a>
41  * @version 1.0
42  */

43 public class LogFormatter
44 {
45   /**
46    * Helper method to convert a byte arror to string. This is typically used for printing Xids.
47    *
48    * @param byteArray a <code>byte[]</code> value
49    * @return a <code>String</code> value
50    */

51   
52     public static String JavaDoc convertToString(byte[] byteArray)
53     {
54         int i;
55         StringBuffer JavaDoc strBuf=new StringBuffer JavaDoc();
56         for(i = 0; i < byteArray.length; i++)
57          {
58             strBuf.append(byteArray[i]);
59         }
60         return strBuf.toString();
61     }
62
63   /**
64    * Converts an array of xids to string that can be printed. Its a helper method.
65    *
66    * @param xidArray a <code>Xid[]</code> value
67    * @return a <code>String</code> value
68    */

69     public static String JavaDoc convertXidArrayToString(Xid[] xidArray)
70     {
71     if(xidArray.length != 0)
72     {
73             int i;
74             StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc("[ ");
75             for(i = 0; i < xidArray.length - 1; i++)
76             {
77                     strBuf.append(xidArray[i].getGlobalTransactionId()).append(", ");
78             }
79             strBuf.append(xidArray[xidArray.length - 1]).append(" ]");
80             return strBuf.toString();
81     }
82     else
83         return " null ";
84     }
85
86   /**
87    * Helper method to convert properties to string.
88    *
89    * @param prop a <code>Properties</code> value
90    * @return a <code>String</code> value
91    */

92     public static String JavaDoc convertPropsToString(Properties prop)
93     {
94         if(prop==null){
95         return "{null}";
96         }
97         StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc("{ ");
98         for(Enumeration e = prop.propertyNames(); e.hasMoreElements(); )
99         {
100             Object JavaDoc obj = e.nextElement();
101             strBuf.append("[ ").append(obj).append("->");
102             Object JavaDoc val=prop.getProperty((String JavaDoc)obj);
103             if(val==null)
104             strBuf.append("null");
105             else
106                 strBuf.append((String JavaDoc)val);
107             strBuf.append(" ] ");
108         }
109         strBuf.append("}");
110         return strBuf.toString();
111     }
112
113     /**
114         getLocalizedMessage is used to localize the messages being used in
115         exceptions
116     **/

117
118     public static String JavaDoc getLocalizedMessage(Logger logger , String JavaDoc key){
119         try{
120             ResourceBundle rb = logger.getResourceBundle();
121             String JavaDoc message = rb.getString(key);
122             return message;
123         }catch ( Exception JavaDoc ex){
124             logger.log(Level.FINE,"JTS:Error while localizing the log message");
125             return key;
126         }
127     }
128     
129     /**
130         getLocalizedMessage is used to localize the messages being used in
131         exceptions with arguments inserted appropriately.
132     **/

133
134     public static String JavaDoc getLocalizedMessage(Logger logger , String JavaDoc key,
135                                             Object JavaDoc[] args){
136         try{
137             ResourceBundle rb = logger.getResourceBundle();
138             String JavaDoc message = rb.getString(key);
139             return MessageFormat.format(message,args);
140         }catch ( Exception JavaDoc ex){
141                 logger.log(Level.FINE,"JTS:Error while localizing the log message");
142             return key;
143         }
144     }
145 }
146
Popular Tags