001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)JMXConnectionImpl.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.ui.client;
030:
031: import com.sun.jbi.ui.common.I18NBundle;
032: import com.sun.jbi.ui.common.JBIRemoteException;
033: import com.sun.jbi.ui.common.JBIResultXmlBuilder;
034: import com.sun.jbi.ui.common.JMXConnectionException;
035:
036: import java.io.IOException;
037: import java.util.Properties;
038: import javax.management.Attribute;
039: import javax.management.InstanceNotFoundException;
040: import javax.management.MBeanException;
041: import javax.management.MBeanServerConnection;
042: import javax.management.ObjectName;
043: import javax.management.ReflectionException;
044: import javax.management.RuntimeMBeanException;
045: import javax.management.RuntimeOperationsException;
046:
047: /**
048: * This class is a base class to implement the jmx connection interface.
049: * apis.
050: * @author Sun Microsystems, Inc.
051: */
052:
053: public abstract class JMXConnectionImpl implements JMXConnection {
054: /** i18n */
055: private static I18NBundle sI18NBundle = null;
056:
057: /** connection to Mbeanserver */
058: protected MBeanServerConnection mMBeanServerConnection;
059:
060: /**
061: * base implementation constructor
062: */
063: public JMXConnectionImpl() {
064: this .mMBeanServerConnection = null;
065: }
066:
067: /** gives the I18N bundle
068: *@return I18NBundle object
069: */
070: protected static I18NBundle getI18NBundle() {
071: // lazzy initialize the JBI Client
072: if (sI18NBundle == null) {
073: sI18NBundle = new I18NBundle("com.sun.jbi.ui.client");
074: }
075: return sI18NBundle;
076: }
077:
078: /**
079: * returns mbean server connection.
080: * @throws IllegalStateException on error
081: * @return mbeanserver interface
082: */
083: public MBeanServerConnection getMBeanServerConnection()
084: throws IllegalStateException {
085: if (this .mMBeanServerConnection == null) {
086: throw new IllegalStateException(JBIResultXmlBuilder
087: .createFailedJbiResultXml(getI18NBundle(),
088: "jbi.ui.jmx.connection.not.open", null));
089: }
090: return this .mMBeanServerConnection;
091: }
092:
093: /**
094: * invokes operation
095: * @param name name fo mbean
096: * @param operationName operation name
097: * @param params parameters
098: * @param signature parameter signatures
099: * @throws JMXConnectionException on error
100: * @throws JBIRemoteException on error
101: * @return result object
102: */
103: public Object invokeMBeanOperation(ObjectName name,
104: String operationName, Object[] params, String[] signature)
105: throws JMXConnectionException, JBIRemoteException {
106: try {
107: Object resultObject = getMBeanServerConnection().invoke(
108: name, operationName, params, signature);
109: return resultObject;
110: } catch (InstanceNotFoundException notFoundEx) {
111: // pass null to the cause as you have already serialized the cause to jbi mgmt xml
112: throw new JMXConnectionException(
113: JBIResultXmlBuilder.createJbiResultXml(
114: getI18NBundle(), "jbi.ui.jmx.invoke.error",
115: null, notFoundEx), null);
116: } catch (ReflectionException rEx) {
117: // pass null to the cause as you have already serialized the cause to jbi mgmt xml
118: throw new JMXConnectionException(JBIResultXmlBuilder
119: .createJbiResultXml(getI18NBundle(),
120: "jbi.ui.jmx.invoke.error", null, rEx), null);
121: } catch (IOException ioEx) {
122: // pass null to the cause as you have already serialized the cause to jbi mgmt xml
123: throw new JMXConnectionException(JBIResultXmlBuilder
124: .createJbiResultXml(getI18NBundle(),
125: "jbi.ui.jmx.invoke.error", null, ioEx),
126: null);
127: } catch (MBeanException mbeanEx) {
128: throw JBIRemoteException.filterJmxExceptions(mbeanEx);
129: } catch (RuntimeMBeanException rtEx) {
130: throw JBIRemoteException.filterJmxExceptions(rtEx);
131: } catch (RuntimeOperationsException rtOpEx) {
132: throw JBIRemoteException.filterJmxExceptions(rtOpEx);
133: } catch (Exception ex) {
134: // pass null to the cause as you have already serialized the cause to jbi mgmt xml
135: throw new JBIRemoteException(JBIResultXmlBuilder
136: .createJbiResultXml(getI18NBundle(),
137: "jbi.ui.jmx.unknown.error", null, ex), null);
138: }
139: }
140:
141: /**
142: * set the attribute on mbean
143: * @param name mbean jmx object name
144: * @param attribute attribute
145: * @throws JMXConnectionException on error
146: * @throws JBIRemoteException on error
147: */
148: public void setMBeanAttribute(ObjectName name, Attribute attribute)
149: throws JMXConnectionException, JBIRemoteException {
150: try {
151: getMBeanServerConnection().setAttribute(name, attribute);
152: } catch (Exception allEx) {
153: //TODO: make explicit catch for better error reporting
154: // all other exceptions
155: throw JBIRemoteException.filterJmxExceptions(allEx);
156: }
157: }
158:
159: /**
160: * return the attribute value
161: * @param name jmx object name
162: * @param attribute attribute name
163: * @throws JMXConnectionException on error
164: * @throws JBIRemoteException on error
165: * @return attribute value
166: */
167: public Object getMBeanAttribute(ObjectName name, String attribute)
168: throws JMXConnectionException, JBIRemoteException {
169: try {
170: Object resultObject = getMBeanServerConnection()
171: .getAttribute(name, attribute);
172: return resultObject;
173: } catch (Exception allEx) {
174: //TODO: make explicit catch for better error reporting
175: // all other exceptions
176: throw JBIRemoteException.filterJmxExceptions(allEx);
177: }
178:
179: }
180:
181: /**
182: * creates jmx connection
183: * @return jmx connection
184: * @param username username
185: * @param password password
186: * @param host host name
187: * @param port port number
188: * @throws JMXConnectionException on error
189: */
190: public static JMXConnection createJmxConnection(String host,
191: String port, String username, String password)
192: throws JMXConnectionException {
193: Properties props = JMXConnectionProperties
194: .getJMXConnectionPropertyMap(null, host, port,
195: username, password);
196: return createJmxConnection(props);
197: }
198:
199: /**
200: * creates jmx connection
201: * @return jmx connection
202: * @param connProps properties
203: * @throws JMXConnectionException on error
204: */
205: public static JMXConnection createJmxConnection(Properties connProps)
206: throws JMXConnectionException {
207: return JMXConnectionFactory.newInstance("").getConnection(
208: connProps);
209: }
210:
211: }
|