KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > modules > weblogic > WLServerConnectionFactory


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.weblogic;
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 weblogic.management.MBeanHome;
23
24 import javax.naming.NamingException JavaDoc;
25 import javax.naming.Context JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import java.util.Hashtable JavaDoc;
28
29 /**
30  *
31  * date: Aug 12, 2004
32  * @author Rakesh Kalra, Shashank Bellary
33  */

34 public class WLServerConnectionFactory implements ServerConnectionFactory{
35
36     /**
37      * @return instance of ServerConnection corresponding to this weblogic
38      * module.
39      */

40     public ServerConnection getServerConnection(ApplicationConfig config)
41         throws ConnectionFailedException {
42
43         try {
44             MBeanHome home = findExternal(config.getURL(), config.getUsername(),
45                     config.getPassword());
46             return new WLServerConnection(home.getMBeanServer());
47         } catch (Throwable JavaDoc e) {
48             throw new ConnectionFailedException(e);
49         }
50     }
51
52     private static MBeanHome findExternal(String JavaDoc url,
53                                           String JavaDoc username,
54                                           String JavaDoc password)
55             throws NamingException JavaDoc {
56
57         Hashtable JavaDoc props = new Hashtable JavaDoc();
58         props.put(Context.INITIAL_CONTEXT_FACTORY,
59                 "weblogic.jndi.WLInitialContextFactory");
60         props.put(Context.PROVIDER_URL, url);
61         props.put(Context.SECURITY_PRINCIPAL, username);
62         props.put(Context.SECURITY_CREDENTIALS, password);
63         Context JavaDoc ctx = new InitialContext JavaDoc(props);
64         MBeanHome home = (MBeanHome) ctx.lookup(MBeanHome.JNDI_NAME + "." +
65                         "localhome");
66         ctx.close();
67         return home;
68     }
69
70 }
71
Popular Tags