KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > trace > TraceRecordFormatter


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.trace;
29
30 import org.omg.CosTransactions.*;
31 import javax.transaction.xa.*;
32 import java.util.*;
33
34 /**
35  * This class is used to format the trace record.
36  *
37  * @author <a HREF="mailto:kannan.srinivasan@sun.com">Kannan Srinivasan</a>
38  * @version 1.0
39  */

40 public class TraceRecordFormatter
41 {
42   /**
43    * Returns the formatted record, by accepting the simple string
44    * message, tid and originator, which can be written to OutputStream
45    * @param tid an <code>Object</code> value
46    * @param origin an <code>Object</code> value
47    * @param message a <code>String</code> value
48    * @return a <code>String</code> value
49    */

50     public static String JavaDoc createTraceRecord(Object JavaDoc tid, Object JavaDoc origin, String JavaDoc message)
51     {
52         StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc(TraceUtil.getTraceRecordTag());
53         strBuf.append(TraceUtil.getCurrentTraceLevel())
54               .append(TraceUtil.getFieldDelimiter());
55         if(tid == null)
56         {
57             strBuf.append("<unknown-tid>");
58         }
59         else
60         {
61             if(tid instanceof String JavaDoc)
62             {
63                 strBuf.append(tid);
64             }
65             else if(tid instanceof otid_t)
66             {
67                     strBuf.append(convertToString(((otid_t)tid).tid));
68             }
69         }
70         strBuf.append(TraceUtil.getFieldDelimiter())
71               .append(System.currentTimeMillis())
72               .append(TraceUtil.getFieldDelimiter());
73         if(origin == null)
74         {
75             strBuf.append("<unknown-origin>");
76         }
77         else
78         {
79             strBuf.append(origin);
80         }
81         strBuf.append(TraceUtil.getFieldDelimiter()).append(message).append("\n");
82         return strBuf.toString();
83     }
84
85   /**
86    * Returns the scheme used to format the record. This prints the different components (fields) of
87    * the trace and record and their order of occurrence.
88    *
89    * @return a <code>String</code> value
90    */

91     public static String JavaDoc getTraceRecordScheme()
92     {
93         String JavaDoc traceRecordScheme = "<trace-record-tag><level><separator><omg-tid><separator><current-time-in-milliseconds><separator><originator><separator><message>";
94         return traceRecordScheme;
95     }
96
97   /**
98    * Helper method to convert a byte arror to string. This is typically used for printing Xids.
99    *
100    * @param byteArray a <code>byte[]</code> value
101    * @return a <code>String</code> value
102    */

103     public static String JavaDoc convertToString(byte[] byteArray)
104     {
105         int i;
106         StringBuffer JavaDoc strBuf=new StringBuffer JavaDoc();
107         for(i = 0; i < byteArray.length; i++)
108          {
109             strBuf.append(byteArray[i]);
110         }
111         return strBuf.toString();
112     }
113
114   /**
115    * Converts an array of xids to string that can be printed. Its a helper method.
116    *
117    * @param xidArray a <code>Xid[]</code> value
118    * @return a <code>String</code> value
119    */

120     public static String JavaDoc convertXidArrayToString(Xid[] xidArray)
121     {
122     if(xidArray.length != 0)
123     {
124             int i;
125             StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc("[ ");
126             for(i = 0; i < xidArray.length - 1; i++)
127             {
128                     strBuf.append(xidArray[i].getGlobalTransactionId()).append(", ");
129             }
130             strBuf.append(xidArray[xidArray.length - 1]).append(" ]");
131             return strBuf.toString();
132     }
133     else
134         return " null ";
135     }
136
137   /**
138    * Helper method to convert properties to string.
139    *
140    * @param prop a <code>Properties</code> value
141    * @return a <code>String</code> value
142    */

143     public static String JavaDoc convertPropsToString(Properties prop)
144     {
145         if(prop==null){
146         return "{null}";
147         }
148         StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc("{ ");
149         for(Enumeration e = prop.propertyNames(); e.hasMoreElements(); )
150         {
151             Object JavaDoc obj = e.nextElement();
152             strBuf.append("[ ").append(obj).append("->");
153             Object JavaDoc val=prop.getProperty((String JavaDoc)obj);
154             if(val==null)
155             strBuf.append("null");
156             else
157                 strBuf.append((String JavaDoc)val);
158             strBuf.append(" ] ");
159         }
160         strBuf.append("}");
161         return strBuf.toString();
162     }
163 }
164
Popular Tags