001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.monitoring.client;
006:
007: import javax.management.*;
008: import javax.management.remote.JMXConnector;
009: import java.io.IOException;
010: import java.util.List;
011: import java.util.Set;
012: import java.util.ArrayList;
013: import java.lang.reflect.Method;
014:
015: public class SunClientImpl extends ClientImpl {
016: public SunClientImpl(String defaultDomain,
017: List immortalMBeanObjectNames,
018: MBeanServerConnection mBeanServerConnection,
019: JMXConnector jmxConnector) {
020: setDefaultDomain(defaultDomain);
021: setImmortalMBeanObjectNames(immortalMBeanObjectNames);
022: this .mBeanServerConnection = mBeanServerConnection;
023: this .jmxConnector = jmxConnector;
024: }
025:
026: public void addNotificationListener(ObjectName on,
027: NotificationListener listener, NotificationFilter filter,
028: Object handback) throws InstanceNotFoundException,
029: IOException {
030: if (mBeanServerConnection != null) {
031: mBeanServerConnection.addNotificationListener(on, listener,
032: filter, handback);
033: }
034: }
035:
036: public Object getAttribute(ObjectName on, String attrName)
037: throws ReflectionException, InstanceNotFoundException,
038: IOException, MBeanException, AttributeNotFoundException {
039: if (mBeanServerConnection != null) {
040: return mBeanServerConnection.getAttribute(on, attrName);
041: }
042:
043: return null;
044: }
045:
046: public AttributeList getAttributes(ObjectName on, String[] attrNames)
047: throws ReflectionException, InstanceNotFoundException,
048: IOException {
049: if (mBeanServerConnection != null) {
050: return mBeanServerConnection.getAttributes(on, attrNames);
051: }
052:
053: return null;
054: }
055:
056: public Integer getMBeanCount() throws IOException {
057: if (mBeanServerConnection != null) {
058: return mBeanServerConnection.getMBeanCount();
059: }
060:
061: return new Integer(0);
062: }
063:
064: public MBeanInfo getMBeanInfo(ObjectName on)
065: throws ReflectionException, InstanceNotFoundException,
066: IOException, IntrospectionException {
067: if (mBeanServerConnection != null) {
068: return mBeanServerConnection.getMBeanInfo(on);
069: }
070:
071: return null;
072: }
073:
074: public Object invoke(ObjectName on, String operationName,
075: Object[] params, String[] signature)
076: throws ReflectionException, InstanceNotFoundException,
077: IOException, MBeanException {
078: if (mBeanServerConnection != null) {
079: return mBeanServerConnection.invoke(on, operationName,
080: params, signature);
081: }
082:
083: return null;
084: }
085:
086: public Boolean isInstanceOf(ObjectName on, String className)
087: throws InstanceNotFoundException, IOException {
088: if (mBeanServerConnection != null) {
089: return mBeanServerConnection.isInstanceOf(on, className) ? Boolean.TRUE
090: : Boolean.FALSE;
091: }
092:
093: return Boolean.FALSE;
094: }
095:
096: public Boolean isRegistered(ObjectName on) throws IOException {
097: if (mBeanServerConnection != null) {
098: return mBeanServerConnection.isRegistered(on) ? Boolean.TRUE
099: : Boolean.FALSE;
100: }
101:
102: return Boolean.FALSE;
103: }
104:
105: public Set queryNames(ObjectName pattern, QueryExp expr)
106: throws IOException {
107: if (mBeanServerConnection != null) {
108: return mBeanServerConnection.queryNames(pattern, expr);
109: }
110:
111: return null;
112: }
113:
114: public void removeNotificationListener(ObjectName on,
115: NotificationListener listener)
116: throws InstanceNotFoundException,
117: ListenerNotFoundException, IOException {
118: if (mBeanServerConnection != null) {
119: mBeanServerConnection.removeNotificationListener(on,
120: listener);
121: }
122: }
123:
124: public void setAttribute(ObjectName on, Attribute attr)
125: throws ReflectionException, InstanceNotFoundException,
126: IOException, MBeanException, AttributeNotFoundException,
127: InvalidAttributeValueException {
128: if (mBeanServerConnection != null) {
129: mBeanServerConnection.setAttribute(on, attr);
130: }
131: }
132:
133: public AttributeList setAttributes(ObjectName on,
134: AttributeList attrs) throws ReflectionException,
135: InstanceNotFoundException, IOException {
136: if (mBeanServerConnection != null) {
137: return mBeanServerConnection.setAttributes(on, attrs);
138: }
139:
140: return null;
141: }
142:
143: public void unregisterMBean(ObjectName on)
144: throws MBeanRegistrationException,
145: InstanceNotFoundException, IOException {
146: if (!getImmortalMBeanObjectNames().contains(on)) {
147: if (mBeanServerConnection != null) {
148: mBeanServerConnection.unregisterMBean(on);
149: }
150: }
151: }
152:
153: public void disconnect() throws IOException {
154: if (mBeanServerConnection != null) {
155: jmxConnector.close();
156: }
157:
158: mBeanServerConnection = null;
159: jmxConnector = null;
160: }
161:
162: private MBeanServerConnection mBeanServerConnection;
163: private JMXConnector jmxConnector;
164:
165: public SunClientImpl() {
166: }
167:
168: public void setMBeanServerConnection(
169: MBeanServerConnection mBeanServerConnection) {
170: this .mBeanServerConnection = mBeanServerConnection;
171: }
172:
173: public void setJMXConnector(JMXConnector jmxConnector) {
174: this .jmxConnector = jmxConnector;
175: }
176:
177: private static final String METHOD_GET_DEFAULT_DOMAIN = "getDefaultDomain";
178: private static final String ATTRIBUTE_DEFAULT_DOMAIN = "DefaultDomain";
179: private static final String METHOD_GET_MBEAN_COUNT = "getMBeanCount";
180: private static final String ATTRIBUTE_MBEAN_COUNT = "MBeanCount";
181:
182: private String getDefaultDomain(Object proxy)
183: throws ReflectionException, IOException,
184: InstanceNotFoundException, MBeanException,
185: AttributeNotFoundException {
186: return (String) mBeanServerConnection.getAttribute(
187: objectNameProxy, ATTRIBUTE_DEFAULT_DOMAIN);
188: }
189:
190: private Integer getMBeanCount(Object proxy)
191: throws ReflectionException, IOException,
192: InstanceNotFoundException, MBeanException,
193: AttributeNotFoundException {
194: return (Integer) mBeanServerConnection.getAttribute(
195: objectNameProxy, ATTRIBUTE_MBEAN_COUNT);
196: }
197:
198: public Object invoke(Object proxy, Method method, Object[] params)
199: throws Throwable {
200: String methodName = method.getName();
201: if (methodName.equals(METHOD_GET_DEFAULT_DOMAIN)) {
202: return getDefaultDomain(null);
203: } else if (methodName.equals(METHOD_GET_MBEAN_COUNT)) {
204: return getMBeanCount(null);
205: }
206:
207: List signature = new ArrayList();
208:
209: Class[] paramTypeClasses = method.getParameterTypes();
210: if (paramTypeClasses != null) {
211: if (paramTypeClasses.length != 0) {
212: for (int i = 0; i < paramTypeClasses.length; i++) {
213: signature.add(paramTypeClasses[i].getName());
214: }
215: }
216: }
217: String[] paramTypes = (signature.size() == 0) ? null
218: : ((String[]) signature.toArray(new String[signature
219: .size()]));
220:
221: return mBeanServerConnection.invoke(objectNameProxy, method
222: .getName(), params, paramTypes);
223: }
224: }
|