1 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 ; 15 import java.io.PrintWriter ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 import javax.management.ObjectName ; 22 import javax.management.remote.JMXConnector ; 23 import javax.management.remote.JMXConnectorFactory ; 24 import javax.management.remote.JMXServiceURL ; 25 26 31 public class RootsTool { 32 private ConnectionContext context; 33 private JMXConnector jmxc; 34 35 public RootsTool(String host, int port) throws Exception { 36 context = new ConnectionContext(host, port); 37 } 38 39 private void connect() throws IOException { 40 String url = "service:jmx:rmi:///jndi/rmi://" + context.host + ":" + context.port + "/jmxrmi"; 42 JMXServiceURL service = new JMXServiceURL (url); 43 44 Map env = new HashMap (); 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 { 53 jmxc.close(); 54 } 55 56 private void print(PrintWriter w) throws Exception { 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 objectName = root.getObjectName(); 63 String rootId = objectName.getKeyProperty("rootID"); 64 w.println(i + " " + root + " id=" + rootId); 65 66 String [] fieldNames = root.getFieldNames(); 67 for (int j = 0; j < fieldNames.length; j++) { 68 String fieldName = fieldNames[i]; 69 Object fieldValue = root.getFieldValue(fieldName); 70 printValue(w, " ", fieldName, root.getFieldType(fieldName), fieldValue, new HashSet ()); 71 } 72 } 73 } finally { 74 disconnect(); 75 } 76 } 77 78 private void printValue(PrintWriter w, String off, String name, String type, Object value, Set seen) throws Exception { 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 sb = new StringBuffer (" {"); 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 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, 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 [] fields = facade.getFields(); 133 for (int k = 0; k < fields.length; k++) { 134 String fieldName = fields[k]; 135 String fieldType = facade.getFieldType(fieldName); 136 Object fieldValue = facade.getFieldValue(fieldName); 137 printValue(w, off + " ", getShortFieldName(fieldName), fieldType, fieldValue, seen); 138 } 139 } 140 } 141 142 private String getSeen(Set seen, ObjectID objectId) { 143 return seen.contains(objectId) ? "[SEEN BEFORE]" : ""; 144 } 145 146 private static String getShortFieldName(String fieldName) { 147 int n = fieldName.lastIndexOf('.'); 148 return n == -1 ? fieldName : fieldName.substring(n + 1); 149 } 150 151 public static void main(String [] args) throws Exception { 152 PrintWriter w = new PrintWriter (System.out); 153 154 String 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 |