All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dellroad.stuff.jibx.IdMappingMarshaller Maven / Gradle / Ivy

There is a newer version: 3.0.8
Show newest version

/*
 * 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() { @Override public Object call() throws Exception { return IdMappingMarshaller.this.jibxMarshaller.unmarshal(source); } }); } catch (Exception e) { this.unwrapException(e); return null; // never reached } } @Override public boolean supports(Class type) { return this.jibxMarshaller.supports(type); } private void unwrapException(Exception e) throws IOException { if (e instanceof IOException) throw (IOException)e.getCause(); if (e instanceof RuntimeException) throw (RuntimeException)e; throw new RuntimeException(e); } }