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