KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > RootsTool


1 /*
2  * Copyright (c) 2006-2007 Terracotta, Inc. All rights reserved.
3  */

4
5 package com.tc.admin;
6
7 import com.tc.admin.dso.DSOHelper;
8 import com.tc.admin.dso.DSORoot;
9 import com.tc.admin.dso.RootsHelper;
10 import com.tc.object.ObjectID;
11 import com.tc.objectserver.mgmt.ManagedObjectFacade;
12 import com.tc.objectserver.mgmt.MapEntryFacade;
13
14 import java.io.IOException JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import javax.management.ObjectName JavaDoc;
22 import javax.management.remote.JMXConnector JavaDoc;
23 import javax.management.remote.JMXConnectorFactory JavaDoc;
24 import javax.management.remote.JMXServiceURL JavaDoc;
25
26 /**
27  * Utility to dump distributed object graph from the server
28  *
29  * @author Eugene Kuleshov
30  */

31 public class RootsTool {
32   private ConnectionContext context;
33   private JMXConnector JavaDoc jmxc;
34
35   public RootsTool(String JavaDoc host, int port) throws Exception JavaDoc {
36     context = new ConnectionContext(host, port);
37   }
38
39   private void connect() throws IOException JavaDoc {
40     // String url = "service:jmx:jmxmp://" + context.host + ":" + context.port;
41
String JavaDoc url = "service:jmx:rmi:///jndi/rmi://" + context.host + ":" + context.port + "/jmxrmi";
42     JMXServiceURL JavaDoc service = new JMXServiceURL JavaDoc(url);
43
44     Map JavaDoc env = new HashMap JavaDoc();
45     jmxc = JMXConnectorFactory.newJMXConnector(service, env);
46     jmxc.connect();
47
48     context.jmxc = jmxc;
49     context.mbsc = jmxc.getMBeanServerConnection();
50   }
51
52   private void disconnect() throws IOException JavaDoc {
53     jmxc.close();
54   }
55
56   private void print(PrintWriter JavaDoc w) throws Exception JavaDoc {
57     connect();
58     try {
59       DSORoot[] roots = RootsHelper.getHelper().getRoots(context);
60       for (int i = 0; i < roots.length; i++) {
61         DSORoot root = roots[i];
62         ObjectName JavaDoc objectName = root.getObjectName();
63         String JavaDoc rootId = objectName.getKeyProperty("rootID");
64         w.println(i + " " + root + " id=" + rootId);
65
66         String JavaDoc[] fieldNames = root.getFieldNames();
67         for (int j = 0; j < fieldNames.length; j++) {
68           String JavaDoc fieldName = fieldNames[i];
69           Object JavaDoc fieldValue = root.getFieldValue(fieldName);
70           printValue(w, " ", fieldName, root.getFieldType(fieldName), fieldValue, new HashSet JavaDoc());
71         }
72       }
73     } finally {
74       disconnect();
75     }
76   }
77
78   private void printValue(PrintWriter JavaDoc w, String JavaDoc off, String JavaDoc name, String JavaDoc type, Object JavaDoc value, Set JavaDoc seen) throws Exception JavaDoc {
79     ObjectID objectId;
80     ManagedObjectFacade facade;
81     if (value instanceof ObjectID) {
82       objectId = (ObjectID) value;
83       if (objectId.isNull()) {
84         w.println(off + " " + name + " = null");
85         return;
86       }
87       facade = DSOHelper.getHelper().lookupFacade(context, objectId, 10);
88       if (facade.isArray()) {
89         int arrayLength = facade.getArrayLength();
90         if (arrayLength > 0 && facade.isPrimitive("0")) {
91           StringBuffer JavaDoc sb = new StringBuffer JavaDoc(" {");
92           for (int i = 0; i < arrayLength; i++) {
93             if (i > 0) sb.append(", ");
94             sb.append("" + facade.getFieldValue("" + i));
95           }
96           sb.append("}");
97           w.println(off + "- " + name + " (" + facade.getClassName() + ") " + sb.toString() + " " + objectId.toLong());
98           return;
99         }
100       }
101       w.println(off + " " + name + " (" + facade.getClassName() + ") " + objectId.toLong() + " "
102                 + getSeen(seen, objectId));
103
104     } else if (value instanceof ManagedObjectFacade) {
105       facade = (ManagedObjectFacade) value;
106       objectId = facade.getObjectId();
107       w.println(off + " " + name + " (" + facade.getClassName() + ") " + objectId.toLong() + " "
108                 + getSeen(seen, objectId));
109
110     } else if (value instanceof MapEntryFacade) {
111       MapEntryFacade entry = (MapEntryFacade) value;
112       Object JavaDoc entryKey = entry.getKey();
113       Object JavaDoc 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, seen);
118       } else {
119         w.println(off + " ENTRY key = " + entryKey);
120         printValue(w, off + " ", "value", null, entryValue, seen);
121       }
122       return;
123
124     } else {
125       w.println(off + "- " + name + " = " + value + " (" + type + ")");
126       return;
127
128     }
129
130     if (!seen.contains(objectId)) {
131       seen.add(objectId);
132       String JavaDoc[] fields = facade.getFields();
133       for (int k = 0; k < fields.length; k++) {
134         String JavaDoc fieldName = fields[k];
135         String JavaDoc fieldType = facade.getFieldType(fieldName);
136         Object JavaDoc fieldValue = facade.getFieldValue(fieldName);
137         printValue(w, off + " ", getShortFieldName(fieldName), fieldType, fieldValue, seen);
138       }
139     }
140   }
141
142   private String JavaDoc getSeen(Set JavaDoc seen, ObjectID objectId) {
143     return seen.contains(objectId) ? "[SEEN BEFORE]" : "";
144   }
145
146   private static String JavaDoc getShortFieldName(String JavaDoc fieldName) {
147     int n = fieldName.lastIndexOf('.');
148     return n == -1 ? fieldName : fieldName.substring(n + 1);
149   }
150
151   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
152     PrintWriter JavaDoc w = new PrintWriter JavaDoc(System.out);
153
154     String JavaDoc host;
155     int port;
156     if (args.length > 1) {
157       host = args[0];
158       port = Integer.parseInt(args[1]);
159     } else {
160       host = "localhost";
161       port = 9520;
162     }
163
164     RootsTool rootsTool = new RootsTool(host, port);
165     rootsTool.print(w);
166
167     w.flush();
168     w.close();
169   }
170
171 }
172
Popular Tags