
org.dellroad.stuff.jibx.IdMappingMarshaller Maven / Gradle / Ivy
Show all versions of dellroad-stuff-jibx Show documentation
/*
* Copyright (C) 2011 Archie L. Cobbs. All rights reserved.
*/
package org.dellroad.stuff.jibx;
import java.io.IOException;
import java.util.concurrent.Callable;
import javax.annotation.PostConstruct;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import org.dellroad.stuff.java.IdGenerator;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.jibx.JibxMarshaller;
/**
* Wrapper for Spring's {@link JibxMarshaller} that performs marshalling and unmarshalling operations
* within an invocation of {@link IdGenerator#run IdGenerator.run()}. Simply set your
* normal {@link JibxMarshaller} to the {@linkplain #setJibxMarshaller jibxMarshaller}
* property and use this class in its place.
*
*
* This is required when marshalling with JiBX mappings that utilize {@link IdMapper}.
*
* @see IdMapper
*/
public class IdMappingMarshaller implements Marshaller, Unmarshaller {
private JibxMarshaller jibxMarshaller;
/**
* Configure the nested {@link JibxMarshaller}. Required property.
*
* @param jibxMarshaller nested marshaller
*/
public void setJibxMarshaller(JibxMarshaller jibxMarshaller) {
this.jibxMarshaller = jibxMarshaller;
}
@PostConstruct
public void afterPropertiesSet() {
if (this.jibxMarshaller == null)
throw new IllegalArgumentException("no jibxMarshaller configured");
}
/**
* Invokdes {@link JibxMarshaller#marshal JibxMarshaller.marshal()} on the configured
* {@link JibxMarshaller} within an invocation of {@link IdGenerator#run(Callable) IdGenerator.run()}.
*/
@Override
public void marshal(final Object graph, final Result result) throws IOException {
try {
IdGenerator.run(new Callable() {
@Override
public Void call() throws Exception {
IdMappingMarshaller.this.jibxMarshaller.marshal(graph, result);
return null;
}
});
} catch (Exception e) {
this.unwrapException(e);
}
}
/**
* Invokdes {@link JibxMarshaller#unmarshal JibxMarshaller.unmarshal()} on the configured
* {@link JibxMarshaller} within an invocation of {@link IdGenerator#run(Callable) IdGenerator.run()}.
*/
@Override
public Object unmarshal(final Source source) throws IOException {
try {
return IdGenerator.run(new Callable