On Mar 8, 2005, at 1:33 PM, Michael Thome wrote:
I'd like to be able to deserialize/serialize data in the form of wsdl2java-generated structures from/to arbitrary streams. For instance, if I serialize such an object to a file, I would like to end up with an xml representation of the object in the file that conforms to the schema I used to generate the data structures.
I currently have a glacially-slow hack for deserialization involving setting up a dummy DeserializationContext and calling parse... but it is *horrendously* slow, seeming to spend all its time creating and releasing thousands of temporary Documents... I'm clearly missing something here.
Chances are you're doing it right. My experience is that axis is pretty slow, especially if you have large object graphs to read and write. Here's some code you look at to see if it helps. This is posted as part of a bug I opened dealing with large amounts of memory being used and released during the ser/deser process. It hasn't really been fixed aside from a few small patches which help a little.
You can look here for complete compilable code: http://issues.apache.org/jira/browse/AXIS-1771
These two methods depend on the following code to register the object types you'd like to serialize first:
...
ServiceFactory serviceFactory = null;
Service service = null;
try {
serviceFactory = ServiceFactory.newInstance();
service = serviceFactory.createService(new QName(endpoint, "MemoryTester"));
} catch (ServiceException e) {
e.printStackTrace();
return;
}
TypeMappingRegistry registry = service.getTypeMappingRegistry();
TypeMapping map = registry.getDefaultTypeMapping();
map.register(Entity.class, qnEntity,
new org.apache.axis.encoding.ser.BeanSerializerFactory(Entity.class, qnEntity),
new org.apache.axis.encoding.ser.BeanDeserializerFactory(Entity.class, qnEntity)
);
....
Note that in this method you can swap out the StringWriter for a FileWriter to write to files:
public String serialize( QName dataQName, Object data ) throws Exception {
AxisServer server = new AxisServer();
server.setOption(AxisEngine.PROP_DOMULTIREFS, Boolean.TRUE);
MessageContext msgContext = new MessageContext(server);
msgContext.setProperty(AxisEngine.PROP_DOMULTIREFS, Boolean.TRUE);
SOAPEnvelope msg = new SOAPEnvelope();
RPCParam arg = new RPCParam("Test", "struct", data);
RPCElement body = new RPCElement("Test", "method1", new Object[]{arg});
msg.addBodyElement(body);
StringWriter writer = new StringWriter();
SerializationContext context = new SerializationContext(writer, msgContext);
msg.output(context);
String messageStr = writer.toString();
return messageStr; }
Here again you can swap out the StringReader for a FileReader:
public Object deserialize( String messageStr ) throws Exception {
AxisServer server = new AxisServer();
server.setOption(AxisEngine.PROP_DOMULTIREFS, Boolean.TRUE);
MessageContext msgContext = new MessageContext(server);
msgContext.setProperty(AxisEngine.PROP_DOMULTIREFS, Boolean.TRUE);
//System.err.println(messageStr);
Reader reader = new StringReader(messageStr);
DeserializationContext dser = new DeserializationContext
(new InputSource(reader), msgContext, org.apache.axis.Message.REQUEST);
dser.parse();
SOAPEnvelope env = dser.getEnvelope();
RPCElement rpcElem = (RPCElement) env.getFirstBody();
MessageElement struct = rpcElem.getChildElement(new QName("Test", "struct"));
Object result = struct.getObjectValue(); return result; }
-- Peter Molettiere Senior Engineer Truereq, Inc. http://www.truereq.com/