KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > management > ServerConnector


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.management;
17
18 import org.jmanage.core.config.*;
19 import org.jmanage.core.util.Loggers;
20
21 import java.lang.reflect.Proxy JavaDoc;
22 import java.util.logging.Logger JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Collections JavaDoc;
26
27 /**
28  *
29  * date: Aug 12, 2004
30  * @author Rakesh Kalra
31  */

32 public class ServerConnector {
33
34     private static final Logger JavaDoc logger =
35             Loggers.getLogger(ServerConnector.class);
36
37     /**
38      * Returns a ServerConnection for the given application config.
39      *
40      * @param appConfig
41      * @return
42      * @throws ConnectionFailedException
43      */

44     public static ServerConnection
45             getServerConnection(ApplicationConfig appConfig)
46         throws ConnectionFailedException{
47
48         ApplicationType appType =
49                 ApplicationTypes.getApplicationType(appConfig.getType());
50         assert appType != null: "Invalid type=" + appConfig.getType();
51         ModuleConfig moduleConfig = appType.getModule();
52         final ClassLoader JavaDoc classLoader = appType.getClassLoader();
53         assert classLoader != null;
54
55         final ClassLoader JavaDoc contextClassLoader =
56                 Thread.currentThread().getContextClassLoader();
57         /* temporarily change the thread context classloader */
58         Thread.currentThread().setContextClassLoader(classLoader);
59
60         try {
61             logger.fine("Connecting to " + appConfig.getURL());
62             final ServerConnectionFactory factory =
63                     getServerConnectionFactory(moduleConfig, classLoader);
64             ServerConnection connection =
65                     factory.getServerConnection(appConfig);
66             logger.fine("Connected to " + appConfig.getURL());
67             ServerConnectionProxy proxy = new ServerConnectionProxy(connection,
68                     classLoader);
69             return (ServerConnection)Proxy.newProxyInstance(
70                     ServerConnector.class.getClassLoader(),
71                     new Class JavaDoc[]{ServerConnection.class},
72                     proxy);
73         } catch(ConnectionFailedException e){
74             logger.info("Failed to connect. error=" + e.getMessage());
75             throw e;
76         } finally {
77             /* change the thread context classloader back to the
78                     original classloader*/

79             Thread.currentThread().setContextClassLoader(contextClassLoader);
80         }
81     }
82
83     private static ServerConnectionFactory
84             getServerConnectionFactory(ModuleConfig moduleConfig,
85                                        ClassLoader JavaDoc classLoader) {
86
87         assert classLoader != null;
88         ServerConnectionFactory factory = (ServerConnectionFactory)
89                 factories.get(moduleConfig.getConnectionFactory());
90         if(factory == null){
91             try {
92                 final Class JavaDoc factoryClass =
93                         Class.forName(moduleConfig.getConnectionFactory(),
94                                 true, classLoader);
95                 factory = (ServerConnectionFactory)factoryClass.newInstance();
96                 factories.put(moduleConfig.getConnectionFactory(), factory);
97             } catch (Exception JavaDoc e) {
98                 throw new RuntimeException JavaDoc(e);
99             }
100         }
101         return factory;
102     }
103
104     /* factory class name to factory instance mapping */
105     private static final Map JavaDoc factories =
106             Collections.synchronizedMap(new HashMap JavaDoc());
107 }
108
Popular Tags