ws-soap-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Scott Nichol <snicholn...@scottnichol.com>
Subject Re: SOAP Serialization problem
Date Tue, 10 Sep 2002 19:35:26 GMT
For an interface, you can use the BeanSerializer to serialize, assuming the
interface has getters for all the properties you want to serialize.  You can
use the BeanSerializer to deserialize, but you must specify a concrete class
to which to deserialize.  (Note: I have not tried this, but it seems to me
it should work.)

BeanSerializer s = new BeanSerializer();
SOAPMappingRegistry smr = new SOAPMappingRegistry();
smr.mapTypes(soapEncURI, QName("urn:my-urn", "MyInterface"),
MyInterface.class, s, null);
smr.mapTypes(soapEncURI, QName("urn:my-urn", "MyInterface"), MyClass.class,
null, s);

With such a mapping, no matter what concrete class is serialized based on
MyInterface, a MyClass will always be de-serialized.  Again, depending on
the actual implementation, this may result in unexpected behavior.  For
example,

public interface Processor {
    public int getProcessCount();
    public void setProcessCount(int c);
    public long process(int arg1, int arg2);
}

public class Adder implements Processor {
    private int processCount;
    public int getProcessCount() {return processCount;}
    public void setProcessCount(int c) {processCount = c;}
    public long process(int arg1, int arg2) { processCount++; return arg1 +
(long) arg2;}
}

public class Multiplier implements Processor {
    private int processCount;
    public int getProcessCount() {return processCount;}
    public void setProcessCount(int c) {processCount = c;}
    public long process(int arg1, int arg2) { return arg1 * (long) arg2;}
}

public class ProcessorService {
    public long doProcess(Processor p, int arg1, int arg2) {
        return p.process(arg1, arg2);
    }
}

Without SOAP, the following

Multiplier m = new Multiplier();
ProcessorService ps = new ProcessorService();
System.out.println(ps.doProcess(m, 9, 11));

would print 99, since we are multiplying 9 times 11.

Using SOAP and the "interface serialization", however, gives a different
result.

BeanSerializer s = new BeanSerializer();
SOAPMappingRegistry smr = new SOAPMappingRegistry();
smr.mapTypes(soapEncURI, QName("urn:my-urn", "Processor"), Processor.class,
s, null);
smr.mapTypes(soapEncURI, QName("urn:my-urn", "Processor"), Adder.class,
null, s);
Vector params = new Vector();
Multiplier m = new Multiplier();
params.addElement(new Parameter("p", Processor.class, m, null));
params.addElement(new Parameter("arg1", Integer.class, new Integer(9),
null));
params.addElement(new Parameter("arg2", Integer.class, new Integer(11),
null));
Call call = new Call();
call.setMethodName("doProcess");
...
Response resp = call.invoke(...);
Parameter ret = resp.getReturnValue();
System.out.println(ret.getValue());

I assume the service deployment descriptor has the equivalent type mappings.
The client has basically asked to multiple 9 times 11.  However, the
Multiplier instance is serialized as a Processor interface, then
de-serialized as an Adder (since some concrete class had to be chosen).  The
adder will generate a result of 20, not 99.

The general problem is that you lose information by serializing an instance
as an interface (or superclass, for that matter).  The process that
de-serializes the instance must choose a concrete class to instantiate, and
the behavior of that concrete class need not be identical to the original
one that was serialized.  Depending on the exact interfaces and classes,
there may not be a problem, but it is important to understand how this
works.  Personally, I wish SOAP provided a means to specify the structure in
which something is serialized separately from its actual type, but it does
not.

Scott Nichol

----- Original Message -----
From: "Alexandros Panaretos" <apanar@essex.ac.uk>
To: "'Scott Nichol'" <snicholnews@scottnichol.com>;
<soap-dev@xml.apache.org>
Cc: "Alexandros Panaretos" <apanar@essex.ac.uk>
Sent: Tuesday, September 10, 2002 2:58 PM
Subject: RE: SOAP Serialization problem


