001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.admin.dso;
005:
006: import com.tc.admin.AdminClient;
007: import com.tc.admin.ConnectionContext;
008: import com.tc.object.LiteralValues;
009: import com.tc.object.dna.impl.EnumInstance;
010: import com.tc.objectserver.mgmt.ManagedObjectFacade;
011:
012: import java.beans.PropertyChangeEvent;
013:
014: import javax.management.ObjectName;
015:
016: public class DSORoot extends DSOObject {
017: private ObjectName m_bean;
018: private String m_name;
019: private ManagedObjectFacade m_facade;
020: private DSOObject[] m_fields;
021: private String m_label;
022: private boolean m_isLiteral;
023:
024: private final static LiteralValues m_literals = new LiteralValues();
025:
026: public DSORoot(ConnectionContext cc, ObjectName bean) {
027: super (cc);
028:
029: m_bean = bean;
030: m_facade = safeLookupFacade();
031: m_isLiteral = isLiteral();
032:
033: updateLabel();
034: }
035:
036: public Object getFacade() {
037: return m_facade;
038: }
039:
040: protected void updateLabel() {
041: m_label = getName() + " (" + convertTypeName(getClassName())
042: + ")";
043:
044: if (isCollection()) {
045: m_label += " [" + getFacadeSize() + "/"
046: + getTrueObjectSize() + "]";
047: } else if (m_isLiteral) {
048: String[] fieldNames = getFieldNames();
049:
050: if (fieldNames != null && fieldNames.length == 1) {
051: Object value = getFieldValue(fieldNames[0]);
052:
053: if (value instanceof EnumInstance) {
054: EnumInstance enumInstance = (EnumInstance) value;
055: String enumType = enumInstance.getClassInstance()
056: .getName().asString();
057: m_label = getName() + " (" + enumType + ")" + "="
058: + enumInstance;
059: } else {
060: m_label += "=" + value;
061: }
062: }
063: }
064: }
065:
066: protected ManagedObjectFacade safeLookupFacade() {
067: try {
068: return lookupFacade();
069: } catch (Exception e) {
070: AdminClient.getContext().log(e);
071: }
072:
073: return null;
074: }
075:
076: protected ManagedObjectFacade lookupFacade() throws Exception {
077: String op = "lookupFacade";
078: String[] types = new String[] { "int" };
079: Object[] args = new Object[] { new Integer(m_batchSize) };
080:
081: return (ManagedObjectFacade) m_cc.invoke(m_bean, op, args,
082: types);
083: }
084:
085: public boolean isLiteral() {
086: String className = getClassName();
087: return className != null ? m_literals.isLiteral(className)
088: || className.equals("java.util.Date") : false;
089: }
090:
091: public String getName() {
092: if (m_name == null) {
093: try {
094: m_name = m_cc.getStringAttribute(m_bean, "RootName");
095: } catch (Exception e) {
096: AdminClient.getContext().log(e);
097: }
098: }
099:
100: return m_name;
101: }
102:
103: public String getClassName() {
104: return m_facade != null ? m_facade.getClassName() : null;
105: }
106:
107: public boolean isCollection() {
108: return !m_isLiteral && (isMap() || isList() || isSet());
109: }
110:
111: public boolean isArray() {
112: return m_facade != null ? m_facade.isArray() : false;
113: }
114:
115: public boolean isMap() {
116: return m_facade != null ? m_facade.isMap() : false;
117: }
118:
119: public boolean isList() {
120: return m_facade != null ? m_facade.isList() : false;
121: }
122:
123: public boolean isSet() {
124: return m_facade != null ? m_facade.isSet() : false;
125: }
126:
127: public int getFieldCount() {
128: if (m_isLiteral) {
129: return 0;
130: } else {
131: String[] names = getFieldNames();
132: return names != null ? names.length : 0;
133: }
134: }
135:
136: public int getFacadeSize() {
137: if (m_isLiteral) {
138: return 0;
139: } else {
140: return m_facade != null ? m_facade.getFacadeSize() : 0;
141: }
142: }
143:
144: public int getTrueObjectSize() {
145: if (m_isLiteral) {
146: return 0;
147: } else {
148: return m_facade != null ? m_facade.getTrueObjectSize() : 0;
149: }
150: }
151:
152: public String[] getFieldNames() {
153: return m_facade != null ? m_facade.getFields() : null;
154: }
155:
156: public String getFieldName(int index) {
157: String[] names = getFieldNames();
158: return names != null ? names[index] : null;
159: }
160:
161: public int getFieldIndex(String fieldName) {
162: String[] names = getFieldNames();
163:
164: if (names != null) {
165: for (int i = 0; i < names.length; i++) {
166: if (fieldName.equals(names[i])) {
167: return i;
168: }
169: }
170: }
171:
172: return -1;
173: }
174:
175: public DSOObject getField(int index) {
176: if (m_fields == null) {
177: m_fields = new DSOObject[getFieldCount()];
178: }
179:
180: if (m_fields[index] == null) {
181: m_fields[index] = newField(getFieldName(index));
182: }
183:
184: return m_fields[index];
185: }
186:
187: public boolean isFieldPrimitive(String fieldName) {
188: return m_facade != null ? m_facade.isPrimitive(fieldName)
189: : true;
190: }
191:
192: public String getFieldType(String fieldName) {
193: return m_facade != null ? m_facade.getFieldType(fieldName)
194: : null;
195: }
196:
197: public Object getFieldValue(String fieldName) {
198: return m_facade != null ? m_facade.getFieldValue(fieldName)
199: : null;
200: }
201:
202: private DSOObject newField(String field) {
203: try {
204: return createField(field, getFieldValue(field),
205: getFieldType(field));
206: } catch (Exception e) {
207: AdminClient.getContext().log(e);
208: }
209:
210: return null;
211: }
212:
213: public DSOObject getField(String fieldName) {
214: return getField(getFieldIndex(fieldName));
215: }
216:
217: public void refresh() {
218: try {
219: m_facade = lookupFacade();
220: m_fields = null;
221: updateLabel();
222:
223: m_changeHelper.firePropertyChange(new PropertyChangeEvent(
224: this , null, null, null));
225: } catch (Exception e) {
226: AdminClient.getContext().log(e);
227: }
228: }
229:
230: public ObjectName getObjectName() {
231: return m_bean;
232: }
233:
234: public String toString() {
235: return m_label;
236: }
237:
238: public void accept(DSOObjectVisitor visitor) {
239: visitor.visitDSORoot(this);
240: }
241: }
|