axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ru...@apache.org
Subject cvs commit: xml-axis/java/src/org/apache/axis/encoding BeanSerializer.java
Date Tue, 05 Jun 2001 15:24:40 GMT
rubys       01/06/05 08:24:40

  Modified:    java/src/org/apache/axis/encoding BeanSerializer.java
  Log:
  Make BeanSerializer serializable.
  
  Revision  Changes    Path
  1.4       +72 -19    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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- BeanSerializer.java	2001/06/04 17:16:02	1.3
  +++ BeanSerializer.java	2001/06/05 15:24:40	1.4
  @@ -55,7 +55,7 @@
   
   package org.apache.axis.encoding;
   
  -import java.io.IOException;
  +import java.io.*;
   import java.util.Hashtable;
   import java.util.Vector;
   
  @@ -73,41 +73,61 @@
    *
    * @author Sam Ruby <rubys@us.ibm.com>
    */
  -public class BeanSerializer extends DeserializerBase implements Serializer {
  +public class BeanSerializer extends DeserializerBase 
  +    implements Serializer, Serializable 
  +{
   
       /**
        * Class being serialized/deserialized
        */
       private Class cls;
   
  +    protected void setCls(Class cls) {
  +        this.cls = cls;
  +    }
  +
       /**
        * Property Descriptors.  Retrieved once and cached in the serializer.
        */
  -    private PropertyDescriptor[] pd;
  -    
  -    /**
  -     * An array of nothing, defined only once.
  -     */
  -    private static final Object[] noArgs = new Object[] {};
  +    private PropertyDescriptor[] pd = null;
  +
  +    protected PropertyDescriptor[] getPd() {
  +        if (pd==null) {
  +            try {
  +               pd = Introspector.getBeanInfo(cls).getPropertyDescriptors();
  +            } catch (Exception e) {
  +               // this should never happen
  +               throw new NullPointerException(e.toString());
  +            }
  +        }
  +
  +        return pd;
  +    }
  +
  +    protected void setPd(PropertyDescriptor[] pd) {
  +        this.pd = pd;
  +    }
   
       /**
  -     * Constructor that takes a class.  Called only for serializers, so it
  -     * does NOT creates an instance of the bean.
  +     * Default constructor.
        */
  -    public BeanSerializer(Class cls) throws Exception {
  -        this.cls = cls;
  -        this.pd = Introspector.getBeanInfo(cls).getPropertyDescriptors();
  +    public BeanSerializer() {
  +        super();
       }
   
       /**
  -     * Constructor that takes a class and a pre-computed PropertyDescriptor.
  -     * Called only for deserializers, so it DOES create an instance.
  +     * Constructor that takes a class.  Provided only for convenience.
  +     * Equivalent to calling setCls(cls) on a new instance.
        */
  -    public BeanSerializer(Class cls, PropertyDescriptor[] pd) throws Exception {
  +    public BeanSerializer(Class cls) {
  +        super();
           this.cls = cls;
  -        this.pd = pd;
  -        this.value = cls.newInstance();
       }
  +     
  +    /**
  +     * An array of nothing, defined only once.
  +     */
  +    private static final Object[] noArgs = new Object[] {};
   
       /**
        * BeanSerializer Factory that creates instances with the specified
  @@ -123,12 +143,18 @@
           }
   
           public DeserializerBase getDeserializer() {
  +            BeanSerializer bs = new BeanSerializer();
  +            bs.setCls(cls);
  +            bs.setPd(pd);
  +
               try {
  -                return new BeanSerializer(cls, pd);
  +                bs.setValue(cls.newInstance());
               } catch (Exception e) {
                   // I'm not allowed to throw much, so I throw what I can!
                   throw new NullPointerException(e.toString());
               }
  +
  +            return bs;
           }
       }
   
  @@ -168,6 +194,16 @@
                                String qName, Attributes attributes)
           throws SAXException
       {
  +        PropertyDescriptor[] pd = getPd();
  +
  +        // create a value if there isn't one already...
  +        if (value==null) {
  +            try {
  +                value=cls.newInstance();
  +            } catch (Exception e) {
  +                throw new SAXException(e.toString());
  +           }
  +        }
   
           // look for a field by this name.  Assumes the the number of
           // properties in a bean is (relatively) small, so uses a linear
  @@ -208,6 +244,7 @@
           throws IOException
       {
           context.startElement(name, attributes);
  +        PropertyDescriptor[] pd = getPd();
   
           try {
               for (int i=0; i<pd.length; i++) {
  @@ -223,4 +260,20 @@
   
           context.endElement();
       }
  +
  +    /**
  +     * Write the serializer out to disk.  Actually, all we need is enough
  +     * to reconsitute the object from scratch, namely 
  +     */
  +    private void writeObject(ObjectOutputStream out) throws IOException {
  +        out.writeObject(cls.getName());
  +    }
  +          
  +    private void readObject(ObjectInputStream stream) 
  +        throws IOException, ClassNotFoundException
  +    {
  +        String clsName = (String) stream.readObject();
  +        setCls(Class.forName(clsName));
  +    }
  +          
   }
  
  
  

Mime
View raw message