KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > modules > jboss > JBossServerConnectionFactory


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.jboss;
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 import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
23 import org.jboss.security.SecurityAssociation;
24 import org.jboss.security.SimplePrincipal;
25
26 import javax.naming.NamingException JavaDoc;
27 import javax.naming.Context JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29
30 import java.util.Hashtable JavaDoc;
31
32 /**
33  *
34  * date: Oct 30, 2004
35  * @author Prem, Shashank Bellary
36  */

37 public class JBossServerConnectionFactory implements ServerConnectionFactory{
38
39     /**
40      * @return instance of ServerConnection corresponding to this jboss
41      * module.
42      */

43     public ServerConnection getServerConnection(ApplicationConfig config)
44         throws ConnectionFailedException {
45
46         try {
47             RMIAdaptor rmiAdaptor = findExternal(config.getURL(),
48                     config.getUsername(), config.getPassword());
49             return new JBossServerConnection(rmiAdaptor);
50         } catch (Throwable JavaDoc e) {
51             throw new ConnectionFailedException(e);
52         }
53     }
54
55     /**
56      *
57      * @param url
58      * @return
59      * @throws NamingException
60      */

61     private static RMIAdaptor findExternal(String JavaDoc url, String JavaDoc username, String JavaDoc password)
62             throws NamingException JavaDoc {
63
64         Hashtable JavaDoc props = new Hashtable JavaDoc();
65         props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
66         props.put(Context.PROVIDER_URL, url);
67         props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
68
69         if(username != null){
70             SecurityAssociation.setPrincipal(new SimplePrincipal(username));
71             SecurityAssociation.setCredential(password);
72         }
73
74         Context JavaDoc ctx = new InitialContext JavaDoc(props);
75         RMIAdaptor rmiAdaptor = (RMIAdaptor)ctx.lookup("jmx/rmi/RMIAdaptor");
76         ctx.close();
77         return rmiAdaptor;
78     }
79
80 }
81
Popular Tags