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: * @(#)SunASEEJMXConnectionImpl.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.sunasee;
030:
031: import com.sun.jbi.ui.common.JBIResultXmlBuilder;
032: import com.sun.jbi.ui.common.JMXConnectionException;
033: import com.sun.jbi.ui.common.ToolsLogManager;
034: import com.sun.jbi.ui.common.Util;
035: import java.io.IOException;
036: import java.util.HashMap;
037: import java.util.Map;
038: import java.util.Properties;
039: import javax.management.remote.JMXConnector;
040: import javax.management.remote.JMXConnectorFactory;
041: import javax.management.remote.JMXServiceURL;
042: import com.sun.jbi.ui.client.JMXConnectionImpl;
043:
044: import com.sun.jbi.ui.client.JMXConnectionProperties;
045:
046: /** This class implements the JMXConnection interface for connecting to
047: * jmx connector server remotely using jmx remote apis
048: *
049: * @author Sun Microsystems, Inc.
050: */
051:
052: public class SunASEEJMXConnectionImpl extends JMXConnectionImpl {
053: /** username */
054: private String mUsername = null;
055: /** password */
056: private String mPassword = null;
057: /** host */
058: private String mHost = null;
059: /** port */
060: private String mPort = null;
061:
062: /**
063: * jmx url
064: */
065: private String mUrl = null;
066:
067: /** jmx url */
068: private JMXServiceURL mJmxUrl = null;
069: /** jmx connector */
070: private JMXConnector mJmxConnector = null;
071:
072: /**
073: * constructor
074: * @throws JMXConnectionException on error
075: */
076: public SunASEEJMXConnectionImpl() throws JMXConnectionException {
077: this (new Properties());
078: }
079:
080: /**
081: * constructor
082: * @param connProps connection props
083: * @throws JMXConnectionException on error
084: */
085: public SunASEEJMXConnectionImpl(Properties connProps)
086: throws JMXConnectionException {
087: super ();
088: initConnectionProperties(connProps);
089: }
090:
091: /**
092: * init
093: * @param connProps props
094: * @throws JMXConnectionException error
095: */
096: protected void initConnectionProperties(Properties connProps)
097: throws JMXConnectionException {
098: String url = connProps
099: .getProperty(JMXConnectionProperties.URL_PROP);
100: String host = connProps
101: .getProperty(JMXConnectionProperties.HOST_PROP);
102: String port = connProps
103: .getProperty(JMXConnectionProperties.PORT_PROP);
104: String username = connProps
105: .getProperty(JMXConnectionProperties.USERNAME_PROP);
106: String password = connProps
107: .getProperty(JMXConnectionProperties.PASSWORD_PROP);
108: this .setUrl(url);
109: this .setHost(host);
110: this .setPort(port);
111: this .setUsername(username);
112: this .setPassword(password);
113: validatePortValue(port);
114: }
115:
116: /**
117: * validate
118: * @param port port
119: * @throws JMXConnectionException on error
120: */
121: protected void validatePortValue(String port)
122: throws JMXConnectionException {
123: if (port == null)
124: return;
125: int portValue = -1;
126: try {
127: portValue = Integer.parseInt(port);
128: } catch (NumberFormatException ex) {
129: throw new JMXConnectionException(JBIResultXmlBuilder
130: .createJbiResultXml(getI18NBundle(),
131: "jbi.ui.jmx.open.error.invalid.port",
132: new Object[] { port }, ex), null);
133:
134: }
135: }
136:
137: /** Getter for property username.
138: * @return Value of property username.
139: *
140: */
141: protected String getUrl() {
142: return this .mUrl;
143: }
144:
145: /**
146: * Setter for property username.
147: * @param aUrl url
148: */
149: protected void setUrl(String aUrl) {
150:
151: try {
152: closeConnection(); // if previously opened
153: } catch (Exception ex) {
154: Util.logDebug(ToolsLogManager.getClientLogger(), ex);
155: // ignore this as a secondary exception.
156: }
157: this .mUrl = aUrl;
158: }
159:
160: /** Getter for property username.
161: * @return Value of property username.
162: *
163: */
164: protected String getUsername() {
165: return this .mUsername;
166: }
167:
168: /**
169: * Setter for property username.
170: * @param aUsername username
171: */
172: protected void setUsername(String aUsername) {
173:
174: try {
175: closeConnection(); // if previously opened
176: } catch (Exception ex) {
177: Util.logDebug(ToolsLogManager.getClientLogger(), ex);
178: // ignore this as a secondary exception.
179: }
180: this .mUsername = aUsername;
181: }
182:
183: /** Getter for property password.
184: * @return Value of property password.
185: *
186: */
187: protected String getPassword() {
188: return this .mPassword;
189: }
190:
191: /**
192: * Setter for property password.
193: * @param aPassword password
194: */
195: protected void setPassword(String aPassword) {
196:
197: try {
198: closeConnection(); // if previously opened
199: } catch (Exception ex) {
200: Util.logDebug(ToolsLogManager.getClientLogger(), ex);
201: // ignore this as a secondary exception.
202: }
203: this .mPassword = aPassword;
204: }
205:
206: /** Getter for property host.
207: * @return Value of property host.
208: *
209: */
210: protected String getHost() {
211: return this .mHost;
212: }
213:
214: /** Setter for property host.
215: * @param aHost New value of property host.
216: *
217: */
218: protected void setHost(String aHost) {
219:
220: try {
221: closeConnection(); // if previously opened
222: } catch (Exception ex) {
223: Util.logDebug(ToolsLogManager.getClientLogger(), ex);
224: // ignore this as a secondary exception.
225: }
226: this .mHost = aHost;
227: }
228:
229: /** Getter for property port.
230: * @return Value of property port.
231: *
232: */
233: protected String getPort() {
234: return this .mPort;
235: }
236:
237: /** Setter for property port.
238: * @param aPort New value of property port.
239: */
240: protected void setPort(String aPort) {
241: // close the previously opened connection
242: try {
243: closeConnection();
244: } catch (Exception ex) {
245: Util.logDebug(ToolsLogManager.getClientLogger(), ex);
246: // ignore this as a secondary exception.
247: }
248: this .mPort = aPort;
249: }
250:
251: /**
252: * returns the creadentials maps that can be passed as env
253: * @return credential map
254: */
255: protected Map getCredentialsMap() {
256: Map env = new HashMap();
257: String username = this .getUsername();
258: String password = this .getPassword();
259:
260: // TODO : using the default from the Connection.properties file is temp.
261: // throw the exception if the username/password combination is invalie.
262: // e.g. username specified, but null password and vice versa.
263: if (username == null) {
264: username = JMXConnectionProperties.getInstance()
265: .getDefaultUserName();
266: }
267:
268: if (password == null) {
269: password = JMXConnectionProperties.getInstance()
270: .getDefaultPassword();
271: }
272:
273: ToolsLogManager.getClientLogger().fine(
274: "username,password : " + username + " : " + password);
275:
276: String[] credentials = new String[] { username, password };
277: env.put("jmx.remote.credentials", credentials);
278: return env;
279: }
280:
281: /**
282: * obtains the the jmx connection if it is not already exists
283: * @throws JMXConnectionException on error
284: */
285: public void openConnection() throws JMXConnectionException {
286:
287: if (this .mMBeanServerConnection != null) {
288: return; // already connected.
289: }
290:
291: String url = null;
292: try {
293:
294: url = JMXConnectionProperties.getInstance()
295: .getConnectionURL(getUrl(), getHost(), getPort());
296: ToolsLogManager.getClientLogger().fine("JMX URL : " + url);
297: // ToolsLogManager.getClientLogger().severe("JMX URL : " + url);
298: this .mJmxUrl = new JMXServiceURL(url);
299: } catch (Exception ex) {
300: throw new JMXConnectionException(JBIResultXmlBuilder
301: .createJbiResultXml(getI18NBundle(),
302: "jbi.ui.jmx.open.error",
303: new Object[] { url }, ex), null);
304:
305: }
306:
307: // connect to the remote connector server
308: try {
309: Map env = this .getCredentialsMap();
310: this .mJmxConnector = JMXConnectorFactory.connect(
311: this .mJmxUrl, env);
312: } catch (IOException ioEx) {
313: // connection communication is wrong port or host wrong.
314: // System.out.println("Expcetion occured in JMX Connect" + ex);
315: this .mJmxUrl = null;
316: throw new JMXConnectionException(JBIResultXmlBuilder
317: .createJbiResultXml(getI18NBundle(),
318: "jbi.ui.jmx.open.io.error",
319: new Object[] { url }, ioEx), null);
320:
321: } catch (SecurityException secEx) {
322: // security exception wrong username or password or other security problems.
323: this .mJmxUrl = null;
324: throw new JMXConnectionException(JBIResultXmlBuilder
325: .createJbiResultXml(getI18NBundle(),
326: "jbi.ui.jmx.open.security.error",
327: new Object[] { url }, secEx), null);
328:
329: } catch (Exception ex) {
330: // any other exception
331: this .mJmxUrl = null;
332: throw new JMXConnectionException(JBIResultXmlBuilder
333: .createJbiResultXml(getI18NBundle(),
334: "jbi.ui.jmx.open.error",
335: new Object[] { url }, ex), null);
336:
337: }
338:
339: // Get an MBeanServerConnection interface
340: try {
341: this .mMBeanServerConnection = this .mJmxConnector
342: .getMBeanServerConnection();
343: } catch (Exception ex) {
344: try {
345: closeConnection();
346: } catch (Exception closeEx) {
347: // ignore close exception
348: Util.logDebug(ToolsLogManager.getClientLogger(),
349: closeEx);
350: this .mMBeanServerConnection = null;
351: }
352: throw new JMXConnectionException(JBIResultXmlBuilder
353: .createJbiResultXml(getI18NBundle(),
354: "jbi.ui.jmx.open.error",
355: new Object[] { url }, ex), null);
356:
357: }
358: }
359:
360: /**
361: * closes the jmx connection
362: * @throws JMXConnectionException on error
363: */
364: public void closeConnection() throws JMXConnectionException {
365:
366: this .mMBeanServerConnection = null;
367: this .mJmxUrl = null;
368:
369: if (this .mJmxConnector != null) {
370: try {
371: this .mJmxConnector.close();
372: this .mJmxConnector = null;
373: } catch (Exception ex) {
374: throw new JMXConnectionException(JBIResultXmlBuilder
375: .createJbiResultXml(getI18NBundle(),
376: "jbi.ui.jmx.close.error", null, ex),
377: null);
378:
379: } finally {
380: this.mJmxConnector = null;
381: }
382: }
383: }
384:
385: }
|