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/07/10 04:29:33 UTC

cvs commit: xml-axis/java/src/org/apache/axis/utils JavaUtils.java

gdaniels    01/07/09 19:29:33

  Modified:    java/samples/echo TestClient.java
               java/src/org/apache/axis/encoding BeanSerializer.java
               java/src/org/apache/axis/providers/java RPCProvider.java
  Added:       java/src/org/apache/axis/utils JavaUtils.java
  Log:
  Move convert() method into a new utility class JavaUtils.
  
  Modify TestClient for the echo test to use convert() to get arrays for
  equality check.
  
  When we have service descriptions fully integrated, the system will automatically
  convert return types from the canonical form (i.e. Vectors for SOAP arrays)
  to the desired class.
  
  Revision  Changes    Path
  1.17      +7 -0      xml-axis/java/samples/echo/TestClient.java
  
  Index: TestClient.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/samples/echo/TestClient.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- TestClient.java	2001/07/09 20:34:22	1.16
  +++ TestClient.java	2001/07/10 02:29:26	1.17
  @@ -57,6 +57,7 @@
   
   import java.lang.reflect.Array;
   import java.util.Date;
  +import java.util.List;
   import java.math.BigDecimal;
   
   import org.apache.axis.AxisFault ;
  @@ -69,6 +70,7 @@
   import org.apache.axis.message.RPCParam;
   import org.apache.axis.utils.Options ;
   import org.apache.axis.utils.QName ;
  +import org.apache.axis.utils.JavaUtils;
   
   /**
    * Test Client for the echo interop service.  See the main entrypoint
  @@ -95,6 +97,11 @@
          if (obj1 instanceof Date && obj2 instanceof Date)
              if (Math.abs(((Date)obj1).getTime()-((Date)obj2).getTime())<1000)
                  return true;
  +       if (obj1 instanceof List)
  +         obj1 = JavaUtils.convert(obj1, new Object[]{}.getClass());
  +       if (obj2 instanceof List)
  +         obj2 = JavaUtils.convert(obj2, new Object[]{}.getClass());
  +       
          if (!obj2.getClass().isArray()) return false;
          if (!obj1.getClass().isArray()) return false;
          if (Array.getLength(obj1) != Array.getLength(obj2)) return false;
  
  
  
  1.8       +2 -2      xml-axis/java/src/org/apache/axis/encoding/BeanSerializer.java
  
  Index: BeanSerializer.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/encoding/BeanSerializer.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- BeanSerializer.java	2001/07/09 21:31:21	1.7
  +++ BeanSerializer.java	2001/07/10 02:29:29	1.8
  @@ -67,7 +67,7 @@
   import org.apache.axis.encoding.*;
   import org.apache.axis.utils.Debug;
   import org.apache.axis.utils.QName;
  -import org.apache.axis.providers.java.RPCProvider;
  +import org.apache.axis.utils.JavaUtils;
   
   import org.xml.sax.*;
   
  @@ -200,7 +200,7 @@
               try {
                   pd.getWriteMethod().invoke(object, new Object[] {value});
               } catch (Exception e) {
  -                value = RPCProvider.convert(value, pd.getPropertyType());
  +                value = JavaUtils.convert(value, pd.getPropertyType());
                   try {
                       pd.getWriteMethod().invoke(object, new Object[] {value});
                   } catch (Exception ex) {
  
  
  
  1.10      +1 -64     xml-axis/java/src/org/apache/axis/providers/java/RPCProvider.java
  
  Index: RPCProvider.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/providers/java/RPCProvider.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- RPCProvider.java	2001/07/09 21:31:46	1.9
  +++ RPCProvider.java	2001/07/10 02:29:31	1.10
  @@ -71,66 +71,6 @@
   public class RPCProvider extends JavaProvider {
       private static final boolean DEBUG_LOG = false;
       
  -    /** Utility function to convert an Object to some desired Class.
  -     * 
  -     * Right now this only works for arrays -> Lists, but it might be
  -     * expanded into a more general form (or pulled out into another
  -     * class) later.
  -     * 
  -     * @param arg the array to convert
  -     * @param destClass the actual class we want (must implement List)
  -     */
  -    public static Object convert(Object arg, Class destClass)
  -    {
  -      if (DEBUG_LOG) {
  -        System.out.println("Converting " + arg + " to " + destClass.getName());
  -      }
  -      
  -      if (!(arg instanceof List))
  -        return arg;
  -      
  -      List list = (List)arg;
  -      int length = list.size();
  -      
  -      if (destClass.isArray()) {
  -        if (destClass.getComponentType().isPrimitive()) {
  -          
  -          Object array = Array.newInstance(destClass.getComponentType(), length);
  -          for (int i = 0; i < length; i++) {
  -            Array.set(array, i, list.get(i));
  -          }
  -          return array;
  -          
  -        } else {
  -          Object [] array;
  -          try {
  -            array = (Object [])Array.newInstance(destClass.getComponentType(), length);
  -          } catch (Exception e) {
  -            return arg;
  -          }
  -          
  -          return list.toArray(array);
  -        }
  -      }
  -      
  -      if (List.class.isAssignableFrom(destClass)) {
  -        List newList = null;
  -        try {
  -          newList = (List)destClass.newInstance();
  -        } catch (Exception e) {
  -          // Couldn't build one for some reason... so forget it.
  -          return arg;
  -        }
  -        
  -        for (int j = 0; j < ((List)arg).size(); j++) {
  -          newList.add(list.get(j));
  -        }
  -        return newList;
  -      }
  -      
  -      return arg;
  -    }
  -    
       public void processMessage (MessageContext msgContext,
                                   String clsName,
                                   String methodName,
  @@ -248,10 +188,7 @@
                   Object thisArg = argValues[i];
                   if (!params[i].isAssignableFrom(thisArg.getClass())) {
                     // Attempt conversion for each non-assignable argument
  -                  Debug.Print(3, "Trying to convert " +
  -                                 thisArg.getClass().getName() +
  -                                 " to " + params[i].getName());
  -                  Object newArg = convert(thisArg, params[i]);
  +                  Object newArg = JavaUtils.convert(thisArg, params[i]);
                     if (newArg != thisArg)
                       argValues[i] = newArg;
                   }
  
  
  
  1.1                  xml-axis/java/src/org/apache/axis/utils/JavaUtils.java
  
  Index: JavaUtils.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.utils;
  
  import java.lang.reflect.*;
  import java.util.*;
  
  /** Utility class to deal with Java language related issues, such
   * as type conversions.
   * 
   * @author Glen Daniels (gdaniels@macromedia.com)
   */
  public class JavaUtils
  {
    /** Utility function to convert an Object to some desired Class.
     * 
     * Right now this only works for arrays <-> Lists, but it might be
     * expanded into a more general form later.
     * 
     * @param arg the array to convert
     * @param destClass the actual class we want
     */
    public static Object convert(Object arg, Class destClass)
    {
      Debug.Print(3, "Trying to convert " + arg.getClass().getName() +
                                   " to " + destClass.getName());
      
      if (!(arg instanceof List))
        return arg;
      
      List list = (List)arg;
      int length = list.size();
      
      if (destClass.isArray()) {
        if (destClass.getComponentType().isPrimitive()) {
          
          Object array = Array.newInstance(destClass.getComponentType(),
                                           length);
          for (int i = 0; i < length; i++) {
            Array.set(array, i, list.get(i));
          }
          return array;
          
        } else {
          Object [] array;
          try {
            array = (Object [])Array.newInstance(destClass.getComponentType(),
                                                 length);
          } catch (Exception e) {
            return arg;
          }
          
          return list.toArray(array);
        }
      }
      
      if (List.class.isAssignableFrom(destClass)) {
        List newList = null;
        try {
          newList = (List)destClass.newInstance();
        } catch (Exception e) {
          // Couldn't build one for some reason... so forget it.
          return arg;
        }
        
        for (int j = 0; j < ((List)arg).size(); j++) {
          newList.add(list.get(j));
        }
        return newList;
      }
      
      return arg;
    }
  }