KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
10
11 import javax.management.AttributeNotFoundException JavaDoc;
12 import javax.management.InstanceNotFoundException JavaDoc;
13 import javax.management.ListenerNotFoundException JavaDoc;
14 import javax.management.MBeanException JavaDoc;
15 import javax.management.MBeanServerConnection JavaDoc;
16 import javax.management.MalformedObjectNameException JavaDoc;
17 import javax.management.NotificationListener JavaDoc;
18 import javax.management.ObjectInstance JavaDoc;
19 import javax.management.ObjectName JavaDoc;
20 import javax.management.ReflectionException JavaDoc;
21 import javax.management.remote.JMXConnector JavaDoc;
22
23 public class ConnectionContext {
24   public static final String JavaDoc HOST_PREF_KEY = "host";
25   public static final String JavaDoc PORT_PREF_KEY = "port";
26   public static final String JavaDoc AUTO_CONNECT_PREF_KEY = "auto-connect";
27
28   public static final String JavaDoc 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 JavaDoc host;
38   public int port;
39   public JMXConnector JavaDoc jmxc;
40   public MBeanServerConnection JavaDoc mbsc;
41   public MBeanHelper mbeanHelper;
42   
43   public ConnectionContext() {
44     this.mbeanHelper = MBeanHelper.getHelper();
45   }
46
47   public ConnectionContext(String JavaDoc 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 JavaDoc 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 JavaDoc {
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 JavaDoc queryMBean(String JavaDoc query)
95     throws MalformedObjectNameException JavaDoc,
96            IOException JavaDoc
97   {
98     return mbsc != null ? mbeanHelper.queryMBean(mbsc, query) : null;
99   }
100
101   public ObjectName JavaDoc queryName(String JavaDoc query)
102     throws MalformedObjectNameException JavaDoc,
103            IOException JavaDoc
104   {
105     return mbsc != null ? mbeanHelper.queryName(mbsc, query) : null;
106   }
107
108   public ObjectName JavaDoc[] queryNames(String JavaDoc query)
109     throws MalformedObjectNameException JavaDoc,
110            IOException JavaDoc
111   {
112     return mbsc != null ? mbeanHelper.queryNames(mbsc, query) : null;
113   }
114
115   public Object JavaDoc getAttribute(ObjectName JavaDoc bean, String JavaDoc attrName)
116     throws MBeanException JavaDoc,
117            AttributeNotFoundException JavaDoc,
118            InstanceNotFoundException JavaDoc,
119            ReflectionException JavaDoc,
120            IOException JavaDoc
121   {
122     return mbsc != null ? mbeanHelper.getAttribute(mbsc, bean, attrName) : null;
123   }
124
125   public String JavaDoc getStringAttribute(ObjectName JavaDoc bean, String JavaDoc attrName)
126     throws MBeanException JavaDoc,
127            AttributeNotFoundException JavaDoc,
128            InstanceNotFoundException JavaDoc,
129            ReflectionException JavaDoc,
130            IOException JavaDoc
131   {
132     return mbsc != null ? mbeanHelper.getStringAttribute(mbsc, bean, attrName) : null;
133   }
134
135   public boolean getBooleanAttribute(ObjectName JavaDoc bean, String JavaDoc attrName)
136     throws MBeanException JavaDoc,
137            AttributeNotFoundException JavaDoc,
138            InstanceNotFoundException JavaDoc,
139            ReflectionException JavaDoc,
140            IOException JavaDoc
141   {
142     return mbsc != null ? mbeanHelper.getBooleanAttribute(mbsc, bean, attrName) : false;
143   }
144
145   public long getLongAttribute(ObjectName JavaDoc bean, String JavaDoc attrName)
146     throws MBeanException JavaDoc,
147            AttributeNotFoundException JavaDoc,
148            InstanceNotFoundException JavaDoc,
149            ReflectionException JavaDoc,
150            IOException JavaDoc
151   {
152     return mbsc != null ? mbeanHelper.getLongAttribute(mbsc, bean, attrName) : 0L;
153   }
154
155   public Object JavaDoc invoke(ObjectName JavaDoc bean, String JavaDoc operation, Object JavaDoc[] args, String JavaDoc[] argTypes)
156     throws InstanceNotFoundException JavaDoc,
157              MBeanException JavaDoc,
158              ReflectionException JavaDoc,
159              IOException JavaDoc
160   {
161     return mbsc != null ? mbeanHelper.invoke(mbsc, bean, operation, args, argTypes): null;
162   }
163
164   public void addNotificationListener(ObjectName JavaDoc bean, NotificationListener JavaDoc listener)
165     throws InstanceNotFoundException JavaDoc,
166              IOException JavaDoc
167   {
168     if(mbsc != null) {
169       mbeanHelper.addNotificationListener(mbsc, bean, listener);
170     }
171   }
172
173   public void removeNotificationListener(ObjectName JavaDoc bean, NotificationListener JavaDoc listener)
174     throws InstanceNotFoundException JavaDoc,
175              ListenerNotFoundException JavaDoc,
176              IOException JavaDoc
177   {
178     if(mbsc != null) {
179       mbeanHelper.removeNotificationListener(mbsc, bean, listener);
180     }
181   }
182
183   public boolean isRegistered(ObjectName JavaDoc bean) throws IOException JavaDoc {
184     return mbsc != null ? mbeanHelper.isRegistered(mbsc, bean) : false;
185   }
186   
187   public String JavaDoc toString() {
188     return this.host+":"+this.port;
189   }
190   
191   public boolean equals(Object JavaDoc 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