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 R J Scheuerle Jr <sc...@us.ibm.com> on 2001/12/20 19:09:28 UTC
Re: Issue - hard to subclass BeanSerializer (Field Case Problems)
There are two issues here, and I think there are easy ways to handle both.
Preliminary:
A bean class cannot have properties that differ only by the case of the
first character. Thus foo and Foo cannot
exist in the same class. (The reason is that in both cases the methods are
named getFoo and setFoo.)
Issue 1:
During deserialization of a bean class, an element named Foo is read. The
bean class does not have a Foo property,
so it dies...even though it has a propery named foo.
Solution 1:
The BeanSerializer should accept the element Foo and set the foo property
since there is no chance for ambiguity.
Issue 2:
A web service requires field names with the first letter capitalized (i.e.
Foo). The client is serializing a java class and
the name of the property being serialized is foo, so the web service
invocation fails. The client could re-write the java classes
to use variables with first letter capitalized, but this is not the Java
convention.
Solution 2:
Add a BeanSerializer constructor which allows the client to force first
letter uppercase element names (or first lowercase element names).
(Also add a constructor which allows user to pass in their own
PropertyDescriptors for mixed case situations).
I think I can add these changes today.
Rich Scheuerle
XML & Web Services Development
512-838-5115 (IBM TL 678-5115)
Mark Roder
<mroder@wamnet To: "'axis-dev@xml.apache.org'" <ax...@xml.apache.org>
.com> cc:
Subject: Issue - hard to subclass BeanSerializer
12/19/2001
03:37 PM
Please respond
to axis-dev
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