001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.admin;
005:
006: import com.tc.admin.common.XTreeNode;
007: import com.tc.config.schema.L2Info;
008:
009: import java.io.IOException;
010:
011: import javax.management.AttributeNotFoundException;
012: import javax.management.InstanceNotFoundException;
013: import javax.management.ListenerNotFoundException;
014: import javax.management.MBeanException;
015: import javax.management.MBeanServerConnection;
016: import javax.management.MalformedObjectNameException;
017: import javax.management.NotificationListener;
018: import javax.management.ObjectInstance;
019: import javax.management.ObjectName;
020: import javax.management.ReflectionException;
021: import javax.management.remote.JMXConnector;
022:
023: public class ConnectionContext {
024: public static final String HOST_PREF_KEY = "host";
025: public static final String PORT_PREF_KEY = "port";
026: public static final String AUTO_CONNECT_PREF_KEY = "auto-connect";
027:
028: public static final String DEFAULT_HOST = "localhost";
029: public static final int DEFAULT_PORT = 9520;
030: public static final boolean DEFAULT_AUTO_CONNECT = false;
031:
032: public static final int DSO_SMALL_BATCH_SIZE = 10;
033: public static final int DSO_MEDIUM_BATCH_SIZE = 100;
034: public static final int DSO_LARGE_BATCH_SIZE = 500;
035: public static final int DSO_MAX_BATCH_SIZE = -1;
036:
037: public String host;
038: public int port;
039: public JMXConnector jmxc;
040: public MBeanServerConnection mbsc;
041: public MBeanHelper mbeanHelper;
042:
043: public ConnectionContext() {
044: this .mbeanHelper = MBeanHelper.getHelper();
045: }
046:
047: public ConnectionContext(String host, int port) {
048: this ();
049:
050: this .host = host;
051: this .port = port;
052: }
053:
054: public ConnectionContext(L2Info l2Info) {
055: this ();
056:
057: this .host = l2Info.host();
058: this .port = l2Info.jmxPort();
059: }
060:
061: public void reset() {
062: if (jmxc != null) {
063: try {
064: jmxc.close();
065: } catch (Exception e) {/**/
066: }
067: }
068:
069: jmxc = null;
070: mbsc = null;
071: }
072:
073: public boolean isConnected() {
074: return mbsc != null;
075: }
076:
077: public void testConnection() throws IOException {
078: if (jmxc != null) {
079: jmxc.getConnectionId();
080: }
081: }
082:
083: public static ConnectionContext getConnectionContext(XTreeNode node) {
084: while (node != null) {
085: if (node instanceof ServerNode) {
086: return ((ServerNode) node).getConnectionContext();
087: }
088:
089: node = (XTreeNode) node.getParent();
090: }
091:
092: return null;
093: }
094:
095: public ObjectInstance queryMBean(String query)
096: throws MalformedObjectNameException, IOException {
097: return mbsc != null ? mbeanHelper.queryMBean(mbsc, query)
098: : null;
099: }
100:
101: public ObjectName queryName(String query)
102: throws MalformedObjectNameException, IOException {
103: return mbsc != null ? mbeanHelper.queryName(mbsc, query) : null;
104: }
105:
106: public ObjectName[] queryNames(String query)
107: throws MalformedObjectNameException, IOException {
108: return mbsc != null ? mbeanHelper.queryNames(mbsc, query)
109: : null;
110: }
111:
112: public Object getAttribute(ObjectName bean, String attrName)
113: throws MBeanException, AttributeNotFoundException,
114: InstanceNotFoundException, ReflectionException, IOException {
115: return mbsc != null ? mbeanHelper.getAttribute(mbsc, bean,
116: attrName) : null;
117: }
118:
119: public String getStringAttribute(ObjectName bean, String attrName)
120: throws MBeanException, AttributeNotFoundException,
121: InstanceNotFoundException, ReflectionException, IOException {
122: return mbsc != null ? mbeanHelper.getStringAttribute(mbsc,
123: bean, attrName) : null;
124: }
125:
126: public boolean getBooleanAttribute(ObjectName bean, String attrName)
127: throws MBeanException, AttributeNotFoundException,
128: InstanceNotFoundException, ReflectionException, IOException {
129: return mbsc != null ? mbeanHelper.getBooleanAttribute(mbsc,
130: bean, attrName) : false;
131: }
132:
133: public long getLongAttribute(ObjectName bean, String attrName)
134: throws MBeanException, AttributeNotFoundException,
135: InstanceNotFoundException, ReflectionException, IOException {
136: return mbsc != null ? mbeanHelper.getLongAttribute(mbsc, bean,
137: attrName) : 0L;
138: }
139:
140: public Object invoke(ObjectName bean, String operation,
141: Object[] args, String[] argTypes)
142: throws InstanceNotFoundException, MBeanException,
143: ReflectionException, IOException {
144: return mbsc != null ? mbeanHelper.invoke(mbsc, bean, operation,
145: args, argTypes) : null;
146: }
147:
148: public void addNotificationListener(ObjectName bean,
149: NotificationListener listener)
150: throws InstanceNotFoundException, IOException {
151: if (mbsc != null) {
152: mbeanHelper.addNotificationListener(mbsc, bean, listener);
153: }
154: }
155:
156: public void removeNotificationListener(ObjectName bean,
157: NotificationListener listener)
158: throws InstanceNotFoundException,
159: ListenerNotFoundException, IOException {
160: if (mbsc != null) {
161: mbeanHelper
162: .removeNotificationListener(mbsc, bean, listener);
163: }
164: }
165:
166: public boolean isRegistered(ObjectName bean) throws IOException {
167: return mbsc != null ? mbeanHelper.isRegistered(mbsc, bean)
168: : false;
169: }
170:
171: public String toString() {
172: return this .host + ":" + this .port;
173: }
174:
175: public boolean equals(Object obj) {
176: if (obj instanceof ConnectionContext) {
177: ConnectionContext other = (ConnectionContext) obj;
178:
179: return (other.host == this .host || other.host != null
180: && other.host.equals(this .host))
181: && other.port == this .port;
182: }
183:
184: return false;
185: }
186: }
|