You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlrpc-dev@ws.apache.org by Oscar Diaz <os...@optivamedia.com> on 2007/06/25 10:24:57 UTC

custom types seem to work with just "implement Serializable"

Hello all,

I am at my first steps working with xmlrpc, but would like to share this
on how to instantly get custom types working.
If someone finds any downsides on following this path for custom types,
rather than the path explained in the 'Custom Data Types' section,
please let me know.

**1) Custom data types with "implements Serializable"

After spending some time with 'Custom data types' section in the
'Advanced Techniques' page ( http://ws.apache.org/xmlrpc/advanced.html )
and not being able to get a MyTypeFactory working , I have realised that
for my custom classes just adding "implements Serializable" to the class
makes the type work perfectly fine with xmlrpc parameters and return
values.

For instance, a serializable class as the example one below will work
ok; with no need of applying the changes in the mentioned 'Custom Data
Types' section. 

public class CustomClass implements Serializable {
    public int number;
    public String text;
    public int[] table;
    public SomeOtherClass soc;
}


** Explanation:

I found this thanks to direct source code inspection.
In particular, it works thanks to the lines below, extracted from
TypeConverterFactoryImpl.java, which seems to return a default type
converter when our custom type is serializable:

org.apache.xmlrpc.common.TypeConverterFactoryImpl.java:

            /** Returns a type converter for the given class.
             */
            public TypeConverter getTypeConverter(Class pClass) {
                ...
                if (Serializable.class.isAssignableFrom(pClass)) {
                    return new CastCheckingTypeConverter(pClass);
                }
                ...
            }
        

** Fixing compilation errors in the 'Custom Data Types' section 

On the other hand, the example code in the mentioned 'Custom data types'
section needs some minor changes to fix compilation errors. In case you
may be interested, below you can find the changes I applied (though they
are not needed if you choose to go the 'implements Serializable' way).
If anyone finds anything wrong, please let me know as well.

Type factory:
        ...
        public TypeParser getParser(XmlRpcStreamConfig pConfig,
NamespaceContextImpl pContext, String pURI, String pLocalName) {
            if (DateSerializer.DATE_TAG.equals(pLocalName)) {
//              return new DateParser(pFormat);
                return new DateParser(newFormat());
            } else {
                return super.getParser(pConfig, pContext, pURI,
pLocalName);
            }
        }
        ...
Client side:
        XmlRpcClient client = new XmlRpcClient();
//      client.setTypeFactory(new MyTypeFactory());
        client.setTypeFactory( new MyTypeFactory(client) );


Server side:

    protected XmlRpcStreamServer newXmlRpcStreamServer() {
//      server.setTypeFactory(new MyTypesFactory( server));
        XmlRpcStreamServer server = super.newXmlRpcStreamServer();
        return server;
    }

Hope this can be of any help for someone.

Kind regards,
Oscar Diaz