Issue #2552 - it is now possible to dynamically inject packages into the JaxbManager via Spring.

Amend: datadelivery jaxables are now injected
Amend: created a getInstance method for EbxmlJaxbManager
Amend: use value instead of bean in spring configuration

Change-Id: Ib6c11b03964fbaeee24db352a8f0acc1b43fa3c0

Former-commit-id: ce16efc9a1 [formerly be78fab116 [formerly 283853ead3588ef8ea296507b11a9ee9a23e9236]]
Former-commit-id: be78fab116
Former-commit-id: 782675951b
This commit is contained in:
Bryan Kowal 2013-11-14 11:03:57 -06:00
parent 2f134e6aa3
commit 8b274264c9
10 changed files with 95 additions and 41 deletions

View file

@ -1,4 +1,5 @@
source.. = src/ source.. = src/
output.. = bin/ output.. = bin/
bin.includes = META-INF/,\ bin.includes = META-INF/,\
. .,\
res/

View file

@ -0,0 +1,9 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean factory-bean="ebxmlJaxbManager" factory-method="findJaxables"
depends-on="ebxmlJaxbManager">
<constructor-arg value="com.raytheon.uf.common.datadelivery.registry" />
</bean>
</beans>

View file

@ -39,6 +39,7 @@ import com.raytheon.uf.common.serialization.SerializationException;
* Sep 07, 2012 1102 djohnson Initial creation * Sep 07, 2012 1102 djohnson Initial creation
* Jun 03, 2013 2038 djohnson Add equals/hashcode. * Jun 03, 2013 2038 djohnson Add equals/hashcode.
* Oct 31, 2013 2361 njensen Use specific JAXBManager instead of SerializationUtil * Oct 31, 2013 2361 njensen Use specific JAXBManager instead of SerializationUtil
* Nov 14, 2013 2552 bkowal EbxmlJaxbManager is now accessed via getInstance
* *
* </pre> * </pre>
* *
@ -61,7 +62,8 @@ class JaxbEncoder extends StringBasedEncoder {
@Override @Override
Object decodeContent(String content) throws SerializationException { Object decodeContent(String content) throws SerializationException {
try { try {
return EbxmlJaxbManager.getJaxbManager().unmarshalFromXml(content); return EbxmlJaxbManager.getInstance().getJaxbManager()
.unmarshalFromXml(content);
} catch (JAXBException e) { } catch (JAXBException e) {
throw new SerializationException("Unable to decode the object!", e); throw new SerializationException("Unable to decode the object!", e);
} }
@ -73,8 +75,8 @@ class JaxbEncoder extends StringBasedEncoder {
@Override @Override
String encodeContent(Object objectToEncode) throws SerializationException { String encodeContent(Object objectToEncode) throws SerializationException {
try { try {
return new String(EbxmlJaxbManager.getJaxbManager().marshalToXml( return new String(EbxmlJaxbManager.getInstance().getJaxbManager()
objectToEncode)); .marshalToXml(objectToEncode));
} catch (JAXBException e) { } catch (JAXBException e) {
throw new SerializationException("Unable to encode the object!", e); throw new SerializationException("Unable to encode the object!", e);
} }

View file

@ -13,7 +13,8 @@ Require-Bundle: javax.persistence;bundle-version="1.0.0",
org.apache.commons.cxf;bundle-version="1.0.0", org.apache.commons.cxf;bundle-version="1.0.0",
com.raytheon.uf.common.status;bundle-version="1.12.1174", com.raytheon.uf.common.status;bundle-version="1.12.1174",
com.raytheon.uf.common.util;bundle-version="1.12.1174", com.raytheon.uf.common.util;bundle-version="1.12.1174",
org.reflections;bundle-version="0.9.9" org.reflections;bundle-version="0.9.9",
org.apache.commons.logging;bundle-version="1.1.2"
Export-Package: com.raytheon.uf.common.registry, Export-Package: com.raytheon.uf.common.registry,
com.raytheon.uf.common.registry.schemas.ebxml.util, com.raytheon.uf.common.registry.schemas.ebxml.util,
com.raytheon.uf.common.registry.schemas.ebxml.util.annotations, com.raytheon.uf.common.registry.schemas.ebxml.util.annotations,

View file

@ -1,4 +1,5 @@
source.. = src/ source.. = src/
output.. = bin/ output.. = bin/
bin.includes = META-INF/,\ bin.includes = META-INF/,\
. .,\
res/

View file

@ -0,0 +1,8 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="ebxmlJaxbManager" factory-method="getInstance"
class="com.raytheon.uf.common.registry.schemas.ebxml.util.EbxmlJaxbManager" />
</beans>

View file

@ -19,6 +19,7 @@
**/ **/
package com.raytheon.uf.common.registry.schemas.ebxml.util; package com.raytheon.uf.common.registry.schemas.ebxml.util;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
@ -29,6 +30,8 @@ import org.reflections.Reflections;
import org.reflections.scanners.TypeAnnotationsScanner; import org.reflections.scanners.TypeAnnotationsScanner;
import org.reflections.util.ClasspathHelper; import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder; import org.reflections.util.ConfigurationBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.raytheon.uf.common.serialization.JAXBManager; import com.raytheon.uf.common.serialization.JAXBManager;
@ -42,6 +45,9 @@ import com.raytheon.uf.common.serialization.JAXBManager;
* Date Ticket# Engineer Description * Date Ticket# Engineer Description
* ------------ ---------- ----------- -------------------------- * ------------ ---------- ----------- --------------------------
* Oct 30, 2013 2361 njensen Initial creation * Oct 30, 2013 2361 njensen Initial creation
* Nov 14, 2013 2252 bkowal Added the ability to dynamically inject packages
* that this jaxb implementation should support.
* Eliminated use of System.out.
* *
* </pre> * </pre>
* *
@ -51,53 +57,70 @@ import com.raytheon.uf.common.serialization.JAXBManager;
public class EbxmlJaxbManager { public class EbxmlJaxbManager {
/** The logger */
private static Log theLogger = LogFactory.getLog(EbxmlJaxbManager.class);
private static EbxmlJaxbManager instance;
private static JAXBManager jaxb; private static JAXBManager jaxb;
/** private static Set<Class<?>> jaxables;
* Uses reflections to scan for ebxml datadelivery registry classes that can
* be transformed to/from xml, and then adds in the ebxml object factories. public static synchronized EbxmlJaxbManager getInstance() {
* if (instance == null) {
* @return the classes it found without any duplicates instance = new EbxmlJaxbManager();
*/ }
private static Class<?>[] getClasses() { return instance;
String[] packageNames = new String[] { }
"com.raytheon.uf.common.datadelivery.registry" };
public String findJaxables(String packageName) {
theLogger.info("Scanning package ... " + packageName);
long t0 = System.currentTimeMillis(); long t0 = System.currentTimeMillis();
ConfigurationBuilder cb = new ConfigurationBuilder(); ConfigurationBuilder cb = new ConfigurationBuilder();
for (String pkg : packageNames) { cb.addUrls(ClasspathHelper.forPackage(packageName));
cb.addUrls(ClasspathHelper.forPackage(pkg));
}
cb.setScanners(new TypeAnnotationsScanner()); cb.setScanners(new TypeAnnotationsScanner());
// the call to build() will do the actual scanning so the separate // the call to build() will do the actual scanning so the separate
// calls to getTypesAnnotatedWith(class, false) will not slow it down // calls to getTypesAnnotatedWith(class, false) will not slow it down
Reflections reflecs = cb.build(); Reflections reflecs = cb.build();
Set<Class<?>> set = reflecs.getTypesAnnotatedWith( Set<Class<?>> set = reflecs.getTypesAnnotatedWith(
XmlAccessorType.class, false); XmlAccessorType.class, false);
set.addAll(reflecs.getTypesAnnotatedWith(XmlRegistry.class, false)); synchronized (jaxables) {
// add them to set for auditing purposes initially
set.addAll(reflecs.getTypesAnnotatedWith(XmlRegistry.class, false));
// copy set to jaxables
jaxables.addAll(set);
}
long t1 = System.currentTimeMillis(); long t1 = System.currentTimeMillis();
System.out.println("Found " + set.size() + " classes for ebxml in " theLogger.info("Found " + set.size() + " classes for ebxml in "
+ (t1 - t0) + " ms"); + (t1 - t0) + " ms");
// if jaxb has already been initialized, reset it so that it will be
// recreated with the latest set of jaxable classes.
synchronized (this) {
jaxb = null;
}
set.add(oasis.names.tc.ebxml.regrep.xsd.lcm.v4.ObjectFactory.class); return packageName;
set.add(oasis.names.tc.ebxml.regrep.xsd.query.v4.ObjectFactory.class);
set.add(oasis.names.tc.ebxml.regrep.xsd.rim.v4.ObjectFactory.class);
set.add(oasis.names.tc.ebxml.regrep.xsd.rs.v4.ObjectFactory.class);
set.add(oasis.names.tc.ebxml.regrep.xsd.spi.v4.ObjectFactory.class);
return set.toArray(new Class[0]);
} }
public static synchronized JAXBManager getJaxbManager() public synchronized JAXBManager getJaxbManager() throws JAXBException {
throws JAXBException {
if (jaxb == null) { if (jaxb == null) {
jaxb = new JAXBManager(getClasses()); jaxb = new JAXBManager(jaxables.toArray(new Class[0]));
} }
return jaxb; return jaxb;
} }
private EbxmlJaxbManager() { private EbxmlJaxbManager() {
jaxables = new HashSet<Class<?>>();
// add the default jaxables
jaxables.add(oasis.names.tc.ebxml.regrep.xsd.lcm.v4.ObjectFactory.class);
jaxables.add(oasis.names.tc.ebxml.regrep.xsd.query.v4.ObjectFactory.class);
jaxables.add(oasis.names.tc.ebxml.regrep.xsd.rim.v4.ObjectFactory.class);
jaxables.add(oasis.names.tc.ebxml.regrep.xsd.rs.v4.ObjectFactory.class);
jaxables.add(oasis.names.tc.ebxml.regrep.xsd.spi.v4.ObjectFactory.class);
theLogger.info("Initialization Complete.");
} }
}
}

View file

@ -42,6 +42,7 @@ import org.hibernate.usertype.UserType;
* ------------ ---------- ----------- -------------------------- * ------------ ---------- ----------- --------------------------
* --/--/---- Initial creation * --/--/---- Initial creation
* Oct 31, 2013 2361 njensen Use specific JAXBManager instead of SerializationUtil * Oct 31, 2013 2361 njensen Use specific JAXBManager instead of SerializationUtil
* Nov 14, 2013 2552 bkowal EbxmlJaxbManager is now accessed via getInstance
* *
* </pre> * </pre>
* *
@ -93,7 +94,8 @@ public class SerializedType implements UserType {
if (obj != null) { if (obj != null) {
try { try {
return EbxmlJaxbManager.getJaxbManager().unmarshalFromXml(obj); return EbxmlJaxbManager.getInstance().getJaxbManager()
.unmarshalFromXml(obj);
} catch (Exception e) { } catch (Exception e) {
throw new HibernateException("Error retrieving AnyType data", e); throw new HibernateException("Error retrieving AnyType data", e);
} }
@ -110,8 +112,8 @@ public class SerializedType implements UserType {
} else { } else {
try { try {
; ;
statement.setString(index, EbxmlJaxbManager.getJaxbManager() statement.setString(index, EbxmlJaxbManager.getInstance()
.marshalToXml(value)); .getJaxbManager().marshalToXml(value));
} catch (Exception e) { } catch (Exception e) {
throw new HibernateException("Error storing AnyType data", e); throw new HibernateException("Error storing AnyType data", e);
} }

View file

@ -89,6 +89,7 @@ import com.raytheon.uf.edex.registry.ebxml.init.RegistryInitializedListener;
* Jun 24, 2013 2106 djohnson Invoke registry initialized listeners in their own transaction so * Jun 24, 2013 2106 djohnson Invoke registry initialized listeners in their own transaction so
* they can't fail the ebxml schema creation/population. * they can't fail the ebxml schema creation/population.
* Nov 01, 2013 2361 njensen Use EbxmlJaxbManager instead of SerializationUtil * Nov 01, 2013 2361 njensen Use EbxmlJaxbManager instead of SerializationUtil
* Nov 14, 2013 2552 bkowal EbxmlJaxbManager is now accessed via getInstance
* </pre> * </pre>
* *
* @author bphillip * @author bphillip
@ -167,8 +168,11 @@ public class DbInit extends com.raytheon.uf.edex.database.init.DbInit implements
SubmitObjectsRequest obj = null; SubmitObjectsRequest obj = null;
try { try {
obj = EbxmlJaxbManager.getJaxbManager().unmarshalFromXmlFile( obj = EbxmlJaxbManager
SubmitObjectsRequest.class, fileList[i]); .getInstance()
.getJaxbManager()
.unmarshalFromXmlFile(SubmitObjectsRequest.class,
fileList[i]);
} catch (JAXBException e) { } catch (JAXBException e) {
throw new SerializationException( throw new SerializationException(
"Error unmarshalling from file: " "Error unmarshalling from file: "
@ -360,8 +364,7 @@ public class DbInit extends com.raytheon.uf.edex.database.init.DbInit implements
listener.executeAfterRegistryInit(); listener.executeAfterRegistryInit();
} }
} catch (Throwable t) { } catch (Throwable t) {
throw new RuntimeException( throw new RuntimeException("Error initializing EBXML database!", t);
"Error initializing EBXML database!", t);
} }
} }

View file

@ -41,6 +41,7 @@ import com.raytheon.uf.common.status.UFStatus;
* Date Ticket# Engineer Description * Date Ticket# Engineer Description
* ------------ ---------- ----------- -------------------------- * ------------ ---------- ----------- --------------------------
* Feb 29, 2012 bphillip Initial creation * Feb 29, 2012 bphillip Initial creation
* Nov 14, 2013 2552 bkowal EbxmlJaxbManager is now accessed via getInstance
* *
* </pre> * </pre>
* *
@ -82,8 +83,11 @@ public class AdhocQueryExpressionManager {
AdhocQueryExpression obj = null; AdhocQueryExpression obj = null;
try { try {
obj = EbxmlJaxbManager.getJaxbManager().unmarshalFromXmlFile( obj = EbxmlJaxbManager
AdhocQueryExpression.class, fileList[i]); .getInstance()
.getJaxbManager()
.unmarshalFromXmlFile(AdhocQueryExpression.class,
fileList[i]);
} catch (Exception e) { } catch (Exception e) {
statusHandler.error("Error getting predefined adhoc queries.", statusHandler.error("Error getting predefined adhoc queries.",
e); e);