KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > gems > monitors > DotDependencyGraphComponentMonitor


1 /*****************************************************************************
2  * Copyright (C) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by Paul Hammant *
9  *****************************************************************************/

10
11 package org.picocontainer.gems.monitors;
12
13 import org.picocontainer.ComponentMonitor;
14 import org.picocontainer.defaults.DelegatingComponentMonitor;
15
16 import java.lang.reflect.Constructor JavaDoc;
17 import java.util.*;
18
19 public class DotDependencyGraphComponentMonitor extends DelegatingComponentMonitor implements ComponentMonitor {
20
21     ArrayList allInstantiated = new ArrayList();
22
23     public DotDependencyGraphComponentMonitor(ComponentMonitor delegate) {
24         super(delegate);
25     }
26
27     public DotDependencyGraphComponentMonitor() {
28     }
29
30     public void instantiated(Constructor JavaDoc constructor, Object JavaDoc instantiated, Object JavaDoc[] injected, long duration) {
31
32         this.allInstantiated.add(new Instantiation(constructor, instantiated, injected, duration));
33
34         super.instantiated(constructor, instantiated, injected, duration);
35     }
36
37
38     public String JavaDoc getClassDependencyGraph() {
39
40         HashSet lines = new HashSet();
41
42         for (int i = 0; i < allInstantiated.size(); i++) {
43             Instantiation instantiation = (Instantiation) allInstantiated.get(i);
44             for (int j = 0; j < instantiation.getInjected().length; j++) {
45                 Object JavaDoc instantiated = instantiation.getInstantiated();
46                 Object JavaDoc injected = instantiation.getInjected()[j];
47                 lines.add(" '" + instantiated.getClass().getName() + "' -> '" + injected.getClass().getName() + "';\n");
48             }
49         }
50
51         return sortLines(lines);
52     }
53
54     private String JavaDoc sortLines(HashSet lines) {
55         ArrayList list = new ArrayList(lines);
56         Collections.sort(list);
57
58         String JavaDoc dependencies = "";
59         for (Iterator iterator = list.iterator(); iterator.hasNext();) {
60             String JavaDoc dep = (String JavaDoc) iterator.next();
61             dependencies = dependencies + dep;
62         }
63         return dependencies.replaceAll("'","\"");
64     }
65
66     public String JavaDoc getInterfaceDependencyGraph() {
67         HashSet lines = new HashSet();
68
69         for (int i = 0; i < allInstantiated.size(); i++) {
70             Instantiation instantiation = (Instantiation) allInstantiated.get(i);
71             for (int j = 0; j < instantiation.getInjected().length; j++) {
72                 Object JavaDoc injected = instantiation.getInjected()[j];
73                 Class JavaDoc injectedType = instantiation.getConstructor().getParameterTypes()[j];
74                 Object JavaDoc instantiated = instantiation.getInstantiated();
75                 if (injected.getClass() != injectedType) {
76                     lines.add(" '" + instantiated.getClass().getName() + "' -> '" + injectedType.getName() + "' [style=dotted,label='needs'];\n");
77                     lines.add(" '" + injected.getClass().getName() + "' -> '" + injectedType.getName() + "' [style=dotted, color=red,label='isA'];\n");
78                     lines.add(" '" + injectedType.getName() + "' [shape=box, label=" + printClassName(injectedType) + "];\n");
79                 } else {
80                     lines.add(" '" + instantiated.getClass().getName() + "' -> '" + injected.getClass().getName() + "' [label='needs'];\n");
81                 }
82                 lines.add(" '" + instantiated.getClass().getName() + "' [label=" + printClassName(instantiated.getClass()) + "];\n");
83
84             }
85         }
86
87         return sortLines(lines);
88     }
89
90     private String JavaDoc printClassName(Class JavaDoc clazz) {
91         String JavaDoc className = clazz.getName();
92         return "'" + className.substring(className.lastIndexOf(".")+1) + "\\n" + clazz.getPackage().getName() + "'";
93
94     }
95
96     private static class Instantiation {
97         Constructor JavaDoc constructor;
98         Object JavaDoc instantiated;
99         Object JavaDoc[] injected;
100         long duration;
101         public Instantiation(Constructor JavaDoc constructor, Object JavaDoc instantiated, Object JavaDoc[] injected, long duration) {
102             this.constructor = constructor;
103             this.instantiated = instantiated;
104             this.injected = injected;
105             this.duration = duration;
106         }
107
108         public Constructor JavaDoc getConstructor() {
109             return constructor;
110         }
111
112         public Object JavaDoc getInstantiated() {
113             return instantiated;
114         }
115         public Object JavaDoc[] getInjected() {
116             return injected;
117         }
118     }
119 }
120
Popular Tags