/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This 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 (at your option) any later version.
 *
 * This software 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 software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.net.axis;

import org.jboss.axis.EngineConfiguration;
import org.jboss.axis.EngineConfigurationFactory;
import org.jboss.axis.client.AxisClient;
import org.jboss.axis.client.Service;
import org.jboss.axis.configuration.EngineConfigurationFactoryFinder;

import javax.naming.Context;
import javax.naming.Name;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;
import java.util.Hashtable;

/**
 * <p>This service factory will reinstall (wsdl-generated) 
 * web service references and re-attach the stubs to the right 
 * configurations with the help of axis engine configuration factory.</p>
 * <p>It is a temporary alternative to the prototypical axis service factory
 * that only deals with untyped services at this point.</p>
 * @author <a href="mailto:Christoph.Jung@infor.de">Christoph G. Jung</a>
 * @version $Revision: 1.1.2.5 $
 * @since  26.04.02
 */

public class ServiceFactory implements ObjectFactory
{

   /* (non-javadoc)
    * does dereference jndi links consisting of stub classname
    * and string-based configuration names.
    */
   public Object getObjectInstance(Object refObject,
                                   Name name,
                                   Context nameCtx,
                                   Hashtable environment)
           throws Exception
   {
      Service instance = null;
      if (refObject instanceof Reference)
      {
         Reference ref = (Reference)refObject;

         Class serviceClass =
                 Thread.currentThread().getContextClassLoader().loadClass(ref.getClassName());

         EngineConfigurationFactory factory =
                 EngineConfigurationFactoryFinder.newFactory((String)ref
                 .get(Constants.CONFIGURATION_CONTEXT)
                 .getContent());

         EngineConfiguration engine = null;

         if (factory != null)
         {
            engine = factory.getClientEngineConfig();
         }

         instance = (Service)serviceClass.newInstance();

         instance.setEngineConfiguration(engine);
         // and reinstall the engine
         instance.setEngine(new AxisClient(engine));
      }

      return instance;
   }

}
