KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > tools > remote > hessian > ssl > Client


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.examples.tools.remote.hessian.ssl;
10
11 import javax.management.MBeanServerConnection JavaDoc;
12 import javax.management.MBeanServerDelegateMBean JavaDoc;
13 import javax.management.MBeanServerInvocationHandler JavaDoc;
14 import javax.management.Notification JavaDoc;
15 import javax.management.NotificationListener JavaDoc;
16 import javax.management.ObjectName JavaDoc;
17 import javax.management.remote.JMXConnector JavaDoc;
18 import javax.management.remote.JMXConnectorFactory JavaDoc;
19 import javax.management.remote.JMXServiceURL JavaDoc;
20 import javax.management.timer.Timer JavaDoc;
21
22 /**
23  * This example shows how to connect to a Hessian JMXConnectorServer over HTTPS.
24  * To run this example, you need the following jars:
25  * <ul>
26  * <li>MX4J 3.x</li>
27  * <ul>
28  * <li>mx4j.jar</li>
29  * <li>mx4j-remote.jar</li>
30  * <li>mx4j-tools.jar</li>
31  * <li>mx4j-examples.jar</li>
32  * </ul>
33  * <li>Hessian 3.0.8</li>
34  * <ul>
35  * <li>hessian-3.0.8.jar</li>
36  * </ul>
37  * </ul>
38  *
39  * @version $Revision: 1.2 $
40  */

41 public class Client
42 {
43    public static void main(String JavaDoc[] args) throws Exception JavaDoc
44    {
45       // Replace the value with the file path of your keystore.
46
// IMPORTANT: this should NOT be done in production environments, it is shown here just as an example.
47
System.setProperty("javax.net.ssl.trustStore", "<your-keystore>");
48
49       // This JMXServiceURL works only if the connector server is on the same host of
50
// the connector. If this is not the case, set the correct host name.
51
JMXServiceURL JavaDoc address = new JMXServiceURL JavaDoc("hessian+ssl", null, 8443, "/hessianssl");
52
53       // Connect a JSR 160 JMXConnector to the server side
54
JMXConnector JavaDoc connector = JMXConnectorFactory.connect(address);
55
56       // Retrieve an MBeanServerConnection that represent the MBeanServer
57
// the remote connector server is bound to
58
MBeanServerConnection JavaDoc connection = connector.getMBeanServerConnection();
59
60       // Call the server side as if it is a local MBeanServer
61
ObjectName JavaDoc delegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");
62       Object JavaDoc proxy = MBeanServerInvocationHandler.newProxyInstance(connection, delegateName, MBeanServerDelegateMBean JavaDoc.class, true);
63       MBeanServerDelegateMBean JavaDoc delegate = (MBeanServerDelegateMBean JavaDoc)proxy;
64
65       System.out.println(delegate.getImplementationVendor() + " is cool !");
66
67       // Register an MBean, and get notifications via the Hessian protocol
68
connection.addNotificationListener(delegateName, new NotificationListener JavaDoc()
69       {
70          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
71          {
72             System.out.println("Got the following notification: " + notification);
73          }
74       }, null, null);
75
76       ObjectName JavaDoc timerName = ObjectName.getInstance("services:type=Timer");
77       connection.createMBean(Timer JavaDoc.class.getName(), timerName, null);
78
79       // Unregistering the MBean to get another notification
80
connection.unregisterMBean(timerName);
81
82       // Allow the unregistration notification to arrive before killing this JVM
83
Thread.sleep(1000);
84
85       connector.close();
86    }
87 }
88
Popular Tags