KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > modules > jsr160 > JSR160ServerConnectionFactory


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jmanage.core.modules.jsr160;
17
18 import org.jmanage.core.management.ServerConnection;
19 import org.jmanage.core.management.ConnectionFailedException;
20 import org.jmanage.core.management.ServerConnectionFactory;
21 import org.jmanage.core.config.ApplicationConfig;
22
23 import javax.management.remote.JMXServiceURL JavaDoc;
24 import javax.management.remote.JMXConnector JavaDoc;
25 import javax.management.remote.JMXConnectorFactory JavaDoc;
26 import javax.management.MBeanServerConnection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  *
32  * date: Aug 12, 2004
33  * @author Rakesh Kalra
34  */

35 public class JSR160ServerConnectionFactory implements ServerConnectionFactory{
36
37     /**
38      * @return instance of ServerConnection corresponding to this jsr160
39      * module.
40      */

41     public ServerConnection getServerConnection(ApplicationConfig config)
42         throws ConnectionFailedException {
43
44         try {
45             /* Create an RMI connector client */
46             HashMap JavaDoc env = new HashMap JavaDoc();
47             String JavaDoc[] credentials = new String JavaDoc[] {config.getUsername(),
48                                                  config.getPassword()};
49             env.put("jmx.remote.credentials", credentials);
50
51             Map JavaDoc params = config.getParamValues();
52             final String JavaDoc jndiFactory =
53                     (String JavaDoc)params.get(JSR160ApplicationConfig.JNDI_FACTORY);
54             final String JavaDoc jndiURL =
55                     (String JavaDoc)params.get(JSR160ApplicationConfig.JNDI_URL);
56
57             if(jndiFactory != null)
58                 env.put(JSR160ApplicationConfig.JNDI_FACTORY, jndiFactory);
59             if(jndiURL != null)
60                 env.put(JSR160ApplicationConfig.JNDI_URL, jndiURL);
61
62             JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc(config.getURL());
63             JMXConnector JavaDoc jmxc = JMXConnectorFactory.connect(url, env);
64             return new JSR160ServerConnection(jmxc,
65                     jmxc.getMBeanServerConnection());
66         } catch (Throwable JavaDoc e) {
67             throw new ConnectionFailedException(e);
68         }
69     }
70 }
71
Popular Tags