Hi Scott,

Thanks for the example.Would I have to use my own interface serializer?
Is there a way of using the BeanSerializer without having to create my
own serializer? The  reason I would like to avoid writing my own
serializer is because when I try to distribute my application then all
the clients that will use this service will have to register my own
serializer which might be very inconvenient.

So on the following example:

MyClass c = new MyClass();
MyInterfaceSerializer s = new MyInterfaceSerializer();
SOAPMappingRegistry smr = new SOAPMappingRegistry();
smr.mapTypes(soapEncURI, QName("urn:my-urn", "MyInterface"),
MyInterface.class, s, s); Vector params = new Vector();
params.addElement(new Parameter("arg1", MyInterface.class, c, null));

If I use the BeanSerializer will it cope with Interface class?


Alex


-----Original Message-----
From: Scott Nichol [mailto:snicholnews@scottnichol.com]
Sent: Tuesday, September 10, 2002 7:03 PM
To: soap-dev@xml.apache.org; apanar@essex.ac.uk
Cc: apanar@essex.ac.uk
Subject: Re: SOAP Serialization problem


You are allowed to map just the interface, but you must provide the
interface as the data type for the parameter, not the class.  For
example, if class MyClass implements interface MyInterface, you can do

MyClass c = new MyClass();
MyInterfaceSerializer s = new MyInterfaceSerializer();
SOAPMappingRegistry smr = new SOAPMappingRegistry();
smr.mapTypes(soapEncURI, QName("urn:my-urn", "MyInterface"),
MyInterface.class, s, s); Vector params = new Vector();
params.addElement(new Parameter("arg1", MyInterface.class, c, null));

Basically, the type you specify when creating a Parameter must match a
type that has been registered (whether explicitly by you or implicitly
in the SOAPMappingRegistry).  The disadvantage of only registering the
interface, however, is that information about the original type is lost.
In the example above, if another class MyClass2 implements MyInterface,
but I serialize it as a MyInterface.class, the serialization of an
instance of MyClass is indistinguishable from the serialization of an
instance of MyClass2.  This may not be a problem, but it depends on the
signatures of the method(s) you will call, as well as differences the
classes may have in the way they implement the interface.

At the other extreme, you can map both interfaces and classes, then on a
per-parameter basis choose which mapping to use:

MyClass c = new MyClass();
MyClass2 c2 = new MyClass2();

MyInterfaceSerializer s1 = new MyInterfaceSerializer();
MyClassSerializer s2 = new MyClassSerializer(); MyClass2Serializer s3 =
new MyClass2Serializer(); SOAPMappingRegistry smr = new
SOAPMappingRegistry(); smr.mapTypes(soapEncURI, QName("urn:my-urn",
"MyInterface"), MyInterface.class, s1, s1); smr.mapTypes(soapEncURI,
QName("urn:my-urn", "MyClass"), MyClass.class, s2, s2);
smr.mapTypes(soapEncURI, QName("urn:my-urn", "MyClass2"),
MyClass2.class, s3, s3); Vector params = new Vector();
params.addElement(new Parameter("arg1", MyInterface.class, c, null));
params.addElement(new Parameter("arg2", MyClass.class, c, null));
params.addElement(new Parameter("arg3", MyClass2.class, c2, null));

Scott Nichol

----- Original Message -----
From: "Alexandros Panaretos" <apanar@essex.ac.uk>
To: <soap-dev@xml.apache.org>; <snicholnews@scottnichol.com>
Cc: <apanar@essex.ac.uk>
Sent: Tuesday, September 10, 2002 1:19 PM
Subject: RE: SOAP Serialization problem


Hi Scott,

Thank you again for your reply I got one of the problems sorted. In the
case of the interface I would like to ask another detail if possible
please: When my Soap Client methods have a Java Interface Object that is
has various concrete implementations do I have to provide the type
mapping of the implementations in the deployment descriptor or only the
interface class.

