You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by gd...@apache.org on 2001/08/22 19:01:24 UTC

cvs commit: xml-axis/java/src/org/apache/axis/providers/java EJBProvider.java JavaProvider.java

gdaniels    01/08/22 10:01:24

  Modified:    java/src/org/apache/axis/providers/java JavaProvider.java
  Added:       java/src/org/apache/axis/providers/java EJBProvider.java
  Log:
  First cut at EJB provider.
  
  This provider works by looking for a "beanJndiName" option on the
  service instead of a "className" one, looking that up in JNDI (it
  should be your bean home object), and calling create() on it to get
  your actual object.  This is quite simple for now, and only really
  works with stateless session beans (no primary key).
  
  To use it, you need four options, which may be set up EITHER on
  the EJBProvider itself OR on your service (service specific values
  will overrride the provider ones):
  
  jndiURL -> the URL to your JNDI server
  jndiContextClass -> the classname of the initial context factory
  jndiUser -> username property for the initialContext
  jndiPassword -> password for same
  
  An example:
  
  <handler name="EJBProvider" class="org.apache.axis.providers.java.EJBProvider">
    <option name="jndiURL" value="ejipt://localhost:2323"/>
    <option name="jndiContextClass" value="allaire.ejipt.ContextFactory"/>
    <option name="jndiUser" value="fred"/>
    <option name="jndiPassword" value="AllYouNeedIsLove"/>
  </handler>
  <service name="EJBService" pivot="EJBProvider">
    <option name="beanJndiName" value="myHomeJNDIName"/>
    <option name="allowedMethods" value="*"/>
  </service>
  
  Revision  Changes    Path
  1.10      +15 -32    xml-axis/java/src/org/apache/axis/providers/java/JavaProvider.java
  
  Index: JavaProvider.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/providers/java/JavaProvider.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- JavaProvider.java	2001/08/21 21:35:13	1.9
  +++ JavaProvider.java	2001/08/22 17:01:24	1.10
  @@ -89,8 +89,9 @@
        * Get the service object whose method actually provides the service.
        * May look up in session table.
        */
  -    public Object getServiceObject (MessageContext msgContext, Handler service,
  -                                    Object so_factory, String clsName)
  +    public Object getServiceObject (MessageContext msgContext,
  +                                    Handler service,
  +                                    String clsName)
           throws Exception
       {
           String serviceName = msgContext.getTargetService();
  @@ -106,7 +107,7 @@
           if (scope.equals("Request")) {
   
               // make a one-off
  -            return getNewServiceObject(so_factory);
  +            return getNewServiceObject(msgContext, clsName);
   
           } else if (scope.equals("Session")) {
   
  @@ -115,13 +116,13 @@
                   // store service objects in session, indexed by class name
                   Object obj = msgContext.getSession().get(serviceName);
                   if (obj == null) {
  -                    obj = getNewServiceObject(so_factory);
  +                    obj = getNewServiceObject(msgContext, clsName);
                       msgContext.getSession().set(serviceName, obj);
                   }
                   return obj;
               } else {
                   // was no incoming session, sigh, treat as request scope
  -                return getNewServiceObject(so_factory);
  +                return getNewServiceObject(msgContext, clsName);
               }
   
           } else if (scope.equals("Application")) {
  @@ -132,13 +133,13 @@
                   // store service objects in session, indexed by class name
                   Object obj = engine.getApplicationSession().get(serviceName);
                   if (obj == null) {
  -                    obj = getNewServiceObject(so_factory);
  +                    obj = getNewServiceObject(msgContext, clsName);
                       engine.getApplicationSession().set(serviceName, obj);
                   }
                   return obj;
               } else {
                   // was no incoming session, sigh, treat as request scope
  -                return getNewServiceObject(so_factory);
  +                return getNewServiceObject(msgContext, clsName);
               }
   
           } else {
  @@ -196,12 +197,6 @@
                   serviceName + "'",
                   null, null);
   
  -        /** ??? Should we enforce setting allowedMethods?  As it was,
  -        * if it's null, we allowed any method.  This seems like it might
  -        * be considered somewhat insecure (it's an easy mistake to
  -        * make).  Tossing an Exception if it's not set, and using "*"
  -        * to explicitly indicate "any method" is probably better.
  -        */
           if ((allowedMethods == null) || allowedMethods.equals(""))
               throw new AxisFault("Server.NoMethodConfig",
                   "No '" +
  @@ -216,9 +211,9 @@
           try {
               int             i ;
   
  -            Object so_factory = getServiceObjectFactory(msgContext, clsName);
  -            Object obj        = getServiceObject(msgContext, service,
  -                                                 so_factory, clsName);
  +            Object obj        = getServiceObject(msgContext,
  +                                                 service,
  +                                                 clsName);
               JavaClass jc	  = new JavaClass(obj.getClass());
   
               Message        reqMsg  = msgContext.getRequestMessage();
  @@ -325,10 +320,12 @@
        *   class wrapped in jc
        *
        */
  -    protected Object getNewServiceObject(Object factory)
  +    protected Object getNewServiceObject(MessageContext msgContext,
  +                                             String clsName)
           throws Exception
       {
  -        JavaClass jc = (JavaClass)factory;
  +        AxisClassLoader cl     = msgContext.getClassLoader();
  +        JavaClass       jc     = cl.lookup(clsName);
   
           return jc.getJavaClass().newInstance();
       }
  @@ -361,18 +358,4 @@
       {
           return allowedMethodsOption;
       }
  -
  -    /**
  -     *
  -     */
  -    protected Object getServiceObjectFactory(MessageContext msgContext,
  -                                             String clsName)
  -        throws Exception
  -    {
  -        AxisClassLoader cl     = msgContext.getClassLoader();
  -        JavaClass       jc     = cl.lookup(clsName);
  -
  -        return jc;
  -    }
  -
   }
  
  
  
  1.1                  xml-axis/java/src/org/apache/axis/providers/java/EJBProvider.java
  
  Index: EJBProvider.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Axis" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.axis.providers.java;
  
  import java.util.* ;
  import org.apache.axis.* ;
  import org.apache.axis.utils.* ;
  import org.apache.axis.utils.cache.* ;
  import org.apache.axis.message.* ;
  import org.apache.axis.providers.BasicProvider;
  
  import java.rmi.*;
  import java.rmi.server.*;
  import java.util.*;
  import javax.naming.*;
  import java.lang.reflect.Method;
  
  /**
   * A basic EJB Provider
   *
   * @author Carl Woolf (cwoolf@macromedia.com)
   */
  public class EJBProvider extends RPCProvider 
  {
      private static final String beanNameOption = "beanJndiName";
      private static final String allowedMethodsOption = "allowedMethods";
      public static final String jndiContextClass = "jndiContextClass";
      public static final String jndiURL = "jndiURL";
      public static final String jndiUsername = "jndiUser";
      public static final String jndiPassword = "jndiPassword";
  
      ///////////////////////////////////////////////////////////////
      ///////////////////////////////////////////////////////////////
      /////// Default methods from JavaProvider ancestor, overridden
      ///////   for ejbeans
      ///////////////////////////////////////////////////////////////
      ///////////////////////////////////////////////////////////////
  
      /**
       *
       */
      protected Object getNewServiceObject(MessageContext msgContext,
                                               String clsName)
          throws Exception
      {
          Handler serviceHandler = msgContext.getServiceHandler();
          Object home;
  
          try
          {
              String username = (String)getStrOption(jndiUsername,
                                                     serviceHandler);
              String password = (String)getStrOption(jndiPassword,
                                                     serviceHandler);
              String factoryClass = (String)getStrOption(jndiContextClass,
                                                         serviceHandler);
              String contextUrl = (String)getStrOption(jndiURL,
                                                       serviceHandler);
  
              Properties properties = new Properties();
  
              properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                                     factoryClass);
              properties.setProperty(Context.PROVIDER_URL,
                                     contextUrl);
  
              properties.setProperty(Context.SECURITY_PRINCIPAL,
                                     username);
              properties.setProperty(Context.SECURITY_CREDENTIALS,
                                     password);
  
              Context context = new InitialContext(properties);
              if (null == context)
              {
                  throw new AxisFault("EJBProvider can't get Context");
              }
  
              home =  context.lookup(clsName);
              if (null == home)
              {
                  throw new AxisFault("EJBProvider can't get Bean Home");
              }
          }
          catch (Exception exception)
          {
              throw new AxisFault(exception);
          }
  
          Class homeClass = home.getClass();
          Class params[] = new Class[0];
          Method createMethod = homeClass.getMethod("create", params);
          
          Object args[] = new Object[0];
          Object result = createMethod.invoke(home, args);
          
          return result;
      }
  
      /**
       *
       */
      protected String getServiceClassName(Handler service)
      {
          return (String) service.getOption( beanNameOption );
      }
      /**
       *
       */
      protected String getServiceAllowedMethods(Handler service)
      {
          return (String) service.getOption( allowedMethodsOption );
      }
      /**
       *
       */
      protected String getServiceClassNameOptionName()
      {
          return beanNameOption;
      }
      /**
       *
       */
      protected String getServiceAllowedMethodsOptionName()
      {
          return allowedMethodsOption;
      }
      
      /**
       * Get a String option by looking first in the service options,
       * and then at the Handler's options.  This allows defaults to be
       * specified at the provider level, and then overriden for particular
       * services.
       *
       * @param optionName the option to retrieve
       * @return String the value of the option or null if not found in
       *                either scope
       */
      private String getStrOption(String optionName, Handler service)
      {
          String value = null;
          if (service != null)
              value = (String)service.getOption(optionName);
          if (value == null)
              value = (String)getOption(optionName);
          return value;
      }
  }