cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1416953 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/provider/ test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/ test/java/org/apache/cxf/jaxrs/provider/
Date Tue, 04 Dec 2012 14:14:52 GMT
Author: sergeyb
Date: Tue Dec  4 14:14:50 2012
New Revision: 1416953

URL: http://svn.apache.org/viewvc?rev=1416953&view=rev
Log:
[CXF-4672] Better support for extraClass property when package JAXB contexts are created

Added:
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/SuperBook3.java
  (with props)
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/jaxb.index
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=1416953&r1=1416952&r2=1416953&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
Tue Dec  4 14:14:50 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;
@@ -126,6 +127,8 @@ public abstract class AbstractJAXBProvid
     
     private boolean skipJaxbChecks;
     private boolean singleJaxbContext;
+    private boolean useSingleContextForPackages;
+    
     private Class<?>[] extraClass;
     
     private boolean validateOutput;
@@ -158,6 +161,10 @@ public abstract class AbstractJAXBProvid
         singleJaxbContext = useSingleContext;
     }
     
+    public void setUseSingleContextForPackages(boolean use) {
+        useSingleContextForPackages = use;
+    }
+    
     public void setExtraClass(Class<?>[] userExtraClass) {
         extraClass = userExtraClass;
     }
@@ -165,14 +172,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);
+                    }
                 }
             }
         }
@@ -473,8 +490,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) {

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/SuperBook3.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/SuperBook3.java?rev=1416953&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/SuperBook3.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/SuperBook3.java
Tue Dec  4 14:14:50 2012
@@ -0,0 +1,44 @@
+/**
+ * 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.jaxrs.fortest.jaxb.index;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.apache.cxf.jaxrs.fortest.jaxb.Book;
+
+@XmlRootElement
+public class SuperBook3 extends Book {
+    private long superId;
+    public SuperBook3() {
+    }
+    
+    public SuperBook3(String name, long id, long superId) {
+        super(name, id);
+        this.superId = superId;
+    }
+    
+    public void setSuperId(long i) {
+        superId = i;
+    }
+    
+    public long getSuperId() {
+        return superId;
+    }
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/SuperBook3.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/SuperBook3.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/jaxb.index
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/jaxb.index?rev=1416953&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/jaxb.index
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/index/jaxb.index
Tue Dec  4 14:14:50 2012
@@ -0,0 +1 @@
+SuperBook3
\ No newline at end of file

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java?rev=1416953&r1=1416952&r2=1416953&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
Tue Dec  4 14:14:50 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