001: /*
002: * Copyright (c) 2006-2007 Terracotta, Inc. All rights reserved.
003: */
004:
005: package com.tc.admin;
006:
007: import com.tc.admin.dso.DSOHelper;
008: import com.tc.admin.dso.DSORoot;
009: import com.tc.admin.dso.RootsHelper;
010: import com.tc.management.JMXConnectorProxy;
011: import com.tc.object.ObjectID;
012: import com.tc.objectserver.mgmt.ManagedObjectFacade;
013: import com.tc.objectserver.mgmt.MapEntryFacade;
014:
015: import java.io.IOException;
016: import java.io.PrintWriter;
017: import java.util.HashSet;
018: import java.util.Set;
019:
020: import javax.management.ObjectName;
021: import javax.management.remote.JMXConnector;
022:
023: /**
024: * Utility to dump distributed object graph from the server
025: *
026: * @author Eugene Kuleshov
027: */
028: public class RootsTool {
029: private ConnectionContext context;
030: private JMXConnector jmxc;
031:
032: public RootsTool(String host, int port) throws Exception {
033: context = new ConnectionContext(host, port);
034: }
035:
036: private void connect() throws IOException {
037: jmxc = new JMXConnectorProxy(context.host, context.port);
038:
039: context.jmxc = jmxc;
040: context.mbsc = jmxc.getMBeanServerConnection();
041: }
042:
043: private void disconnect() throws IOException {
044: jmxc.close();
045: }
046:
047: private void print(PrintWriter w) throws Exception {
048: connect();
049: try {
050: DSORoot[] roots = RootsHelper.getHelper().getRoots(context);
051: for (int i = 0; i < roots.length; i++) {
052: DSORoot root = roots[i];
053: ObjectName objectName = root.getObjectName();
054: String rootId = objectName.getKeyProperty("rootID");
055: w.println(i + " " + root + " id=" + rootId);
056:
057: String[] fieldNames = root.getFieldNames();
058: for (int j = 0; j < fieldNames.length; j++) {
059: String fieldName = fieldNames[i];
060: Object fieldValue = root.getFieldValue(fieldName);
061: printValue(w, " ", fieldName, root
062: .getFieldType(fieldName), fieldValue,
063: new HashSet());
064: }
065: }
066: } finally {
067: disconnect();
068: }
069: }
070:
071: private void printValue(PrintWriter w, String off, String name,
072: String type, Object value, Set seen) throws Exception {
073: ObjectID objectId;
074: ManagedObjectFacade facade;
075: if (value instanceof ObjectID) {
076: objectId = (ObjectID) value;
077: if (objectId.isNull()) {
078: w.println(off + " " + name + " = null");
079: return;
080: }
081: facade = DSOHelper.getHelper().lookupFacade(context,
082: objectId, 10);
083: if (facade.isArray()) {
084: int arrayLength = facade.getArrayLength();
085: if (arrayLength > 0 && facade.isPrimitive("0")) {
086: StringBuffer sb = new StringBuffer(" {");
087: for (int i = 0; i < arrayLength; i++) {
088: if (i > 0)
089: sb.append(", ");
090: sb.append("" + facade.getFieldValue("" + i));
091: }
092: sb.append("}");
093: w.println(off + "- " + name + " ("
094: + facade.getClassName() + ") "
095: + sb.toString() + " " + objectId.toLong());
096: return;
097: }
098: }
099: w.println(off + " " + name + " (" + facade.getClassName()
100: + ") " + objectId.toLong() + " "
101: + getSeen(seen, objectId));
102:
103: } else if (value instanceof ManagedObjectFacade) {
104: facade = (ManagedObjectFacade) value;
105: objectId = facade.getObjectId();
106: w.println(off + " " + name + " (" + facade.getClassName()
107: + ") " + objectId.toLong() + " "
108: + getSeen(seen, objectId));
109:
110: } else if (value instanceof MapEntryFacade) {
111: MapEntryFacade entry = (MapEntryFacade) value;
112: Object entryKey = entry.getKey();
113: Object entryValue = entry.getValue();
114: if (entryKey instanceof ObjectID) {
115: w.println(off + "ENTRY");
116: printValue(w, off + " ", "key", null, entryKey, seen);
117: printValue(w, off + " ", "value", null, entryValue,
118: seen);
119: } else {
120: w.println(off + " ENTRY key = " + entryKey);
121: printValue(w, off + " ", "value", null, entryValue,
122: seen);
123: }
124: return;
125:
126: } else {
127: w.println(off + "- " + name + " = " + value + " (" + type
128: + ")");
129: return;
130:
131: }
132:
133: if (!seen.contains(objectId)) {
134: seen.add(objectId);
135: String[] fields = facade.getFields();
136: for (int k = 0; k < fields.length; k++) {
137: String fieldName = fields[k];
138: String fieldType = facade.getFieldType(fieldName);
139: Object fieldValue = facade.getFieldValue(fieldName);
140: printValue(w, off + " ", getShortFieldName(fieldName),
141: fieldType, fieldValue, seen);
142: }
143: }
144: }
145:
146: private String getSeen(Set seen, ObjectID objectId) {
147: return seen.contains(objectId) ? "[SEEN BEFORE]" : "";
148: }
149:
150: private static String getShortFieldName(String fieldName) {
151: int n = fieldName.lastIndexOf('.');
152: return n == -1 ? fieldName : fieldName.substring(n + 1);
153: }
154:
155: public static void main(String[] args) throws Exception {
156: PrintWriter w = new PrintWriter(System.out);
157:
158: String host;
159: int port;
160: if (args.length > 1) {
161: host = args[0];
162: port = Integer.parseInt(args[1]);
163: } else {
164: host = "localhost";
165: port = 9520;
166: }
167:
168: RootsTool rootsTool = new RootsTool(host, port);
169: rootsTool.print(w);
170:
171: w.flush();
172: w.close();
173: }
174:
175: }
|