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 *
009: *****************************************************************************/package org.picocontainer.gems.monitors;
010:
011: import java.lang.reflect.Constructor;
012:
013: import org.picocontainer.monitors.AbstractComponentMonitor;
014: import org.picocontainer.gems.monitors.prefuse.ComponentDependencyListener;
015: import org.picocontainer.ComponentAdapter;
016: import org.picocontainer.PicoContainer;
017:
018: /**
019: * Understands how to capture component dependency information from
020: * picocontainer.
021: *
022: * @author Peter Barry
023: * @author Kent R. Spillner
024: */
025: public final class ComponentDependencyMonitor extends
026: AbstractComponentMonitor {
027:
028: /**
029: * Serialization UUID.
030: */
031: private static final long serialVersionUID = 9104656171384560235L;
032:
033: private final ComponentDependencyListener listener;
034:
035: public ComponentDependencyMonitor(
036: ComponentDependencyListener listener) {
037: this .listener = listener;
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: Class<?> componentType = instantiated.getClass();
045: int count = injected.length;
046:
047: if (count == 0) {
048: listener.addDependency(new Dependency(componentType, null));
049: }
050:
051: for (int i = 0; i < count; i++) {
052: Object dependent = injected[i];
053: Dependency dependency = new Dependency(componentType,
054: dependent.getClass());
055: listener.addDependency(dependency);
056: }
057: }
058:
059: /**
060: * Understands which other classes are required to instantiate a component.
061: *
062: * @author Peter Barry
063: * @author Kent R. Spillner
064: */
065: public static final class Dependency {
066:
067: private final Class<?> componentType;
068:
069: private final Class<?> dependencyType;
070:
071: public Dependency(Class<?> componentType,
072: Class<?> dependencyType) {
073: this .componentType = componentType;
074: this .dependencyType = dependencyType;
075: }
076:
077: public boolean dependsOn(Class<?> type) {
078: return (type != null) && type.equals(dependencyType);
079: }
080:
081: public boolean equals(Object other) {
082: if (other instanceof Dependency) {
083: Dependency otherDependency = (Dependency) other;
084: return areEqualOrNull(componentType,
085: otherDependency.componentType)
086: && areEqualOrNull(dependencyType,
087: otherDependency.dependencyType);
088: }
089: return false;
090: }
091:
092: public Class<?> getComponentType() {
093: return componentType;
094: }
095:
096: public Class<?> getDependencyType() {
097: return dependencyType;
098: }
099:
100: public String toString() {
101: return componentType + " depends on " + dependencyType;
102: }
103:
104: private static boolean areEqualOrNull(Class<?> type,
105: Class<?> otherType) {
106: if (type != null) {
107: return type.equals(otherType);
108: }
109: return (otherType == null);
110: }
111: }
112: }
|