KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > dso > DSORoot


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.admin.dso;
5
6 import java.beans.PropertyChangeEvent JavaDoc;
7
8 import javax.management.ObjectName JavaDoc;
9
10 import com.tc.admin.AdminClient;
11 import com.tc.admin.ConnectionContext;
12 import com.tc.objectserver.mgmt.ManagedObjectFacade;
13
14 public class DSORoot extends DSOObject {
15   private ObjectName JavaDoc m_bean;
16   private String JavaDoc m_name;
17   private ManagedObjectFacade m_facade;
18   private DSOObject[] m_fields;
19   private String JavaDoc m_label;
20
21   public DSORoot(ConnectionContext cc, ObjectName JavaDoc bean) {
22     super(cc);
23
24     m_bean = bean;
25     m_facade = safeLookupFacade();
26     
27     updateLabel();
28   }
29
30   public Object JavaDoc getFacade() {
31     return m_facade;
32   }
33   
34   protected void updateLabel() {
35     m_label = getName() + " (" + getClassName() + ")";
36
37     if(isCollection()) {
38       m_label += " [" + getFacadeSize() + "/" + getTrueObjectSize() + "]";
39     }
40   }
41   
42   protected ManagedObjectFacade safeLookupFacade() {
43     try {
44       return lookupFacade();
45     } catch(Exception JavaDoc e) {
46       AdminClient.getContext().log(e);
47     }
48
49     return null;
50   }
51   
52   protected ManagedObjectFacade lookupFacade()
53     throws Exception JavaDoc
54   {
55     String JavaDoc op = "lookupFacade";
56     String JavaDoc[] types = new String JavaDoc[]{"int"};
57     Object JavaDoc[] args = new Object JavaDoc[]{new Integer JavaDoc(m_batchSize)};
58
59     return (ManagedObjectFacade)m_cc.invoke(m_bean, op, args, types);
60   }
61
62   public String JavaDoc getName() {
63     if(m_name == null) {
64       try {
65         m_name = m_cc.getStringAttribute(m_bean, "RootName");
66       }
67       catch(Exception JavaDoc e) {
68         AdminClient.getContext().log(e);
69       }
70     }
71
72     return m_name;
73   }
74
75   public String JavaDoc getClassName() {
76     return m_facade != null ? m_facade.getClassName() : null;
77   }
78
79   public boolean isCollection() {
80     return isMap() || isList() || isSet();
81   }
82   
83   public boolean isArray() {
84     return m_facade != null ? m_facade.isArray() : false;
85   }
86
87   public boolean isMap() {
88     return m_facade != null ? m_facade.isMap() : false;
89   }
90
91   public boolean isList() {
92     return m_facade != null ? m_facade.isList() : false;
93   }
94
95   public boolean isSet() {
96     return m_facade != null ? m_facade.isSet() : false;
97   }
98
99   public int getFieldCount() {
100     String JavaDoc[] names = getFieldNames();
101     return names != null ? names.length : 0;
102   }
103
104   public int getFacadeSize() {
105     return m_facade != null ? m_facade.getFacadeSize() : 0;
106   }
107
108   public int getTrueObjectSize() {
109     return m_facade != null ? m_facade.getTrueObjectSize() : 0;
110   }
111
112   public String JavaDoc[] getFieldNames() {
113     return m_facade != null ? m_facade.getFields() : null;
114   }
115
116   public String JavaDoc getFieldName(int index) {
117     String JavaDoc[] names = getFieldNames();
118     return names != null ? names[index] : null;
119   }
120
121   public int getFieldIndex(String JavaDoc fieldName) {
122     String JavaDoc[] names = getFieldNames();
123
124     if(names != null) {
125       for(int i = 0; i < names.length; i++) {
126         if(fieldName.equals(names[i])) {
127           return i;
128         }
129       }
130     }
131
132     return -1;
133   }
134
135   public DSOObject getField(int index) {
136     if(m_fields == null) {
137       m_fields = new DSOObject[getFieldCount()];
138     }
139
140     if(m_fields[index] == null) {
141       m_fields[index] = newField(getFieldName(index));
142     }
143
144     return m_fields[index];
145   }
146
147   public boolean isFieldPrimitive(String JavaDoc fieldName) {
148     return m_facade != null ? m_facade.isPrimitive(fieldName) : true;
149   }
150
151   public String JavaDoc getFieldType(String JavaDoc fieldName) {
152     return m_facade != null ? m_facade.getFieldType(fieldName) : null;
153   }
154
155   public Object JavaDoc getFieldValue(String JavaDoc fieldName) {
156     return m_facade != null ? m_facade.getFieldValue(fieldName) : null;
157   }
158
159   private DSOObject newField(String JavaDoc field) {
160      try {
161        return createField(field, getFieldValue(field));
162      }
163      catch(Exception JavaDoc e) {
164        AdminClient.getContext().log(e);
165      }
166
167      return null;
168   }
169
170   public DSOObject getField(String JavaDoc fieldName) {
171     return getField(getFieldIndex(fieldName));
172   }
173
174   public void refresh() {
175     try {
176       m_facade = lookupFacade();
177       m_fields = null;
178       updateLabel();
179
180       m_changeHelper.firePropertyChange(
181         new PropertyChangeEvent JavaDoc(this, null, null, null));
182     }
183     catch(Exception JavaDoc e) {
184       AdminClient.getContext().log(e);
185     }
186   }
187
188   public ObjectName JavaDoc getObjectName() {
189     return m_bean;
190   }
191
192   public String JavaDoc toString() {
193     return m_label;
194   }
195
196   public void accept(DSOObjectVisitor visitor) {
197     visitor.visitDSORoot(this);
198   }
199 }
200
Popular Tags