01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.planning.ldm.asset;
28:
29: import java.beans.BeanInfo;
30: import java.beans.Introspector;
31: import java.beans.PropertyDescriptor;
32: import java.lang.reflect.Method;
33: import java.util.Enumeration;
34: import java.util.Vector;
35:
36: import org.cougaar.core.util.PropertyNameValue;
37:
38: public class AssetIntrospection {
39:
40: public static Vector fetchAllProperties(Asset asset) {
41: Vector propertyNameValues = new Vector(10);
42:
43: try {
44: // get all properties of asset
45: BeanInfo info = Introspector.getBeanInfo(asset.getClass());
46: PropertyDescriptor[] pds = info.getPropertyDescriptors();
47: for (int i = 0; i < pds.length; i++) {
48: PropertyDescriptor pd = pds[i];
49: Method readMethod = pd.getReadMethod();
50: try {
51: Object result = readMethod.invoke(asset, null);
52: if (result != null) {
53: // System.out.println("Property: " + pd.getName() +
54: // " Value: " + result.toString() +
55: // " Class: " + result.getClass().toString());
56: propertyNameValues.add(new PropertyNameValue(pd
57: .getName(), result));
58: }
59: } catch (Exception e) {
60: System.out
61: .println("Asset introspection invocation exception: "
62: + e.toString());
63: }
64: }
65:
66: // get all dynamic properties of asset
67: Enumeration dynamicProperties = asset.getOtherProperties();
68: while (dynamicProperties.hasMoreElements()) {
69: Object dynamicProperty = dynamicProperties
70: .nextElement();
71: // System.out.println("Adding dynamic property: " +
72: // dynamicProperty.toString() +
73: // " Value: " + dynamicProperty.toString() +
74: // " Class: " + dynamicProperty.getClass().toString());
75: propertyNameValues.add(new PropertyNameValue(
76: prettyName(dynamicProperty.getClass()
77: .toString()), dynamicProperty));
78: }
79:
80: } catch (Exception e) {
81: System.out.println("Asset introspection exception: "
82: + e.toString());
83: }
84: return propertyNameValues;
85: }
86:
87: // Return the last field of a fully qualified name.
88: // If the input string contains an "@" then it's assumed
89: // that the fully qualified name preceeds it.
90:
91: private static String prettyName(String s) {
92: int i = s.indexOf("@");
93: if (i != -1)
94: s = s.substring(0, i);
95: return (s.substring(s.lastIndexOf(".") + 1));
96: }
97:
98: }
|