cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1416956 - in /cxf/branches/2.6.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/
Date Tue, 04 Dec 2012 14:17:36 GMT
Author: sergeyb
Date: Tue Dec  4 14:17:35 2012
New Revision: 1416956

URL: http://svn.apache.org/viewvc?rev=1416956&view=rev
Log:
Merged revisions 1416953 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1416953 | sergeyb | 2012-12-04 14:14:50 +0000 (Tue, 04 Dec 2012) | 1 line
  
  [CXF-4672] Better support for extraClass property when package JAXB contexts are created
........

Added:
    cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/
      - copied from r1416953, cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/
    cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/SuperBook3.java
      - copied unchanged from r1416953, cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/SuperBook3.java
    cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/jaxb.index
      - copied unchanged from r1416953, cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/jaxb.index
Modified:
    cxf/branches/2.6.x-fixes/   (props changed)
    cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
    cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java

Propchange: cxf/branches/2.6.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/trunk:r1416953

Propchange: cxf/branches/2.6.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=1416956&r1=1416955&r2=1416956&view=diff
==============================================================================
--- cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
(original)
+++ cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
Tue Dec  4 14:17:35 2012
@@ -28,6 +28,7 @@ import java.lang.annotation.Annotation;
 import java.lang.reflect.Array;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -124,6 +125,8 @@ public abstract class AbstractJAXBProvid
     
     private boolean skipJaxbChecks;
     private boolean singleJaxbContext;
+    private boolean useSingleContextForPackages;
+    
     private Class<?>[] extraClass;
     
     private boolean validateOutput;
@@ -156,6 +159,10 @@ public abstract class AbstractJAXBProvid
         singleJaxbContext = useSingleContext;
     }
     
+    public void setUseSingleContextForPackages(boolean use) {
+        useSingleContextForPackages = use;
+    }
+    
     public void setExtraClass(Class<?>[] userExtraClass) {
         extraClass = userExtraClass;
     }
@@ -163,14 +170,24 @@ public abstract class AbstractJAXBProvid
     @Override
     public void init(List<ClassResourceInfo> cris) {
         if (singleJaxbContext) {
-            Set<Class<?>> allTypes = 
-                new HashSet<Class<?>>(ResourceUtils.getAllRequestResponseTypes(cris,
true)
+            JAXBContext context = null;
+            Set<Class<?>> allTypes = null;
+            if (cris != null) {    
+                allTypes = new HashSet<Class<?>>(ResourceUtils.getAllRequestResponseTypes(cris,
true)
                     .getAllTypes().keySet());
-            JAXBContext context = 
-                ResourceUtils.createJaxbContext(allTypes, extraClass, cProperties);
+                context = ResourceUtils.createJaxbContext(allTypes, extraClass, cProperties);
+            } else if (extraClass != null) {
+                allTypes = new HashSet<Class<?>>(Arrays.asList(extraClass));
+                context = ResourceUtils.createJaxbContext(allTypes, null, cProperties);
+            }
+            
             if (context != null) {
                 for (Class<?> cls : allTypes) {
-                    classContexts.put(cls, context);
+                    if (useSingleContextForPackages) {
+                        packageContexts.put(PackageUtils.getPackageName(cls), context);
+                    } else {
+                        classContexts.put(cls, context);
+                    }
                 }
             }
         }
@@ -471,8 +488,21 @@ public abstract class AbstractJAXBProvid
             JAXBContext context = packageContexts.get(packageName);
             if (context == null) {
                 try {
-                    if (type.getClassLoader() != null && objectFactoryOrIndexAvailable(type))
{ 
-                        context = JAXBContext.newInstance(packageName, type.getClassLoader(),
cProperties);
+                    if (type.getClassLoader() != null && objectFactoryOrIndexAvailable(type))
{
+                        
+                        String contextName = packageName;
+                        if (extraClass != null) {
+                            StringBuilder sb = new StringBuilder(contextName);
+                            for (Class<?> extra : extraClass) {
+                                String extraPackage = PackageUtils.getPackageName(extra);
+                                if (!extraPackage.equals(packageName)) {
+                                    sb.append(":").append(extraPackage);
+                                }
+                            }
+                            contextName = sb.toString();
+                        }
+                        
+                        context = JAXBContext.newInstance(contextName, type.getClassLoader(),
cProperties);
                         packageContexts.put(packageName, context);
                     }
                 } catch (JAXBException ex) {

Modified: cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java?rev=1416956&r1=1416955&r2=1416956&view=diff
==============================================================================
--- cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
(original)
+++ cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
Tue Dec  4 14:17:35 2012
@@ -528,6 +528,54 @@ public class JAXBElementProviderTest ext
         assertEquals(124L, book.getSuperId());
     }
     
+    @Test
+    public void testObjectFactoryExtraClass() throws Exception {
+        JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book> provider 
+            = new JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book>();
+        provider.setExtraClass(new Class[]{org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3.class});
+
+        doTestObjectFactoryExtraClass(provider);
+    }
+    
+    @Test
+    public void testObjectFactoryExtraClass2() throws Exception {
+        JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book> provider 
+            = new JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book>();
+        provider.setExtraClass(new Class[] {
+            org.apache.cxf.jaxrs.fortest.jaxb.Book.class,                               
+            org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3.class});
+        provider.setSingleJaxbContext(true);
+        provider.setUseSingleContextForPackages(true);
+        provider.init(null);
+        
+        doTestObjectFactoryExtraClass(provider);
+    }
+    
+    private void doTestObjectFactoryExtraClass(JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book>
provider)
+        throws Exception {
+
+        org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3 b = 
+            new org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3("CXF in Action", 123L,
124L);
+        
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        provider.writeTo(b, org.apache.cxf.jaxrs.fortest.jaxb.Book.class, 
+                         org.apache.cxf.jaxrs.fortest.jaxb.Book.class,
+                         new Annotation[0], MediaType.TEXT_XML_TYPE, 
+                         new MetadataMap<String, Object>(), bos);
+        JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book> provider2 
+            = new JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book>();
+        provider2.setExtraClass(new Class[]{org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3.class});
+        
+        ByteArrayInputStream is = new ByteArrayInputStream(bos.toByteArray());
+        org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3 book = 
+            (org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3)provider2.readFrom(
+                       org.apache.cxf.jaxrs.fortest.jaxb.Book.class, 
+                       org.apache.cxf.jaxrs.fortest.jaxb.Book.class,
+                       new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String,
String>(), is);
+        assertEquals(124L, book.getSuperId());
+    }
+    
+    
     private void readSuperBook(String data, boolean xsiTypeExpected) throws Exception {
         if (xsiTypeExpected) {
             assertTrue(data.contains("xsi:type"));



Mime
View raw message