001: /*
002: * Created on Nov 2, 2003
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.hammurapi.inspectors.metrics.callertrace;
008:
009: import java.io.File;
010: import java.util.Enumeration;
011: import java.util.Hashtable;
012: import java.util.Vector;
013:
014: import org.w3c.dom.Document;
015: import org.w3c.dom.Element;
016:
017: /**
018: * @author mucbj0
019: *
020: * To change the template for this generated type comment go to
021: * Window>Preferences>Java>Code Generation>Code and Comments
022: */
023: public class CallerTraceService {
024: private File projectBaseDir = null;
025: // private static Logger logger = Logger.getLogger(CallerTraceService.class.getName());
026:
027: private Hashtable allClasses = new Hashtable();
028: private Hashtable allInterfacesAndImplementors = new Hashtable();
029: private Vector allInterfaceOperations = new Vector();
030: private MethodMap allInvokedMethodTable = new MethodMap();
031: private MethodMap allMethodsImplemented = new MethodMap();
032:
033: private MethodMap allMethodsImplementedButNotInvoked = new MethodMap();
034:
035: // private AdjacencyMatrix adjacencyMatrix = new AdjacencyMatrix(new DepthFirstSearch( ));
036: private AdjacencyMatrix adjacencyMatrix = new AdjacencyMatrix(
037: new BreadthSearch());
038: private MethodMap allMethods = new MethodMap();
039: private TraceTable traceTable = new TraceTable();
040:
041: public void doIt() {
042: init();
043: traceCaller();
044: return;
045: }
046:
047: public void init() {
048: adjacencyMatrix.setAllMethods(allMethods);
049: }
050:
051: public static boolean doYouPassMethodNameFilter(String methodName) {
052: if (methodName != null) {
053: return true;
054: }
055: return false;
056: }
057:
058: public void traceCaller() {
059:
060: MethodWrapper mi = null;
061: int i = 0;
062: String[] key = {
063: // "java.sql.Statement>>executeQuery(String)",
064: // "java.sql.Connection>>rollback()",
065: // "java.sql.Connection>>commit()"
066:
067: //-- Na Fac Induction
068: /* "ConnectionObject>>exeSelectPreparedStmt",
069: "ConnectionObject>>exeSelectPrepStmt",
070: "ConnectionObject>>exeUpdatePrepStmt",
071:
072: "ConnectionObject>>rollbackDataBase",
073: "ConnectionObject>>commitDataBase",
074: "ConnectionObject>>closeAllStmt",
075: "ConnectionObject>>closeAllStatement",
076: "ConnectionObject>>close",
077: */
078: //NPI
079: // "DocumentDAO>>update",
080: // "ApplicationUserDAO>>update",
081: // "DocumentActionReportDAO>>update",
082: // "DocumentTaskDAO>>update",
083: "java.sql.Connection>>createStatement()",
084: "java.sql.Connection>>close()",
085: "java.sql.Statement>>executeQuery(java.lang.String)",
086: "java.sql.PreparedStatement>>executeQuery(java.lang.String)",
087: "java.sql.Connection>>rollback()",
088: "java.sql.Connection>>commit()",
089: "javax.transaction.UserTransaction>>commit()",
090: "javax.transaction.UserTransaction>>rollback()",
091: "com.inconcert.icjava.IcTask>>acquire()",
092: "com.inconcert.icjava.IcTask>>complete()",
093: "com.inconcert.icjava.IcTask>>release()"
094:
095: //ADB shot
096: // "BaseDao>>doCommit","BaseDao>>doRollback","BaseDao>>doUpdate",
097:
098: /*
099: "ConnectionObject>>updateDataBase","ConnectionObject>>exeUpdatePrepStmt",
100: "ConnectionObject>>deleteDataBase","ConnectionObject>>insertDataBase",
101: "ConnectionObject>>close", "ConnectionObject>>rollbackTransaction", "ConnectionObject>>commitTransaction"
102: */
103:
104: // "Dao>>insert",
105: // "IcClient>>close"
106: // "ukcBeanDataAccess>>updateRecord",
107: // "ukcBeanDataAccess>>insertRecord",
108: // "ukcBeanDataAccess>>deleteRecord",
109: };
110:
111: for (int j = 0; j < key.length; j++) {
112:
113: // System.out.println("Trace Callers for : " + key[j]);
114: adjacencyMatrix.clearAllVisitFlags();
115:
116: boolean isLeaf;
117: Trace trace;
118: // TraceList traceList = depthFirstSearchForTraces(key[j]);
119: TraceList traceList = breadthSearchForTraces(key[j]);
120: traceTable.put(key[j], traceList);
121: }
122: checkForSiblings(key);
123: return;
124: }
125:
126: private void checkForSiblings(String[] key) {
127:
128: for (Enumeration enum = traceTable.elements(); enum.hasMoreElements();) {
129: TraceList traceLst = (TraceList) enum.nextElement();
130:
131: for (Enumeration enumLst = traceLst.elements(); enumLst.hasMoreElements();) {
132: Trace trace = (Trace) enumLst.nextElement();
133:
134: for (Enumeration enumTrc = trace.elements(); enumTrc.hasMoreElements();) {
135: TracedMethod tm = (TracedMethod) enumTrc.nextElement();
136:
137: innerCheckForSiblings(key, tm);
138: }
139: }
140: }
141: }
142:
143: /**
144: * @param key
145: * @param tm
146: */
147: private void innerCheckForSiblings(String[] key, TracedMethod tm) {
148: tm.innerCheckForSiblings(key);
149: }
150:
151: private TraceList breadthSearchForTraces(String key) {
152: TraceList traceList = new TraceList(key);
153:
154: traceList.setEndpointMethod(
155: (MethodWrapper) allMethods.get(key), key);
156: traceList.addAll(this .adjacencyMatrix.getTraceOf(key));
157: // System.out.println(traceList);
158: return traceList;
159: }
160:
161: private TraceList depthFirstSearchForTraces(String key) {
162: TraceList traceList = new TraceList(key);
163:
164: // logger.error(" ** " + (MethodWrapper)allMethods.get(key));
165: traceList.setEndpointMethod(
166: (MethodWrapper) allMethods.get(key), key);
167:
168: boolean isLeaf = false;
169: while (!isLeaf) {
170:
171: Trace trace = new Trace(adjacencyMatrix.getTraceOf(key),
172: this .allMethods);
173: // System.out.println(trace);
174:
175: //-- termination
176: if (trace.size() < 2) {
177: isLeaf = true;
178: } else {
179: traceList.add(trace);
180: }
181: }
182: // System.out.println(traceList);
183: return traceList;
184: }
185:
186: public Element toDom(Document document, Element root) {
187: root.appendChild(traceTable.toDom(document));
188: root.appendChild(allMethodsImplemented.toDom(document));
189: return root;
190: }
191:
192: /**
193: * @return Returns the allMethods.
194: */
195: public MethodMap getAllMethods() {
196: return allMethods;
197: }
198:
199: /**
200: * @return Returns the allMethodsImplemented.
201: */
202: public MethodMap getAllMethodsImplemented() {
203: return allMethodsImplemented;
204: }
205:
206: /**
207: * @return Returns the adjacencyMatrix.
208: */
209: public AdjacencyMatrix getAdjacencyMatrix() {
210: return adjacencyMatrix;
211: }
212:
213: /**
214: * @param adjacencyMatrix The adjacencyMatrix to set.
215: */
216: public void setAdjacencyMatrix(AdjacencyMatrix adjacencyMatrix) {
217: this .adjacencyMatrix = adjacencyMatrix;
218: }
219:
220: /**
221: * @return Returns the allInterfacesAndImplementors.
222: */
223: public Hashtable getAllInterfacesAndImplementors() {
224: return allInterfacesAndImplementors;
225: }
226:
227: /**
228: * @return Returns the allInvokedMethodTable.
229: */
230: public Hashtable getAllInvokedMethodTable() {
231: return allInvokedMethodTable;
232: }
233:
234: /**
235: * @return Returns the allInterfaceOperations.
236: */
237: public Vector getAllInterfaceOperations() {
238: return allInterfaceOperations;
239: }
240:
241: public void resolveInvokeesImplementors() {
242:
243: Enumeration enumAdj = getAdjacencyMatrix().getAllKeys();
244: MethodMap mp = getAllMethodsImplemented();
245: // System.out.println( mp );
246: while (enumAdj.hasMoreElements()) {
247: EdgeImpl edge = (EdgeImpl) enumAdj.nextElement();
248: // System.out.println( edge );
249: String leftHandKey = (String) edge.getNodeA();
250: String rightHandKey = (String) edge.getNodeA();
251: // System.out.println( leftHandKey );
252: MethodWrapperDeclaration mwd = (MethodWrapperDeclaration) mp
253: .get(leftHandKey);
254: if (mwd == null) {
255: mwd = (MethodWrapperDeclaration) mp
256: .selectMethodsBothWithoutHashkey(leftHandKey);
257: if (mwd != null) {
258: // System.out.println( "Found: " + mwd.getMethodKey());
259: //!! delete the original one ..
260: mwd.afferentMethodCoupling++;
261: mwd.setCalled();
262: // System.out.println("Ma " + mwd.afferentMethodCoupling );
263: edge.setNodeA(mwd.getMethodKey());
264: // this.callerTraceService.getAdjacencyMatrix().put(mwd.getMethodKey(), rightHandKey );
265: } else {
266: //-- keep the exisitin edge, because no implemetor is known
267: }
268: }
269: }
270: }
271: }
|