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     */
017    package org.apache.geronimo.axis2.client;
018    
019    import java.util.Hashtable;
020    import java.util.Map;
021    
022    import org.apache.axis2.AxisFault;
023    import org.apache.axis2.context.ConfigurationContext;
024    import org.apache.axis2.context.ConfigurationContextFactory;
025    import org.apache.axis2.jaxws.ClientConfigurationFactory;
026    import org.apache.axis2.jaxws.util.ClassLoaderUtils;
027    import org.apache.axis2.jaxws.util.Constants;
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    
031    public class Axis2ClientConfigurationFactory extends ClientConfigurationFactory {
032    
033        private static final Log LOG = LogFactory.getLog(Axis2ClientConfigurationFactory.class);
034        
035        private Map<ClassLoader, ConfigurationContext> contextCache = 
036            new Hashtable<ClassLoader, ConfigurationContext>();
037    
038        private boolean reuseConfigurationContext;
039        
040        public Axis2ClientConfigurationFactory(boolean reuse) {
041            this.reuseConfigurationContext = reuse;
042        }
043        
044        public ConfigurationContext getClientConfigurationContext() {
045            ClassLoader cl = ClassLoaderUtils.getContextClassLoader();
046            if (cl == null) {
047                if (this.reuseConfigurationContext) {
048                    cl = ClientConfigurationFactory.class.getClassLoader();
049                } else {
050                    return createConfigurationContext();
051                }
052            }
053    
054            synchronized (cl) {
055                return getConfigurationContext(cl);
056            }
057        }
058        
059        private ConfigurationContext getConfigurationContext(ClassLoader cl) {
060            ConfigurationContext context = this.contextCache.get(cl);
061            if (context == null) {            
062                context = createConfigurationContext();
063                this.contextCache.put(cl, context);
064                if (LOG.isDebugEnabled()) {
065                    LOG.debug("Created new configuration context " + context + "  for " + cl);
066                }
067            } else {
068                if (LOG.isDebugEnabled()) {
069                    LOG.debug("Configuration context " + context + " reused for " + cl);
070                }
071            }
072            return context;
073        }
074        
075        private ConfigurationContext removeConfigurationContext(ClassLoader cl) {
076            return this.contextCache.remove(cl);
077        }
078        
079        public void clearCache() {
080            this.contextCache.clear();
081        }
082        
083        public ConfigurationContext clearCache(ClassLoader cl) {
084            ConfigurationContext context = null;
085            if (cl != null) {
086                synchronized (cl) {
087                    context = removeConfigurationContext(cl);
088                }
089                
090                if (LOG.isDebugEnabled()) {
091                    LOG.debug("Removed configuration context " + context + " for " + cl);
092                }
093            }
094    
095            return context;
096        }
097        
098        private ConfigurationContext createConfigurationContext() {
099            String repoPath = System.getProperty(Constants.AXIS2_REPO_PATH);
100            String axisConfigPath = System.getProperty(Constants.AXIS2_CONFIG_PATH);
101            try {
102                return ConfigurationContextFactory.createConfigurationContextFromFileSystem(repoPath, axisConfigPath);
103            } catch (AxisFault e) {
104                throw new RuntimeException(e.getMessage(), e);
105            }
106        }
107    
108    }