Also When I create my SOAP Call Object and I pass the parameter do I
again pass the Interface class or the Implementation of that class?

I have tried it passing interface objects and it did not work.

Anyway thank you very much for your help.

Alex

-----Original Message-----
From: Scott Nichol [mailto:snicholnews@scottnichol.com]
Sent: Tuesday, September 10, 2002 5:42 PM
To: notconfigured@essex.ac.uk; soap-dev@xml.apache.org
Cc: apanar@essex.ac.uk
Subject: Re: SOAP Serialization problem


You can pass a JavaBean that has a property that is a Vector of another
JavaBean, but you must supply a mapping for the other class.  Basically,
any type you want to serialize that is not a Java primitive (e.g. float,
int), wrapper (e.g. Float, Integer) or one of the few others with
built-in serializer mappings (e.g. Date), you have to provide a
serializer mapping.

For working with interfaces, you would typically write your own
serializer, specifically because the deserializer must create an
instance of a concrete class.  This is done for collection interfaces.
Serializing interfaces, on the other hand, is no different than
serializing any class.  If you have an interface that follows the
JavaBean pattern, for example, I think you could serialize it using the
BeanSerializer.

Scott Nichol

----- Original Message -----
From: "Alexandros Panaretos" <apanar@essex.ac.uk>
To: "'Scott Nichol'" <snicholnews@scottnichol.com>;
<soap-dev@xml.apache.org>
Cc: <apanar@essex.ac.uk>
Sent: Tuesday, September 10, 2002 5:27 AM
Subject: RE: SOAP Serialization problem


Hi Scott,

Thanks for your e-mail. The problems I had before I managed to solve. I
discovered that when you are passing JavaBeans through SOAP using the
Bean Serializer despite the setters & getters you should and the empty
constructor if you have any other methods that start with a get or set
you should provide an equivalent set or get (empty) in order to work.

Anyway, I have another problem now: Is it possible to pass a JavaBean
through SOAP that one of its properties is Vector of another user
defined objects which are JavaBeans as well ?

Moreover I would like to ask if you can pass an Interface as a parameter
of a SOAP method and then inside the SOAP Client specify the
implementation class of the interface you want the Serializer and
Deserializer to use.

Thank you in advance.

Alex



-----Original Message-----
From: Scott Nichol [mailto:snicholnews@scottnichol.com]
Sent: Monday, September 09, 2002 9:08 PM
To: soap-dev@xml.apache.org
Cc: apanar@essex.ac.uk
Subject: Re: SOAP Serialization problem


Can you describe the problems you are having?  Assuming you have getters
and setters for all properties you wish to serialize, and the properties
have data types that have serializers, BeanSerializer should work.  As
you have experienced, there are no serializers for URL or
DataInputStream.

Scott Nichol

----- Original Message -----
From: "Alexandros Panaretos" <apanar@essex.ac.uk>
To: <soap-dev@xml.apache.org>
Sent: Monday, September 02, 2002 11:03 AM
Subject: SOAP Serialization problem


Hi there,

I am quite new to SOAP and would like some help with the following if
possible:

Well, I am trying to pass my user defined object through SOAP. Now, I
have read various tutorials about passing user defined objects through
SOAP. From what I have understood there two ways of doing so:

A. Code your object class as a JavaBean and the use the provided
beanSerializer or B. Create your own custom serializer & deserializer.


I am trying to do the first and I have a lot problems serializing the
following Java class:

<---------------  Start of Java Code ---------------->

import java.io.InputStream;
import java.io.DataInputStream;
import java.net.URL;
import java.util.Vector;


public class MNistDataset implements ImageDataset {

    //InputStream is;
    //DataInputStream dis;

    //URL labelSetUrl;
    //URL imageSetUrl;

