001: /*****************************************************************************
002: * Copyright (C) PicoContainer Organization. All rights reserved. *
003: * ------------------------------------------------------------------------- *
004: * The software in this package is published under the terms of the BSD *
005: * style license a copy of which has been included with this distribution in *
006: * the LICENSE.txt file. *
007: * *
008: * Original code by Paul Hammant *
009: *****************************************************************************/package org.picocontainer.gems.monitors;
010:
011: import java.lang.reflect.Constructor;
012: import java.util.ArrayList;
013: import java.util.Collections;
014: import java.util.HashSet;
015: import java.util.List;
016: import java.util.Set;
017:
018: import org.picocontainer.ComponentAdapter;
019: import org.picocontainer.ComponentMonitor;
020: import org.picocontainer.PicoContainer;
021: import org.picocontainer.monitors.AbstractComponentMonitor;
022:
023: public final class DotDependencyGraphComponentMonitor extends
024: AbstractComponentMonitor implements ComponentMonitor {
025:
026: /**
027: * Serialization UUID.
028: */
029: private static final long serialVersionUID = 2220639049409365618L;
030:
031: final List<Instantiation> allInstantiated = new ArrayList<Instantiation>();
032:
033: public DotDependencyGraphComponentMonitor(ComponentMonitor delegate) {
034: super (delegate);
035: }
036:
037: public DotDependencyGraphComponentMonitor() {
038: }
039:
040: public <T> void instantiated(PicoContainer container,
041: ComponentAdapter<T> componentAdapter,
042: Constructor<T> constructor, Object instantiated,
043: Object[] injected, long duration) {
044:
045: this .allInstantiated.add(new Instantiation(constructor,
046: instantiated, injected, duration));
047:
048: super .instantiated(container, componentAdapter, constructor,
049: instantiated, injected, duration);
050: }
051:
052: public String getClassDependencyGraph() {
053:
054: Set<String> lines = new HashSet<String>();
055:
056: for (Object anAllInstantiated : allInstantiated) {
057: Instantiation instantiation = (Instantiation) anAllInstantiated;
058: for (int j = 0; j < instantiation.getInjected().length; j++) {
059: Object instantiated = instantiation.getInstantiated();
060: Object injected = instantiation.getInjected()[j];
061: lines.add(" '" + instantiated.getClass().getName()
062: + "' -> '" + injected.getClass().getName()
063: + "';\n");
064: }
065: }
066:
067: return sortLines(lines);
068: }
069:
070: private String sortLines(Set<String> lines) {
071: List<String> list = new ArrayList<String>(lines);
072: Collections.sort(list);
073:
074: String dependencies = "";
075: for (Object aList : list) {
076: String dep = (String) aList;
077: dependencies = dependencies + dep;
078: }
079: return dependencies.replaceAll("'", "\"");
080: }
081:
082: public String getInterfaceDependencyGraph() {
083: Set<String> lines = new HashSet<String>();
084:
085: for (Object anAllInstantiated : allInstantiated) {
086: Instantiation instantiation = (Instantiation) anAllInstantiated;
087: for (int j = 0; j < instantiation.getInjected().length; j++) {
088: Object injected = instantiation.getInjected()[j];
089: Class<?> injectedType = instantiation.getConstructor()
090: .getParameterTypes()[j];
091: Object instantiated = instantiation.getInstantiated();
092: if (injected.getClass() != injectedType) {
093: lines.add(" '" + instantiated.getClass().getName()
094: + "' -> '" + injectedType.getName()
095: + "' [style=dotted,label='needs'];\n");
096: lines
097: .add(" '"
098: + injected.getClass().getName()
099: + "' -> '"
100: + injectedType.getName()
101: + "' [style=dotted, color=red,label='isA'];\n");
102: lines.add(" '" + injectedType.getName()
103: + "' [shape=box, label="
104: + printClassName(injectedType) + "];\n");
105: } else {
106: lines.add(" '" + instantiated.getClass().getName()
107: + "' -> '" + injected.getClass().getName()
108: + "' [label='needs'];\n");
109: }
110: lines.add(" '" + instantiated.getClass().getName()
111: + "' [label="
112: + printClassName(instantiated.getClass())
113: + "];\n");
114:
115: }
116: }
117:
118: return sortLines(lines);
119: }
120:
121: private String printClassName(Class<?> clazz) {
122: String className = clazz.getName();
123: return "'"
124: + className.substring(className.lastIndexOf(".") + 1)
125: + "\\n" + clazz.getPackage().getName() + "'";
126:
127: }
128:
129: private static final class Instantiation {
130: final Constructor<?> constructor;
131: final Object instantiated;
132: final Object[] injected;
133: final long duration;
134:
135: public Instantiation(Constructor<?> constructor,
136: Object instantiated, Object[] injected, long duration) {
137: this .constructor = constructor;
138: this .instantiated = instantiated;
139: this .injected = injected;
140: this .duration = duration;
141: }
142:
143: public Constructor<?> getConstructor() {
144: return constructor;
145: }
146:
147: public Object getInstantiated() {
148: return instantiated;
149: }
150:
151: public Object[] getInjected() {
152: return injected;
153: }
154: }
155: }
|