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: * @(#)JBIAdminCommandsClientFactory.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 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.jbi.ui.common.I18NBundle;
045: import com.sun.jbi.ui.common.JBIAdminCommands;
046: import com.sun.jbi.ui.common.JBIJMXObjectNames;
047: import com.sun.jbi.ui.common.JBIRemoteException;
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 JBIAdminCommands object.
053: *
054: * @author graj
055: */
056: public class JBIAdminCommandsClientFactory {
057:
058: /** i18n */
059: private static I18NBundle sI18NBundle = null;
060:
061: /** Creates a new instance of JBIAdminCommandsClientFactory */
062: private JBIAdminCommandsClientFactory() {
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(JBIAdminCommandsClientFactory
093: .getI18NBundle(), bundleKey, args,
094: sourceException);
095: exception = new Exception(xmlManagementMessage);
096: return exception;
097: }
098:
099: /**
100: * Creates a new instance of JBIAdminCommands object
101: *
102: * @param hostName
103: * @param portNumber
104: * @param userName
105: * @param password
106: * @param connectionType
107: * @return JBIAdminCommands object
108: * @throws JBIRemoteException
109: */
110: public static JBIAdminCommands getInstance(String hostName,
111: int portNumber, String userName, String password,
112: ConnectionType connectionType) throws JBIRemoteException {
113:
114: JBIAdminCommandsClientFactory factory = new JBIAdminCommandsClientFactory();
115: JBIAdminCommands commands = null;
116: // Create a remote connection
117: MBeanServerConnection connection = null;
118:
119: try {
120: connection = factory.getMBeanServerConnection(hostName,
121: portNumber, userName, password, connectionType);
122: } catch (JBIRemoteException rEx) {
123: throw rEx;
124: } catch (Exception e) {
125: String[] args = { hostName,
126: (new Integer(portNumber)).toString(), userName,
127: connectionType.getProtocol() };
128: Exception exception = createManagementException(
129: "jbi.ui.client.factory.connection.host.port.uname.password.protocol",
130: args, e);
131: throw new JBIRemoteException(exception);
132: }
133: boolean isLocalHost = false;
134: try {
135: isLocalHost = Util.isLocalHost(hostName);
136: } catch (UnknownHostException e) {
137: String[] args = { hostName };
138: Exception exception = createManagementException(
139: "jbi.ui.client.factory.connection.unknown.host",
140: args, null);
141: throw new JBIRemoteException(exception);
142: }
143: if (isLocalHost == true) {
144: commands = new JBIAdminCommandsClientImpl(connection, false);
145: } else {
146: commands = new JBIAdminCommandsClientImpl(connection, true);
147: }
148: return commands;
149: }
150:
151: /**
152: * Creates a new instance of JBIAdminCommands object
153: *
154: * @param url
155: * @param userName
156: * @param password
157: * @param isRemoteConnection -
158: * true if remote, false if local
159: * @return JBIAdminCommands object
160: * @throws JBIRemoteException
161: */
162: public static JBIAdminCommands getInstance(String url,
163: String userName, String password, boolean isRemoteConnection)
164: throws JBIRemoteException {
165:
166: JBIAdminCommandsClientFactory factory = new JBIAdminCommandsClientFactory();
167: JBIAdminCommands commands = null;
168:
169: MBeanServerConnection connection = null;
170:
171: try {
172: connection = factory.getMBeanServerConnection(url,
173: userName, password);
174: } catch (JBIRemoteException rEx) {
175: throw rEx;
176: } catch (Exception e) {
177: String[] args = { url, userName };
178: Exception exception = createManagementException(
179: "jbi.ui.client.factory.connection.url.uname.password",
180: args, e);
181: throw new JBIRemoteException(exception);
182: }
183:
184: commands = new JBIAdminCommandsClientImpl(connection,
185: isRemoteConnection);
186: return commands;
187: }
188:
189: /**
190: * Creates a new instance of JBIAdminCommands 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 JBIAdminCommands object
199: * @throws JBIRemoteException
200: */
201: public static JBIAdminCommands getInstance(String hostName,
202: int portNumber, String userName, String password)
203: throws JBIRemoteException {
204:
205: JBIAdminCommandsClientFactory factory = new JBIAdminCommandsClientFactory();
206: JBIAdminCommands 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: .getJbiReferenceAdminUiMBeanObjectName();
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: .getJbiReferenceAdminUiMBeanObjectName();
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: .getJbiReferenceAdminUiMBeanObjectName();
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: .getJbiReferenceAdminUiMBeanObjectName();
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 JBIRemoteException(exception);
327: }
328: if (connection != null) {
329: if (isLocalHost == true) {
330: commands = new JBIAdminCommandsClientImpl(connection,
331: false);
332: } else {
333: commands = new JBIAdminCommandsClientImpl(connection,
334: true);
335: }
336: } else {
337: String[] args = { hostName,
338: Integer.valueOf(portNumber).toString(), userName };
339: Exception exception = createManagementException(
340: "jbi.ui.client.connection.failure", args, null);
341: throw new JBIRemoteException(exception);
342: }
343: return commands;
344: }
345:
346: /**
347: * Creates a new instance of JBIAdminCommands object for a remote connection
348: *
349: * @param connection
350: * @return JBIAdminCommands object
351: * @throws JBIRemoteException
352: */
353: public static JBIAdminCommands getInstance(
354: MBeanServerConnection connection) throws JBIRemoteException {
355: JBIAdminCommands commands = null;
356: if (connection != null) {
357: commands = new JBIAdminCommandsClientImpl(connection, true);
358: } else {
359: Exception exception = createManagementException(
360: "jbi.ui.client.factory.connection", null, null);
361: throw new JBIRemoteException(exception);
362: }
363:
364: return commands;
365: }
366:
367: /**
368: * Creates a new instance of JBIAdminCommands object
369: *
370: * @param connection
371: * @param isRemoteConnection -
372: * true if remote, false if local
373: * @return JBIAdminCommands object
374: * @throws JBIRemoteException
375: */
376: public static JBIAdminCommands getInstance(
377: MBeanServerConnection connection, boolean isRemoteConnection)
378: throws JBIRemoteException {
379: JBIAdminCommands commands = null;
380: if (connection != null) {
381: commands = new JBIAdminCommandsClientImpl(connection,
382: isRemoteConnection);
383: } else {
384: Exception exception = createManagementException(
385: "jbi.ui.client.factory.connection", null, null);
386: throw new JBIRemoteException(exception);
387: }
388:
389: return commands;
390: }
391:
392: /**
393: * This method returns the MBeanServerConnection to used to invoke the MBean
394: * methods via HTTP connector.
395: *
396: * @param url -
397: * service:jmx:rmi:///jndi/rmi://<hostName>:<portNumber>/management/rmi-jmx-connector
398: * @userName - the userName name for authenticating with MBeanServer
399: * @password - the password for authenticating with MBeanServer
400: * @return MBeanServerConnection
401: * @throws JBIRemoteException
402: */
403: protected MBeanServerConnection getMBeanServerConnection(
404: String urlString, String userName, String password)
405: throws JBIRemoteException {
406: try {
407: // Create a JMXMP connector client and
408: // connect it to the JMXMP connector server
409: // final JMXServiceURL url = new JMXServiceURL(urlString);
410: // final JMXServiceURL url = new JMXServiceURL(null, hostName,
411: // portNumber);
412: final JMXServiceURL url = new JMXServiceURL(urlString);
413: String[] credentials = new String[] { userName, password };
414: Map<String, String[]> environment = new HashMap<String, String[]>();
415: environment.put("jmx.remote.credentials", credentials);
416: final JMXConnector connector = JMXConnectorFactory.connect(
417: url, environment);
418: return connector.getMBeanServerConnection();
419: } catch (Exception e) {
420: String[] args = { urlString, userName };
421: Exception exception = createManagementException(
422: "jbi.ui.client.factory.connection.url.uname.password",
423: args, e);
424: throw new JBIRemoteException(exception);
425: }
426: }
427:
428: /**
429: * This method returns the MBeanServerConnection to used to invoke the MBean
430: * methods via HTPP connector.
431: *
432: * @param hostName -
433: * the hostName part of the URL. If null, defaults to the local
434: * hostName name, as determined by
435: * InetAddress.getLocalHost().getHostName(). If it is a numeric
436: * IPv6 address, it can optionally be enclosed in square brackets
437: * [].
438: * @portNumber - the portNumber part of the URL.
439: * @userName - the userName name for authenticating with MBeanServer
440: * @password - the password for authenticating with MBeanServer
441: * @return MBeanServerConnection
442: * @throws JBIRemoteException
443: */
444: protected MBeanServerConnection getMBeanServerConnection(
445: String hostName, int portNumber, String userName,
446: String password, ConnectionType type)
447: throws JBIRemoteException {
448: try {
449: if (type == ConnectionType.JRMP) {
450: // Create a JMXMP connector client and
451: // connect it to the JMXMP connector server
452: // final JMXServiceURL url = new JMXServiceURL(null, hostName,
453: // portNumber);
454: // String urlString =
455: // "service:jmx:rmi:///jndi/rmi://"+hostName+":"+portNumber+"/jmxri";
456: String urlString = "service:jmx:rmi:///jndi/rmi://"
457: + hostName + ":" + portNumber + "/jmxrmi";
458: return this .getMBeanServerConnection(urlString,
459: userName, password);
460: } else if (type == ConnectionType.IIOP) {
461: String urlString = "service:jmx:iiop://" + hostName
462: + ":" + portNumber + "/jndi/JMXConnector";
463: return this .getMBeanServerConnection(urlString,
464: userName, password);
465: } else {
466: final JMXServiceURL url = new JMXServiceURL(type
467: .getProtocol(), hostName, portNumber);
468: final JMXConnector connector = JMXConnectorFactory
469: .connect(url, this .initEnvironment(userName,
470: password));
471: return connector.getMBeanServerConnection();
472: }
473: } catch (Exception e) {
474: String[] args = { hostName,
475: (new Integer(portNumber)).toString(), userName,
476: type.getProtocol() };
477: // Exception exception = createManagementException(
478: // "jbi.ui.client.factory.connection.host.port.uname.password.protocol",
479: // args, e);
480: Exception exception = createManagementException(
481: "jbi.ui.client.factory.connection.host.port.uname.password.protocol",
482: args, null);
483:
484: throw new JBIRemoteException(exception);
485: }
486: }
487:
488: /**
489: * This method initialize the environment for creating the JMXConnector.
490: *
491: * @return Map - HashMap of environemtn
492: */
493: private Map<String, Object> initEnvironment(String userName,
494: String password) {
495: final Map<String, Object> environment = new HashMap<String, Object>();
496: final String PKGS = "com.sun.enterprise.admin.jmx.remote.protocol";
497:
498: environment.put(
499: JMXConnectorFactory.PROTOCOL_PROVIDER_CLASS_LOADER,
500: getClass().getClassLoader());
501: environment.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
502: PKGS);
503: environment.put("USER", userName);
504: environment.put("PASSWORD", password);
505: environment.put("com.sun.enterprise.as.http.auth", "BASIC");
506: return (environment);
507: }
508:
509: /**
510: * This method creates the JMXServiceURL
511: *
512: * @param protocol
513: * @param hostName
514: * @param portNumber
515: * @throws JBIRemoteException
516: */
517: private JMXServiceURL getJMXServiceURL(String protocol,
518: String hostName, int portNumber) throws JBIRemoteException {
519: try {
520: // Create a JMXMP connector client and connect it to the JMXMP
521: // connector server
522:
523: final JMXServiceURL url = new JMXServiceURL(protocol,
524: hostName, portNumber);
525: System.out.println("url = " + url.toString());
526: return url;
527: } catch (MalformedURLException mue) {
528: String[] args = { protocol, hostName,
529: (new Integer(portNumber)).toString(), };
530: Exception exception = createManagementException(
531: "jbi.ui.client.factory.connection.protocol.host.port",
532: args, mue);
533: throw new JBIRemoteException(exception);
534: }
535: }
536:
537: /**
538: * @param args
539: */
540: public static void main(String[] args) {
541: JBIAdminCommandsClientFactory factory = null;
542: JBIAdminCommands commands = null;
543:
544: String hostName = "localhost";
545: // int port = 18451;
546: // int port = 18449;
547: int port = 5651; // CAS
548: port = 5649; // JBITest
549: String userName = "admin";
550: String password = "adminadmin";
551: String dummyName = "foo";
552:
553: // Test 1
554: // try {
555: // commands = JBIAdminCommandsClientFactory
556: // .getInstance(
557: // "service:jmx:rmi:///jndi/rmi://"+hostName+":"+port+"/management/rmi-jmx-connector",
558: // userName, password, false);
559: // } catch (JBIRemoteException e) {
560:
561: // e.printStackTrace();
562: // }
563:
564: // Test 2
565: // try {
566: // commands = JBIAdminCommandsClientFactory.getInstance(hostName,
567: // port, userName, password, ConnectionType.JRMP);
568: // } catch (JBIRemoteException e) {
569:
570: // e.printStackTrace();
571: // }
572:
573: // // Test 3
574: // try {
575: // commands = JBIAdminCommandsClientFactory.getInstance(hostName,
576: // port, userName, password, ConnectionType.HTTP);
577: // } catch (JBIRemoteException e) {
578:
579: // e.printStackTrace();
580: // }
581:
582: // // Test 4
583: try {
584: commands = JBIAdminCommandsClientFactory.getInstance(
585: hostName, port, userName, password);
586: } catch (JBIRemoteException e) {
587:
588: e.printStackTrace();
589: }
590:
591: // // Test 5
592: // factory = new JBIAdminCommandsClientFactory();
593: // MBeanServerConnection connection = null;
594:
595: // try {
596: // connection = factory.getMBeanServerConnection(hostName,
597: // port, userName, password, ConnectionType.HTTP);
598: // commands = JBIAdminCommandsClientFactory.getInstance(connection);
599: // } catch (JBIRemoteException e) {
600:
601: // e.printStackTrace();
602: // }
603:
604: // boolean isJBIRuntimeEnabled = false;
605: // if (commands != null) {
606: // try {
607: // isJBIRuntimeEnabled = commands.isJBIRuntimeEnabled();
608: // } catch (JBIRemoteException e) {
609:
610: // e.printStackTrace();
611: // }
612: // if (isJBIRuntimeEnabled == true) {
613: // try {
614: // String result = commands.listBindingComponents("domain1");
615: // System.out.println("Result is: " + result);
616: // } catch (JBIRemoteException e) {
617:
618: // e.printStackTrace();
619: // }
620: // }
621: // }
622: // System.out.println("The JBI Framework is "
623: // + (isJBIRuntimeEnabled ? "Enabled." : "NOT Enabled."));
624: }
625:
626: }
|