Omaha #3373 jaxb manager refactor

api changes
reworked ogc jaxb manager to use parent class

Change-Id: I92b4cd6a48faf80462b9ca483a4f5d6cba78de48

Former-commit-id: e428f257cb [formerly 125c31f035057676d8c97a9b6c7a1d7b1b7f5bcb]
Former-commit-id: e4c85f7cd5
This commit is contained in:
Brian Clements 2014-07-15 15:13:26 -05:00
parent 6eb299d022
commit 33684f3c97
10 changed files with 255 additions and 323 deletions

View file

@ -23,6 +23,7 @@ import java.util.Arrays;
import org.jivesoftware.smack.util.Base64; import org.jivesoftware.smack.util.Base64;
import com.raytheon.uf.common.serialization.MarshalOptions;
import com.raytheon.uf.common.serialization.SerializationUtil; import com.raytheon.uf.common.serialization.SerializationUtil;
import com.raytheon.uf.common.status.IUFStatusHandler; import com.raytheon.uf.common.status.IUFStatusHandler;
import com.raytheon.uf.common.status.UFStatus; import com.raytheon.uf.common.status.UFStatus;
@ -46,6 +47,7 @@ import com.raytheon.uf.viz.collaboration.comm.provider.SerializationMode;
* Dec 11, 2013 2562 bclement Initial creation * Dec 11, 2013 2562 bclement Initial creation
* Feb 27, 2013 2756 bclement extends BaseExtension * Feb 27, 2013 2756 bclement extends BaseExtension
* Jun 12, 2013 2903 bclement default to wrap jaxb xml in base64 * Jun 12, 2013 2903 bclement default to wrap jaxb xml in base64
* Jul 15, 2014 3373 bclement added fragment marshal options
* *
* </pre> * </pre>
* *
@ -69,6 +71,9 @@ public class SessionPayload extends BaseExtension {
public static final String ENCODING_ATTRIBUTE = "encoding"; public static final String ENCODING_ATTRIBUTE = "encoding";
private static final MarshalOptions UNFORMATTED_FRAGMENT = new MarshalOptions(
false, true);
private final PayloadType payloadType; private final PayloadType payloadType;
private final SerializationMode mode; private final SerializationMode mode;
@ -135,7 +140,7 @@ public class SessionPayload extends BaseExtension {
try { try {
CollaborationXmlManager jaxb = CollaborationXmlManager CollaborationXmlManager jaxb = CollaborationXmlManager
.getInstance(); .getInstance();
String xml = jaxb.marshalToFragment(data); String xml = jaxb.marshalToXml(data, UNFORMATTED_FRAGMENT);
/* /*
* wrap JAXB XML in base64 to avoid problems with openfire * wrap JAXB XML in base64 to avoid problems with openfire
* disconnecting due to complex XML * disconnecting due to complex XML

View file

@ -19,13 +19,12 @@
**/ **/
package com.raytheon.uf.viz.collaboration.comm.provider; package com.raytheon.uf.viz.collaboration.comm.provider;
import java.io.StringWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller; import javax.xml.bind.Unmarshaller;
import javax.xml.bind.UnmarshallerHandler; import javax.xml.bind.UnmarshallerHandler;
@ -60,6 +59,7 @@ import com.raytheon.uf.viz.core.reflect.SubClassLocator;
* ------------- -------- ----------- -------------------------- * ------------- -------- ----------- --------------------------
* Oct 31, 2013 2491 bsteffen Initial creation * Oct 31, 2013 2491 bsteffen Initial creation
* Dec 18, 2013 2562 bclement extend jaxb manager, xpp/fragment support * Dec 18, 2013 2562 bclement extend jaxb manager, xpp/fragment support
* Jul 15, 2014 3373 bclement jaxb manager changes, unmarshalFromXPP() doesn't pool
* *
* </pre> * </pre>
* *
@ -147,43 +147,14 @@ public class CollaborationXmlManager extends JAXBManager {
*/ */
public Object unmarshalFromXPP(XmlPullParser parser) public Object unmarshalFromXPP(XmlPullParser parser)
throws CollaborationException { throws CollaborationException {
Unmarshaller unmarshaller = null;
try { try {
unmarshaller = getUnmarshaller(); JAXBContext ctx = getJaxbContext();
Unmarshaller unmarshaller = ctx.createUnmarshaller();
UnmarshallerHandler handler = unmarshaller.getUnmarshallerHandler(); UnmarshallerHandler handler = unmarshaller.getUnmarshallerHandler();
PullParserJaxbAdapter adapter = new PullParserJaxbAdapter(parser, handler); PullParserJaxbAdapter adapter = new PullParserJaxbAdapter(parser, handler);
return adapter.unmarshal(); return adapter.unmarshal();
} catch (Exception e) { } catch (Exception e) {
throw new CollaborationException("Unable to unmarshal data", e); throw new CollaborationException("Unable to unmarshal data", e);
} finally {
// TODO magic number 10 because QUEUE_SIZE isn't visible
if ((unmarshaller != null) && (unmarshallers.size() < 10)) {
unmarshallers.add(unmarshaller);
}
}
}
/**
* Marshal object to unformatted (not pretty-printed) XML fragment (no XML
* preamble)
*
* @param obj
* @return
* @throws JAXBException
*/
public String marshalToFragment(Object obj) throws JAXBException {
Marshaller msh = getMarshaller();
try {
StringWriter writer = new StringWriter();
msh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
msh.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
msh.marshal(obj, writer);
return writer.toString();
} finally {
// TODO magic number 10 because QUEUE_SIZE isn't visible
if ((msh != null) && (marshallers.size() < 10)) {
marshallers.add(msh);
}
} }
} }

View file

@ -19,10 +19,12 @@
**/ **/
package com.raytheon.uf.common.registry; package com.raytheon.uf.common.registry;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller; import javax.xml.bind.Marshaller;
import com.raytheon.uf.common.serialization.JAXBManager; import com.raytheon.uf.common.serialization.JAXBManager;
import com.raytheon.uf.common.serialization.jaxb.JaxbMarshallerStrategy;
import com.sun.xml.bind.marshaller.NamespacePrefixMapper; import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
/** /**
@ -38,6 +40,7 @@ import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
* Date Ticket# Engineer Description * Date Ticket# Engineer Description
* ------------ ---------- ----------- -------------------------- * ------------ ---------- ----------- --------------------------
* 8/8/2013 1692 bphillip Initial implementation * 8/8/2013 1692 bphillip Initial implementation
* Jul 15, 2014 3373 bclement jaxb manager changes
* </pre> * </pre>
* *
* @author bphillip * @author bphillip
@ -48,17 +51,6 @@ public class RegistryJaxbManager extends JAXBManager {
/** The namespace mapper property name on the marshaller */ /** The namespace mapper property name on the marshaller */
private static final String NAMESPACE_PREFIX_MAPPER_PROPERTY = "com.sun.xml.bind.namespacePrefixMapper"; private static final String NAMESPACE_PREFIX_MAPPER_PROPERTY = "com.sun.xml.bind.namespacePrefixMapper";
protected NamespacePrefixMapper namespaceMapper;
/**
* Creates a new RegistryJaxbManager. Hidden from public use
*
* @throws JAXBException
*/
protected RegistryJaxbManager() throws JAXBException {
super();
}
/** /**
* Creates a new RegistryJaxbManager with the given namespace mapper * Creates a new RegistryJaxbManager with the given namespace mapper
* *
@ -72,25 +64,29 @@ public class RegistryJaxbManager extends JAXBManager {
public RegistryJaxbManager(RegistryNamespaceMapper namespaceMapper) public RegistryJaxbManager(RegistryNamespaceMapper namespaceMapper)
throws JAXBException { throws JAXBException {
super( super(
createStrategy(namespaceMapper),
oasis.names.tc.ebxml.regrep.xsd.lcm.v4.ObjectFactory.class, oasis.names.tc.ebxml.regrep.xsd.lcm.v4.ObjectFactory.class,
oasis.names.tc.ebxml.regrep.xsd.query.v4.ObjectFactory.class, oasis.names.tc.ebxml.regrep.xsd.query.v4.ObjectFactory.class,
oasis.names.tc.ebxml.regrep.xsd.rim.v4.ObjectFactory.class, oasis.names.tc.ebxml.regrep.xsd.rim.v4.ObjectFactory.class,
oasis.names.tc.ebxml.regrep.xsd.rs.v4.ObjectFactory.class, oasis.names.tc.ebxml.regrep.xsd.rs.v4.ObjectFactory.class,
oasis.names.tc.ebxml.regrep.xsd.spi.v4.ObjectFactory.class, oasis.names.tc.ebxml.regrep.xsd.spi.v4.ObjectFactory.class,
com.raytheon.uf.common.registry.services.rest.response.RestCollectionResponse.class); com.raytheon.uf.common.registry.services.rest.response.RestCollectionResponse.class);
this.namespaceMapper = namespaceMapper;
} }
@Override private static JaxbMarshallerStrategy createStrategy(
protected Marshaller getMarshaller() throws JAXBException { final NamespacePrefixMapper namespaceMapper) {
Marshaller m = marshallers.poll(); return new JaxbMarshallerStrategy() {
if (m == null) { @Override
m = getJaxbContext().createMarshaller(); protected Marshaller createMarshaller(JAXBContext context)
if (namespaceMapper != null) { throws JAXBException {
m.setProperty(NAMESPACE_PREFIX_MAPPER_PROPERTY, namespaceMapper); Marshaller rval = super.createMarshaller(context);
if (namespaceMapper != null) {
rval.setProperty(NAMESPACE_PREFIX_MAPPER_PROPERTY,
namespaceMapper);
}
return rval;
} }
} };
return m;
} }
} }

View file

@ -38,6 +38,7 @@ import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
* Date Ticket# Engineer Description * Date Ticket# Engineer Description
* ------------ ---------- ----------- -------------------------- * ------------ ---------- ----------- --------------------------
* 8/8/2013 1692 bphillip Initial implementation * 8/8/2013 1692 bphillip Initial implementation
* Jul 15, 2014 3373 bclement removed warning
* </pre> * </pre>
* *
* @author bphillip * @author bphillip
@ -73,7 +74,7 @@ public class RegistryNamespaceMapper extends NamespacePrefixMapper implements
} }
@Override @Override
public Iterator getPrefixes(String namespaceURI) { public Iterator<?> getPrefixes(String namespaceURI) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }

View file

@ -50,6 +50,7 @@ import com.raytheon.uf.common.util.ReflectionUtil;
* ------------ ---------- ----------- -------------------------- * ------------ ---------- ----------- --------------------------
* Nov 12, 2013 ---- njensen Initial release. * Nov 12, 2013 ---- njensen Initial release.
* Nov 24, 2013 2584 dhladky versioning * Nov 24, 2013 2584 dhladky versioning
* Jul 15, 2014 3373 bclement pooling jaxb manager
* </pre> * </pre>
* *
* @author njensen * @author njensen
@ -118,7 +119,7 @@ public class EbxmlJaxbManager {
public synchronized JAXBManager getJaxbManager() throws JAXBException { public synchronized JAXBManager getJaxbManager() throws JAXBException {
if (jaxb == null) { if (jaxb == null) {
jaxb = new JAXBManager(jaxables.toArray(new Class[0])); jaxb = new JAXBManager(true, jaxables.toArray(new Class[0]));
} }
return jaxb; return jaxb;
} }

View file

@ -16,46 +16,20 @@
* *
* See the AWIPS II Master Rights File ("Master Rights File.pdf") for * See the AWIPS II Master Rights File ("Master Rights File.pdf") for
* further licensing information. * further licensing information.
*
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Mar 30, 2011 bclement Initial creation
* Aug 18, 2013 #2097 dhladky extended JAXBManager
*
*/ */
package com.raytheon.uf.edex.ogc.common.jaxb; package com.raytheon.uf.edex.ogc.common.jaxb;
import java.io.ByteArrayOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller; import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import com.raytheon.uf.common.serialization.JAXBManager; import com.raytheon.uf.common.serialization.JAXBManager;
import com.raytheon.uf.common.status.IUFStatusHandler;
import com.raytheon.uf.common.status.UFStatus;
import com.sun.xml.bind.api.JAXBRIContext; import com.sun.xml.bind.api.JAXBRIContext;
import com.sun.xml.bind.marshaller.NamespacePrefixMapper; import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
@ -68,7 +42,9 @@ import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
* *
* Date Ticket# Engineer Description * Date Ticket# Engineer Description
* ------------ ---------- ----------- -------------------------- * ------------ ---------- ----------- --------------------------
* 2011 bclement Initial creation * Mar 30, 2011 bclement Initial creation
* Aug 18, 2013 #2097 dhladky extended JAXBManager
* Jul 15, 2014 3373 bclement rewritten to use JAXBManager
* *
* </pre> * </pre>
* *
@ -77,31 +53,72 @@ import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
*/ */
public class OgcJaxbManager extends JAXBManager { public class OgcJaxbManager extends JAXBManager {
protected final JAXBContext jaxbContext;
protected static final int QUEUE_SIZE = 10;
protected final Queue<Unmarshaller> unmarshallers = new ConcurrentLinkedQueue<Unmarshaller>();
protected final Queue<Marshaller> marshallers = new ConcurrentLinkedQueue<Marshaller>();
protected volatile int unmarshallersCreated = 0;
protected volatile int marshallersCreated = 0;
protected final IUFStatusHandler log = UFStatus.getHandler(this.getClass());
private volatile NamespacePrefixMapper mapper;
protected final ReadWriteLock prefixLock = new ReentrantReadWriteLock();
protected static final String JAXB_NAMESPACE_MAPPER = "com.sun.xml.bind.namespacePrefixMapper"; protected static final String JAXB_NAMESPACE_MAPPER = "com.sun.xml.bind.namespacePrefixMapper";
public OgcJaxbManager(Class<?>[] classes) throws JAXBException { private final OgcMarshallerStrategy marshStrategy;
jaxbContext = JAXBContext.newInstance(classes, getJaxbConfig());
/**
* @param classes
* @throws JAXBException
*/
public OgcJaxbManager(Class<?>[] classes) throws JAXBException {
this(new OgcMarshallerStrategy(), classes);
} }
private static Map<String, Object> getJaxbConfig() throws JAXBException { /**
* @param mapper
* mapping of namespaces to namespace prefixes
* @param classes
* @throws JAXBException
*/
public OgcJaxbManager(NamespacePrefixMapper mapper, Class<?>[] classes)
throws JAXBException {
this(createStrategy(mapper), classes);
}
/**
* @see JAXBManager#JAXBManager(boolean,
* com.raytheon.uf.common.serialization.jaxb.JaxbMarshallerStrategy,
* Class...)
* @param strategy
* @param classes
* @throws JAXBException
*/
public OgcJaxbManager(OgcMarshallerStrategy strategy, Class<?>[] classes)
throws JAXBException {
super(strategy, classes);
this.marshStrategy = strategy;
}
/**
* Create a marshaller strategy that uses the provided namespace prefix
* mapping
*
* @param mapper
* @return
*/
private static OgcMarshallerStrategy createStrategy(
final NamespacePrefixMapper mapper) {
return new OgcMarshallerStrategy() {
@Override
protected Marshaller createMarshaller(JAXBContext context)
throws JAXBException {
Marshaller rval = super.createMarshaller(context);
if (mapper != null) {
rval.setProperty(JAXB_NAMESPACE_MAPPER, mapper);
}
return rval;
}
};
}
/*
* (non-Javadoc)
*
* @see com.raytheon.uf.common.serialization.JAXBManager#getJaxbConfig()
*/
@Override
protected Map<String, Object> getJaxbConfig() throws JAXBException {
Map<String, Object> jaxbConfig = new HashMap<String, Object>(); Map<String, Object> jaxbConfig = new HashMap<String, Object>();
TransientAnnotationReader reader = new TransientAnnotationReader(); TransientAnnotationReader reader = new TransientAnnotationReader();
try { try {
@ -116,210 +133,28 @@ public class OgcJaxbManager extends JAXBManager {
return jaxbConfig; return jaxbConfig;
} }
protected Unmarshaller getUnmarshaller() throws JAXBException { /**
Unmarshaller m = unmarshallers.poll(); * @see OgcMarshallerStrategy#unmarshal(JAXBContext, Node)
if (m == null) { * @param node
if (unmarshallersCreated < QUEUE_SIZE) { * @return
synchronized (unmarshallers) { * @throws JAXBException
m = jaxbContext.createUnmarshaller(); */
++unmarshallersCreated;
}
} else {
int tries = 0;
do {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// ignore
}
m = unmarshallers.poll();
tries++;
if (tries >= 20) {
log.debug("Unable to get jaxb unmarshaller from pool after "
+ tries + " tries. Growing pool size.");
synchronized (unmarshallers) {
m = jaxbContext.createUnmarshaller();
++unmarshallersCreated;
}
}
} while (m == null);
}
}
return m;
}
protected Marshaller getMarshaller() throws JAXBException {
Marshaller m = marshallers.poll();
if (m == null) {
if (marshallersCreated < QUEUE_SIZE) {
synchronized (marshallers) {
m = jaxbContext.createMarshaller();
++marshallersCreated;
}
} else {
int tries = 0;
do {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// ignore
}
m = marshallers.poll();
tries++;
if (tries >= 20) {
log.debug("Unable to get jaxb marshaller from pool after "
+ tries + " tries. Growing pool size.");
synchronized (marshallers) {
m = jaxbContext.createMarshaller();
++marshallersCreated;
}
}
} while (m == null);
}
}
return m;
}
public Object unmarshal(String xml) throws JAXBException {
Unmarshaller msh = null;
try {
msh = getUnmarshaller();
StringReader reader = new StringReader(xml);
Object obj = msh.unmarshal(reader);
if (obj instanceof JAXBElement<?>) {
obj = ((JAXBElement<?>) obj).getValue();
}
return obj;
} finally {
if (msh != null) {
unmarshallers.add(msh);
}
}
}
public Object unmarshal(Node node) throws JAXBException { public Object unmarshal(Node node) throws JAXBException {
Unmarshaller msh = null; JAXBContext ctx = getJaxbContext();
try { return marshStrategy.unmarshal(ctx, node);
msh = getUnmarshaller();
Object obj = msh.unmarshal(node);
if (obj instanceof JAXBElement<?>) {
obj = ((JAXBElement<?>) obj).getValue();
}
return obj;
} finally {
if (msh != null) {
unmarshallers.add(msh);
}
}
}
public Object unmarshal(FileReader reader) throws JAXBException {
Unmarshaller msh = null;
try {
msh = getUnmarshaller();
Object obj = msh.unmarshal(reader);
if (obj instanceof JAXBElement<?>) {
obj = ((JAXBElement<?>) obj).getValue();
}
return obj;
} finally {
if (msh != null) {
unmarshallers.add(msh);
}
}
}
public Object unmarshal(InputStream xml) throws JAXBException {
Unmarshaller msh = null;
try {
msh = getUnmarshaller();
Object obj = msh.unmarshal(xml);
if (obj instanceof JAXBElement<?>) {
obj = ((JAXBElement<?>) obj).getValue();
}
return obj;
} finally {
if (msh != null) {
unmarshallers.add(msh);
}
}
}
public String marshal(Object obj) throws JAXBException {
return marshal(obj, true);
}
public String marshal(Object obj, boolean formatted) throws JAXBException {
return marshal(obj, null, formatted, false);
}
public String marshal(Object obj, boolean formatted, boolean fragment)
throws JAXBException {
return marshal(obj, null, formatted, fragment);
} }
/**
* @see OgcMarshallerStrategy#marshalToNode(JAXBContext, Object)
* @param obj
* @return
* @throws JAXBException
* @throws ParserConfigurationException
*/
public Node marshalToNode(Object obj) throws JAXBException, public Node marshalToNode(Object obj) throws JAXBException,
ParserConfigurationException { ParserConfigurationException {
Marshaller msh = getMarshaller(); JAXBContext ctx = getJaxbContext();
try { return marshStrategy.marshalToNode(ctx, obj);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
msh.marshal(obj, doc);
return doc.getFirstChild();
} finally {
marshallers.add(msh);
}
} }
public void marshal(Object obj, OutputStream out, String schemaLocation,
boolean formatted, boolean fragment) throws JAXBException {
Marshaller msh = getMarshaller();
try {
msh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatted);
msh.setProperty(Marshaller.JAXB_FRAGMENT, fragment);
if (mapper != null) {
Lock read = prefixLock.readLock();
read.lock();
try {
msh.setProperty(JAXB_NAMESPACE_MAPPER, mapper);
} finally {
read.unlock();
}
}
if (schemaLocation != null && !schemaLocation.isEmpty()) {
msh.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation);
}
msh.marshal(obj, out);
} finally {
marshallers.add(msh);
}
}
public String marshal(Object obj, String schemaLocation, boolean formatted,
boolean fragment)
throws JAXBException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
marshal(obj, out, schemaLocation, formatted, fragment);
return out.toString();
}
public void setPrefixMap(final Map<String, String> prefixMap) {
Lock write = prefixLock.writeLock();
write.lock();
try {
this.mapper = new NamespacePrefixMapper() {
@Override
public String getPreferredPrefix(String namespaceUri,
String suggestion, boolean requirePrefix) {
return prefixMap.get(namespaceUri);
}
};
} finally {
write.unlock();
}
}
} }

