001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.server.axis2.client;
017:
018: import org.apache.axis2.AxisFault;
019: import org.apache.axis2.context.ConfigurationContext;
020: import org.apache.axis2.context.ConfigurationContextFactory;
021: import org.apache.axis2.jaxws.ClientConfigurationFactory;
022: import org.apache.axis2.jaxws.util.ClassLoaderUtils;
023: import org.apache.axis2.jaxws.util.Constants;
024: import org.apache.openejb.util.Logger;
025: import org.apache.openejb.util.LogCategory;
026:
027: import java.util.Hashtable;
028: import java.util.Map;
029:
030: public class Axis2ClientConfigurationFactory extends
031: ClientConfigurationFactory {
032: private static final Logger logger = Logger.getInstance(
033: LogCategory.AXIS2, Axis2ClientConfigurationFactory.class);
034:
035: private Map<ClassLoader, ConfigurationContext> contextCache = new Hashtable<ClassLoader, ConfigurationContext>();
036:
037: private boolean reuseConfigurationContext;
038:
039: public Axis2ClientConfigurationFactory(boolean reuse) {
040: this .reuseConfigurationContext = reuse;
041: }
042:
043: public ConfigurationContext getClientConfigurationContext() {
044: ClassLoader cl = ClassLoaderUtils.getContextClassLoader();
045: if (cl == null) {
046: if (this .reuseConfigurationContext) {
047: cl = ClientConfigurationFactory.class.getClassLoader();
048: } else {
049: return createConfigurationContext();
050: }
051: }
052:
053: synchronized (cl) {
054: return getConfigurationContext(cl);
055: }
056: }
057:
058: private ConfigurationContext getConfigurationContext(ClassLoader cl) {
059: ConfigurationContext context = this .contextCache.get(cl);
060: if (context == null) {
061: context = createConfigurationContext();
062: this .contextCache.put(cl, context);
063: if (logger.isDebugEnabled()) {
064: logger.debug("Created new configuration context "
065: + context + " for " + cl);
066: }
067: } else {
068: if (logger.isDebugEnabled()) {
069: logger.debug("Configuration context " + context
070: + " reused for " + cl);
071: }
072: }
073: return context;
074: }
075:
076: private ConfigurationContext removeConfigurationContext(
077: ClassLoader cl) {
078: return this .contextCache.remove(cl);
079: }
080:
081: public void clearCache() {
082: this .contextCache.clear();
083: }
084:
085: public ConfigurationContext clearCache(ClassLoader cl) {
086: ConfigurationContext context = null;
087: if (cl != null) {
088: synchronized (cl) {
089: context = removeConfigurationContext(cl);
090: }
091:
092: if (logger.isDebugEnabled()) {
093: logger.debug("Removed configuration context " + context
094: + " for " + cl);
095: }
096: }
097:
098: return context;
099: }
100:
101: private ConfigurationContext createConfigurationContext() {
102: String repoPath = System.getProperty(Constants.AXIS2_REPO_PATH);
103: String axisConfigPath = System
104: .getProperty(Constants.AXIS2_CONFIG_PATH);
105: try {
106: return ConfigurationContextFactory
107: .createConfigurationContextFromFileSystem(repoPath,
108: axisConfigPath);
109: } catch (AxisFault e) {
110: throw new RuntimeException(e.getMessage(), e);
111: }
112: }
113:
114: }
|