org.kie.server.api.marshalling.jaxb.JaxbMarshaller Maven / Gradle / Ivy
The newest version!
package org.kie.server.api.marshalling.jaxb;
import org.kie.server.api.commands.*;
import org.kie.server.api.marshalling.Marshaller;
import org.kie.server.api.marshalling.MarshallingException;
import org.kie.server.api.model.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
public class JaxbMarshaller implements Marshaller {
public static final Class>[] KIE_SERVER_JAXB_CLASSES;
static {
KIE_SERVER_JAXB_CLASSES = new Class>[]{
CallContainerCommand.class,
CommandScript.class,
CreateContainerCommand.class,
DisposeContainerCommand.class,
ListContainersCommand.class,
GetContainerInfoCommand.class,
GetScannerInfoCommand.class,
GetServerInfoCommand.class,
UpdateScannerCommand.class,
UpdateReleaseIdCommand.class,
KieContainerResource.class,
KieContainerResourceList.class,
KieContainerStatus.class,
KieServerInfo.class,
ReleaseId.class,
ServiceResponse.class,
ServiceResponsesList.class
};
}
private final JAXBContext jaxbContext;
private final javax.xml.bind.Marshaller marshaller;
private final Unmarshaller unmarshaller;
public JaxbMarshaller() {
try {
this.jaxbContext = JAXBContext.newInstance( KIE_SERVER_JAXB_CLASSES );
this.marshaller = jaxbContext.createMarshaller();
this.unmarshaller = jaxbContext.createUnmarshaller();
} catch ( JAXBException e ) {
throw new MarshallingException( "Error while creating JAXB context from default classes!", e );
}
}
@Override
public String marshall(Object input) {
StringWriter writer = new StringWriter();
try {
marshaller.marshal( input, writer );
} catch ( JAXBException e ) {
throw new MarshallingException( "Can't marshall input object: "+input, e );
}
return writer.toString();
}
@Override
public T unmarshall(String input, Class type) {
try {
return (T) unmarshaller.unmarshal( new StringReader( input ) );
} catch ( JAXBException e ) {
throw new MarshallingException( "Can't unmarshall input string: "+input, e );
}
}
@Override
public void dispose() {
}
}