View file

@ -0,0 +1,106 @@
/**
* This software was developed and / or modified by Raytheon Company,
* pursuant to Contract DG133W-05-CQ-1067 with the US Government.
*
* U.S. EXPORT CONTROLLED TECHNICAL DATA
* This software product contains export-restricted data whose
* export/transfer/disclosure is restricted by U.S. law. Dissemination
* to non-U.S. persons whether in the United States or abroad requires
* an export license or other authorization.
*
* Contractor Name: Raytheon Company
* Contractor Address: 6825 Pine Street, Suite 340
* Mail Stop B8
* Omaha, NE 68106
* 402.291.0100
*
* See the AWIPS II Master Rights File ("Master Rights File.pdf") for
* further licensing information.
**/
package com.raytheon.uf.edex.ogc.common.jaxb;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import com.raytheon.uf.common.serialization.jaxb.PooledJaxbMarshallerStrategy;
/**
* Marshaller strategy for OGC XML operations. Includes w3c dom operations.
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Jul 15, 2014 3373 bclement Initial creation
*
* </pre>
*
* @author bclement
* @version 1.0
*/
public class OgcMarshallerStrategy extends PooledJaxbMarshallerStrategy {
/**
*
*/
public OgcMarshallerStrategy() {
super();
}
/**
* @param poolSize
* @param pooledObjectSizeLimit
*/
public OgcMarshallerStrategy(int poolSize, int pooledObjectSizeLimit) {
super(poolSize, pooledObjectSizeLimit);
}
/**
* Unmarshal object from w3c node.
*
* @param ctx
* @param node
* @return
* @throws JAXBException
*/
public Object unmarshal(JAXBContext ctx, Node node) throws JAXBException {
Unmarshaller msh = createUnmarshaller(ctx);
Object obj = msh.unmarshal(node);
if (obj instanceof JAXBElement<?>) {
obj = ((JAXBElement<?>) obj).getValue();
}
return obj;
}
/**
* Marshal object to w3c node
*
* @param ctx
* @param obj
* @return
* @throws JAXBException
* @throws ParserConfigurationException
*/
public Node marshalToNode(JAXBContext ctx, Object obj)
throws JAXBException, ParserConfigurationException {
Marshaller msh = createMarshaller(ctx);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
msh.marshal(obj, doc);
return doc.getFirstChild();
}
}