    String labelSetUrl;
    String imageSetUrl;

    byte[] labels;
    byte[] buf;

    Vector images;

    int maxPatterns = 1000000;
    int nImages;
    int nRows;
    int nCols;
    int nBytes;
    int nClasses;


    public MNistDataset(){

    }

    public MNistDataset(String labelSetUrl, String imageSetUrl) throws
Exception {
        this.labelSetUrl = labelSetUrl;
        this.imageSetUrl = imageSetUrl;
        readLabelSet(labelSetUrl);
        readImageSet(imageSetUrl);
    }

    public MNistDataset(String labelSetUrl, String imageSetUrl, int
maxPatterns) throws Exception {
        this.labelSetUrl = labelSetUrl;
        this.imageSetUrl = imageSetUrl;
        this.maxPatterns = maxPatterns;
        readLabelSet(labelSetUrl);
        readImageSet(imageSetUrl);
    }

    public void readImageSet(String imageSetUrl) throws Exception {
        InputStream is;
        DataInputStream dis;

        is = new URL(imageSetUrl).openStream();
        dis = new DataInputStream(is);
        int magic = dis.readInt();
        //System.out.println("magic number is equal: " + magic);
        int n = dis.readInt();
        n = Math.min( maxPatterns , n );
        if (n != nImages) {
            throw new Exception(nImages + " <> " + n);
        }
        nRows = dis.readInt();
        nCols = dis.readInt();
        images = new Vector();
        for (int i = 0; i < nImages; i++) {
            images.addElement(readImage(dis, nImages, nRows, nCols));

        }

    }

    public byte[][] readImage(DataInputStream dis, int nImages, int
nRows, int nCols) throws Exception {
        byte[][] imageByteArray = new byte[nRows][nCols];
        nBytes = nRows * nCols;
        buf = new byte[nBytes];
        dis.read(buf, 0, nBytes);
        for (int x = 0; x < nRows; x++)  {
            for (int y = 0; y < nCols; y++) {
                imageByteArray[x][y] = buf[ x +  y * nRows];
            }
        }
        return imageByteArray;
    }


    public byte[][] getImage(int i) {
        return (byte[][]) images.elementAt(i);
    }

    public Object getPattern(int i) {
        return images.elementAt(i);
    }

    public int getClass(int i) {
        return labels[i];
    }


    public int nClasses() {
        return nClasses + 1;
    }


    public int nPatterns() {
        return nImages;
    }

    public ClassifiedDataset  emptyCopy() {
        // buggy - but probably not used!
        return this;
    }

    /**

I have ommitted the setters and getters of the class
      variables in order to keep it short

    **/


}


<---------------  End of Java Code ---------------->


I already had a problem with the URLs and DataInputStream and
DataOutputStream variables so I declared within the methods rather than
global and I am not sure but I think SOAP can not handle the byte arrays
as properties of the JavaBean. Do you think there is a way of passing
this object through SOAP or is very complex and it can not serialize it?

Your help would be very much appreciated because this has been very
frustrating for me.

Thank you very much in advance for patience in reading this post and
your help.


Alexandros Panaretos



--
To unsubscribe, e-mail:   <mailto:soap-dev-unsubscribe@xml.apache.org>
For additional commands, e-mail: <mailto:soap-dev-help@xml.apache.org>





--
To unsubscribe, e-mail:   <mailto:soap-dev-unsubscribe@xml.apache.org>
For additional commands, e-mail: <mailto:soap-dev-help@xml.apache.org>


--
To unsubscribe, e-mail:   <mailto:soap-dev-unsubscribe@xml.apache.org>
For additional commands, e-mail: <mailto:soap-dev-help@xml.apache.org>




--
To unsubscribe, e-mail:   <mailto:soap-dev-unsubscribe@xml.apache.org>
For additional commands, e-mail: <mailto:soap-dev-help@xml.apache.org>




Mime
View raw message