cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1082032 - in /cxf/trunk: api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/ api/src/main/java/org/apache/cxf/wsdl/ common/common/src/main/java/org/apache/cxf/configuration/spring/ common/common/src/main/java/org/apache/cxf/jaxb/ rt/d...
Date Wed, 16 Mar 2011 02:36:40 GMT
Author: dkulp
Date: Wed Mar 16 02:36:39 2011
New Revision: 1082032

URL: http://svn.apache.org/viewvc?rev=1082032&view=rev
Log:
Move all the JAXBContext creation stuff into a common JAXBContextCache
to ruduce number of contexts created.   Have not done JAX-RS yet as that
has issues that will prevent this from really working.  Need to talk to
Sergey.

Added:
    cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBContextCache.java   (with props)
Modified:
    cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertion.java
    cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionBuilder.java
    cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/EndpointReferenceUtils.java
    cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/JAXBExtensionHelper.java
    cxf/trunk/common/common/src/main/java/org/apache/cxf/configuration/spring/AbstractBeanDefinitionParser.java
    cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBUtils.java
    cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java
    cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/spring/JettyHTTPServerEngineBeanDefinitionParser.java
    cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/spring/JettyHTTPServerEngineFactoryBeanDefinitionParser.java
    cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/spring/JettySpringTypesFactory.java
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/configuration/jsse/spring/TLSClientParametersConfig.java
    cxf/trunk/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/ContextUtils.java
    cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/JaxwsServiceBuilderNoAnnoTest.java
    cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/JaxwsServiceBuilderRPCTest.java
    cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/JaxwsServiceBuilderTest.java

Modified: cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertion.java
URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertion.java?rev=1082032&r1=1082031&r2=1082032&view=diff
==============================================================================
--- cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertion.java (original)
+++ cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertion.java Wed Mar 16 02:36:39 2011
@@ -19,6 +19,8 @@
 
 package org.apache.cxf.ws.policy.builder.jaxb;
 
+import java.util.Set;
+
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
@@ -26,7 +28,8 @@ import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 
-import org.apache.cxf.common.util.PackageUtils;
+import org.apache.cxf.jaxb.JAXBContextCache;
+import org.apache.cxf.jaxb.JAXBContextCache.CachedContextAndSchemas;
 import org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion;
 import org.apache.neethi.Assertion;
 import org.apache.neethi.PolicyComponent;
@@ -36,6 +39,8 @@ import org.apache.neethi.PolicyComponent
  * 
  */
 public class JaxbAssertion<T> extends PrimitiveAssertion {
+    private JAXBContext context;
+    private Set<Class<?>> classes;
     
     private T data;
 
@@ -84,12 +89,21 @@ public class JaxbAssertion<T> extends Pr
         return (JaxbAssertion<T>)a;
     }
 