View file

@ -43,6 +43,7 @@ import com.raytheon.uf.common.message.Header;
import com.raytheon.uf.common.message.Message; import com.raytheon.uf.common.message.Message;
import com.raytheon.uf.common.message.Property; import com.raytheon.uf.common.message.Property;
import com.raytheon.uf.common.serialization.JAXBManager; import com.raytheon.uf.common.serialization.JAXBManager;
import com.raytheon.uf.common.serialization.MarshalOptions;
import com.raytheon.uf.common.serialization.SerializationException; import com.raytheon.uf.common.serialization.SerializationException;
import com.raytheon.uf.common.serialization.SerializationUtil; import com.raytheon.uf.common.serialization.SerializationUtil;
import com.raytheon.uf.common.site.SiteMap; import com.raytheon.uf.common.site.SiteMap;
@ -72,6 +73,7 @@ import com.raytheon.uf.edex.plugin.text.db.TextDB;
* -------------------------------- * --------------------------------
* 27Apr2012 564 jkorman Added sort to ALL times retrieval. * 27Apr2012 564 jkorman Added sort to ALL times retrieval.
* May 15, 2014 2536 bclement moved from uf.edex.textdbsrv, added marshalToStream() * May 15, 2014 2536 bclement moved from uf.edex.textdbsrv, added marshalToStream()
* Jul 15, 2014 3373 bclement jaxb manager api changes
* *
* </pre> * </pre>
* *
@ -457,7 +459,7 @@ public class TextViewAdapter implements ICommandExecutor {
} catch (JAXBException e) { } catch (JAXBException e) {
throw new SerializationException("Unable to create JAXB manager", e); throw new SerializationException("Unable to create JAXB manager", e);
} }
jaxbManager.marshalToStream(prod, stream, false); jaxbManager.marshalToStream(prod, stream, MarshalOptions.UNFORMATTED);
} }
/** /**

View file

@ -38,6 +38,8 @@ import net.opengis.gml.v_3_1_1.ObjectFactory;
import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.ArrayUtils;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import com.raytheon.uf.common.serialization.MarshalOptions;
import com.raytheon.uf.common.serialization.SerializationException;
import com.raytheon.uf.common.status.IUFStatusHandler; import com.raytheon.uf.common.status.IUFStatusHandler;
import com.raytheon.uf.common.status.UFStatus; import com.raytheon.uf.common.status.UFStatus;
import com.raytheon.uf.common.util.registry.RegistryException; import com.raytheon.uf.common.util.registry.RegistryException;
@ -47,6 +49,7 @@ import com.raytheon.uf.edex.ogc.common.jaxb.OgcJaxbManager;
import com.raytheon.uf.edex.wfs.WfsException; import com.raytheon.uf.edex.wfs.WfsException;
import com.raytheon.uf.edex.wfs.WfsFeatureType; import com.raytheon.uf.edex.wfs.WfsFeatureType;
import com.raytheon.uf.edex.wfs.request.QualifiedName; import com.raytheon.uf.edex.wfs.request.QualifiedName;
import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
/** /**
* Wfs registry implementation. Handles wfs sources and the JAXB context * Wfs registry implementation. Handles wfs sources and the JAXB context
@ -59,6 +62,7 @@ import com.raytheon.uf.edex.wfs.request.QualifiedName;
* ------------ ---------- ----------- -------------------------- * ------------ ---------- ----------- --------------------------
* Apr 11, 2011 bclement Initial creation * Apr 11, 2011 bclement Initial creation
* May 30, 2013 753 dhladky reverted to original * May 30, 2013 753 dhladky reverted to original
* Jul 15, 2014 3373 bclement jaxb manager api changes
* *
* </pre> * </pre>
* *
@ -173,8 +177,14 @@ public class WfsRegistryImpl implements IWfsRegistry {
write.lock(); write.lock();
try { try {
jaxbContextVersion = currentVersion; jaxbContextVersion = currentVersion;
jaxbManager = new OgcJaxbManager(jaxbClasses); NamespacePrefixMapper mapper = new NamespacePrefixMapper() {
jaxbManager.setPrefixMap(NS_MAP); @Override
public String getPreferredPrefix(String uri,
String suggestion, boolean requirePrefix) {
return NS_MAP.get(uri);
}
};
jaxbManager = new OgcJaxbManager(mapper, jaxbClasses);
} finally { } finally {
write.unlock(); write.unlock();
} }
@ -188,7 +198,7 @@ public class WfsRegistryImpl implements IWfsRegistry {
* @throws JAXBException * @throws JAXBException
*/ */
public Object unmarshal(String xml) throws JAXBException { public Object unmarshal(String xml) throws JAXBException {
return getManager().unmarshal(xml); return getManager().unmarshalFromXml(xml);
} }
public Object unmarshal(Node node) throws JAXBException { public Object unmarshal(Node node) throws JAXBException {
@ -199,9 +209,11 @@ public class WfsRegistryImpl implements IWfsRegistry {
* @param in * @param in
* @return * @return
* @throws JAXBException * @throws JAXBException
* @throws SerializationException
*/ */
public Object unmarshal(InputStream in) throws JAXBException { public Object unmarshal(InputStream in) throws JAXBException,
return getManager().unmarshal(in); SerializationException {
return getManager().unmarshalFromInputStream(in);
} }
/** /**
@ -210,30 +222,32 @@ public class WfsRegistryImpl implements IWfsRegistry {
* @throws JAXBException * @throws JAXBException
*/ */
public String marshal(Object obj) throws JAXBException { public String marshal(Object obj) throws JAXBException {
return getManager().marshal(obj, false); return getManager().marshalToXml(obj, MarshalOptions.UNFORMATTED);
} }
public String marshal(Object obj, boolean fragment) throws JAXBException {
MarshalOptions options = new MarshalOptions(false, fragment);
return getManager().marshalToXml(obj, options);
}
public Node marshalToNode(Object obj) throws JAXBException, public Node marshalToNode(Object obj) throws JAXBException,
ParserConfigurationException { ParserConfigurationException {
return getManager().marshalToNode(obj); return getManager().marshalToNode(obj);
} }
public String marshal(Object obj, boolean fragment) throws JAXBException { public String marshal(Object obj, MarshalOptions options) throws JAXBException {
return getManager().marshal(obj, false, fragment); return getManager().marshalToXml(obj, options);
} }
/** /**
* @param obj * @param obj
* @param out * @param out
* @throws JAXBException * @throws JAXBException
* @throws SerializationException
*/ */
public void marshal(Object obj, OutputStream out) throws JAXBException { public void marshal(Object obj, OutputStream out) throws JAXBException,
getManager().marshal(obj, out, null, false, false); SerializationException {
} getManager().marshalToStream(obj, out, MarshalOptions.UNFORMATTED);
public void marshal(Object obj, OutputStream out, boolean fragment)
throws JAXBException {
getManager().marshal(obj, out, null, false, fragment);
} }
/** /**

View file

@ -116,8 +116,9 @@ import com.raytheon.uf.edex.wfs.util.XMLGregorianCalendarConverter;
* Date Ticket# Engineer Description * Date Ticket# Engineer Description
* ------------ ---------- ----------- -------------------------- * ------------ ---------- ----------- --------------------------
* Oct 17, 2012 bclement Initial creation * Oct 17, 2012 bclement Initial creation
* Sep 18, 2013 #411 skorolev Added required RESPONSE METADATA * Sep 18, 2013 #411 skorolev Added required RESPONSE METADATA
* Nov 11, 2013 2539 bclement moved registry/marshal to parent * Nov 11, 2013 2539 bclement moved registry/marshal to parent
* Jul 15, 2014 3373 bclement jaxb manager api changes
* *
* </pre> * </pre>
* *
@ -1063,7 +1064,7 @@ public class Wfs2_0_0Provider extends AbstractWfsProvider implements
try { try {
String xml = registry.marshal(object, true); String xml = registry.marshal(object, true);
rval.put(key, xml); rval.put(key, xml);
} catch (JAXBException e) { } catch (Exception e) {
log.error("Problem marshalling parameter value", e); log.error("Problem marshalling parameter value", e);
// is it possible that this isn't our fault? // is it possible that this isn't our fault?
throw new WfsException(Code.OperationProcessingFailed); throw new WfsException(Code.OperationProcessingFailed);