KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > tracing > server > DbToXml


1 /***************************************************************************/
2 /* COACH: Component Based Open Source Architecture for */
3 /* Distributed Telecom Applications */
4 /* See: http://www.objectweb.org/ */
5 /* */
6 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
7 /* Bell Labs Advanced Technologies - EMEA */
8 /* */
9 /* Initial developer(s): Wim Hellenthal */
10 /* */
11 /* This library is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public */
13 /* License as published by the Free Software Foundation; either */
14 /* version 2.1 of the License, or (at your option) any later version. */
15 /* */
16 /* This library is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
19 /* Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public */
22 /* License along with this library; if not, write to the Free Software */
23 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 /***************************************************************************/
25 package org.coach.tracing.server;
26
27 import java.util.*;
28 import org.coach.idltree.*;
29 import org.coach.tracing.api.*;
30
31 public class DbToXml
32 {
33     private EventDataBase db;
34
35     public DbToXml(EventDataBase db)
36     {
37         this.db = db;
38     }
39
40     public String JavaDoc getXmlEventAt(long index)
41     {
42         EventRecord tr = null;
43
44         tr = db.getEventAt(index);
45
46         return tr.toXml(db);
47     }
48
49     public String JavaDoc getXmlEvent(long key_id)
50     {
51         EventRecord tr = null;
52
53         tr = db.readEvent(key_id);
54
55         return tr.toXml(db);
56     }
57
58     public String JavaDoc getXmlEvents(long index, int len, Hashtable operation_filter, Hashtable entity_filter, Hashtable toself_filter, String JavaDoc filter_unmatched, long hide_to)
59     {
60         EventRecord[] er = null;
61         EventRecord ser = null;
62         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
63         int cnt = 0;
64         int visibles = 0;
65         long offset = 0;
66
67         sb.append ("<trace_events>\n");
68
69         // pretend we cleared the database up to index 'hide_to'
70

71         index += hide_to;
72
73         er = db.getEvents(index, index + len);
74
75         while ( visibles < len )
76         {
77             try
78             {
79                 if ( cnt == len )
80                 {
81                    index += len;
82
83                    er = db.getEvents(index, index + len);
84
85                    cnt = 0;
86                 }
87
88                 ser = er[cnt++];
89
90                 long link_key = ser.getTraceRecord().linkKey;
91
92                 // filter on unmatched
93

94                 if ( filter_unmatched.equals("false") || link_key != -1 )
95                 {
96                     // filter on operation name
97

98                     if (!operation_filter.containsKey (ser.getTraceRecord().op_name) )
99                     {
100                         // filter on entity (including peer)
101

102                         String JavaDoc identity_key = Long.toString(ser.getTraceRecord().identityKey);
103
104                         EventRecord peer = null;
105
106                         if ( link_key != -1 )
107                         {
108                             peer = db.readEvent(link_key);
109
110                             String JavaDoc peer_identity_key = Long.toString(peer.getTraceRecord().identityKey);
111
112                             if ( !entity_filter.containsKey(identity_key) && !entity_filter.containsKey(peer_identity_key) )
113                             {
114                                 // filter on operations to self
115

116                                 if ( toself_filter.containsKey (identity_key) && toself_filter.containsKey(peer_identity_key) )
117                                 {
118                                     /* check if they have the same collapsed node as ancestor meaning that both events are positioned on
119                                      * the same entity/identity line
120                                      */

121
122                                     if ( !((String JavaDoc)toself_filter.get(identity_key)).equals((String JavaDoc)toself_filter.get(peer_identity_key)) )
123                                     {
124                                         sb.append (ser.toXml(db));
125                                         visibles++;
126
127                                         if ( visibles == 1 )
128                                             offset = ser.getCurrentIndex();
129                                     }
130                                     else
131                                         cnt++; // filtered on operation to self so we can skip the peer event as well
132
}
133                                 else
134                                 {
135                                     sb.append (ser.toXml(db));
136                                     visibles++;
137
138                                     if ( visibles == 1 )
139                                         offset = ser.getCurrentIndex();
140                                 }
141                             }
142                             else
143                                cnt++; // filtered on identity so we can skip the peer event as well
144
}
145                         else
146                         {
147                             // unmatched event for specified entity (no unmatched event filtering)
148

149                             if ( !entity_filter.containsKey(identity_key) )
150                             {
151                                 sb.append (ser.toXml(db));
152
153                                 visibles++;
154
155                                 if ( visibles == 1 )
156                                     offset = ser.getCurrentIndex();
157                             }
158                         }
159                     }
160                     else if ( link_key != -1 )
161                         cnt++; // filtered on operation name so we can skip the peer event as well
162
}
163             }
164             catch (Exception JavaDoc _ex)
165             {
166                 break;
167             }
168         }
169
170         sb.append ("<count>" + Long.toString(db.getEventCount() - hide_to ) + "</count>\n");
171         sb.append ("<offset>" + Long.toString(offset) + "</offset>\n");
172
173         sb.append ("</trace_events>\n");
174
175         return sb.toString();
176     }
177
178     public String JavaDoc getXmlIdentities(long[] keys) throws org.coach.tracing.api.InvalidKey
179     {
180         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
181
182         sb.append ("<trace_entities>\n");
183
184         for (int i = 0; i < keys.length; i++)
185         {
186             try
187             {
188                 sb.append ("<entity id='" + keys[i] + "'>\n");
189                 sb.append (" <identityHierarchy>\n");
190
191                 Identity id = null;
192
193                 long identityKey = keys[i];
194
195                 while(identityKey != -1)
196                 {
197                     try
198                     {
199                         id = db.getIdentity(new Long JavaDoc(identityKey));
200
201                         if ( id == null )
202                             break;
203
204                         sb.append (" <" + EventRecord.getIdentityKindName(id.kind) + " name='" + id.name + "' type='" + id.type + "'/>\n");
205                     }
206                     catch (Exception JavaDoc _ex)
207                     {
208                         break;
209                     }
210
211                     identityKey = id.linkKey;
212                 }
213
214                 sb.append (" </identityHierarchy>\n");
215
216                 sb.append ("</entity>\n");
217             }
218             catch (Exception JavaDoc _ex)
219             {
220                 break;
221             }
222         }
223
224         sb.append ("</trace_entities>\n");
225
226         return sb.toString();
227     }
228
229     public String JavaDoc getXmlParameterValues(long parameterKey)
230     {
231         IdlNode idlNode = db.readValue(parameterKey);
232
233         if (idlNode == null)
234         {
235             // no parameter information available.
236

237             XmlWriter w = new XmlWriter();
238
239             return w.toString();
240         }
241
242         XmlWriter w = new XmlWriter();
243
244         XmlNode.write(idlNode, w);
245
246         return w.toString();
247     }
248 }
249
250
Popular Tags