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: * @(#)ManagementClientFactory.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.esb.management.client;
030:
031: import java.io.IOException;
032: import java.net.MalformedURLException;
033: import java.net.UnknownHostException;
034: import java.util.HashMap;
035: import java.util.Map;
036:
037: import javax.management.MBeanServerConnection;
038: import javax.management.MalformedObjectNameException;
039: import javax.management.ObjectName;
040: import javax.management.remote.JMXConnector;
041: import javax.management.remote.JMXConnectorFactory;
042: import javax.management.remote.JMXServiceURL;
043:
044: import com.sun.esb.management.common.ManagementRemoteException;
045: import com.sun.jbi.ui.client.ConnectionType;
046: import com.sun.jbi.ui.common.I18NBundle;
047: import com.sun.jbi.ui.common.JBIJMXObjectNames;
048: import com.sun.jbi.ui.common.JBIResultXmlBuilder;
049: import com.sun.jbi.ui.common.Util;
050:
051: /**
052: * This class is a factory class to create a ManagementClient object.
053: *
054: * @author graj
055: */
056: public class ManagementClientFactory {
057:
058: /** i18n */
059: private static I18NBundle sI18NBundle = null;
060:
061: /** Creates a new instance of ManagementClientFactory */
062: private ManagementClientFactory() {
063: }
064:
065: /**
066: * gives the I18N bundle
067: *
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: * Creates a management message string and populates the exception
080: *
081: * @param bundleKey
082: * @param args
083: * of Strings
084: * @param sourceException -
085: * the source exception to propagate
086: * @return Exception object created with a valid XML Management Message
087: */
088: static Exception createManagementException(String bundleKey,
089: String[] args, Exception sourceException) {
090: Exception exception = null;
091: String xmlManagementMessage = JBIResultXmlBuilder
092: .createJbiResultXml(ManagementClientFactory
093: .getI18NBundle(), bundleKey, args,
094: sourceException);
095: exception = new Exception(xmlManagementMessage);
096: return exception;
097: }
098:
099: /**
100: * Creates a new instance of ManagementClient object
101: *
102: * @param hostName
103: * @param portNumber
104: * @param userName
105: * @param password
106: * @param connectionType
107: * @return ManagementClient object
108: * @throws ManagementRemoteException
109: */
110: public static ManagementClient getInstance(String hostName,
111: int portNumber, String userName, String password,
112: ConnectionType connectionType)
113: throws ManagementRemoteException {
114:
115: ManagementClientFactory factory = new ManagementClientFactory();
116: ManagementClient commands = null;
117: // Create a remote connection
118: MBeanServerConnection connection = null;
119:
120: try {
121: connection = factory.getMBeanServerConnection(hostName,
122: portNumber, userName, password, connectionType);
123: } catch (ManagementRemoteException rEx) {
124: throw rEx;
125: } catch (Exception e) {
126: String[] args = { hostName,
127: (new Integer(portNumber)).toString(), userName,
128: connectionType.getProtocol() };
129: Exception exception = createManagementException(
130: "jbi.ui.client.factory.connection.host.port.uname.password.protocol",
131: args, e);
132: throw new ManagementRemoteException(exception);
133: }
134: boolean isLocalHost = false;
135: try {
136: isLocalHost = Util.isLocalHost(hostName);
137: } catch (UnknownHostException e) {
138: String[] args = { hostName };
139: Exception exception = createManagementException(
140: "jbi.ui.client.factory.connection.unknown.host",
141: args, null);
142: throw new ManagementRemoteException(exception);
143: }
144: if (isLocalHost == true) {
145: commands = new ManagementClient(connection, false);
146: } else {
147: commands = new ManagementClient(connection, true);
148: }
149: return commands;
150: }
151:
152: /**
153: * Creates a new instance of ManagementClient object
154: *
155: * @param url
156: * @param userName
157: * @param password
158: * @param isRemoteConnection -
159: * true if remote, false if local
160: * @return ManagementClient object
161: * @throws ManagementRemoteException
162: */
163: public static ManagementClient getInstance(String url,
164: String userName, String password, boolean isRemoteConnection)
165: throws ManagementRemoteException {
166:
167: ManagementClientFactory factory = new ManagementClientFactory();
168: ManagementClient commands = null;
169:
170: MBeanServerConnection connection = null;
171:
172: try {
173: connection = factory.getMBeanServerConnection(url,
174: userName, password);
175: } catch (ManagementRemoteException rEx) {
176: throw rEx;
177: } catch (Exception e) {
178: String[] args = { url, userName };
179: Exception exception = createManagementException(
180: "jbi.ui.client.factory.connection.url.uname.password",
181: args, e);
182: throw new ManagementRemoteException(exception);
183: }
184:
185: commands = new ManagementClient(connection, isRemoteConnection);
186: return commands;
187: }
188:
189: /**
190: * Creates a new instance of ManagementClient object First tries to
191: * establish a HTTP connection. If that fails, tries to establish a HTTPS
192: * connection, and if that fails tries to establish a JRMP Connection.
193: *
194: * @param hostName
195: * @param portNumber
196: * @param userName
197: * @param password
198: * @return ManagementClient object
199: * @throws ManagementRemoteException
200: */
201: public static ManagementClient getInstance(String hostName,
202: int portNumber, String userName, String password)
203: throws ManagementRemoteException {
204:
205: ManagementClientFactory factory = new ManagementClientFactory();
206: ManagementClient commands = null;
207:
208: MBeanServerConnection connection = null;
209: boolean result = false;
210: ObjectName mbeanName = null;
211: Exception exceptionArgument = null;
212:
213: // Try to obtain a HTTP connection
214: try {
215: connection = factory
216: .getMBeanServerConnection(hostName, portNumber,
217: userName, password, ConnectionType.HTTP);
218: mbeanName = JBIJMXObjectNames
219: .getJavaCapsAdministrationServiceMBeanObjectName();
220: result = connection.isRegistered(mbeanName);
221: } catch (MalformedObjectNameException e) {
222: connection = null;
223: exceptionArgument = e;
224: } catch (IOException e) {
225: connection = null;
226: exceptionArgument = e;
227: } catch (RuntimeException runtimeException) {
228: connection = null;
229: exceptionArgument = runtimeException;
230: } catch (Exception e) {
231: connection = null;
232: exceptionArgument = e;
233: }
234:
235: if (connection == null) {
236: // Try to obtain a HTTPS (secure) connection
237: try {
238: connection = factory.getMBeanServerConnection(hostName,
239: portNumber, userName, password,
240: ConnectionType.HTTPS);
241: mbeanName = JBIJMXObjectNames
242: .getJavaCapsAdministrationServiceMBeanObjectName();
243: result = connection.isRegistered(mbeanName);
244: } catch (MalformedObjectNameException e) {
245: connection = null;
246: exceptionArgument = e;
247: } catch (IOException e) {
248: connection = null;
249: exceptionArgument = e;
250: } catch (RuntimeException runtimeException) {
251: connection = null;
252: exceptionArgument = runtimeException;
253: } catch (Exception e) {
254: connection = null;
255: exceptionArgument = e;
256: }
257: }
258:
259: if (connection == null) {
260: // Try to obtain a JRMP connection
261: try {
262: connection = factory.getMBeanServerConnection(hostName,
263: portNumber, userName, password,
264: ConnectionType.JRMP);
265: mbeanName = JBIJMXObjectNames
266: .getJavaCapsAdministrationServiceMBeanObjectName();
267: result = connection.isRegistered(mbeanName);
268: } catch (MalformedObjectNameException e) {
269: connection = null;
270: exceptionArgument = e;
271: } catch (IOException e) {
272: connection = null;
273: exceptionArgument = e;
274: } catch (RuntimeException runtimeException) {
275: connection = null;
276: exceptionArgument = runtimeException;
277: } catch (Exception e) {
278: connection = null;
279: exceptionArgument = e;
280: }
281: }
282:
283: if (connection == null) {
284: // Try to obtain a IIOP connection for websphere
285: // The CORBA calls below send errors to stderr,
286: // leading to very ugly ant output (CR 6586235).
287: // It turns out that this is documented as CR 5068014
288: // and the suggested workaround there is to redirect
289: // System.err, as we do below:
290: java.io.PrintStream oldErr = System.err;
291: try {
292: java.io.PrintStream newErr = new java.io.PrintStream(
293: new java.io.ByteArrayOutputStream());
294: System.setErr(newErr);
295: connection = factory.getMBeanServerConnection(hostName,
296: portNumber, userName, password,
297: ConnectionType.IIOP);
298: mbeanName = JBIJMXObjectNames
299: .getJavaCapsAdministrationServiceMBeanObjectName();
300: result = connection.isRegistered(mbeanName);
301: newErr.close();
302: } catch (MalformedObjectNameException e) {
303: connection = null;
304: exceptionArgument = e;
305: } catch (IOException e) {
306: connection = null;
307: exceptionArgument = e;
308: } catch (RuntimeException runtimeException) {
309: connection = null;
310: exceptionArgument = runtimeException;
311: } catch (Exception e) {
312: connection = null;
313: exceptionArgument = e;
314: }
315: System.setErr(oldErr);
316: }
317:
318: boolean isLocalHost = false;
319: try {
320: isLocalHost = Util.isLocalHost(hostName);
321: } catch (UnknownHostException e) {
322: String[] args = { hostName };
323: Exception exception = createManagementException(
324: "jbi.ui.client.factory.connection.unknown.host",
325: args, null);
326: throw new ManagementRemoteException(exception);
327: }
328: if (connection != null) {
329: if (isLocalHost == true) {
330: commands = new ManagementClient(connection, false);
331: } else {
332: commands = new ManagementClient(connection, true);
333: }
334: } else {
335: String[] args = { hostName,
336: Integer.valueOf(portNumber).toString(), userName };
337: Exception exception = createManagementException(
338: "jbi.ui.client.connection.failure", args, null);
339: throw new ManagementRemoteException(exception);
340: }
341: return commands;
342: }
343:
344: /**
345: * Creates a new instance of ManagementClient object for a remote connection
346: *
347: * @param connection
348: * @return ManagementClient object
349: * @throws ManagementRemoteException
350: */
351: public static ManagementClient getInstance(
352: MBeanServerConnection connection)
353: throws ManagementRemoteException {
354: ManagementClient commands = null;
355: if (connection != null) {
356: commands = new ManagementClient(connection, true);
357: } else {
358: Exception exception = createManagementException(
359: "jbi.ui.client.factory.connection", null, null);
360: throw new ManagementRemoteException(exception);
361: }
362:
363: return commands;
364: }
365:
366: /**
367: * Creates a new instance of ManagementClient object
368: *
369: * @param connection
370: * @param isRemoteConnection -
371: * true if remote, false if local
372: * @return ManagementClient object
373: * @throws ManagementRemoteException
374: */
375: public static ManagementClient getInstance(
376: MBeanServerConnection connection, boolean isRemoteConnection)
377: throws ManagementRemoteException {
378: ManagementClient commands = null;
379: if (connection != null) {
380: commands = new ManagementClient(connection,
381: isRemoteConnection);
382: } else {
383: Exception exception = createManagementException(
384: "jbi.ui.client.factory.connection", null, null);
385: throw new ManagementRemoteException(exception);
386: }
387:
388: return commands;
389: }
390:
391: /**
392: * This method returns the MBeanServerConnection to used to invoke the MBean
393: * methods via HTTP connector.
394: *
395: * @param url -
396: * service:jmx:rmi:///jndi/rmi://<hostName>:<portNumber>/management/rmi-jmx-connector
397: * @userName - the userName name for authenticating with MBeanServer
398: * @password - the password for authenticating with MBeanServer
399: * @return MBeanServerConnection
400: * @throws ManagementRemoteException
401: */
402: protected MBeanServerConnection getMBeanServerConnection(
403: String urlString, String userName, String password)
404: throws ManagementRemoteException {
405: try {
406: // Create a JMXMP connector client and
407: // connect it to the JMXMP connector server
408: // final JMXServiceURL url = new JMXServiceURL(urlString);
409: // final JMXServiceURL url = new JMXServiceURL(null, hostName,
410: // portNumber);
411: final JMXServiceURL url = new JMXServiceURL(urlString);
412: String[] credentials = new String[] { userName, password };
413: Map<String, String[]> environment = new HashMap<String, String[]>();
414: environment.put("jmx.remote.credentials", credentials);
415: final JMXConnector connector = JMXConnectorFactory.connect(
416: url, environment);
417: return connector.getMBeanServerConnection();
418: } catch (Exception e) {
419: String[] args = { urlString, userName };
420: Exception exception = createManagementException(
421: "jbi.ui.client.factory.connection.url.uname.password",
422: args, e);
423: throw new ManagementRemoteException(exception);
424: }
425: }
426:
427: /**
428: * This method returns the MBeanServerConnection to used to invoke the MBean
429: * methods via HTPP connector.
430: *
431: * @param hostName -
432: * the hostName part of the URL. If null, defaults to the local
433: * hostName name, as determined by
434: * InetAddress.getLocalHost().getHostName(). If it is a numeric
435: * IPv6 address, it can optionally be enclosed in square brackets
436: * [].
437: * @portNumber - the portNumber part of the URL.
438: * @userName - the userName name for authenticating with MBeanServer
439: * @password - the password for authenticating with MBeanServer
440: * @return MBeanServerConnection
441: * @throws ManagementRemoteException
442: */
443: protected MBeanServerConnection getMBeanServerConnection(
444: String hostName, int portNumber, String userName,
445: String password, ConnectionType type)
446: throws ManagementRemoteException {
447: try {
448: if (type == ConnectionType.JRMP) {
449: // Create a JMXMP connector client and
450: // connect it to the JMXMP connector server
451: // final JMXServiceURL url = new JMXServiceURL(null, hostName,
452: // portNumber);
453: // String urlString =
454: // "service:jmx:rmi:///jndi/rmi://"+hostName+":"+portNumber+"/jmxri";
455: String urlString = "service:jmx:rmi:///jndi/rmi://"
456: + hostName + ":" + portNumber + "/jmxrmi";
457: return this .getMBeanServerConnection(urlString,
458: userName, password);
459: } else if (type == ConnectionType.IIOP) {
460: String urlString = "service:jmx:iiop://" + hostName
461: + ":" + portNumber + "/jndi/JMXConnector";
462: return this .getMBeanServerConnection(urlString,
463: userName, password);
464: } else {
465: final JMXServiceURL url = new JMXServiceURL(type
466: .getProtocol(), hostName, portNumber);
467: final JMXConnector connector = JMXConnectorFactory
468: .connect(url, this .initEnvironment(userName,
469: password));
470: return connector.getMBeanServerConnection();
471: }
472: } catch (Exception e) {
473: String[] args = { hostName,
474: (new Integer(portNumber)).toString(), userName,
475: type.getProtocol() };
476: Exception exception = createManagementException(
477: "jbi.ui.client.factory.connection.host.port.uname.password.protocol",
478: args, null);
479:
480: throw new ManagementRemoteException(exception);
481: }
482: }
483:
484: /**
485: * This method initialize the environment for creating the JMXConnector.
486: *
487: * @return Map - HashMap of environemtn
488: */
489: private Map<String, Object> initEnvironment(String userName,
490: String password) {
491: final Map<String, Object> environment = new HashMap<String, Object>();
492: final String PKGS = "com.sun.enterprise.admin.jmx.remote.protocol";
493:
494: environment.put(
495: JMXConnectorFactory.PROTOCOL_PROVIDER_CLASS_LOADER,
496: getClass().getClassLoader());
497: environment.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
498: PKGS);
499: environment.put("USER", userName);
500: environment.put("PASSWORD", password);
501: environment.put("com.sun.enterprise.as.http.auth", "BASIC");
502: return (environment);
503: }
504:
505: /**
506: * This method creates the JMXServiceURL
507: *
508: * @param protocol
509: * @param hostName
510: * @param portNumber
511: * @throws ManagementRemoteException
512: */
513: private JMXServiceURL getJMXServiceURL(String protocol,
514: String hostName, int portNumber)
515: throws ManagementRemoteException {
516: try {
517: // Create a JMXMP connector client and connect it to the JMXMP
518: // connector server
519:
520: final JMXServiceURL url = new JMXServiceURL(protocol,
521: hostName, portNumber);
522: System.out.println("url = " + url.toString());
523: return url;
524: } catch (MalformedURLException mue) {
525: String[] args = { protocol, hostName,
526: (new Integer(portNumber)).toString(), };
527: Exception exception = createManagementException(
528: "jbi.ui.client.factory.connection.protocol.host.port",
529: args, mue);
530: throw new ManagementRemoteException(exception);
531: }
532: }
533:
534: /**
535: * @param args
536: */
537: public static void main(String[] args) {
538: ManagementClientFactory factory = null;
539: ManagementClient commands = null;
540:
541: String hostName = "localhost";
542: // int port = 18451;
543: // int port = 18449;
544: int port = 5651; // CAS
545: port = 5649; // JBITest
546: String userName = "admin";
547: String password = "adminadmin";
548: String dummyName = "foo";
549:
550: // Test 1
551: // try {
552: // commands = ManagementClientFactory
553: // .getInstance(
554: // "service:jmx:rmi:///jndi/rmi://"+hostName+":"+port+"/management/rmi-jmx-connector",
555: // userName, password, false);
556: // } catch (ManagementRemoteException e) {
557:
558: // e.printStackTrace();
559: // }
560:
561: // Test 2
562: // try {
563: // commands = ManagementClientFactory.getInstance(hostName,
564: // port, userName, password, ConnectionType.JRMP);
565: // } catch (ManagementRemoteException e) {
566:
567: // e.printStackTrace();
568: // }
569:
570: // // Test 3
571: // try {
572: // commands = ManagementClientFactory.getInstance(hostName,
573: // port, userName, password, ConnectionType.HTTP);
574: // } catch (ManagementRemoteException e) {
575:
576: // e.printStackTrace();
577: // }
578:
579: // // Test 4
580: try {
581: commands = ManagementClientFactory.getInstance(hostName,
582: port, userName, password);
583: } catch (ManagementRemoteException e) {
584:
585: e.printStackTrace();
586: }
587:
588: // // Test 5
589: // factory = new ManagementClientFactory();
590: // MBeanServerConnection connection = null;
591:
592: // try {
593: // connection = factory.getMBeanServerConnection(hostName,
594: // port, userName, password, ConnectionType.HTTP);
595: // commands = ManagementClientFactory.getInstance(connection);
596: // } catch (ManagementRemoteException e) {
597:
598: // e.printStackTrace();
599: // }
600:
601: // boolean isJBIRuntimeEnabled = false;
602: // if (commands != null) {
603: // try {
604: // isJBIRuntimeEnabled = commands.isJBIRuntimeEnabled();
605: // } catch (ManagementRemoteException e) {
606:
607: // e.printStackTrace();
608: // }
609: // if (isJBIRuntimeEnabled == true) {
610: // try {
611: // String result = commands.listBindingComponents("domain1");
612: // System.out.println("Result is: " + result);
613: // } catch (ManagementRemoteException e) {
614:
615: // e.printStackTrace();
616: // }
617: // }
618: // }
619: // System.out.println("The JBI Framework is "
620: // + (isJBIRuntimeEnabled ? "Enabled." : "NOT Enabled."));
621: }
622:
623: }
|