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.ConnectionContext;
007: import com.tc.object.ObjectID;
008: import com.tc.objectserver.mgmt.ManagedObjectFacade;
009: import com.tc.objectserver.mgmt.MapEntryFacade;
010:
011: import java.beans.PropertyChangeListener;
012: import java.beans.PropertyChangeSupport;
013:
014: public abstract class DSOObject {
015: protected ConnectionContext m_cc;
016: protected DSOObject m_parent;
017: protected PropertyChangeSupport m_changeHelper;
018: protected int m_batchSize;
019:
020: public DSOObject(ConnectionContext cc) {
021: m_cc = cc;
022: m_changeHelper = new PropertyChangeSupport(this );
023: m_batchSize = ConnectionContext.DSO_SMALL_BATCH_SIZE;
024: }
025:
026: public DSOObject(ConnectionContext cc, DSOObject parent) {
027: this (cc);
028: m_parent = parent;
029: }
030:
031: public abstract Object getFacade();
032:
033: public abstract void accept(DSOObjectVisitor visitor);
034:
035: public DSOObject getParent() {
036: return m_parent;
037: }
038:
039: public void setBatchSize(int batchSize) {
040: m_batchSize = batchSize;
041: }
042:
043: public abstract String getName();
044:
045: public DSORoot getRoot() {
046: DSOObject obj = this ;
047:
048: while (obj != null) {
049: if (obj.getParent() == null) {
050: return (DSORoot) obj;
051: }
052: obj = obj.getParent();
053: }
054:
055: return null;
056: }
057:
058: protected DSOObject createField(String fieldName, Object value,
059: String type) throws Exception {
060: DSOObject result = null;
061:
062: if (value instanceof MapEntryFacade) {
063: MapEntryFacade mef = (MapEntryFacade) value;
064: result = new DSOMapEntryField(m_cc, fieldName, mef, this );
065: } else if (value instanceof ObjectID) {
066: ObjectID id = (ObjectID) value;
067:
068: if (!id.isNull()) {
069: value = DSOHelper.getHelper().lookupFacade(m_cc, id,
070: m_batchSize);
071: type = ((ManagedObjectFacade) value).getClassName();
072: } else {
073: value = null;
074: type = null;
075: }
076:
077: result = new DSOField(m_cc, fieldName, false,
078: convertTypeName(type), value, this );
079: } else {
080: result = new DSOField(m_cc, fieldName, true,
081: convertTypeName(type), value, this );
082: }
083:
084: return result;
085: }
086:
087: private static final char C_ARRAY = '[';
088:
089: public static int getArrayCount(char[] typeSignature)
090: throws IllegalArgumentException {
091: try {
092: int count = 0;
093: while (typeSignature[count] == C_ARRAY) {
094: ++count;
095: }
096: return count;
097: } catch (ArrayIndexOutOfBoundsException e) { // signature is syntactically incorrect if last character is C_ARRAY
098: throw new IllegalArgumentException();
099: }
100: }
101:
102: static String convertTypeName(String typeName) {
103: if (typeName != null && typeName.length() > 0) {
104: if (typeName.charAt(0) == C_ARRAY) {
105: try {
106: int arrayCount = getArrayCount(typeName
107: .toCharArray());
108: typeName = typeName.substring(arrayCount);
109: if (typeName.charAt(0) == 'L') {
110: int pos = 1;
111: while (typeName.charAt(pos) != ';')
112: pos++;
113: typeName = typeName.substring(1, pos);
114: } else {
115: typeName = nativeTypeFor(typeName.charAt(0));
116: }
117: StringBuffer sb = new StringBuffer(typeName);
118: for (int i = 0; i < arrayCount; i++) {
119: sb.append("[]");
120: }
121: typeName = sb.toString();
122: } catch (IllegalArgumentException iae) {/**/
123: }
124: } else if (typeName.length() == 1) {
125: typeName = nativeTypeFor(typeName.charAt(0));
126: }
127: }
128: return typeName;
129: }
130:
131: private static String nativeTypeFor(char c) {
132: switch (c) {
133: case 'Z':
134: return "boolean";
135: case 'I':
136: return "int";
137: case 'F':
138: return "float";
139: case 'C':
140: return "char";
141: case 'D':
142: return "double";
143: }
144: return String.valueOf(c);
145: }
146:
147: public void addPropertyChangeListener(
148: PropertyChangeListener listener) {
149: m_changeHelper.addPropertyChangeListener(listener);
150: }
151:
152: public void removePropertyChangeListener(
153: PropertyChangeListener listener) {
154: m_changeHelper.removePropertyChangeListener(listener);
155: }
156: }
|