KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SimpleJmxClient


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
6  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Initial developer(s): Marc Wick.
21  * Contributor(s): Emmanuel Cecchet.
22  */

23
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import javax.management.MBeanServerConnection JavaDoc;
30 import javax.management.MBeanServerInvocationHandler JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.remote.JMXConnector JavaDoc;
33 import javax.management.remote.JMXConnectorFactory JavaDoc;
34 import javax.management.remote.JMXServiceURL JavaDoc;
35 import javax.naming.Context JavaDoc;
36 import javax.security.auth.Subject JavaDoc;
37
38 import org.continuent.sequoia.common.authentication.PasswordAuthenticator;
39 import org.continuent.sequoia.common.jmx.JmxConstants;
40 import org.continuent.sequoia.common.jmx.mbeans.VirtualDatabaseMBean;
41 import org.continuent.sequoia.common.users.VirtualDatabaseUser;
42
43 /**
44  * This class defines a SimpleJmxClient
45  *
46  * @author <a HREF="mailto:marc.wick@monte-bre.ch">Marc Wick </a>
47  * @version 1.0
48  */

49 public class SimpleJmxClient
50 {
51
52   /**
53    * a simple jmx client exmaple to show how to access the c-jdbc controller via
54    * jmx
55    *
56    * @param args - no args
57    * @throws Exception - an exception
58    */

59   public static void main(String JavaDoc[] args) throws Exception JavaDoc
60   {
61     String JavaDoc port = "8091";
62     String JavaDoc host = "localhost";
63     String JavaDoc vdbName = "rubis";
64     JMXServiceURL JavaDoc address = new JMXServiceURL JavaDoc("rmi", host, 0, "/jndi/jrmp");
65
66     Map JavaDoc environment = new HashMap JavaDoc();
67     environment.put(Context.INITIAL_CONTEXT_FACTORY,
68         "com.sun.jndi.rmi.registry.RegistryContextFactory");
69     environment.put(Context.PROVIDER_URL, "rmi://" + host + ":" + port);
70
71     // use username and password for authentication of connections
72
// with the controller, the values are compared to the ones
73
// specified in the controller.xml config file.
74
// this line is not required if no username/password has been configered
75
environment.put(JMXConnector.CREDENTIALS, PasswordAuthenticator
76         .createCredentials("jmxuser", "jmxpassword"));
77
78     JMXConnector JavaDoc connector = JMXConnectorFactory.connect(address, environment);
79
80     ObjectName JavaDoc db = JmxConstants.getVirtualDataBaseObjectName(vdbName);
81
82     // we build a subject for authentication
83
VirtualDatabaseUser dbUser = new VirtualDatabaseUser("admin", "c-jdbc");
84     Set JavaDoc principals = new HashSet JavaDoc();
85     principals.add(dbUser);
86     Subject JavaDoc subj = new Subject JavaDoc(true, principals, new HashSet JavaDoc(), new HashSet JavaDoc());
87
88     // we open a connection for this subject, all susequent calls with this
89
// connection will be executed on the behalf of our subject.
90
MBeanServerConnection JavaDoc delegateConnection = connector
91         .getMBeanServerConnection(subj);
92
93     // we create a proxy to the virtual database
94
VirtualDatabaseMBean proxy = (VirtualDatabaseMBean) MBeanServerInvocationHandler
95         .newProxyInstance(delegateConnection, db, VirtualDatabaseMBean.class,
96             false);
97
98     // we call a method on the virtual database
99
System.out.println(proxy.getAllBackendNames());
100
101   }
102 }
103
Popular Tags