/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.geronimo.axis2.client;

import java.net.URL;

import org.apache.axis2.jaxws.ClientConfigurationFactory;
import org.apache.axis2.metadata.registry.MetadataFactoryRegistry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.geronimo.gbean.AbstractName;
import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoBuilder;
import org.apache.geronimo.gbean.GBeanLifecycle;
import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
import org.apache.geronimo.kernel.Kernel;

public class Axis2ConfigGBean implements GBeanLifecycle {

    private static final Log LOG = LogFactory.getLog(Axis2ConfigGBean.class);
    
    private AbstractName moduleName;
    private ClassLoader classLoder;   

    public Axis2ConfigGBean(ClassLoader classLoader,                          
                            Kernel kernel,                          
                            URL configurationBaseUrl,
                            AbstractName moduleName) {
        this.moduleName = moduleName;
        this.classLoder = classLoader;
    }

    private synchronized static Axis2ClientConfigurationFactory registerClientConfigurationFactory() {
        ClientConfigurationFactory factory =
            (ClientConfigurationFactory)MetadataFactoryRegistry.getFactory(ClientConfigurationFactory.class);
        if (factory instanceof Axis2ClientConfigurationFactory) {
            return (Axis2ClientConfigurationFactory)factory;
        } else {
            factory = new Axis2ClientConfigurationFactory(false);
            MetadataFactoryRegistry.setFactory(ClientConfigurationFactory.class, factory);
            LOG.debug("Registered client configuration factory: " + factory);
            return (Axis2ClientConfigurationFactory)factory;
        }
    }
    
    public void doStart() throws Exception {
        registerClientConfigurationFactory();
    }
    
    public void doStop() throws Exception {             
        registerClientConfigurationFactory().clearCache(this.classLoder);
    }

    public void doFail() {
    }

    public static final GBeanInfo GBEAN_INFO;

    static {
        GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(Axis2ConfigGBean.class, Axis2ConfigGBean.class, NameFactory.GERONIMO_SERVICE);
                
        infoFactory.addAttribute("classLoader", ClassLoader.class, false);
        infoFactory.addAttribute("kernel", Kernel.class, false);
        infoFactory.addAttribute("configurationBaseUrl", URL.class, true);
        infoFactory.addAttribute("moduleName", AbstractName.class, true);
        
        infoFactory.setConstructor(new String[]{"classLoader", "kernel", "configurationBaseUrl", "moduleName"});
        
        GBEAN_INFO = infoFactory.getBeanInfo();
    }

    public static GBeanInfo getGBeanInfo() {
        return GBEAN_INFO;
    }

}

@@ -889,40 +889,165 @@
 is2.
-jaxws.ClientConfigurationFactory
+context.ConfigurationContext;%0Aimport org.apache.axis2.jaxws.ClientConfigurationFactory;%0Aimport org.apache.axis2.jaxws.description.impl.DescriptionFactoryImpl
 ;%0Aim
@@ -2921,32 +2921,81 @@
  %7B             %0A
+        ConfigurationContext configContext =%0A    
         register
@@ -3052,16 +3052,92 @@
 Loder);%0A
+        DescriptionFactoryImpl.clearServiceDescriptionCache(configContext);%0A
     %7D%0A%0A 

clear axis2 ServiceDescription cache on module shutdonw