/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999-2004 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
*/

package org.objectweb.jonas.ws.axis;

import javax.naming.Context;
import javax.xml.namespace.QName;

import org.apache.axis.AxisProperties;
import org.apache.axis.deployment.wsdd.WSDDConstants;
import org.apache.axis.deployment.wsdd.WSDDProvider;

import org.objectweb.jonas_lib.loader.ThreadContextClassLoader;

import org.objectweb.jonas.ws.AbsWebServicesServiceImpl;
import org.objectweb.jonas.ws.WSServiceException;

import org.objectweb.util.monolog.api.BasicLevel;

/**
 * Implements commons methods declared within the abstract class. It's used
 * AXIS classes for specific deployment aspects.
 *
 * @author Guillaume Sauthier
 * @author Xavier Delplanque
 */
public class AxisWSServiceImpl extends AbsWebServicesServiceImpl {

    /** Used to tell Axis engine to use a new EngineConfigurationFactory */
    private static final String AXIS_CONFIG_FACTORY_PROP =
        "axis.EngineConfigFactory";

    /** The class name of the new Factory */
    private static final String AXIS_MODULE_CONFIG_CLASS =
        "org.objectweb.jonas.ws.axis.JServletEngineConfigurationFactory";

    /**
     * Init the AxisWSService.
     *
     * @param ctx Context for configuration.
     *
     * @throws WSServiceException when init fails.
     */
    public void doInit(Context ctx) throws WSServiceException {
        super.doInit(ctx);

        getLogger().log(BasicLevel.DEBUG, "Adding " + AXIS_MODULE_CONFIG_CLASS + " as ConfigurationFactory");

        //set our custom provider
        QName javaURI = new QName(WSDDConstants.URI_WSDD_JAVA, WSDDJOnASEJBProvider.PROVIDER_NAME);
        WSDDProvider.registerProvider(javaURI, new WSDDJOnASEJBProvider());

        // Add our Config Factory in Axis
        AxisProperties.setProperty(AXIS_CONFIG_FACTORY_PROP,
                                   AXIS_MODULE_CONFIG_CLASS);

        // Fix for Bug #300844
        // Use a ClassLoader that will delegate to call-time real context ClassLoader
        ClassLoader old = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(new ThreadContextClassLoader());
        AxisProperties.getNameDiscoverer();
        Thread.currentThread().setContextClassLoader(old);
    }

}
