From e428f257cbaf12db705f0a2532765900c87e6bab Mon Sep 17 00:00:00 2001 From: Brian Clements Date: Tue, 15 Jul 2014 15:13:26 -0500 Subject: [PATCH] Omaha #3373 jaxb manager refactor api changes reworked ogc jaxb manager to use parent class Change-Id: I92b4cd6a48faf80462b9ca483a4f5d6cba78de48 Former-commit-id: 125c31f035057676d8c97a9b6c7a1d7b1b7f5bcb --- .../comm/packet/SessionPayload.java | 7 +- .../provider/CollaborationXmlManager.java | 37 +- .../common/registry/RegistryJaxbManager.java | 38 +- .../registry/RegistryNamespaceMapper.java | 3 +- .../schemas/ebxml/util/EbxmlJaxbManager.java | 3 +- .../edex/ogc/common/jaxb/OgcJaxbManager.java | 329 +++++------------- .../common/jaxb/OgcMarshallerStrategy.java | 106 ++++++ .../text/dbsrv/impl/TextViewAdapter.java | 4 +- .../uf/edex/wfs/reg/WfsRegistryImpl.java | 44 ++- .../uf/edex/wfs/v2_0_0/Wfs2_0_0Provider.java | 7 +- 10 files changed, 255 insertions(+), 323 deletions(-) create mode 100644 edexOsgi/com.raytheon.uf.edex.ogc.common/src/com/raytheon/uf/edex/ogc/common/jaxb/OgcMarshallerStrategy.java diff --git a/cave/com.raytheon.uf.viz.collaboration.comm/src/com/raytheon/uf/viz/collaboration/comm/packet/SessionPayload.java b/cave/com.raytheon.uf.viz.collaboration.comm/src/com/raytheon/uf/viz/collaboration/comm/packet/SessionPayload.java index b4564d915d..aec0ba9a2c 100644 --- a/cave/com.raytheon.uf.viz.collaboration.comm/src/com/raytheon/uf/viz/collaboration/comm/packet/SessionPayload.java +++ b/cave/com.raytheon.uf.viz.collaboration.comm/src/com/raytheon/uf/viz/collaboration/comm/packet/SessionPayload.java @@ -23,6 +23,7 @@ import java.util.Arrays; 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.status.IUFStatusHandler; 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 * Feb 27, 2013 2756 bclement extends BaseExtension * Jun 12, 2013 2903 bclement default to wrap jaxb xml in base64 + * Jul 15, 2014 3373 bclement added fragment marshal options * * * @@ -69,6 +71,9 @@ public class SessionPayload extends BaseExtension { public static final String ENCODING_ATTRIBUTE = "encoding"; + private static final MarshalOptions UNFORMATTED_FRAGMENT = new MarshalOptions( + false, true); + private final PayloadType payloadType; private final SerializationMode mode; @@ -135,7 +140,7 @@ public class SessionPayload extends BaseExtension { try { CollaborationXmlManager jaxb = CollaborationXmlManager .getInstance(); - String xml = jaxb.marshalToFragment(data); + String xml = jaxb.marshalToXml(data, UNFORMATTED_FRAGMENT); /* * wrap JAXB XML in base64 to avoid problems with openfire * disconnecting due to complex XML diff --git a/cave/com.raytheon.uf.viz.collaboration.comm/src/com/raytheon/uf/viz/collaboration/comm/provider/CollaborationXmlManager.java b/cave/com.raytheon.uf.viz.collaboration.comm/src/com/raytheon/uf/viz/collaboration/comm/provider/CollaborationXmlManager.java index ece70e17fa..fe3706da2a 100644 --- a/cave/com.raytheon.uf.viz.collaboration.comm/src/com/raytheon/uf/viz/collaboration/comm/provider/CollaborationXmlManager.java +++ b/cave/com.raytheon.uf.viz.collaboration.comm/src/com/raytheon/uf/viz/collaboration/comm/provider/CollaborationXmlManager.java @@ -19,13 +19,12 @@ **/ package com.raytheon.uf.viz.collaboration.comm.provider; -import java.io.StringWriter; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.bind.UnmarshallerHandler; @@ -60,6 +59,7 @@ import com.raytheon.uf.viz.core.reflect.SubClassLocator; * ------------- -------- ----------- -------------------------- * Oct 31, 2013 2491 bsteffen Initial creation * Dec 18, 2013 2562 bclement extend jaxb manager, xpp/fragment support + * Jul 15, 2014 3373 bclement jaxb manager changes, unmarshalFromXPP() doesn't pool * * * @@ -147,43 +147,14 @@ public class CollaborationXmlManager extends JAXBManager { */ public Object unmarshalFromXPP(XmlPullParser parser) throws CollaborationException { - Unmarshaller unmarshaller = null; try { - unmarshaller = getUnmarshaller(); + JAXBContext ctx = getJaxbContext(); + Unmarshaller unmarshaller = ctx.createUnmarshaller(); UnmarshallerHandler handler = unmarshaller.getUnmarshallerHandler(); PullParserJaxbAdapter adapter = new PullParserJaxbAdapter(parser, handler); return adapter.unmarshal(); } catch (Exception 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); - } } } diff --git a/edexOsgi/com.raytheon.uf.common.registry.ebxml/src/com/raytheon/uf/common/registry/RegistryJaxbManager.java b/edexOsgi/com.raytheon.uf.common.registry.ebxml/src/com/raytheon/uf/common/registry/RegistryJaxbManager.java index e58d994d65..cf7710f9d4 100644 --- a/edexOsgi/com.raytheon.uf.common.registry.ebxml/src/com/raytheon/uf/common/registry/RegistryJaxbManager.java +++ b/edexOsgi/com.raytheon.uf.common.registry.ebxml/src/com/raytheon/uf/common/registry/RegistryJaxbManager.java @@ -19,10 +19,12 @@ **/ package com.raytheon.uf.common.registry; +import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import com.raytheon.uf.common.serialization.JAXBManager; +import com.raytheon.uf.common.serialization.jaxb.JaxbMarshallerStrategy; import com.sun.xml.bind.marshaller.NamespacePrefixMapper; /** @@ -38,6 +40,7 @@ import com.sun.xml.bind.marshaller.NamespacePrefixMapper; * Date Ticket# Engineer Description * ------------ ---------- ----------- -------------------------- * 8/8/2013 1692 bphillip Initial implementation + * Jul 15, 2014 3373 bclement jaxb manager changes * * * @author bphillip @@ -48,17 +51,6 @@ public class RegistryJaxbManager extends JAXBManager { /** The namespace mapper property name on the marshaller */ 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 * @@ -72,25 +64,29 @@ public class RegistryJaxbManager extends JAXBManager { public RegistryJaxbManager(RegistryNamespaceMapper namespaceMapper) throws JAXBException { super( + createStrategy(namespaceMapper), 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.rim.v4.ObjectFactory.class, oasis.names.tc.ebxml.regrep.xsd.rs.v4.ObjectFactory.class, oasis.names.tc.ebxml.regrep.xsd.spi.v4.ObjectFactory.class, com.raytheon.uf.common.registry.services.rest.response.RestCollectionResponse.class); - this.namespaceMapper = namespaceMapper; } - @Override - protected Marshaller getMarshaller() throws JAXBException { - Marshaller m = marshallers.poll(); - if (m == null) { - m = getJaxbContext().createMarshaller(); - if (namespaceMapper != null) { - m.setProperty(NAMESPACE_PREFIX_MAPPER_PROPERTY, namespaceMapper); + private static JaxbMarshallerStrategy createStrategy( + final NamespacePrefixMapper namespaceMapper) { + return new JaxbMarshallerStrategy() { + @Override + protected Marshaller createMarshaller(JAXBContext context) + throws JAXBException { + Marshaller rval = super.createMarshaller(context); + if (namespaceMapper != null) { + rval.setProperty(NAMESPACE_PREFIX_MAPPER_PROPERTY, + namespaceMapper); + } + return rval; } - } - return m; + }; } } diff --git a/edexOsgi/com.raytheon.uf.common.registry.ebxml/src/com/raytheon/uf/common/registry/RegistryNamespaceMapper.java b/edexOsgi/com.raytheon.uf.common.registry.ebxml/src/com/raytheon/uf/common/registry/RegistryNamespaceMapper.java index 65d1d763fd..887327e9ca 100644 --- a/edexOsgi/com.raytheon.uf.common.registry.ebxml/src/com/raytheon/uf/common/registry/RegistryNamespaceMapper.java +++ b/edexOsgi/com.raytheon.uf.common.registry.ebxml/src/com/raytheon/uf/common/registry/RegistryNamespaceMapper.java @@ -38,6 +38,7 @@ import com.sun.xml.bind.marshaller.NamespacePrefixMapper; * Date Ticket# Engineer Description * ------------ ---------- ----------- -------------------------- * 8/8/2013 1692 bphillip Initial implementation + * Jul 15, 2014 3373 bclement removed warning * * * @author bphillip @@ -73,7 +74,7 @@ public class RegistryNamespaceMapper extends NamespacePrefixMapper implements } @Override - public Iterator getPrefixes(String namespaceURI) { + public Iterator getPrefixes(String namespaceURI) { throw new UnsupportedOperationException(); } diff --git a/edexOsgi/com.raytheon.uf.common.registry.schemas.ebxml/src/com/raytheon/uf/common/registry/schemas/ebxml/util/EbxmlJaxbManager.java b/edexOsgi/com.raytheon.uf.common.registry.schemas.ebxml/src/com/raytheon/uf/common/registry/schemas/ebxml/util/EbxmlJaxbManager.java index e83be90f8d..68ce843ad2 100644 --- a/edexOsgi/com.raytheon.uf.common.registry.schemas.ebxml/src/com/raytheon/uf/common/registry/schemas/ebxml/util/EbxmlJaxbManager.java +++ b/edexOsgi/com.raytheon.uf.common.registry.schemas.ebxml/src/com/raytheon/uf/common/registry/schemas/ebxml/util/EbxmlJaxbManager.java @@ -50,6 +50,7 @@ import com.raytheon.uf.common.util.ReflectionUtil; * ------------ ---------- ----------- -------------------------- * Nov 12, 2013 ---- njensen Initial release. * Nov 24, 2013 2584 dhladky versioning + * Jul 15, 2014 3373 bclement pooling jaxb manager * * * @author njensen @@ -118,7 +119,7 @@ public class EbxmlJaxbManager { public synchronized JAXBManager getJaxbManager() throws JAXBException { if (jaxb == null) { - jaxb = new JAXBManager(jaxables.toArray(new Class[0])); + jaxb = new JAXBManager(true, jaxables.toArray(new Class[0])); } return jaxb; } diff --git a/edexOsgi/com.raytheon.uf.edex.ogc.common/src/com/raytheon/uf/edex/ogc/common/jaxb/OgcJaxbManager.java b/edexOsgi/com.raytheon.uf.edex.ogc.common/src/com/raytheon/uf/edex/ogc/common/jaxb/OgcJaxbManager.java index 92830231f7..bbbf4029e6 100644 --- a/edexOsgi/com.raytheon.uf.edex.ogc.common/src/com/raytheon/uf/edex/ogc/common/jaxb/OgcJaxbManager.java +++ b/edexOsgi/com.raytheon.uf.edex.ogc.common/src/com/raytheon/uf/edex/ogc/common/jaxb/OgcJaxbManager.java @@ -16,46 +16,20 @@ * * See the AWIPS II Master Rights File ("Master Rights File.pdf") for * 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; -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.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.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.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.marshaller.NamespacePrefixMapper; @@ -68,7 +42,9 @@ import com.sun.xml.bind.marshaller.NamespacePrefixMapper; * * 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 * * * @@ -77,31 +53,72 @@ import com.sun.xml.bind.marshaller.NamespacePrefixMapper; */ public class OgcJaxbManager extends JAXBManager { - protected final JAXBContext jaxbContext; - - protected static final int QUEUE_SIZE = 10; - - protected final Queue unmarshallers = new ConcurrentLinkedQueue(); - - protected final Queue marshallers = new ConcurrentLinkedQueue(); - - 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"; - public OgcJaxbManager(Class[] classes) throws JAXBException { - jaxbContext = JAXBContext.newInstance(classes, getJaxbConfig()); + private final OgcMarshallerStrategy marshStrategy; + + /** + * @param classes + * @throws JAXBException + */ + public OgcJaxbManager(Class[] classes) throws JAXBException { + this(new OgcMarshallerStrategy(), classes); } - private static Map 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 getJaxbConfig() throws JAXBException { Map jaxbConfig = new HashMap(); TransientAnnotationReader reader = new TransientAnnotationReader(); try { @@ -116,210 +133,28 @@ public class OgcJaxbManager extends JAXBManager { return jaxbConfig; } - protected Unmarshaller getUnmarshaller() throws JAXBException { - Unmarshaller m = unmarshallers.poll(); - if (m == null) { - if (unmarshallersCreated < QUEUE_SIZE) { - synchronized (unmarshallers) { - 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); - } - } - } - + /** + * @see OgcMarshallerStrategy#unmarshal(JAXBContext, Node) + * @param node + * @return + * @throws JAXBException + */ public Object unmarshal(Node node) throws JAXBException { - Unmarshaller msh = null; - try { - 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); + JAXBContext ctx = getJaxbContext(); + return marshStrategy.unmarshal(ctx, node); } + /** + * @see OgcMarshallerStrategy#marshalToNode(JAXBContext, Object) + * @param obj + * @return + * @throws JAXBException + * @throws ParserConfigurationException + */ public Node marshalToNode(Object obj) throws JAXBException, ParserConfigurationException { - Marshaller msh = getMarshaller(); - try { - 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); - } + JAXBContext ctx = getJaxbContext(); + return marshStrategy.marshalToNode(ctx, obj); } - 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 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(); - } - } - } diff --git a/edexOsgi/com.raytheon.uf.edex.ogc.common/src/com/raytheon/uf/edex/ogc/common/jaxb/OgcMarshallerStrategy.java b/edexOsgi/com.raytheon.uf.edex.ogc.common/src/com/raytheon/uf/edex/ogc/common/jaxb/OgcMarshallerStrategy.java new file mode 100644 index 0000000000..6fcab90f11 --- /dev/null +++ b/edexOsgi/com.raytheon.uf.edex.ogc.common/src/com/raytheon/uf/edex/ogc/common/jaxb/OgcMarshallerStrategy.java @@ -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. + * + *
+ * 
+ * SOFTWARE HISTORY
+ * 
+ * Date         Ticket#    Engineer    Description
+ * ------------ ---------- ----------- --------------------------
+ * Jul 15, 2014 3373       bclement     Initial creation
+ * 
+ * 
+ * + * @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(); + } + +} diff --git a/edexOsgi/com.raytheon.uf.edex.plugin.text/src/com/raytheon/uf/edex/plugin/text/dbsrv/impl/TextViewAdapter.java b/edexOsgi/com.raytheon.uf.edex.plugin.text/src/com/raytheon/uf/edex/plugin/text/dbsrv/impl/TextViewAdapter.java index 5005e201d5..6d3f11ad83 100644 --- a/edexOsgi/com.raytheon.uf.edex.plugin.text/src/com/raytheon/uf/edex/plugin/text/dbsrv/impl/TextViewAdapter.java +++ b/edexOsgi/com.raytheon.uf.edex.plugin.text/src/com/raytheon/uf/edex/plugin/text/dbsrv/impl/TextViewAdapter.java @@ -43,6 +43,7 @@ import com.raytheon.uf.common.message.Header; import com.raytheon.uf.common.message.Message; import com.raytheon.uf.common.message.Property; 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.SerializationUtil; 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. * May 15, 2014 2536 bclement moved from uf.edex.textdbsrv, added marshalToStream() + * Jul 15, 2014 3373 bclement jaxb manager api changes * * * @@ -457,7 +459,7 @@ public class TextViewAdapter implements ICommandExecutor { } catch (JAXBException e) { throw new SerializationException("Unable to create JAXB manager", e); } - jaxbManager.marshalToStream(prod, stream, false); + jaxbManager.marshalToStream(prod, stream, MarshalOptions.UNFORMATTED); } /** diff --git a/edexOsgi/com.raytheon.uf.edex.wfs/src/com/raytheon/uf/edex/wfs/reg/WfsRegistryImpl.java b/edexOsgi/com.raytheon.uf.edex.wfs/src/com/raytheon/uf/edex/wfs/reg/WfsRegistryImpl.java index cdfbddf64f..7f395b031f 100644 --- a/edexOsgi/com.raytheon.uf.edex.wfs/src/com/raytheon/uf/edex/wfs/reg/WfsRegistryImpl.java +++ b/edexOsgi/com.raytheon.uf.edex.wfs/src/com/raytheon/uf/edex/wfs/reg/WfsRegistryImpl.java @@ -38,6 +38,8 @@ import net.opengis.gml.v_3_1_1.ObjectFactory; import org.apache.commons.lang.ArrayUtils; 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.UFStatus; 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.WfsFeatureType; 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 @@ -59,6 +62,7 @@ import com.raytheon.uf.edex.wfs.request.QualifiedName; * ------------ ---------- ----------- -------------------------- * Apr 11, 2011 bclement Initial creation * May 30, 2013 753 dhladky reverted to original + * Jul 15, 2014 3373 bclement jaxb manager api changes * * * @@ -173,8 +177,14 @@ public class WfsRegistryImpl implements IWfsRegistry { write.lock(); try { jaxbContextVersion = currentVersion; - jaxbManager = new OgcJaxbManager(jaxbClasses); - jaxbManager.setPrefixMap(NS_MAP); + NamespacePrefixMapper mapper = new NamespacePrefixMapper() { + @Override + public String getPreferredPrefix(String uri, + String suggestion, boolean requirePrefix) { + return NS_MAP.get(uri); + } + }; + jaxbManager = new OgcJaxbManager(mapper, jaxbClasses); } finally { write.unlock(); } @@ -188,7 +198,7 @@ public class WfsRegistryImpl implements IWfsRegistry { * @throws JAXBException */ public Object unmarshal(String xml) throws JAXBException { - return getManager().unmarshal(xml); + return getManager().unmarshalFromXml(xml); } public Object unmarshal(Node node) throws JAXBException { @@ -199,9 +209,11 @@ public class WfsRegistryImpl implements IWfsRegistry { * @param in * @return * @throws JAXBException + * @throws SerializationException */ - public Object unmarshal(InputStream in) throws JAXBException { - return getManager().unmarshal(in); + public Object unmarshal(InputStream in) throws JAXBException, + SerializationException { + return getManager().unmarshalFromInputStream(in); } /** @@ -210,30 +222,32 @@ public class WfsRegistryImpl implements IWfsRegistry { * @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, ParserConfigurationException { return getManager().marshalToNode(obj); } - public String marshal(Object obj, boolean fragment) throws JAXBException { - return getManager().marshal(obj, false, fragment); + public String marshal(Object obj, MarshalOptions options) throws JAXBException { + return getManager().marshalToXml(obj, options); } /** * @param obj * @param out * @throws JAXBException + * @throws SerializationException */ - public void marshal(Object obj, OutputStream out) throws JAXBException { - getManager().marshal(obj, out, null, false, false); - } - - public void marshal(Object obj, OutputStream out, boolean fragment) - throws JAXBException { - getManager().marshal(obj, out, null, false, fragment); + public void marshal(Object obj, OutputStream out) throws JAXBException, + SerializationException { + getManager().marshalToStream(obj, out, MarshalOptions.UNFORMATTED); } /** diff --git a/edexOsgi/com.raytheon.uf.edex.wfs/src/com/raytheon/uf/edex/wfs/v2_0_0/Wfs2_0_0Provider.java b/edexOsgi/com.raytheon.uf.edex.wfs/src/com/raytheon/uf/edex/wfs/v2_0_0/Wfs2_0_0Provider.java index afa2484116..a2036d7871 100644 --- a/edexOsgi/com.raytheon.uf.edex.wfs/src/com/raytheon/uf/edex/wfs/v2_0_0/Wfs2_0_0Provider.java +++ b/edexOsgi/com.raytheon.uf.edex.wfs/src/com/raytheon/uf/edex/wfs/v2_0_0/Wfs2_0_0Provider.java @@ -116,8 +116,9 @@ import com.raytheon.uf.edex.wfs.util.XMLGregorianCalendarConverter; * Date Ticket# Engineer Description * ------------ ---------- ----------- -------------------------- * Oct 17, 2012 bclement Initial creation - * Sep 18, 2013 #411 skorolev Added required RESPONSE METADATA - * Nov 11, 2013 2539 bclement moved registry/marshal to parent + * Sep 18, 2013 #411 skorolev Added required RESPONSE METADATA + * Nov 11, 2013 2539 bclement moved registry/marshal to parent + * Jul 15, 2014 3373 bclement jaxb manager api changes * * * @@ -1063,7 +1064,7 @@ public class Wfs2_0_0Provider extends AbstractWfsProvider implements try { String xml = registry.marshal(object, true); rval.put(key, xml); - } catch (JAXBException e) { + } catch (Exception e) { log.error("Problem marshalling parameter value", e); // is it possible that this isn't our fault? throw new WfsException(Code.OperationProcessingFailed);