001: /**
002: * Copyright 2004-2005 jManage.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package org.jmanage.core.management;
016:
017: import org.jmanage.core.config.*;
018: import org.jmanage.core.util.Loggers;
019:
020: import java.lang.reflect.Proxy;
021: import java.util.logging.Logger;
022: import java.util.Map;
023: import java.util.HashMap;
024: import java.util.Collections;
025:
026: /**
027: *
028: * date: Aug 12, 2004
029: * @author Rakesh Kalra
030: */
031: public class ServerConnector {
032:
033: private static final Logger logger = Loggers
034: .getLogger(ServerConnector.class);
035:
036: /**
037: * Returns a ServerConnection for the given application config.
038: *
039: * @param appConfig
040: * @return
041: * @throws ConnectionFailedException
042: */
043: public static ServerConnection getServerConnection(
044: ApplicationConfig appConfig)
045: throws ConnectionFailedException {
046:
047: ApplicationType appType = ApplicationTypes
048: .getApplicationType(appConfig.getType());
049: assert appType != null : "Invalid type=" + appConfig.getType();
050: ModuleConfig moduleConfig = appType.getModule();
051: final ClassLoader classLoader = appType.getClassLoader();
052: assert classLoader != null;
053:
054: final ClassLoader contextClassLoader = Thread.currentThread()
055: .getContextClassLoader();
056: /* temporarily change the thread context classloader */
057: Thread.currentThread().setContextClassLoader(classLoader);
058:
059: try {
060: logger.fine("Connecting to " + appConfig.getURL());
061: final ServerConnectionFactory factory = getServerConnectionFactory(
062: moduleConfig, classLoader);
063: ServerConnection connection = factory
064: .getServerConnection(appConfig);
065: logger.fine("Connected to " + appConfig.getURL());
066: ServerConnectionProxy proxy = new ServerConnectionProxy(
067: connection, classLoader);
068: return (ServerConnection) Proxy.newProxyInstance(
069: ServerConnector.class.getClassLoader(),
070: new Class[] { ServerConnection.class }, proxy);
071: } catch (ConnectionFailedException e) {
072: logger.info("Failed to connect. error=" + e.getMessage());
073: throw e;
074: } finally {
075: /* change the thread context classloader back to the
076: original classloader*/
077: Thread.currentThread().setContextClassLoader(
078: contextClassLoader);
079: }
080: }
081:
082: private static ServerConnectionFactory getServerConnectionFactory(
083: ModuleConfig moduleConfig, ClassLoader classLoader) {
084:
085: assert classLoader != null;
086: ServerConnectionFactory factory = (ServerConnectionFactory) factories
087: .get(moduleConfig.getConnectionFactory());
088: if (factory == null) {
089: try {
090: final Class factoryClass = Class.forName(moduleConfig
091: .getConnectionFactory(), true, classLoader);
092: factory = (ServerConnectionFactory) factoryClass
093: .newInstance();
094: factories.put(moduleConfig.getConnectionFactory(),
095: factory);
096: } catch (Exception e) {
097: throw new RuntimeException(e);
098: }
099: }
100: return factory;
101: }
102:
103: /* factory class name to factory instance mapping */
104: private static final Map factories = Collections
105: .synchronizedMap(new HashMap());
106: }
|