KickJava   Java API By Example, From Geeks To Geeks.

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


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): Harold Batteram */
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 org.coach.idltree.*;
28 import org.coach.tracing.*;
29 import org.coach.tracing.api.*;
30 import java.io.*;
31 import java.util.*;
32
33 public class EventDataBase
34 {
35     private static int CashSize = 1001;
36     private EventRecord[] eventCash = new EventRecord[CashSize];
37
38     private RandomAccessFile valueFile = null;
39     private RandomAccessFile eventFile = null;
40     private Hashtable nameIndexTable = new Hashtable();
41     private Hashtable indexNameTable = new Hashtable();
42     private HashMap identityTreeTable = new HashMap();
43     private HashMap identityIndexTable = new HashMap();
44     private Identity rootIdentity = new Identity(IdentityKind.CCM_NODE, "", "", (long)-1);
45     private static Long JavaDoc rootKey = new Long JavaDoc(-1);
46     private static HashMap trailMap = new HashMap();
47     private static HashMap unmatched = new HashMap();
48     private static EventSorter eventSorter = null;
49     private static EventDataBase eventDB = null;
50
51     private File dbDir = null;
52     private long eventCount = 0;
53     private int identityCount = 0;
54     private long messageCount = 0;
55     private int nameIndex = 0;
56     private int unmatchCount = 0;
57     
58     public static EventDataBase getEventDB()
59     {
60         if (eventDB == null)
61         {
62             eventDB = new EventDataBase();
63             eventSorter = new EventSorter(eventDB);
64         }
65         return eventDB;
66     }
67                  
68     private EventDataBase()
69     {
70         if (eventFile == null)
71         {
72             try
73             {
74                 identityTreeTable.put(rootIdentity, new HashMap());
75                 identityIndexTable.put(rootKey, rootIdentity);
76                 
77                 dbDir = new File(System.getProperty("coach.tracing.db.dir", "."));
78                 dbDir.mkdirs();
79                 
80                 File f = new File(dbDir, "value.db");
81                 if (f.exists())
82                 {
83                     f.delete();
84                 }
85                 valueFile = new RandomAccessFile(f.getPath(), "rw");
86
87                 f = new File(dbDir, "event.db");
88                 if (f.exists())
89                 {
90                     f.delete();
91                 }
92                 eventFile = new RandomAccessFile(f.getPath(), "rw");
93
94                 f = new File(dbDir, "identity.db");
95                 if (f.exists())
96                 {
97                     f.delete();
98                 }
99             }
100             catch (Exception JavaDoc e)
101             {
102                 e.printStackTrace();
103             }
104         }
105     }
106
107 // public EventDataBase(String name)
108
// {
109
// load(name);
110
// }
111

112     public synchronized void save(String JavaDoc name)
113     {
114         try
115         {
116             File dbDir = new File(System.getProperty("coach.event.db", "."));
117             File saveDir = new File(dbDir, name);
118             eventFile.close();
119             valueFile.close();
120             saveDir.mkdirs();
121
122             saveIdentities(new File(saveDir, "identity.db"));
123             
124             File f1 = new File(saveDir, "names.db");
125             PrintWriter nameFile = new PrintWriter(new FileWriter(f1.getPath()));
126             for (int i = 0; i < nameIndex; i++)
127             {
128                 nameFile.println(readName(i));
129             }
130             nameFile.close();
131             
132             f1 = new File(dbDir, "value.db");
133             File f2 = new File(saveDir, "value.db");
134             f1.renameTo(f2);
135             valueFile = new RandomAccessFile(f1.getPath(), "rw");
136
137             f1 = new File(dbDir, "event.db");
138             f2 = new File(saveDir, "event.db");
139             f1.renameTo(f2);
140             eventFile = new RandomAccessFile(f1.getPath(), "rw");
141         }
142         catch (Exception JavaDoc e)
143         {
144             e.printStackTrace();
145             throw new RuntimeException JavaDoc("Failed to save events");
146         }
147     }
148     
149     public synchronized void load(String JavaDoc name)
150     {
151         try
152         {
153             File dbDir = new File(System.getProperty("coach.event.db", "."));
154             File saveDir = new File(dbDir, name);
155             File f = new File(saveDir, "names.db");
156
157             BufferedReader nameFile = new BufferedReader(new FileReader(f.getPath()));
158             nameIndexTable.clear();
159             indexNameTable.clear();
160             identityTreeTable.clear();
161             identityIndexTable.clear();
162             identityCount = 0;
163             
164             nameIndex = 0;
165             String JavaDoc line = nameFile.readLine();
166             while (line != null)
167             {
168                 writeName(line);
169                 line = nameFile.readLine();
170             }
171             nameFile.close();
172
173             f = new File(saveDir, "value.db");
174             valueFile = new RandomAccessFile(f.getPath(), "rw");
175             f = new File(saveDir, "event.db");
176             eventFile = new RandomAccessFile(f.getPath(), "rw");
177
178 // eventCount = keyFile.length() / 8;
179

180             f = new File(saveDir, "identity.db");
181             loadIdentities(f);
182
183         }
184         catch (Exception JavaDoc e)
185         {
186             e.printStackTrace();
187             throw new RuntimeException JavaDoc("Failed to load events");
188         }
189     }
190     
191     public synchronized void clear()
192     {
193         try
194         {
195             // close all open files
196
if (eventFile != null)
197             {
198                 eventFile.close();
199                 valueFile.close();
200             }
201
202             File f = new File(dbDir, "value.db");
203             if (f.exists())
204             {
205                 f.delete();
206             }
207             valueFile = new RandomAccessFile(f.getPath(), "rw");
208
209             f = new File(dbDir, "event.db");
210             if (f.exists())
211             {
212                 f.delete();
213             }
214             eventFile = new RandomAccessFile(f.getPath(), "rw");
215
216             f = new File(dbDir, "identity.db");
217             if (f.exists())
218             {
219                 f.delete();
220             }
221
222             f = new File(dbDir, "key.db");
223             if (f.exists())
224             {
225                 f.delete();
226             }
227             nameIndexTable.clear();
228             indexNameTable.clear();
229             nameIndex = 0;
230             eventCount = 0;
231             identityCount = 0;
232         }
233         catch (Exception JavaDoc e)
234         {
235             e.printStackTrace();
236         }
237     }
238
239     public synchronized void addEvents(TraceEvent[] events)
240     {
241         for (int i = 0; i < events.length; i++)
242         {
243             EventRecord r = new EventRecord(events[i]);
244             writeEvent(r);
245         }
246     }
247     
248     public synchronized EventRecord getEventAt(long index)
249     {
250         return eventSorter.getEventAt(index);
251     }
252
253     public synchronized long getEventIndex(long key)
254     {
255         return eventSorter.getEventIndex(key);
256     }
257
258     public synchronized EventRecord[] getEvents(long start, long end)
259     {
260         return eventSorter.getEvents(start, end);
261     }
262     
263     public void link(EventRecord r1, EventRecord r2)
264     {
265         r2.tr.linkKey = r1.getKey();
266         r1.tr.linkKey = r2.getKey();
267         updateEvent(r1);
268         updateEvent(r2);
269         messageCount++;
270     }
271     
272     private String JavaDoc readName(int index)
273     {
274         return (String JavaDoc)indexNameTable.get(new Integer JavaDoc(index));
275     }
276
277     private int writeName(String JavaDoc name)
278     {
279         if (name == null)
280         {
281             throw new RuntimeException JavaDoc("name should not be null");
282         }
283         Integer JavaDoc index = (Integer JavaDoc)nameIndexTable.get(name);
284         if (index == null)
285         {
286             index = new Integer JavaDoc(nameIndex++);
287             nameIndexTable.put(name, index);
288             indexNameTable.put(index, name);
289         }
290         return index.intValue();
291     }
292
293     private long writeValue(IdlNode idlNode)
294     {
295         if (idlNode == null)
296         {
297             // no parameter infromation available.
298
return -1;
299         }
300         try
301         {
302             ByteArrayOutputStream bout = new ByteArrayOutputStream();
303             ObjectOutputStream out = new ObjectOutputStream(bout);
304             out.writeObject(idlNode);
305             out.close();
306             byte[] buf = bout.toByteArray();
307             long index = valueFile.length();
308             valueFile.seek(index);
309             valueFile.writeInt(buf.length);
310             valueFile.write(buf);
311             return index;
312         }
313         catch (Exception JavaDoc ex)
314         {
315             ex.printStackTrace();
316         }
317         throw new RuntimeException JavaDoc("Failed to write event");
318     }
319
320     public String JavaDoc getParameterValues(long key) {
321         IdlNode idlNode = readValue(key);
322         if (idlNode == null) {
323             // no parameter information available.
324
return "";
325         }
326         XmlWriter w = new XmlWriter();
327         XmlNode.write(idlNode, w);
328         return w.toString();
329     }
330
331     public IdlNode readValue(long index)
332     {
333         if (index == -1)
334         {
335             // no paramater information available
336
return null;
337         }
338         try
339         {
340             valueFile.seek(index);
341             int size = valueFile.readInt();
342             byte[] buf = new byte[size];
343             valueFile.read(buf);
344             ByteArrayInputStream bin = new ByteArrayInputStream(buf);
345             ObjectInputStream in = new ObjectInputStream(bin);
346             IdlNode n = (IdlNode)in.readObject();
347             in.close();
348             return n;
349         }
350         catch (Exception JavaDoc e)
351         {
352             e.printStackTrace();
353         }
354         throw new RuntimeException JavaDoc("Failed to read value");
355     }
356    
357     public long getEventCount()
358     {
359         return eventCount;
360     }
361
362     public int getIdentityCount()
363     {
364         return identityCount;
365     }
366     
367     public long getMessageCount()
368     {
369         return messageCount;
370     }
371
372     public long getUnmatchedEventCount()
373     {
374         return eventCount - (messageCount * 2);
375     }
376             
377     public synchronized void writeEvent(EventRecord e)
378     {
379         try
380         {
381             e.tr.key_id = eventFile.length();
382             writeEvent(e, e.tr.key_id);
383             eventCash[(int)(e.tr.key_id % CashSize)] = e;
384             eventCount++;
385
386             match(e);
387
388             eventSorter.add(e);
389             return;
390         }
391         catch (Exception JavaDoc ex)
392         {
393             ex.printStackTrace();
394         }
395         throw new RuntimeException JavaDoc("Failed to write event");
396     }
397
398     public synchronized void updateEvent(EventRecord e)
399     {
400         writeEvent(e, e.tr.key_id);
401         eventCash[(int)(e.tr.key_id % CashSize)] = e;
402     }
403     
404     private void writeEvent(EventRecord e, long index)
405     {
406         try
407         {
408             eventFile.seek(e.tr.key_id);
409             // Take care that the write and read order match!
410
eventFile.writeLong(e.tr.key_id);
411             if (e.identity != null)
412             {
413                 e.tr.identityKey = writeIdentity(e.identity);
414             }
415             eventFile.writeLong(e.tr.identityKey);
416             eventFile.writeInt(writeName(e.tr.trail_label));
417             eventFile.writeInt(writeName(e.tr.message_id));
418             eventFile.writeInt(writeName(e.tr.trail_id));
419             eventFile.writeInt(e.tr.event_counter);
420             eventFile.writeInt(writeName(e.tr.op_name));
421             eventFile.writeInt(e.tr.interaction_point.value());
422             e.tr.parametersKey = writeValue(e.idlNode);
423             eventFile.writeLong(e.tr.parametersKey);
424             eventFile.writeLong(e.tr.linkKey);
425             eventFile.writeInt(writeName(e.tr.thread_id));
426             eventFile.writeLong(e.tr.time_stamp);
427             return;
428         }
429         catch (Exception JavaDoc ex)
430         {
431             ex.printStackTrace();
432         }
433         throw new RuntimeException JavaDoc("Failed to write event");
434     }
435
436     public synchronized EventRecord readEvent(long key)
437     {
438         EventRecord e = eventCash[(int)(key % CashSize)];
439         if (e != null && e.getKey() == key)
440         {
441             return e;
442         }
443        
444         try
445         {
446             eventFile.seek(key);
447
448             e = new EventRecord();
449             // Take care that the write and read order match!
450
e.tr.key_id = eventFile.readLong();
451             if (e.tr.key_id != key)
452             {
453                 throw new RuntimeException JavaDoc("Key does not match database record key value!");
454             }
455             e.tr.identityKey = eventFile.readLong();
456             e.tr.trail_label = readName(eventFile.readInt());
457             e.tr.message_id = readName(eventFile.readInt());
458             e.tr.trail_id = readName(eventFile.readInt());
459             e.tr.event_counter = eventFile.readInt();
460             e.tr.op_name = readName(eventFile.readInt());
461             e.tr.interaction_point = InteractionPoint.from_int(eventFile.readInt());
462             e.tr.parametersKey = eventFile.readLong();
463             e.tr.linkKey = eventFile.readLong();
464             e.tr.thread_id = readName(eventFile.readInt());
465             e.tr.time_stamp = eventFile.readLong();
466
467             eventCash[(int)(e.tr.key_id % CashSize)] = e;
468
469             return e;
470         }
471         catch (Exception JavaDoc ex)
472         {
473             ex.printStackTrace();
474         }
475         throw new RuntimeException JavaDoc("Failed to read event");
476     }
477
478     private long writeIdentity(IdentityDescriptor d)
479     {
480         Long JavaDoc parent = rootKey;
481         Identity id = null;
482
483         Long JavaDoc key = getIdentityKey(parent, d.node_name);
484
485         if (key == null)
486         {
487             id = new Identity();
488             id.kind = IdentityKind.CCM_NODE;
489             id.name = d.node_name;
490             id.type = d.node_ip;
491             id.linkKey = parent.longValue();
492             key = putIdentity(parent, id);
493         }
494         parent = key;
495
496         key = getIdentityKey(parent, d.process_id);
497
498         if (key == null)
499         {
500             id = new Identity();
501             id.kind = IdentityKind.CCM_PROCESS;
502             id.name = d.process_id;
503             id.type = "";
504             id.linkKey = parent.longValue();
505             key = putIdentity(parent, id);
506         }
507
508         parent = key;
509
510         if (!d.cnt_name.equals(""))
511         {
512             key = getIdentityKey(parent, d.cnt_name);
513
514             if (key == null)
515             {
516                 id = new Identity();
517                 id.kind = IdentityKind.CCM_CONTAINER;
518                 id.name = d.cnt_name;
519                 id.type = d.cnt_type;
520                 id.linkKey = parent.longValue();
521                 key = putIdentity(parent, id);
522             }
523
524             parent = key;
525         }
526
527         if (!d.cmp_name.equals(""))
528         {
529             key = getIdentityKey(parent, d.cmp_name);
530
531             if (key == null)
532             {
533                 id = new Identity();
534                 id.kind = IdentityKind.CCM_COMPONENT;
535                 id.name = d.cmp_name;
536                 id.type = d.cmp_type;
537                 id.linkKey = parent.longValue();
538                 key = putIdentity(parent, id);
539             }
540
541             parent = key;
542         }
543
544         key = getIdentityKey(parent, d.object_instance_id);
545
546         if (key == null)
547         {
548             id = new Identity();
549             id.kind = IdentityKind.CCM_OBJECT;
550             id.name = d.object_instance_id;
551             id.type = d.object_repository_id;
552             id.linkKey = parent.longValue();
553             key = putIdentity(parent, id);
554         }
555
556         return key.longValue();
557     }
558
559     private Long JavaDoc getIdentityKey(Long JavaDoc parent, String JavaDoc name)
560     {
561         Identity id = (Identity)identityIndexTable.get(parent);
562
563         if (id == null)
564             throw new RuntimeException JavaDoc("unexpected null entry in identityIndexTable for: " + parent);
565
566         HashMap t = (HashMap)identityTreeTable.get(id);
567
568         if (t == null)
569             return null;
570
571         return (Long JavaDoc)t.get(name);
572     }
573
574     private Long JavaDoc putIdentity(Long JavaDoc parent, Identity id)
575     {
576         Identity parentId = (Identity)identityIndexTable.get(parent);
577
578         if (parentId == null)
579             throw new RuntimeException JavaDoc("unexpected null entry in identityIndexTable for: " + parent);
580
581         // Get the child map for this parent.
582

583         HashMap t = (HashMap)identityTreeTable.get(parentId);
584
585         if (t == null)
586         {
587             // there are no children yet, create the child map
588

589             t = new HashMap();
590
591             identityTreeTable.put(parentId, t);
592         }
593
594         // Get the key if it already exists.
595

596         Long JavaDoc key = (Long JavaDoc)t.get(id.name);
597
598         if (key == null)
599         {
600             // The identity is new, add it to the tree.
601

602             key = new Long JavaDoc(identityCount++);
603
604             identityIndexTable.put(key, id);
605
606             t.put(id.name, key);
607         }
608
609         return key;
610     }
611
612     private void saveIdentities(File f)
613     {
614         try
615         {
616             DataOutputStream identityFile = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
617
618             for (long i = 0; i < identityCount; i++)
619             {
620                 Identity id = (Identity)identityIndexTable.get(new Long JavaDoc(i));
621                 identityFile.writeInt(id.kind.value());
622                 identityFile.writeInt(writeName(id.name));
623                 identityFile.writeInt(writeName(id.type));
624                 identityFile.writeLong(id.linkKey);
625             }
626
627             identityFile.close();
628         }
629         catch (Exception JavaDoc _ex)
630         {
631             _ex.printStackTrace();
632
633             throw new RuntimeException JavaDoc("Failed to write identity");
634         }
635     }
636
637     private void loadIdentities(File f)
638     {
639         try
640         {
641             DataInputStream identityFile = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));
642             identityCount = 0;
643
644             while(identityFile.available() > 0)
645             {
646                 Identity id = new Identity();
647                 id.kind = IdentityKind.from_int(identityFile.readInt());
648                 id.name = readName(identityFile.readInt());
649                 id.type = readName(identityFile.readInt());
650                 id.linkKey = identityFile.readLong();
651                 identityIndexTable.put(new Long JavaDoc(identityCount++), id);
652             }
653             identityFile.close();
654         }
655         catch (Exception JavaDoc _ex)
656         {
657             _ex.printStackTrace();
658             throw new RuntimeException JavaDoc("Failed to read identity");
659         }
660     }
661             
662     public Identity getIdentity(Long JavaDoc key)
663     {
664         return (Identity)identityIndexTable.get(key);
665     }
666
667     private void match(EventRecord r)
668     {
669         try
670         {
671             String JavaDoc messageId = r.getMessageId();
672             // distinguish forward messages from return messages
673

674             EventRecord r2 = (EventRecord)unmatched.get(messageId);
675             if (r2 != null)
676             {
677                 link(r, r2);
678                 unmatched.remove(messageId);
679                 unmatchCount--;
680             }
681             else
682             {
683                 unmatched.put(messageId, r);
684                 unmatchCount++;
685             }
686         }
687         catch(Exception JavaDoc e)
688         {
689             e.printStackTrace();
690         }
691     }
692 }
693
Popular Tags