KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > tracing > Recorder


1 /*
2   Copyright (C) 2001 Renaud Pawlak
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.tracing;
19
20 import org.objectweb.jac.core.*;
21 import java.util.*;
22 import org.objectweb.jac.core.rtti.ClassRepository;
23
24 /**
25  * This class is able to record a set of methods call and replay them.
26  */

27
28 public class Recorder {
29
30    static Recorder recorder;
31    Vector calls = new Vector();
32    Vector newObjectsClasses = new Vector();
33    Hashtable newObjectsIndexes = new Hashtable();
34    boolean recording = false;
35    int newObjectsCount = 0;
36
37    public void printRecordState() {
38       Iterator it = calls.iterator();
39       while( it.hasNext() ) {
40          Object JavaDoc[] call = (Object JavaDoc[])it.next();
41          System.out.println("- "+call[0]+"."+call[1]+
42                             Arrays.asList((Object JavaDoc[])call[2]));
43       }
44    }
45
46    public static Recorder get() {
47       return recorder;
48    }
49
50    public boolean isRecording() {
51       return recording;
52    }
53
54    public void start() {
55       calls.clear();
56       newObjectsClasses.clear();
57       newObjectsIndexes.clear();
58       newObjectsCount = 0;
59       recording = true;
60    }
61
62    public void stop() {
63       recording = false;
64    }
65
66    public Vector getCalls() {
67       return calls;
68    }
69
70    public Vector getNewObjectsClasses() {
71       return newObjectsClasses;
72    }
73
74    public void replay( Vector newObjectsClasses, Vector calls ) {
75       if( recording ) return;
76       // create the objects that have been instanciated
77
// during the record
78
Vector createdObjects = new Vector();
79       Iterator it = newObjectsClasses.iterator();
80       while( it.hasNext() ) {
81          Class JavaDoc newObjectClass = (Class JavaDoc)it.next();
82          try {
83             Object JavaDoc o = newObjectClass.newInstance();
84             createdObjects.add(o);
85             System.out.println("REPLAY: creating new object "+o);
86          } catch( Exception JavaDoc e ) {
87             System.out.println("FATAL ERROR: replay failed!!");
88             e.printStackTrace();
89             return;
90          }
91       }
92       // invoke the same method that have been called during
93
// the record
94
it = calls.iterator();
95       while( it.hasNext() ) {
96          Object JavaDoc[] call = (Object JavaDoc[])it.next();
97          Wrappee o;
98          if( call[0] instanceof Integer JavaDoc ) {
99             // resolve a created object
100
o = (Wrappee)createdObjects.get( ((Integer JavaDoc)call[0]).intValue() );
101          } else {
102             // resolve a pre-exisisting object
103
o = (Wrappee)NameRepository.get().getObject( (String JavaDoc)call[0] );
104          }
105          System.out.println("REPLAY: calling recorded "+o+"."+
106                             (String JavaDoc)call[1]+Arrays.asList((Object JavaDoc[])call[2]));
107          ClassRepository.get().getClass(o).getMethod((String JavaDoc)call[1]).invoke(
108             o, (Object JavaDoc[])call[2] );
109       }
110    }
111
112    public void recordMethodCall(Object JavaDoc o, String JavaDoc methodName, Object JavaDoc[] args) {
113       if( isNewObject(o) ) {
114          calls.add( new Object JavaDoc[] { newObjectsIndexes.get(o),
115                                    methodName, args } );
116       } else {
117          calls.add( new Object JavaDoc[] { NameRepository.get().getName(o),
118                                    methodName, args } );
119       }
120    }
121
122    public void recordNewObject(Object JavaDoc o) {
123       newObjectsClasses.add(o.getClass());
124       newObjectsIndexes.put(o,new Integer JavaDoc(newObjectsCount++));
125    }
126
127    boolean isNewObject(Object JavaDoc o) {
128       if( newObjectsIndexes.get(o) == null ) {
129          return false;
130       } else {
131          return true;
132       }
133    }
134
135 }
136
137
138    
139
140
141
142
143
144
Popular Tags