+    private synchronized JAXBContext getContext() throws JAXBException {
+        if (context == null || classes == null) {
+            CachedContextAndSchemas ccs 
+                = JAXBContextCache.getCachedContextAndSchemas(data.getClass());
+            classes = ccs.getClasses();
+            context = ccs.getContext();
+        }
+        return context;
+    }
+    
     @Override
     public void serialize(XMLStreamWriter writer) throws XMLStreamException {
         try {
-            JAXBContext context = JAXBContext.newInstance(PackageUtils.getPackageName(data.getClass()),
-                                                          data.getClass().getClassLoader());
-            Marshaller marshaller = context.createMarshaller();
+            JAXBContext ctx = getContext();
+            Marshaller marshaller = ctx.createMarshaller();
             marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
             marshaller.marshal(data, writer);
         } catch (JAXBException ex) {

Modified: cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionBuilder.java
URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionBuilder.java?rev=1082032&r1=1082031&r2=1082032&view=diff
==============================================================================
--- cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionBuilder.java (original)
+++ cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionBuilder.java Wed Mar 16 02:36:39 2011
@@ -21,6 +21,7 @@ package org.apache.cxf.ws.policy.builder
 
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -34,7 +35,8 @@ import org.w3c.dom.Element;
 
 import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.common.logging.LogUtils;
-import org.apache.cxf.common.util.PackageUtils;
+import org.apache.cxf.jaxb.JAXBContextCache;
+import org.apache.cxf.jaxb.JAXBContextCache.CachedContextAndSchemas;
 import org.apache.cxf.jaxb.JAXBUtils;
 import org.apache.neethi.Assertion;
 import org.apache.neethi.AssertionBuilderFactory;
@@ -45,10 +47,11 @@ import org.apache.neethi.builders.xml.XM
 public class JaxbAssertionBuilder<T> implements AssertionBuilder<Element> {
 
     private static final Logger LOG = LogUtils.getL7dLogger(JaxbAssertionBuilder.class);
-    private Unmarshaller unmarshaller;
     private Collection<QName> supportedTypes;
     private Class<T> type;
-    
+    private JAXBContext context;
+    private Set<Class<?>> classes;
+
     /**
      * Constructs a JAXBAssertionBuilder from the QName of the schema type
      * @param qn the schema type
@@ -84,28 +87,25 @@ public class JaxbAssertionBuilder<T> imp
         supportedTypes = Collections.singletonList(qn);
     }
        
-    protected Unmarshaller getUnmarshaller() {
-        if (unmarshaller == null) {
-            try {
-                createUnmarhsaller();
-            } catch (JAXBException e) {
-                throw new RuntimeException(e);
-            }
+    private synchronized JAXBContext getContext() throws JAXBException {
+        if (context == null || classes == null) {
+            CachedContextAndSchemas ccs 
+                = JAXBContextCache.getCachedContextAndSchemas(type);
+            classes = ccs.getClasses();
+            context = ccs.getContext();
         }
-        
-        return unmarshaller;
+        return context;
     }
     
-    protected synchronized void createUnmarhsaller()  throws JAXBException {
-        if (unmarshaller != null) {
-            return;
+    protected Unmarshaller getUnmarshaller() {
+        try {
+            return getContext().createUnmarshaller();
+        } catch (JAXBException e) {
+            throw new RuntimeException(e);
         }
-        
-        JAXBContext context = JAXBContext.newInstance(PackageUtils.getPackageName(type), 
-                                                      type.getClassLoader());
-        unmarshaller = context.createUnmarshaller();
     }
     
+    
     public Assertion build(Element element, AssertionBuilderFactory factory) {
         QName name = new QName(element.getNamespaceURI(), element.getLocalName());
         JaxbAssertion<T> assertion = buildAssertion();

Modified: cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/EndpointReferenceUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/EndpointReferenceUtils.java?rev=1082032&r1=1082031&r2=1082032&view=diff
==============================================================================
--- cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/EndpointReferenceUtils.java (original)
+++ cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/EndpointReferenceUtils.java Wed Mar 16 02:36:39 2011
@@ -75,6 +75,7 @@ import org.apache.cxf.endpoint.ServerReg
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.helpers.LoadingByteArrayOutputStream;
 import org.apache.cxf.helpers.XMLUtils;
+import org.apache.cxf.jaxb.JAXBContextCache;
 import org.apache.cxf.resource.ExtendedURIResolver;
 import org.apache.cxf.resource.ResourceManager;
 import org.apache.cxf.service.model.SchemaInfo;
@@ -258,6 +259,13 @@ public final class EndpointReferenceUtil
     private static final org.apache.cxf.ws.addressing.wsdl.ObjectFactory WSA_WSDL_OBJECT_FACTORY = 
         new org.apache.cxf.ws.addressing.wsdl.ObjectFactory();
     
+    
+    private static final Set<Class<?>> ADDRESSING_CLASSES = new HashSet<Class<?>>();
+    static {
+        ADDRESSING_CLASSES.add(WSA_WSDL_OBJECT_FACTORY.getClass());
+        ADDRESSING_CLASSES.add(WSAEndpointReferenceUtils.WSA_OBJECT_FACTORY.getClass());
+    }
+    
     private EndpointReferenceUtils() {
         // Utility class - never constructed
     }
@@ -1007,10 +1015,9 @@ public final class EndpointReferenceUtil
     
     public static Source convertToXML(EndpointReferenceType epr) {
         try {
-            JAXBContext jaxbContext = 
-                JAXBContext.newInstance(new Class[] {
-                                            WSA_WSDL_OBJECT_FACTORY.getClass(), 
-                                            WSAEndpointReferenceUtils.WSA_OBJECT_FACTORY.getClass()});
+            JAXBContext jaxbContext = JAXBContextCache.getCachedContextAndSchemas(ADDRESSING_CLASSES,
+                                                                                  null, null, null,
+                                                                                  false).getContext();
             javax.xml.bind.Marshaller jm = jaxbContext.createMarshaller();
             jm.setProperty(Marshaller.JAXB_FRAGMENT, true);
             QName qname = new QName("http://www.w3.org/2005/08/addressing", "EndpointReference");

Modified: cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/JAXBExtensionHelper.java
URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/JAXBExtensionHelper.java?rev=1082032&r1=1082031&r2=1082032&view=diff
==============================================================================
--- cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/JAXBExtensionHelper.java (original)
+++ cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/JAXBExtensionHelper.java Wed Mar 16 02:36:39 2011
@@ -23,6 +23,7 @@ import java.io.PrintWriter;
 import java.lang.reflect.Method;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -52,6 +53,8 @@ import org.apache.cxf.common.classloader
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.PackageUtils;
 import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.jaxb.JAXBContextCache;
+import org.apache.cxf.jaxb.JAXBContextCache.CachedContextAndSchemas;
 import org.apache.cxf.staxutils.StaxUtils;
 
 
@@ -63,10 +66,13 @@ import org.apache.cxf.staxutils.StaxUtil
 public class JAXBExtensionHelper implements ExtensionSerializer, ExtensionDeserializer {
     private static final Logger LOG = LogUtils.getL7dLogger(JAXBExtensionHelper.class);
 
-    JAXBContext context;
     final Class<? extends ExtensibilityElement> typeClass;
     final String namespace;
     String jaxbNamespace;
+
+    private JAXBContext context;
+    private Set<Class<?>> classes;
+
       
     public JAXBExtensionHelper(Class<? extends ExtensibilityElement> cls,
                                String ns) {
@@ -176,10 +182,13 @@ public class JAXBExtensionHelper impleme
         }
     }
 
-    protected JAXBContext getJAXBContext() {
-        if (context == null) {
+    private synchronized JAXBContext getContext() throws JAXBException {
+        if (context == null || classes == null) {
             try {
-                createJAXBContext();
+                CachedContextAndSchemas ccs 
+                    = JAXBContextCache.getCachedContextAndSchemas(typeClass);
+                classes = ccs.getClasses();
+                context = ccs.getContext();
             } catch (JAXBException e) {
                 throw new RuntimeException(e);
             }
@@ -187,15 +196,6 @@ public class JAXBExtensionHelper impleme
         return context;
     }
     
-    protected synchronized void createJAXBContext() throws JAXBException {
-        if (context != null) {
-            return;
-        }
-        
-        context = JAXBContext.newInstance(PackageUtils.getPackageName(typeClass), 
-                                          typeClass.getClassLoader());
-    }
-    
     /* (non-Javadoc)
      * @see javax.wsdl.extensions.ExtensionSerializer#marshall(java.lang.Class,
      *  javax.xml.namespace.QName, javax.wsdl.extensions.ExtensibilityElement,
@@ -205,7 +205,7 @@ public class JAXBExtensionHelper impleme
                          final Definition wsdl, ExtensionRegistry registry) throws WSDLException {
         // TODO Auto-generated method stub
         try {
-            Marshaller u = getJAXBContext().createMarshaller();
+            Marshaller u = getContext().createMarshaller();
             u.setProperty("jaxb.encoding", "UTF-8");
             u.setProperty("jaxb.fragment", Boolean.TRUE);
             u.setProperty("jaxb.formatted.output", Boolean.TRUE);
@@ -274,7 +274,7 @@ public class JAXBExtensionHelper impleme
     public ExtensibilityElement unmarshall(Class parent, QName qname, Element element, Definition wsdl,
                                            ExtensionRegistry registry) throws WSDLException {
         try {
-            Unmarshaller u = getJAXBContext().createUnmarshaller();
+            Unmarshaller u = getContext().createUnmarshaller();
         
             Object o = null;
             if (namespace == null) {

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/configuration/spring/AbstractBeanDefinitionParser.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/configuration/spring/AbstractBeanDefinitionParser.java?rev=1082032&r1=1082031&r2=1082032&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/configuration/spring/AbstractBeanDefinitionParser.java (original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/configuration/spring/AbstractBeanDefinitionParser.java Wed Mar 16 02:36:39 2011
@@ -20,7 +20,8 @@ package org.apache.cxf.configuration.spr
 
 import java.io.StringReader;
 import java.io.StringWriter;
-import java.util.Map;
+import java.util.HashSet;
+import java.util.Set;
 import java.util.StringTokenizer;
 import java.util.logging.Logger;
 
@@ -39,9 +40,9 @@ import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 
 import org.apache.cxf.common.logging.LogUtils;
-import org.apache.cxf.common.util.CacheMap;
-import org.apache.cxf.common.util.PackageUtils;
 import org.apache.cxf.helpers.DOMUtils;
+import org.apache.cxf.jaxb.JAXBContextCache;
+import org.apache.cxf.jaxb.JAXBContextCache.CachedContextAndSchemas;
 import org.apache.cxf.staxutils.StaxUtils;
 import org.springframework.beans.factory.BeanDefinitionStoreException;
 import org.springframework.beans.factory.config.BeanDefinition;
@@ -55,13 +56,12 @@ import org.springframework.util.StringUt
 public abstract class AbstractBeanDefinitionParser 
     extends org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser {
     public static final String WIRE_BUS_ATTRIBUTE = AbstractBeanDefinitionParser.class.getName() + ".wireBus";
-
-    private static Map<String, JAXBContext> packageContextCache = new CacheMap<String, JAXBContext>(); 
-    
     private static final Logger LOG = LogUtils.getL7dLogger(AbstractBeanDefinitionParser.class);
     
     private Class beanClass;
-    
+    private JAXBContext context;
+    private Set<Class<?>> classes;
+
     @Override
     protected void doParse(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
         boolean setBus = parseAttributes(element, ctx, bean);        
@@ -245,23 +245,48 @@ public abstract class AbstractBeanDefini
         }
         mapElementToJaxbProperty(data, bean, propertyName, c);
     }
-    
+
+    private synchronized JAXBContext getContext(Class<?> cls) {
+        if (context == null || classes == null || !classes.contains(cls)) {
+            try {
+                Set<Class<?>> tmp = new HashSet<Class<?>>();
+                if (classes != null) {
+                    tmp.addAll(classes);
+                }
+                JAXBContextCache.addPackage(tmp, getJaxbPackage(), 
+                                            cls == null 
+                                            ? getClass().getClassLoader() 
+                                                : cls.getClassLoader());
+                if (cls != null) {
+                    boolean hasOf = false;
+                    for (Class<?> c : tmp) {
+                        if (c.getPackage() == cls.getPackage()
+                            && "ObjectFactory".equals(c.getSimpleName())) {
+                            hasOf = true;
+                        }
+                    }
+                    if (!hasOf) {
+                        tmp.add(cls);
+                    }
+                }
+                JAXBContextCache.scanPackages(tmp);
+                CachedContextAndSchemas ccs 
+                    = JAXBContextCache.getCachedContextAndSchemas(tmp, null, null, null, false);
+                classes = ccs.getClasses();
+                context = ccs.getContext();
+            } catch (JAXBException e) {
+                throw new RuntimeException(e);
+            }
+        }
+        return context;
+    }
+
     @SuppressWarnings("deprecation")
     protected void mapElementToJaxbProperty(Element data, 
                                             BeanDefinitionBuilder bean, 
                                             String propertyName, 
                                             Class<?> c) {
-        JAXBContext context = null;
         try {
-            String pkg = getJaxbPackage();
-            if (null != c) {
-                pkg = PackageUtils.getPackageName(c);
-            }
-            context = packageContextCache.get(pkg);
-            if (context == null) {
-                context = JAXBContext.newInstance(pkg, getClass().getClassLoader());
-                packageContextCache.put(pkg, context);
-            }
             try {
                 StringWriter writer = new StringWriter();
                 XMLStreamWriter xmlWriter = StaxUtils.createXMLStreamWriter(writer);
@@ -271,12 +296,12 @@ public abstract class AbstractBeanDefini
                 BeanDefinitionBuilder jaxbbean 
                     = BeanDefinitionBuilder.rootBeanDefinition(JAXBBeanFactory.class);
                 jaxbbean.getRawBeanDefinition().setFactoryMethodName("createJAXBBean");
-                jaxbbean.addConstructorArg(context);
+                jaxbbean.addConstructorArg(getContext(c));
                 jaxbbean.addConstructorArg(writer.toString());
                 jaxbbean.addConstructorArg(c);
                 bean.addPropertyValue(propertyName, jaxbbean.getBeanDefinition());
             } catch (Exception ex) {
-                Unmarshaller u = context.createUnmarshaller();
+                Unmarshaller u = getContext(c).createUnmarshaller();
                 Object obj;
                 if (c != null) {
                     obj = u.unmarshal(data, c);
@@ -300,17 +325,26 @@ public abstract class AbstractBeanDefini
     public void mapElementToJaxbPropertyFactory(Element data, 
                                                 BeanDefinitionBuilder bean, 
                                                 String propertyName, 
+                                                Class<?> type,
                                                 Class<?> factory,
                                                 String method,
                                                 Object ... args) {
-        bean.addPropertyValue(propertyName, mapElementToJaxbBean(data, factory,
-                                                                 null, method, args));
+        bean.addPropertyValue(propertyName, mapElementToJaxbBean(data, 
+                                                                 factory,
+                                                                 null, type, method, args));
     }
-    
+    public AbstractBeanDefinition mapElementToJaxbBean(Element data, 
+                                                       Class<?> cls,
+                                                      Class<?> factory,
+                                                      String method,
+                                                      Object ... args) {
+        return mapElementToJaxbBean(data, cls, factory, cls, method, args);
+    }    
     @SuppressWarnings("deprecation")
     public AbstractBeanDefinition mapElementToJaxbBean(Element data, 
                                                        Class<?> cls,
                                                       Class<?> factory,
+                                                      Class<?> jaxbClass,
                                                       String method,
                                                       Object ... args) {
         StringWriter writer = new StringWriter();
@@ -329,6 +363,7 @@ public abstract class AbstractBeanDefini
         }
         jaxbbean.getRawBeanDefinition().setFactoryMethodName(method);
         jaxbbean.addConstructorArg(writer.toString());
+        jaxbbean.addConstructorArg(getContext(jaxbClass));
         if (args != null) {
             for (Object o : args) {
                 jaxbbean.addConstructorArg(o);
@@ -337,18 +372,11 @@ public abstract class AbstractBeanDefini
         return jaxbbean.getBeanDefinition();
     }
     
-    protected static <T> T unmarshalFactoryString(String s, Class<T> cls) {
+    protected static <T> T unmarshalFactoryString(String s, JAXBContext ctx, Class<T> cls) {
         StringReader reader = new StringReader(s);
         XMLStreamReader data = StaxUtils.createXMLStreamReader(reader);
         try {
-            String pkg = cls.getPackage().getName();
-            JAXBContext context = packageContextCache.get(pkg);
-            if (context == null) {
-                context = JAXBContext.newInstance(pkg, cls.getClassLoader());
-                packageContextCache.put(pkg, context);
-            }
-            
-            Unmarshaller u = context.createUnmarshaller();
+            Unmarshaller u = ctx.createUnmarshaller();
             JAXBElement<?> obj = u.unmarshal(data, cls);
             return cls.cast(obj.getValue());
         } catch (RuntimeException e) {

Added: cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBContextCache.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBContextCache.java?rev=1082032&view=auto
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBContextCache.java (added)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBContextCache.java Wed Mar 16 02:36:39 2011
@@ -0,0 +1,334 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.jaxb;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.lang.ref.WeakReference;
+import java.lang.reflect.Method;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.annotation.XmlElementDecl;
+import javax.xml.transform.dom.DOMSource;
+
+import org.apache.cxf.common.classloader.ClassLoaderUtils;
+import org.apache.cxf.common.util.CacheMap;
+import org.apache.cxf.common.util.CachedClass;
+import org.apache.cxf.common.util.StringUtils;
+
+/**
+ * 
+ */
+public final class JAXBContextCache {
+    public static final class CachedContextAndSchemas {
+        private WeakReference<JAXBContext> context;
+        private WeakReference<Set<Class<?>>> classes;
+        private Collection<DOMSource> schemas;
+
+        CachedContextAndSchemas(JAXBContext context, Set<Class<?>> classes) {
+            this.context = new WeakReference<JAXBContext>(context);
+            this.classes = new WeakReference<Set<Class<?>>>(classes);
+        }
+
+        public JAXBContext getContext() {
+            return context.get();
+        }
+        public Set<Class<?>> getClasses() {
+            return classes.get();
+        }
+        public void setClasses(Set<Class<?>> cls) {
+            classes = new WeakReference<Set<Class<?>>>(cls);
+        }
+        
+        public Collection<DOMSource> getSchemas() {
+            return schemas;
+        }
+
+        public void setSchemas(Collection<DOMSource> schemas) {
+            this.schemas = schemas;
+        }
+    } 
+    
+    private static final Map<Set<Class<?>>, CachedContextAndSchemas> JAXBCONTEXT_CACHE
+        = new CacheMap<Set<Class<?>>, CachedContextAndSchemas>();
+
+    private static final Map<Package, CachedClass> OBJECT_FACTORY_CACHE
+        = new CacheMap<Package, CachedClass>(); 
+    
+    private JAXBContextCache() {
+        //utility class
+    }
+    
+    /**
+     * Clear any caches to make sure new contexts are created
+     */
+    public static void clearCaches() {
+        synchronized (JAXBCONTEXT_CACHE) {
+            JAXBCONTEXT_CACHE.clear();
+        }
+        synchronized (OBJECT_FACTORY_CACHE) {
+            OBJECT_FACTORY_CACHE.clear();
+        }
+    }
+
+    public static void scanPackages(Set<Class<?>> classes) {
+        JAXBUtils.scanPackages(classes, OBJECT_FACTORY_CACHE);
+    }
+    
+    public static CachedContextAndSchemas getCachedContextAndSchemas(Class<?> cls) throws JAXBException {
+        Set<Class<?>> classes = new HashSet<Class<?>>();
+        classes.add(cls);
+        scanPackages(classes);
+        return JAXBContextCache.getCachedContextAndSchemas(classes, null, null, null, false);
+    }
+    
+    public static CachedContextAndSchemas getCachedContextAndSchemas(String pkg,
+                                                                     Map<String, Object> props,
+                                                                     ClassLoader loader) 
+        throws JAXBException {
+        Set<Class<?>> classes = new HashSet<Class<?>>();
+        addPackage(classes, pkg, loader);
+        return getCachedContextAndSchemas(classes, null, props, null, true);
+    }
+    
+    public static CachedContextAndSchemas getCachedContextAndSchemas(Set<Class<?>> classes,
+                                                                     String defaultNs,
+                                                                     Map<String, Object> props,
+                                                                     Collection<Object> typeRefs,
+                                                                     boolean exact)
+        throws JAXBException {
+        for (Class<?> clz : classes) {
+            if (clz.getName().endsWith("ObjectFactory")
+                && checkObjectFactoryNamespaces(clz)) {
+                // kind of a hack, but ObjectFactories may be created with empty
+                // namespaces
+                defaultNs = null;
+            }
+        }
+
+        Map<String, Object> map = new HashMap<String, Object>();
+        if (defaultNs != null) {
+            map.put("com.sun.xml.bind.defaultNamespaceRemap", defaultNs);
+        }
+        if (props != null) {
+            map.putAll(props);
+        }
+        CachedContextAndSchemas cachedContextAndSchemas = null;
+        JAXBContext context = null;
+        if (typeRefs == null || typeRefs.isEmpty()) {
+            synchronized (JAXBCONTEXT_CACHE) {
+                if (exact) {
+                    cachedContextAndSchemas = JAXBCONTEXT_CACHE.get(classes);
+                } else {
+                    for (Map.Entry<Set<Class<?>>, CachedContextAndSchemas> k : JAXBCONTEXT_CACHE.entrySet()) {
+                        Set<Class<?>> key = k.getKey();
+                        if (key != null && key.containsAll(classes)) {
+                            cachedContextAndSchemas = k.getValue();
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+        if (cachedContextAndSchemas != null) {
+            context = cachedContextAndSchemas.getContext();
+            if (context ==  null) {
+                JAXBCONTEXT_CACHE.remove(cachedContextAndSchemas.getClasses());
+                cachedContextAndSchemas = null;
+            }
+        }
+        if (context == null) {
+            try {
+                context = createContext(classes, map, typeRefs);
+            } catch (JAXBException ex) {
+                // load jaxb needed class and try to create jaxb context 
+                boolean added = addJaxbObjectFactory(ex, classes);
+                while (cachedContextAndSchemas == null && added) {
+                    try {
+                        context = JAXBContext.newInstance(classes
+                                                      .toArray(new Class[classes.size()]), null);
+                    } catch (JAXBException e) {
+                        //second attempt failed as well, rethrow the original exception
+                        throw ex;
+                    }
+                }
+
+            }
+            cachedContextAndSchemas = new CachedContextAndSchemas(context, classes);
+            synchronized (JAXBCONTEXT_CACHE) {
+                if (typeRefs == null || typeRefs.isEmpty()) {
+                    JAXBCONTEXT_CACHE.put(classes, cachedContextAndSchemas);
+                }
+            }
+        }
+
+        return cachedContextAndSchemas;
+    }
+    
+    private static boolean checkObjectFactoryNamespaces(Class<?> clz) {
+        for (Method meth : clz.getMethods()) {
+            XmlElementDecl decl = meth.getAnnotation(XmlElementDecl.class);
+            if (decl != null
+                && XmlElementDecl.GLOBAL.class.equals(decl.scope())
+                && StringUtils.isEmpty(decl.namespace())) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+    
+    
+    private static JAXBContext createContext(Set<Class<?>> classes,
+                                      Map<String, Object> map,
+                                      Collection<Object> typeRefs)
+        throws JAXBException {
+        JAXBContext ctx;
+        if (typeRefs != null && !typeRefs.isEmpty()) {
+            Class<?> fact = null;
+            String pfx = "com.sun.xml.bind.";
+            try {
+                fact = ClassLoaderUtils.loadClass("com.sun.xml.bind.v2.ContextFactory",
+                                                  JAXBContextCache.class);
+            } catch (Throwable t) {
+                try {
+                    fact = ClassLoaderUtils.loadClass("com.sun.xml.internal.bind.v2.ContextFactory",
+                                                      JAXBContextCache.class);
+                    pfx = "com.sun.xml.internal.bind.";
+                } catch (Throwable t2) {
+                    //ignore
+                }
+            }
+            if (fact != null) {
+                for (Method m : fact.getMethods()) {
+                    if ("createContext".equals(m.getName())
+                        && m.getParameterTypes().length == 9) {
+                        try {
+                            return (JAXBContext)m.invoke(null,
+                                     classes.toArray(new Class[classes.size()]),
+                                     typeRefs,
+                                     map.get(pfx + "subclassReplacements"),
+                                     map.get(pfx + "defaultNamespaceRemap"),
+                                     map.get(pfx + "c14n") == null
+                                         ? Boolean.FALSE
+                                             : map.get(pfx + "c14n"),
+                                     map.get(pfx + "v2.model.annotation.RuntimeAnnotationReader"),
+                                     map.get(pfx + "XmlAccessorFactory") == null
+                                         ? Boolean.FALSE
+                                             : map.get(pfx + "XmlAccessorFactory"),
+                                     map.get(pfx + "treatEverythingNillable") == null
+                                         ? Boolean.FALSE : map.get(pfx + "treatEverythingNillable"),
+                                     map.get("retainReferenceToInfo") == null
+                                         ? Boolean.FALSE : map.get("retainReferenceToInfo"));
+                        } catch (Throwable e) {
+                            //ignore
+                        }
+                    }
+                }
+            }
+        }
+        try {
+            ctx = JAXBContext.newInstance(classes.toArray(new Class[classes.size()]), map);
+        } catch (JAXBException ex) {
+            if (map.containsKey("com.sun.xml.bind.defaultNamespaceRemap")
+                && ex.getMessage().contains("com.sun.xml.bind.defaultNamespaceRemap")) {
+                map.put("com.sun.xml.internal.bind.defaultNamespaceRemap",
+                        map.remove("com.sun.xml.bind.defaultNamespaceRemap"));
+                ctx = JAXBContext.newInstance(classes.toArray(new Class[classes.size()]), map);
+            } else {
+                throw ex;
+            }
+        }
+        return ctx;
+    }
+    // Now we can not add all the classes that Jaxb needed into JaxbContext,
+    // especially when
+    // an ObjectFactory is pointed to by an jaxb @XmlElementDecl annotation
+    // added this workaround method to load the jaxb needed ObjectFactory class
+    private static boolean addJaxbObjectFactory(JAXBException e1, Set<Class<?>> classes) {
+        boolean added = false;
+        java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
+        java.io.PrintStream pout = new java.io.PrintStream(bout);
+        e1.printStackTrace(pout);
+        String str = new String(bout.toByteArray());
+        Pattern pattern = Pattern.compile("(?<=There's\\sno\\sObjectFactory\\swith\\san\\s"
+                                          + "@XmlElementDecl\\sfor\\sthe\\selement\\s\\{)\\S*(?=\\})");
+        java.util.regex.Matcher matcher = pattern.matcher(str);
+        while (matcher.find()) {
+            String pkgName = JAXBUtils.namespaceURIToPackage(matcher.group());
+            try {
+                Class clz = JAXBContextCache.class.getClassLoader()
+                    .loadClass(pkgName + "." + "ObjectFactory");
+
+                if (!classes.contains(clz)) {
+                    classes.add(clz);
+                    added = true;
+                }
+            } catch (ClassNotFoundException e) {
+                // do nothing
+            }
+
+        }
+        return added;
+    }
+
+    public static void addPackage(Set<Class<?>>  classes, String pkg, ClassLoader loader) {
+        try {
+            classes.add(Class.forName(pkg + ".ObjectFactory", false, loader));
+        } catch (Exception ex) {
+            //ignore
+        }
+        try {
+            InputStream ins = loader.getResourceAsStream("/" + pkg.replace('.', '/') + "/jaxb.index");
+            BufferedReader reader = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
+            if (!StringUtils.isEmpty(pkg)) {
+                pkg += ".";
+            }
+    
+            String line = reader.readLine();
+            while (line != null) {
+                line = line.trim();
+                if (line.indexOf("#") != -1) {
+                    line = line.substring(0, line.indexOf("#"));
+                }
+                if (!StringUtils.isEmpty(line)) {
+                    try {
+                        Class<?> ncls = Class.forName(pkg + line, false, loader);
+                        classes.add(ncls);
+                    } catch (Exception e) {
+                        // ignore
+                    }
+                }
+                line = reader.readLine();
+            }
+        } catch (Exception ex) {
+            //ignore
+        }
+    }
+
+}

Propchange: cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBContextCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBContextCache.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBUtils.java?rev=1082032&r1=1082031&r2=1082032&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBUtils.java (original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBUtils.java Wed Mar 16 02:36:39 2011
@@ -790,6 +790,10 @@ public final class JAXBUtils {
     }
     
     public static void scanPackages(Set<Class<?>> classes,
+                                    Map<Package, CachedClass> objectFactoryCache) {
+        scanPackages(classes, null, objectFactoryCache);
+    }
+    public static void scanPackages(Set<Class<?>> classes,
                                     Class[] extraClass,
                                     Map<Package, CachedClass> objectFactoryCache) {
         

Modified: cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java?rev=1082032&r1=1082031&r2=1082032&view=diff
==============================================================================
--- cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java (original)
+++ cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java Wed Mar 16 02:36:39 2011
@@ -22,7 +22,6 @@ package org.apache.cxf.jaxb;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.lang.ref.WeakReference;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
@@ -38,7 +37,6 @@ import java.util.Map;
 import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
-import java.util.regex.Pattern;
 
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBElement;
@@ -47,7 +45,6 @@ import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.ValidationEventHandler;
 import javax.xml.bind.annotation.XmlElement;
-import javax.xml.bind.annotation.XmlElementDecl;
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLEventReader;
 import javax.xml.stream.XMLEventWriter;
@@ -59,14 +56,10 @@ import javax.xml.transform.dom.DOMSource
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 
-import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.common.injection.NoJSR250Annotations;
 import org.apache.cxf.common.logging.LogUtils;
-import org.apache.cxf.common.util.CacheMap;
-import org.apache.cxf.common.util.CachedClass;
 import org.apache.cxf.common.util.ModCountCopyOnWriteArrayList;
 import org.apache.cxf.common.util.PackageUtils;
-import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.common.xmlschema.SchemaCollection;
 import org.apache.cxf.databinding.AbstractDataBinding;
 import org.apache.cxf.databinding.AbstractWrapperHelper;
@@ -77,6 +70,7 @@ import org.apache.cxf.databinding.Wrappe
 import org.apache.cxf.helpers.XMLUtils;
 import org.apache.cxf.interceptor.Interceptor;
 import org.apache.cxf.interceptor.InterceptorProvider;
+import org.apache.cxf.jaxb.JAXBContextCache.CachedContextAndSchemas;
 import org.apache.cxf.jaxb.attachment.JAXBAttachmentSchemaValidationHack;
 import org.apache.cxf.jaxb.io.DataReaderImpl;
 import org.apache.cxf.jaxb.io.DataWriterImpl;
@@ -107,34 +101,6 @@ public class JAXBDataBinding extends Abs
                                                                                XMLEventWriter.class,
                                                                                XMLStreamWriter.class};
 
-    private static final class CachedContextAndSchemas {
-        private WeakReference<JAXBContext> context;
-        private Collection<DOMSource> schemas;
-
-        CachedContextAndSchemas(JAXBContext context) {
-            this.context = new WeakReference<JAXBContext>(context);
-        }
-
-        public JAXBContext getContext() {
-            return context.get();
-        }
-
-        public Collection<DOMSource> getSchemas() {
-            return schemas;
-        }
-
-        public void setSchemas(Collection<DOMSource> schemas) {
-            this.schemas = schemas;
-        }
-
-    }
-
-    private static final Map<Set<Class<?>>, CachedContextAndSchemas> JAXBCONTEXT_CACHE
-        = new CacheMap<Set<Class<?>>, CachedContextAndSchemas>();
-
-    private static final Map<Package, CachedClass> OBJECT_FACTORY_CACHE
-        = new CacheMap<Package, CachedClass>();
-
     private static final Map<String, DOMResult> BUILT_IN_SCHEMAS = new HashMap<String, DOMResult>();
     static {
         URIResolver resolver = new URIResolver();
@@ -323,27 +289,7 @@ public class JAXBDataBinding extends Abs
         try {
             cachedContextAndSchemas = createJAXBContextAndSchemas(contextClasses, tns);
         } catch (JAXBException e1) {
-            // load jaxb needed class and try to create jaxb context for more
-            // times
-            boolean added = addJaxbObjectFactory(e1);
-            while (cachedContextAndSchemas == null && added) {
-                try {
-                    ctx = JAXBContext.newInstance(contextClasses
-                                                  .toArray(new Class[contextClasses.size()]), null);
-                    cachedContextAndSchemas = new CachedContextAndSchemas(ctx);
-                } catch (JAXBException e) {
-                    e1 = e;
-                    added = addJaxbObjectFactory(e1);
-                }
-            }
-
-            if (ctx == null) {
-                throw new ServiceConstructionException(e1);
-            } else {
-                synchronized (JAXBCONTEXT_CACHE) {
-                    JAXBCONTEXT_CACHE.put(contextClasses, cachedContextAndSchemas);
-                }
-            }
+            throw new ServiceConstructionException(e1);
         }
         ctx = cachedContextAndSchemas.getContext();
         if (LOG.isLoggable(Level.FINE)) {
@@ -460,126 +406,21 @@ public class JAXBDataBinding extends Abs
     public CachedContextAndSchemas createJAXBContextAndSchemas(Set<Class<?>> classes,
                                                                String defaultNs)
         throws JAXBException {
-
-        JAXBUtils.scanPackages(classes, extraClass, OBJECT_FACTORY_CACHE);
-        //JAXBUtils.scanPackages(classes, new HashMap<Package, CachedClass>());
-        addWsAddressingTypes(classes);
-
-        for (Class<?> clz : classes) {
-            if (clz.getName().endsWith("ObjectFactory")
-                && checkObjectFactoryNamespaces(clz)) {
-                // kind of a hack, but ObjectFactories may be created with empty
-                // namespaces
-                defaultNs = null;
-            }
-        }
-
-        Map<String, Object> map = new HashMap<String, Object>();
-        if (defaultNs != null) {
-            map.put("com.sun.xml.bind.defaultNamespaceRemap", defaultNs);
-        }
-
-        if (contextProperties != null) {
-            // add any specified context properties into the properties map
-            map.putAll(contextProperties);
-        }
-
-        CachedContextAndSchemas cachedContextAndSchemas = null;
-        if (typeRefs.isEmpty()) {
-            synchronized (JAXBCONTEXT_CACHE) {
-                cachedContextAndSchemas = JAXBCONTEXT_CACHE.get(classes);
-            }
-        }
-        if (cachedContextAndSchemas != null) {
-            context = cachedContextAndSchemas.getContext();
-        }
-        if (context == null) {
-            context = createContext(classes, map);
-            cachedContextAndSchemas = new CachedContextAndSchemas(context);
-            synchronized (JAXBCONTEXT_CACHE) {
-                if (typeRefs.isEmpty()) {
-                    JAXBCONTEXT_CACHE.put(classes, cachedContextAndSchemas);
-                }
+        //add user extra class into jaxb context
+        if (extraClass != null && extraClass.length > 0) {
+            for (Class clz : extraClass) {
+                classes.add(clz);
             }
         }
+        JAXBContextCache.scanPackages(classes);
+        addWsAddressingTypes(classes);
 
-        return cachedContextAndSchemas;
-    }
+        return JAXBContextCache.getCachedContextAndSchemas(classes, defaultNs,  
+                                                          contextProperties, 
+                                                          typeRefs, true);
 
-    private JAXBContext createContext(Set<Class<?>> classes,
-                                      Map<String, Object> map)
-        throws JAXBException {
-        JAXBContext ctx;
-        if (!typeRefs.isEmpty()) {
-            Class<?> fact = null;
-            String pfx = "com.sun.xml.bind.";
-            try {
-                fact = ClassLoaderUtils.loadClass("com.sun.xml.bind.v2.ContextFactory",
-                                                  getClass());
-            } catch (Throwable t) {
-                try {
-                    fact = ClassLoaderUtils.loadClass("com.sun.xml.internal.bind.v2.ContextFactory",
-                                                      getClass());
-                    pfx = "com.sun.xml.internal.bind.";
-                } catch (Throwable t2) {
-                    //ignore
-                }
-            }
-            if (fact != null) {
-                for (Method m : fact.getMethods()) {
-                    if ("createContext".equals(m.getName())
-                        && m.getParameterTypes().length == 9) {
-                        try {
-                            return (JAXBContext)m.invoke(null,
-                                     classes.toArray(new Class[classes.size()]),
-                                     typeRefs,
-                                     map.get(pfx + "subclassReplacements"),
-                                     map.get(pfx + "defaultNamespaceRemap"),
-                                     map.get(pfx + "c14n") == null
-                                         ? Boolean.FALSE
-                                             : map.get(pfx + "c14n"),
-                                     map.get(pfx + "v2.model.annotation.RuntimeAnnotationReader"),
-                                     map.get(pfx + "XmlAccessorFactory") == null
-                                         ? Boolean.FALSE
-                                             : map.get(pfx + "XmlAccessorFactory"),
-                                     map.get(pfx + "treatEverythingNillable") == null
-                                         ? Boolean.FALSE : map.get(pfx + "treatEverythingNillable"),
-                                     map.get("retainReferenceToInfo") == null
-                                         ? Boolean.FALSE : map.get("retainReferenceToInfo"));
-                        } catch (Throwable e) {
-                            //ignore
-                        }
-                    }
-                }
-            }
-        }
-        try {
-            ctx = JAXBContext.newInstance(classes.toArray(new Class[classes.size()]), map);
-        } catch (JAXBException ex) {
-            if (map.containsKey("com.sun.xml.bind.defaultNamespaceRemap")
-                && ex.getMessage().contains("com.sun.xml.bind.defaultNamespaceRemap")) {
-                map.put("com.sun.xml.internal.bind.defaultNamespaceRemap",
-                        map.remove("com.sun.xml.bind.defaultNamespaceRemap"));
-                ctx = JAXBContext.newInstance(classes.toArray(new Class[classes.size()]), map);
-            } else {
-                throw ex;
-            }
-        }
-        return ctx;
     }
 
-    private boolean checkObjectFactoryNamespaces(Class<?> clz) {
-        for (Method meth : clz.getMethods()) {
-            XmlElementDecl decl = meth.getAnnotation(XmlElementDecl.class);
-            if (decl != null
-                && XmlElementDecl.GLOBAL.class.equals(decl.scope())
-                && StringUtils.isEmpty(decl.namespace())) {
-                return true;
-            }
-        }
-
-        return false;
-    }
 
     private void addWsAddressingTypes(Set<Class<?>> classes) {
         if (classes.contains(ObjectFactory.class)) {
@@ -600,36 +441,6 @@ public class JAXBDataBinding extends Abs
         return Collections.unmodifiableSet(this.contextClasses);
     }
 
-    // Now we can not add all the classes that Jaxb needed into JaxbContext,
-    // especially when
-    // an ObjectFactory is pointed to by an jaxb @XmlElementDecl annotation
-    // added this workaround method to load the jaxb needed ObjectFactory class
-    public boolean addJaxbObjectFactory(JAXBException e1) {
-        boolean added = false;
-        java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
-        java.io.PrintStream pout = new java.io.PrintStream(bout);
-        e1.printStackTrace(pout);
-        String str = new String(bout.toByteArray());
-        Pattern pattern = Pattern.compile("(?<=There's\\sno\\sObjectFactory\\swith\\san\\s"
-                                          + "@XmlElementDecl\\sfor\\sthe\\selement\\s\\{)\\S*(?=\\})");
-        java.util.regex.Matcher matcher = pattern.matcher(str);
-        while (matcher.find()) {
-            String pkgName = JAXBUtils.namespaceURIToPackage(matcher.group());
-            try {
-                Class clz = getClass().getClassLoader().loadClass(pkgName + "." + "ObjectFactory");
-
-                if (!contextClasses.contains(clz)) {
-                    contextClasses.add(clz);
-                    added = true;
-                }
-            } catch (ClassNotFoundException e) {
-                // do nothing
-            }
-
-        }
-        return added;
-    }
-
     /**
      * Return a map of properties. These properties are passed to
      * JAXBContext.newInstance when this object creates a context.
@@ -746,15 +557,6 @@ public class JAXBDataBinding extends Abs
         this.unwrapJAXBElement = unwrapJAXBElement;
     }
 
-    public static void clearCaches() {
-        synchronized (JAXBCONTEXT_CACHE) {
-            JAXBCONTEXT_CACHE.clear();
-        }
-        synchronized (OBJECT_FACTORY_CACHE) {
-            OBJECT_FACTORY_CACHE.clear();
-        }
-    }
-
     public WrapperHelper createWrapperHelper(Class<?> wrapperType, QName wrapperName, List<String> partNames,
                                              List<String> elTypeNames, List<Class<?>> partClasses) {
         List<Method> getMethods = new ArrayList<Method>(partNames.size());

Modified: cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/spring/JettyHTTPServerEngineBeanDefinitionParser.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/spring/JettyHTTPServerEngineBeanDefinitionParser.java?rev=1082032&r1=1082031&r2=1082032&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/spring/JettyHTTPServerEngineBeanDefinitionParser.java (original)
+++ cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/spring/JettyHTTPServerEngineBeanDefinitionParser.java Wed Mar 16 02:36:39 2011
@@ -25,6 +25,7 @@ import java.security.GeneralSecurityExce
 import java.util.List;
 
 import javax.annotation.PostConstruct;
+import javax.xml.bind.JAXBContext;
 
 import org.w3c.dom.Element;
 
@@ -81,24 +82,28 @@ public class JettyHTTPServerEngineBeanDe
                     mapElementToJaxbPropertyFactory(elem,
                                                     bean,
                                                     "tlsServerParameters",
+                                                    TLSServerParametersType.class,
                                                     JettyHTTPServerEngineBeanDefinitionParser.class,
                                                     "createTLSServerParametersConfig");
                 } else if ("threadingParameters".equals(name)) {
                     mapElementToJaxbPropertyFactory(elem,
                                                     bean,
                                                     "threadingParameters",
+                                                    ThreadingParametersType.class,
                                                     JettyHTTPServerEngineBeanDefinitionParser.class,
                                                     "createThreadingParameters");
                 } else if ("tlsServerParametersRef".equals(name)) {
                     mapElementToJaxbPropertyFactory(elem,
                                                     bean,
                                                     "tlsServerParametersRef",
+                                                    TLSServerParametersIdentifiedType.class,
                                                     JettyHTTPServerEngineBeanDefinitionParser.class,
                                                     "createTLSServerParametersConfigRef");
                 } else if ("threadingParametersRef".equals(name)) {
                     mapElementToJaxbPropertyFactory(elem,
                                                     bean,
                                                     "threadingParametersRef",
+                                                    ThreadingParametersIdentifiedType.class,
                                                     JettyHTTPServerEngineBeanDefinitionParser.class,
                                                     "createThreadingParametersRef"
                                                     );
@@ -209,35 +214,35 @@ public class JettyHTTPServerEngineBeanDe
         
 
     
-    public static TLSServerParametersConfig createTLSServerParametersConfig(String s) 
+    public static TLSServerParametersConfig createTLSServerParametersConfig(String s, 
+                                                                            JAXBContext context) 
         throws GeneralSecurityException, IOException {
         
-        TLSServerParametersType parametersType = unmarshalFactoryString(s, 
+        TLSServerParametersType parametersType = unmarshalFactoryString(s, context,
                                                                         TLSServerParametersType.class);
         
         return new TLSServerParametersConfig(parametersType);
     }
-    public static String createTLSServerParametersConfigRef(String s)
+    public static String createTLSServerParametersConfigRef(String s, JAXBContext context)
     
         throws GeneralSecurityException, IOException {
         
         TLSServerParametersIdentifiedType parameterTypeRef 
-            = unmarshalFactoryString(s, TLSServerParametersIdentifiedType.class);
+            = unmarshalFactoryString(s, context, TLSServerParametersIdentifiedType.class);
         
         return parameterTypeRef.getId(); 
     } 
     
-    public static ThreadingParameters createThreadingParameters(String s) {
+    public static ThreadingParameters createThreadingParameters(String s, JAXBContext context) {
         
-        ThreadingParametersType parametersType = unmarshalFactoryString(s, 
+        ThreadingParametersType parametersType = unmarshalFactoryString(s, context,
                                                                         ThreadingParametersType.class);
         
         return toThreadingParameters(parametersType);
     }
-    public static String createThreadingParametersRef(String s) {
-
+    public static String createThreadingParametersRef(String s, JAXBContext context) {
         ThreadingParametersIdentifiedType parametersType 
-            = unmarshalFactoryString(s, ThreadingParametersIdentifiedType.class);
+            = unmarshalFactoryString(s, context, ThreadingParametersIdentifiedType.class);
         return parametersType.getId();
     }
 }

Modified: cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/spring/JettyHTTPServerEngineFactoryBeanDefinitionParser.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/spring/JettyHTTPServerEngineFactoryBeanDefinitionParser.java?rev=1082032&r1=1082031&r2=1082032&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/spring/JettyHTTPServerEngineFactoryBeanDefinitionParser.java (original)
+++ cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/spring/JettyHTTPServerEngineFactoryBeanDefinitionParser.java Wed Mar 16 02:36:39 2011
@@ -36,6 +36,8 @@ import org.apache.cxf.configuration.spri
 import org.apache.cxf.helpers.DOMUtils;
 import org.apache.cxf.transport.http_jetty.JettyHTTPServerEngineFactory;
 import org.apache.cxf.transport.http_jetty.ThreadingParameters;
+import org.apache.cxf.transports.http_jetty.configuration.TLSServerParametersIdentifiedType;
+import org.apache.cxf.transports.http_jetty.configuration.ThreadingParametersIdentifiedType;
 
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.BeanDefinitionStoreException;
@@ -85,11 +87,11 @@ public class JettyHTTPServerEngineFactor
             }
 
             bean.addConstructorArg(mapElementToJaxbBean(element,
-                                                        Map.class,
+                                                        TLSServerParametersIdentifiedType.class,
                                                         JettySpringTypesFactory.class,
-                                "createTLSServerParametersMap"));
+                                                        "createTLSServerParametersMap"));
             bean.addConstructorArg(mapElementToJaxbBean(element,
-                                                        Map.class,
+                                                        ThreadingParametersIdentifiedType.class,
                                                         JettySpringTypesFactory.class,
                                 "createThreadingParametersMap"));
             
@@ -159,6 +161,4 @@ public class JettyHTTPServerEngineFactor
             }
         }
     }
-    
-
 }

Modified: cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/spring/JettySpringTypesFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/spring/JettySpringTypesFactory.java?rev=1082032&r1=1082031&r2=1082032&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/spring/JettySpringTypesFactory.java (original)
+++ cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/spring/JettySpringTypesFactory.java Wed Mar 16 02:36:39 2011
@@ -80,7 +80,8 @@ public final class JettySpringTypesFacto
         }
         return map;
     }
-    public Map<String, ThreadingParameters> createThreadingParametersMap(String s) 
+    public Map<String, ThreadingParameters> createThreadingParametersMap(String s,
+                                                                         JAXBContext ctx) 
         throws Exception {
         Document doc = DOMUtils.readXml(new StringReader(s));
         List <ThreadingParametersIdentifiedType> threadingParametersIdentifiedTypes = 
@@ -88,13 +89,14 @@ public final class JettySpringTypesFacto
                 .parseListElement(doc.getDocumentElement(), 
                                   new QName(JettyHTTPServerEngineFactoryBeanDefinitionParser.HTTP_JETTY_NS,
                                             "identifiedThreadingParameters"), 
-                                  ThreadingParametersIdentifiedType.class);
+                                  ThreadingParametersIdentifiedType.class, ctx);
         Map<String, ThreadingParameters> threadingParametersMap =
             toThreadingParameters(threadingParametersIdentifiedTypes);
         return threadingParametersMap;
     }
     
-    public Map<String, TLSServerParameters> createTLSServerParametersMap(String s) 
+    public Map<String, TLSServerParameters> createTLSServerParametersMap(String s,
+                                                                         JAXBContext ctx) 
         throws Exception {
         Document doc = DOMUtils.readXml(new StringReader(s));
         
@@ -103,7 +105,8 @@ public final class JettySpringTypesFacto
                 .parseListElement(doc.getDocumentElement(), 
                                   new QName(JettyHTTPServerEngineFactoryBeanDefinitionParser.HTTP_JETTY_NS,
                                             "identifiedTLSServerParameters"),
-                                  TLSServerParametersIdentifiedType.class);
+                                  TLSServerParametersIdentifiedType.class,
+                                  ctx);
         Map<String, TLSServerParameters> tlsServerParametersMap =
             toTLSServerParamenters(tlsServerParameters);
         return tlsServerParametersMap;
@@ -112,18 +115,11 @@ public final class JettySpringTypesFacto
     @SuppressWarnings("unchecked")
     public static <V> List<V> parseListElement(Element parent, 
                                            QName name, 
-                                           Class<?> c) throws JAXBException {                                
+                                           Class<?> c,
+                                           JAXBContext context) throws JAXBException {
         List<V> list = new ArrayList<V>();
         Node data = null;
            
-        JAXBContext context = null;
-        String pkg = "";
-        if (null != c && c.getPackage() != null) {
-            pkg = c.getPackage().getName();
-            context = JAXBContext.newInstance(pkg, c.getClassLoader());
-        } else {
-            context = JAXBContext.newInstance(pkg);
-        }
         Unmarshaller u = context.createUnmarshaller();
         Node node = parent.getFirstChild();           
         while (node != null) {

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/configuration/jsse/spring/TLSClientParametersConfig.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/configuration/jsse/spring/TLSClientParametersConfig.java?rev=1082032&r1=1082031&r2=1082032&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/configuration/jsse/spring/TLSClientParametersConfig.java (original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/configuration/jsse/spring/TLSClientParametersConfig.java Wed Mar 16 02:36:39 2011
@@ -21,6 +21,8 @@ package org.apache.cxf.configuration.jss
 import java.io.IOException;
 import java.io.StringReader;
 import java.security.GeneralSecurityException;
+import java.util.HashSet;
+import java.util.Set;
 
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBElement;
@@ -33,6 +35,8 @@ import org.apache.cxf.common.injection.N
 import org.apache.cxf.common.util.PackageUtils;
 import org.apache.cxf.configuration.jsse.TLSClientParameters;
 import org.apache.cxf.configuration.security.TLSClientParametersType;
+import org.apache.cxf.jaxb.JAXBContextCache;
+import org.apache.cxf.jaxb.JAXBContextCache.CachedContextAndSchemas;
 import org.apache.cxf.staxutils.StaxUtils;
 
 /**
@@ -43,16 +47,23 @@ import org.apache.cxf.staxutils.StaxUtil
  */
 @NoJSR250Annotations
 public final class TLSClientParametersConfig {
-    static JAXBContext context;
+    private static Set<Class<?>> classes;
+    private static JAXBContext context;
     
     private TLSClientParametersConfig() {
         //not constructed
     }
     
     private static synchronized JAXBContext getContext() throws JAXBException {
-        if (context == null) {
-            context = JAXBContext.newInstance(PackageUtils.getPackageName(TLSClientParametersType.class), 
-                                              TLSClientParametersConfig.class.getClassLoader());            
+        if (context == null || classes == null) {
+            Set<Class<?>> c2 = new HashSet<Class<?>>();
+            JAXBContextCache.addPackage(c2, 
+                                        PackageUtils.getPackageName(TLSClientParametersType.class), 
+                                        TLSClientParametersConfig.class.getClassLoader());
+            CachedContextAndSchemas ccs 
+                = JAXBContextCache.getCachedContextAndSchemas(c2, null, null, null, false);
+            classes = ccs.getClasses();
+            context = ccs.getContext();
         }
         return context;
     }

Modified: cxf/trunk/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/ContextUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/ContextUtils.java?rev=1082032&r1=1082031&r2=1082032&view=diff
==============================================================================
--- cxf/trunk/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/ContextUtils.java (original)
+++ cxf/trunk/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/ContextUtils.java Wed Mar 16 02:36:39 2011
@@ -22,6 +22,8 @@ package org.apache.cxf.ws.addressing;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.HashSet;
+import java.util.Set;
 import java.util.UUID;
 import java.util.concurrent.Executor;
 import java.util.logging.Level;
@@ -45,6 +47,8 @@ import org.apache.cxf.interceptor.Fault;
 import org.apache.cxf.interceptor.InterceptorChain;
 import org.apache.cxf.interceptor.OutgoingChainInterceptor;
 import org.apache.cxf.io.DelegatingInputStream;
+import org.apache.cxf.jaxb.JAXBContextCache;
+import org.apache.cxf.jaxb.JAXBContextCache.CachedContextAndSchemas;
 import org.apache.cxf.message.Exchange;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageImpl;
@@ -93,6 +97,7 @@ public final class ContextUtils {
     private static final String URN_UUID = "urn:uuid:";
     
     private static JAXBContext jaxbContext;
+    private static Set<Class<?>> jaxbContextClasses;
      
     /**
      * Used by MAPAggregator to cache bad MAP fault name
@@ -722,11 +727,15 @@ public final class ContextUtils {
      */
     public static JAXBContext getJAXBContext() throws JAXBException {
         synchronized (ContextUtils.class) {
-            if (jaxbContext == null) {
-                jaxbContext =
-                    JAXBContext.newInstance(
-                        WSA_OBJECT_FACTORY.getClass().getPackage().getName(),
-                        WSA_OBJECT_FACTORY.getClass().getClassLoader());
+            if (jaxbContext == null || jaxbContextClasses == null) {
+                Set<Class<?>> tmp = new HashSet<Class<?>>();
+                JAXBContextCache.addPackage(tmp, WSA_OBJECT_FACTORY.getClass().getPackage().getName(), 
+                                            WSA_OBJECT_FACTORY.getClass().getClassLoader());
+                JAXBContextCache.scanPackages(tmp);
+                CachedContextAndSchemas ccs 
+                    = JAXBContextCache.getCachedContextAndSchemas(tmp, null, null, null, false);
+                jaxbContextClasses = ccs.getClasses();
+                jaxbContext = ccs.getContext();
             }
         }
         return jaxbContext;
@@ -740,6 +749,7 @@ public final class ContextUtils {
     public static void setJAXBContext(JAXBContext ctx) throws JAXBException {
         synchronized (ContextUtils.class) {
             jaxbContext = ctx;
+            jaxbContextClasses = new HashSet<Class<?>>();
         }
     }
     

Modified: cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/JaxwsServiceBuilderNoAnnoTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/JaxwsServiceBuilderNoAnnoTest.java?rev=1082032&r1=1082031&r2=1082032&view=diff
==============================================================================
--- cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/JaxwsServiceBuilderNoAnnoTest.java (original)
+++ cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/JaxwsServiceBuilderNoAnnoTest.java Wed Mar 16 02:36:39 2011
@@ -23,7 +23,7 @@ import java.io.File;
 import java.net.URI;
 
 import org.apache.cxf.BusFactory;
-import org.apache.cxf.jaxb.JAXBDataBinding;
+import org.apache.cxf.jaxb.JAXBContextCache;
 import org.apache.cxf.jaxws.JaxwsServiceBuilder;
 import org.apache.cxf.service.model.ServiceInfo;
 import org.apache.cxf.tools.common.ProcessorTestBase;
@@ -38,7 +38,7 @@ public class JaxwsServiceBuilderNoAnnoTe
 
     @Before
     public void setUp() throws Exception {
-        JAXBDataBinding.clearCaches();
+        JAXBContextCache.clearCaches();
         builder.setBus(BusFactory.getDefaultBus());
         generator.setBus(builder.getBus());
     }

Modified: cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/JaxwsServiceBuilderRPCTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/JaxwsServiceBuilderRPCTest.java?rev=1082032&r1=1082031&r2=1082032&view=diff
==============================================================================
--- cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/JaxwsServiceBuilderRPCTest.java (original)
+++ cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/JaxwsServiceBuilderRPCTest.java Wed Mar 16 02:36:39 2011
@@ -22,7 +22,7 @@ package org.apache.cxf.tools.java2wsdl.p
 import java.io.File;
 
 import org.apache.cxf.BusFactory;
-import org.apache.cxf.jaxb.JAXBDataBinding;
+import org.apache.cxf.jaxb.JAXBContextCache;
 import org.apache.cxf.jaxws.JaxwsServiceBuilder;
 import org.apache.cxf.service.model.ServiceInfo;
 import org.apache.cxf.tools.common.ProcessorTestBase;
@@ -36,7 +36,7 @@ public class JaxwsServiceBuilderRPCTest 
 
     @Before
     public void setUp() throws Exception {
-        JAXBDataBinding.clearCaches();
+        JAXBContextCache.clearCaches();
         builder.setBus(BusFactory.getDefaultBus());
         generator.setBus(builder.getBus());
     }

Modified: cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/JaxwsServiceBuilderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/JaxwsServiceBuilderTest.java?rev=1082032&r1=1082031&r2=1082032&view=diff
==============================================================================
--- cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/JaxwsServiceBuilderTest.java (original)
+++ cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/JaxwsServiceBuilderTest.java Wed Mar 16 02:36:39 2011
@@ -29,7 +29,7 @@ import javax.xml.namespace.QName;
 import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
 import org.apache.cxf.helpers.IOUtils;
-import org.apache.cxf.jaxb.JAXBDataBinding;
+import org.apache.cxf.jaxb.JAXBContextCache;
 import org.apache.cxf.jaxws.JaxwsServiceBuilder;
 import org.apache.cxf.service.model.BindingInfo;
 import org.apache.cxf.service.model.ServiceInfo;
@@ -49,7 +49,7 @@ public class JaxwsServiceBuilderTest ext
 
     @Before
     public void setUp() throws Exception {
-        JAXBDataBinding.clearCaches();
+        JAXBContextCache.clearCaches();
         builder = new JaxwsServiceBuilder();
         builder.setBus(BusFactory.getDefaultBus());
         generator.setBus(builder.getBus());



Mime
View raw message