1 4 package com.tc.admin; 5 6 import com.tc.admin.common.XTreeNode; 7 import com.tc.config.schema.L2Info; 8 9 import java.io.IOException ; 10 11 import javax.management.AttributeNotFoundException ; 12 import javax.management.InstanceNotFoundException ; 13 import javax.management.ListenerNotFoundException ; 14 import javax.management.MBeanException ; 15 import javax.management.MBeanServerConnection ; 16 import javax.management.MalformedObjectNameException ; 17 import javax.management.NotificationListener ; 18 import javax.management.ObjectInstance ; 19 import javax.management.ObjectName ; 20 import javax.management.ReflectionException ; 21 import javax.management.remote.JMXConnector ; 22 23 public class ConnectionContext { 24 public static final String HOST_PREF_KEY = "host"; 25 public static final String PORT_PREF_KEY = "port"; 26 public static final String AUTO_CONNECT_PREF_KEY = "auto-connect"; 27 28 public static final String DEFAULT_HOST = "localhost"; 29 public static final int DEFAULT_PORT = 9520; 30 public static final boolean DEFAULT_AUTO_CONNECT = false; 31 32 public static final int DSO_SMALL_BATCH_SIZE = 10; 33 public static final int DSO_MEDIUM_BATCH_SIZE = 100; 34 public static final int DSO_LARGE_BATCH_SIZE = 500; 35 public static final int DSO_MAX_BATCH_SIZE = -1; 36 37 public String host; 38 public int port; 39 public JMXConnector jmxc; 40 public MBeanServerConnection mbsc; 41 public MBeanHelper mbeanHelper; 42 43 public ConnectionContext() { 44 this.mbeanHelper = MBeanHelper.getHelper(); 45 } 46 47 public ConnectionContext(String host, int port) { 48 this(); 49 50 this.host = host; 51 this.port = port; 52 } 53 54 public ConnectionContext(L2Info l2Info) { 55 this(); 56 57 this.host = l2Info.host(); 58 this.port = l2Info.jmxPort(); 59 } 60 61 public void reset() { 62 if(jmxc != null) { 63 try { 64 jmxc.close(); 65 } catch(Exception e) {} 66 } 67 68 jmxc = null; 69 mbsc = null; 70 } 71 72 public boolean isConnected() { 73 return mbsc != null; 74 } 75 76 public void testConnection() throws IOException { 77 if(jmxc != null) { 78 jmxc.getConnectionId(); 79 } 80 } 81 82 public static ConnectionContext getConnectionContext(XTreeNode node) { 83 while(node != null) { 84 if(node instanceof ServerNode) { 85 return ((ServerNode)node).getConnectionContext(); 86 } 87 88 node = (XTreeNode)node.getParent(); 89 } 90 91 return null; 92 } 93 94 public ObjectInstance queryMBean(String query) 95 throws MalformedObjectNameException , 96 IOException 97 { 98 return mbsc != null ? mbeanHelper.queryMBean(mbsc, query) : null; 99 } 100 101 public ObjectName queryName(String query) 102 throws MalformedObjectNameException , 103 IOException 104 { 105 return mbsc != null ? mbeanHelper.queryName(mbsc, query) : null; 106 } 107 108 public ObjectName [] queryNames(String query) 109 throws MalformedObjectNameException , 110 IOException 111 { 112 return mbsc != null ? mbeanHelper.queryNames(mbsc, query) : null; 113 } 114 115 public Object getAttribute(ObjectName bean, String attrName) 116 throws MBeanException , 117 AttributeNotFoundException , 118 InstanceNotFoundException , 119 ReflectionException , 120 IOException 121 { 122 return mbsc != null ? mbeanHelper.getAttribute(mbsc, bean, attrName) : null; 123 } 124 125 public String getStringAttribute(ObjectName bean, String attrName) 126 throws MBeanException , 127 AttributeNotFoundException , 128 InstanceNotFoundException , 129 ReflectionException , 130 IOException 131 { 132 return mbsc != null ? mbeanHelper.getStringAttribute(mbsc, bean, attrName) : null; 133 } 134 135 public boolean getBooleanAttribute(ObjectName bean, String attrName) 136 throws MBeanException , 137 AttributeNotFoundException , 138 InstanceNotFoundException , 139 ReflectionException , 140 IOException 141 { 142 return mbsc != null ? mbeanHelper.getBooleanAttribute(mbsc, bean, attrName) : false; 143 } 144 145 public long getLongAttribute(ObjectName bean, String attrName) 146 throws MBeanException , 147 AttributeNotFoundException , 148 InstanceNotFoundException , 149 ReflectionException , 150 IOException 151 { 152 return mbsc != null ? mbeanHelper.getLongAttribute(mbsc, bean, attrName) : 0L; 153 } 154 155 public Object invoke(ObjectName bean, String operation, Object [] args, String [] argTypes) 156 throws InstanceNotFoundException , 157 MBeanException , 158 ReflectionException , 159 IOException 160 { 161 return mbsc != null ? mbeanHelper.invoke(mbsc, bean, operation, args, argTypes): null; 162 } 163 164 public void addNotificationListener(ObjectName bean, NotificationListener listener) 165 throws InstanceNotFoundException , 166 IOException 167 { 168 if(mbsc != null) { 169 mbeanHelper.addNotificationListener(mbsc, bean, listener); 170 } 171 } 172 173 public void removeNotificationListener(ObjectName bean, NotificationListener listener) 174 throws InstanceNotFoundException , 175 ListenerNotFoundException , 176 IOException 177 { 178 if(mbsc != null) { 179 mbeanHelper.removeNotificationListener(mbsc, bean, listener); 180 } 181 } 182 183 public boolean isRegistered(ObjectName bean) throws IOException { 184 return mbsc != null ? mbeanHelper.isRegistered(mbsc, bean) : false; 185 } 186 187 public String toString() { 188 return this.host+":"+this.port; 189 } 190 191 public boolean equals(Object obj) { 192 if(obj instanceof ConnectionContext) { 193 ConnectionContext other = (ConnectionContext)obj; 194 195 return (other.host == this.host || 196 other.host != null && other.host.equals(this.host)) && 197 other.port == this.port; 198 } 199 200 return false; 201 } 202 } 203 | Popular Tags |