001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.admin.dso;
006:
007: import org.apache.xmlbeans.XmlOptions;
008: import org.dijon.ContainerResource;
009:
010: import com.tc.admin.AdminClient;
011: import com.tc.admin.AdminClientContext;
012: import com.tc.admin.ConnectionContext;
013: import com.tc.admin.common.XAbstractAction;
014: import com.tc.admin.common.XContainer;
015: import com.tc.admin.common.XTextArea;
016: import com.tc.admin.common.XTree;
017: import com.tc.object.LiteralValues;
018: import com.tc.stats.DSOClassInfo;
019: import com.terracottatech.config.InstrumentedClasses;
020: import com.terracottatech.config.TcConfigDocument;
021: import com.terracottatech.config.TcConfigDocument.TcConfig;
022:
023: import java.awt.event.ActionEvent;
024: import java.awt.event.KeyEvent;
025: import java.util.ArrayList;
026: import java.util.HashMap;
027: import java.util.Iterator;
028:
029: import javax.swing.KeyStroke;
030:
031: public class ClassesPanel extends XContainer {
032: private ConnectionContext m_cc;
033: private ClassesTable m_table;
034: private XTree m_tree;
035: private ClassesTreeMap m_treeMap;
036: private XTextArea m_configText;
037: private static XmlOptions m_xmlOpts;
038:
039: private static final String REFRESH = "Refresh";
040:
041: private static final String[] IGNORE_CLASS_LIST = { "com.tcclient",
042: "java." };
043:
044: private static LiteralValues LITERALS = new LiteralValues();
045:
046: static {
047: m_xmlOpts = new XmlOptions();
048: m_xmlOpts.setSavePrettyPrint();
049: m_xmlOpts.setSavePrettyPrintIndent(2);
050: }
051:
052: public ClassesPanel(ConnectionContext cc) {
053: super ();
054:
055: AdminClientContext cntx = AdminClient.getContext();
056:
057: load((ContainerResource) cntx.topRes
058: .getComponent("ClassesPanel"));
059:
060: m_cc = cc;
061:
062: DSOClassInfo[] classInfo = getClassInfos();
063:
064: m_table = (ClassesTable) findComponent("ClassTable");
065: m_table.setClassInfo(classInfo);
066:
067: m_tree = (XTree) findComponent("ClassTree");
068: m_tree.setShowsRootHandles(true);
069: m_tree.setModel(new ClassTreeModel(classInfo));
070:
071: m_treeMap = (ClassesTreeMap) findComponent("ClassesTreeMap");
072: m_treeMap.setModel((ClassTreeModel) m_tree.getModel());
073:
074: m_configText = (XTextArea) findComponent("ClassesConfigTextArea");
075: updateConfigText();
076:
077: KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0, true);
078: getActionMap().put(REFRESH, new RefreshAction());
079: getInputMap().put(ks, REFRESH);
080: }
081:
082: private DSOClassInfo[] getClassInfos() {
083: DSOClassInfo[] classInfo = ClassesHelper.getHelper()
084: .getClassInfo(m_cc);
085: ArrayList<DSOClassInfo> list = new ArrayList<DSOClassInfo>();
086:
087: for (DSOClassInfo info : classInfo) {
088: String className = info.getClassName();
089: if (className.startsWith("com.tcclient"))
090: continue;
091: if (className.startsWith("[")) {
092: int i = 0;
093: while (className.charAt(i) == '[')
094: i++;
095: if (className.charAt(i) == 'L') {
096: className = className.substring(i + 1, className
097: .length() - 1);
098: } else {
099: switch (className.charAt(i)) {
100: case 'Z':
101: className = "boolean";
102: break;
103: case 'I':
104: className = "int";
105: break;
106: case 'F':
107: className = "float";
108: break;
109: case 'C':
110: className = "char";
111: break;
112: case 'D':
113: className = "double";
114: break;
115: }
116: }
117: StringBuffer sb = new StringBuffer(className);
118: for (int j = 0; j < i; j++)
119: sb.append("[]");
120: className = sb.toString();
121: }
122: list.add(new DSOClassInfo(className, info
123: .getInstanceCount()));
124: }
125:
126: return list.toArray(new DSOClassInfo[0]);
127: }
128:
129: private void updateConfigText() {
130: DSOClassInfo[] classInfo = ((ClassTreeModel) m_tree.getModel())
131: .getClassInfo();
132: HashMap<String, String> map = new HashMap<String, String>();
133: for (DSOClassInfo info : classInfo) {
134: String className = info.getClassName();
135: int brace = className.indexOf("[");
136: if (brace != -1) {
137: className = className.substring(0, brace);
138: }
139: map.put(className, className);
140: }
141: InstrumentedClasses instrumentedClasses = InstrumentedClasses.Factory
142: .newInstance();
143: Iterator<String> iter = map.keySet().iterator();
144: while (iter.hasNext()) {
145: String className = iter.next();
146: if (ignoreClass(className)) {
147: continue;
148: }
149: instrumentedClasses.addNewInclude().setClassExpression(
150: className);
151: }
152: TcConfigDocument configDoc = TcConfigDocument.Factory
153: .newInstance();
154: TcConfig config = configDoc.addNewTcConfig();
155: config.addNewApplication().addNewDso().setInstrumentedClasses(
156: instrumentedClasses);
157: m_configText.setText(config.xmlText(m_xmlOpts));
158: }
159:
160: private boolean ignoreClass(String className) {
161: if (LITERALS.isLiteral(className)) {
162: return true;
163: }
164: for (String pattern : IGNORE_CLASS_LIST) {
165: if (className.startsWith(pattern))
166: return true;
167: }
168: return false;
169: }
170:
171: public class RefreshAction extends XAbstractAction {
172: public void actionPerformed(ActionEvent ae) {
173: refresh();
174: }
175: }
176:
177: public void refresh() {
178: AdminClientContext acc = AdminClient.getContext();
179:
180: acc.controller.setStatus(acc
181: .getMessage("dso.classes.refreshing"));
182: acc.controller.block();
183:
184: DSOClassInfo[] classInfo = getClassInfos();
185: m_table.setClassInfo(classInfo);
186: ((ClassTreeModel) m_tree.getModel()).setClassInfo(classInfo);
187: m_treeMap.setModel((ClassTreeModel) m_tree.getModel());
188: updateConfigText();
189:
190: acc.controller.clearStatus();
191: acc.controller.unblock();
192: }
193: }
|