axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Roder <mro...@wamnet.com>
Subject Issue - hard to subclass BeanSerializer
Date Wed, 19 Dec 2001 21:37:51 GMT


I am trying to create a subclass of BeanSerializer.  I am working on a
UpperCaseBeanSerializer that will encode attributes to start with uppercase
instead of lowercase. ie.
<User><Name>foo</Name><Password>bar</Password></User>
instead of
<User><name>foo</name><password>bar</password></User>
 Note: I am doing the
client side soap app that hits a 3rd party soap service that I can not
touch.

In just trying to do a rename the class type operation to get it into my
package space I am seeing a couple of issues.

1)There is no easy way to get getDeserializer(Class cls) in a subclass
without a lot of copy/paste of the code.  My complete subclass is included.
Ideas on how to clean this up?  

2)  The following code happens in two locations.  
                    pd =
Introspector.getBeanInfo(cls).getPropertyDescriptors();
                    pd = sortPropertyDescriptors(pd, cls);
In doing my subclass, the first line is where I am going to have to change
the behavior for my subclass. I will modify the case of the properties that
the normal BeanInfo gives back.  One of these is expected in the getPd
method, but one is in the getDeserializer() method.   I would just replace
the call in getDeserializer() with getPd, but getPd has the side affect of
setting the enumSerializer.  Ideas on how to clean this up?

Couple of questions.  

Why does BeanSerializer sneak in a EnumSerializer if it thinks it needs it?
Shouldn't the class just be registered with a EnumSerializer instead of a
BeanSerializer?  

I am having a hard time reading and understanding the getDeserializer code
and how the factory is working and what the local Hashtable cache is good
for.  It seems it might be missing a static on the hashtable?  Or is is an
instance of the factory going to have getDeserializer called on it that many
times? A new factory instance is returned each time getFactory.  


I don't have much time to wrap my head around this today, so if someone can
throw out pointers, that would be great!

Thanks.

Later

Mark


public class UpperCaseBeanSerializer extends BeanSerializer
    implements Serializer, Serializable
{
    static Category LOG =
            Category.getInstance(UpperCaseBeanSerializer.class.getName());

    /**
     * Default constructor.
     */
    public UpperCaseBeanSerializer() {
        super();
    }

    /**
     * Constructor that takes a class.  Provided only for convenience.
     * Equivalent to calling setCls(cls) on a new instance.
     */
    public UpperCaseBeanSerializer(Class cls) {
        super(cls);
    }

    public static DeserializerFactory getFactory()
    {
      return new UpperCaseBeanSerFactory();
    }

    /**
     * UpperCaseBeanSerializer Factory that creates instances with the
specified
     * class.  Caches the PropertyDescriptor
     */
    public static class UpperCaseBeanSerFactory implements
DeserializerFactory {
        private Hashtable propertyDescriptors = new Hashtable();
      
        public Deserializer getDeserializer(Class cls) {
            PropertyDescriptor [] pd =
                  (PropertyDescriptor [])propertyDescriptors.get(cls);
            if (pd == null) {
                try {
                    pd =
Introspector.getBeanInfo(cls).getPropertyDescriptors();
                    pd = sortPropertyDescriptors(pd, cls);
                } catch (IntrospectionException e) {
                    return null;
                }
                propertyDescriptors.put(cls, pd);
            }

            // If an enum class.  Return the Deserializer for Enumeration
            if (isEnumClass(cls)) {
                return EnumSerializer.getFactory().getDeserializer(cls);
            }                

            UpperCaseBeanSerializer bs = new UpperCaseBeanSerializer();
            bs.setCls(cls);
            bs.setPd(pd);

            try {
                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;
        }

        
      }

}
I won't have time to look into this  of the   

Mime
View raw message