In JAXB RI, construction of JAXBContext is expensive - only do it once.
-----------------------------------------------------------------------

                 Key: AXIS2-1436
                 URL: http://issues.apache.org/jira/browse/AXIS2-1436
             Project: Apache Axis 2.0 (Axis2)
          Issue Type: Improvement
          Components: databinding
    Affects Versions: 1.1
            Reporter: Chris McClelland
            Priority: Minor


I propose a patch to:

http://svn.apache.org/viewvc/webservices/axis2/branches/java/1_1/modules/jaxbri/src/org/apache/axis2/jaxbri/template/JaxbRIDatabindingTemplate.xsl?revision=464450&content-type=text%2Fplain&pathrev=464450

in order to achieve this. The idea is to keep just one static final copy of the 
JAXBContext for each type, with name mangling to replace the "." with "_". So 
for each class (e.g, com.foo.xsd.Echo)...

private static final javax.xml.bind.JAXBContext com_foo_xsd_Echo;

private static final java.util.HashMap<Class, javax.xml.bind.JAXBContext> 
classContextMap =
    new java.util.HashMap<Class, javax.xml.bind.JAXBContext>();

static {
    javax.xml.bind.JAXBContext jc;
    jc = null;
    try {
        jc = javax.xml.bind.JAXBContext.newInstance(com.foo.xsd.Echo.class);
    }
    catch ( javax.xml.bind.JAXBException ex ) {
        // Should never happen
        System.err.println("Unable to create JAXBContext for class: 
com.foo.xsd.Echo");
        Runtime.getRuntime().exit(-1);
    }
    finally {
        com_foo_xsd_Echo = jc;
        classContextMap.put(com.foo.xsd.Echo.class, jc);
    }
}

I'm fairly sure that the JAXBException above will never be thrown under 
"well-configured" circumstances. Even so, forcing the JVM to quit might be a 
bit extreme. Suggestions welcome!

Patch follows...

--- old/org/apache/axis2/jaxbri/template/JaxbRIDatabindingTemplate.xsl  
2006-10-17 23:01:21.000000000 +0100
+++ new/org/apache/axis2/jaxbri/template/JaxbRIDatabindingTemplate.xsl  
2006-10-17 23:06:13.000000000 +0100
@@ -17,10 +17,38 @@
 
         <xsl:for-each select="param[not(@type = 
preceding-sibling::param/@type)]">
             <xsl:if test="@type!=''">
+                private static final javax.xml.bind.JAXBContext <xsl:value-of 
select="translate(@type,'.','_')"/>;
+            </xsl:if>
+        </xsl:for-each>
+
+        private static final 
java.util.HashMap&lt;Class,javax.xml.bind.JAXBContext&gt; classContextMap = new 
java.util.HashMap&lt;Class,javax.xml.bind.JAXBContext&gt;();
+
+        static {
+            javax.xml.bind.JAXBContext jc;
+            <xsl:for-each select="param[not(@type = 
preceding-sibling::param/@type)]">
+                <xsl:if test="@type!=''">
+                    jc = null;
+                    try {
+                        jc = 
javax.xml.bind.JAXBContext.newInstance(<xsl:value-of select="@type"/>.class);
+                    }
+                    catch ( javax.xml.bind.JAXBException ex ) {
+                        System.err.println("Unable to create JAXBContext for 
class: <xsl:value-of select='@type'/>");
+                        Runtime.getRuntime().exit(-1);
+                    }
+                    finally {
+                        <xsl:value-of select="translate(@type,'.','_')"/> = jc;
+                        classContextMap.put(<xsl:value-of 
select="@type"/>.class, jc);
+                    }
+                </xsl:if>
+            </xsl:for-each>
+        }
+
+        <xsl:for-each select="param[not(@type = 
preceding-sibling::param/@type)]">
+            <xsl:if test="@type!=''">
 
                 private org.apache.axiom.om.OMElement toOM(<xsl:value-of 
select="@type"/> param, org.apache.axiom.soap.SOAPFactory factory, boolean 
optimizeContent) {
                     try {
-                        javax.xml.bind.JAXBContext context = 
javax.xml.bind.JAXBContext.newInstance(<xsl:value-of select="@type"/>.class);
+                        javax.xml.bind.JAXBContext context = <xsl:value-of 
select="translate(@type,'.','_')"/>;
                         javax.xml.bind.Marshaller marshaller = 
context.createMarshaller();
                         
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FRAGMENT, Boolean.TRUE);  
          
         
@@ -56,7 +84,7 @@
             java.lang.Class type,
             java.util.Map extraNamespaces) {
             try {
-                javax.xml.bind.JAXBContext context = 
javax.xml.bind.JAXBContext.newInstance( type );
+                javax.xml.bind.JAXBContext context = classContextMap.get(type);
                 javax.xml.bind.Unmarshaller unmarshaller = 
context.createUnmarshaller();
 
                 return unmarshaller.unmarshal(param.getXMLStreamReader(), 
type).getValue();
@@ -119,7 +147,7 @@
 
             public javax.xml.stream.XMLStreamReader getReader() throws 
javax.xml.stream.XMLStreamException {
                 try {
-                    javax.xml.bind.JAXBContext context = 
javax.xml.bind.JAXBContext.newInstance(outClazz);
+                    javax.xml.bind.JAXBContext context = 
classContextMap.get(outClazz);
                     org.apache.axiom.om.impl.builder.SAXOMBuilder builder = 
new org.apache.axiom.om.impl.builder.SAXOMBuilder();
                     javax.xml.bind.Marshaller marshaller = 
context.createMarshaller();
                     marshaller.marshal(outObject, builder);


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to