/*
 * 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.server;

import org.jboss.axis.Handler;
import org.jboss.axis.MessageContext;
import org.jboss.axis.providers.java.RPCProvider;

import javax.ejb.EJBHome;
import javax.naming.InitialContext;

/**
 * A JBoss-compatible Provider that exposes the methods of
 * a bean's home, such as a stateless session bean or an entity
 * bean. It is working under the presumption that the right classloader 
 * has already been set by the invocation chain 
 * (@see org.jboss.net.axis.SetClassLoaderHandler).
 * <br>
 * <h3>Change History</h3>
 * <ul>
 * </ul>
 * <br>
 * <h3>To Do</h3>
 * <ul>
 * <li> jung, 22.03.02: Service-Reference serialisation. </li>
 * </ul>
 * @author <a href="mailto:Christoph.Jung@infor.de">Christoph G. Jung</a>
 * @created 22.03.2002
 * @version $Revision: 1.1.4.5 $
 */

public class EJBHomeProvider extends RPCProvider
{

   private static final String beanNameOption = "beanJndiName";
   private static final String homeInterfaceNameOption = "homeInterfaceName";

   /** Creates new EJBProvider */
   public EJBHomeProvider()
   {
   }

   /**
    * Return the object which implements the service. Makes the usual
    * JNDI->lookup call wo the  PortableRemoteDaDaDa for the sake of Corba.
    * @param msgContext the message context
    * @param clsName The JNDI name of the EJB home class
    * @return an object that implements the service
    */
   protected Object getNewServiceObject(MessageContext msgContext, String clsName)
           throws Exception
   {
      // Get the EJB Home object from JNDI
      Object result = new InitialContext().lookup(clsName);

      return result;
   }

   /**
    * Return the option in the configuration that contains the service class
    * name.  In the EJB case, it is the JNDI name of the bean.
    */
   protected String getServiceClassNameOptionName()
   {
      return beanNameOption;
   }

   /**
    * Get the class description for the EJB Remote Interface, which is what
    * we are interested in exposing to the world (i.e. in WSDL).
    * 
    * @param msgContext the message context
    * @param beanJndiName the JNDI name of the EJB
    * @return the class info of the EJB home interface
    */
   protected Class getServiceClass(MessageContext msgContext,
                                   String beanJndiName) throws Exception
   {
      Handler serviceHandler = msgContext.getService();
      Class interfaceClass = null;
        
      // First try to get the interface class from the configuation
      String homeName =
              (String)serviceHandler.getOption(homeInterfaceNameOption);
      if (homeName != null)
      {
         interfaceClass = msgContext.getClassLoader().loadClass(homeName);
      }
      else
      {
         // we look into the metadata
         EJBHome home = (EJBHome)getNewServiceObject(msgContext, beanJndiName);
         interfaceClass = home.getEJBMetaData().getHomeInterfaceClass();
      }
            
      // got it, return it
      return interfaceClass;
   }

}