cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1084664 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/spring/ systests/jaxrs/src/test/jav...
Date Wed, 23 Mar 2011 18:21:55 GMT
Author: sergeyb
Date: Wed Mar 23 18:21:54 2011
New Revision: 1084664

URL: http://svn.apache.org/viewvc?rev=1084664&view=rev
Log:
[CXF-3415] Initial attempt at making JAXB providers endpoint-scoped

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanTest.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.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=1084664&r1=1084663&r2=1084664&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
Wed Mar 23 18:21:54 2011
@@ -83,12 +83,7 @@ public abstract class AbstractJAXBProvid
     private static final String JAXB_DEFAULT_NAMESPACE = "##default";
     private static final String JAXB_DEFAULT_NAME = "##default";
     
-    private static Map<String, JAXBContext> packageContexts = new HashMap<String,
JAXBContext>();
-    private static Map<Class<?>, JAXBContext> classContexts = new HashMap<Class<?>,
JAXBContext>();
-
-   
     protected Set<Class<?>> collectionContextClasses = new HashSet<Class<?>>();
-    protected JAXBContext collectionContext; 
     
     protected Map<String, String> jaxbElementClassMap;
     protected boolean unmarshalAsJaxbElement;
@@ -102,6 +97,9 @@ public abstract class AbstractJAXBProvid
     protected Map<String, String> inAppendMap;
     private boolean attributesToElements;
     
+    private Map<String, JAXBContext> packageContexts = new HashMap<String, JAXBContext>();
+    private Map<Class<?>, JAXBContext> classContexts = new HashMap<Class<?>,
JAXBContext>();
+    
     private MessageContext mc;
     private Schema schema;
     private String collectionWrapperName;
@@ -236,9 +234,8 @@ public abstract class AbstractJAXBProvid
                 collectionContextClasses.add(CollectionWrapper.class);
                 collectionContextClasses.add(type);
             }
-            collectionContext = JAXBContext.newInstance(collectionContextClasses.toArray(new
Class[]{}), 
+            return JAXBContext.newInstance(collectionContextClasses.toArray(new Class[]{}),

                                                         cProperties);
-            return collectionContext;
         }
     }
     
@@ -533,7 +530,7 @@ public abstract class AbstractJAXBProvid
     }
 
     
-    public static void clearContexts() {
+    public void clearContexts() {
         classContexts.clear();
         packageContexts.clear();
     }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java?rev=1084664&r1=1084663&r2=1084664&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
Wed Mar 23 18:21:54 2011
@@ -61,10 +61,14 @@ public final class ProviderFactory {
     private static final Logger LOG = LogUtils.getL7dLogger(ProviderFactory.class);
     private static final ProviderFactory SHARED_FACTORY = new ProviderFactory();
     
+    private static final String JAXB_PROVIDER_NAME = "org.apache.cxf.jaxrs.provider.JAXBElementProvider";
+    private static final String JSON_PROVIDER_NAME = "org.apache.cxf.jaxrs.provider.JSONProvider";
+    
     static {
-        setDefaultProvider(SHARED_FACTORY, "org.apache.cxf.jaxrs.provider.JAXBElementProvider");
-        setDefaultProvider(SHARED_FACTORY, "org.apache.cxf.jaxrs.provider.JSONProvider");
       
-        SHARED_FACTORY.setProviders(new BinaryDataProvider(),
+        SHARED_FACTORY.setProviders(true,
+                                    createProvider(JAXB_PROVIDER_NAME),
+                                    createProvider(JSON_PROVIDER_NAME),
+                                    new BinaryDataProvider(),
                                     new SourceProvider(),
                                     new FormEncodingProvider(),
                                     new PrimitiveTextProvider(),
@@ -90,16 +94,16 @@ public final class ProviderFactory {
     private List<ProviderInfo<ResponseExceptionMapper>> responseExceptionMappers
= 
         new ArrayList<ProviderInfo<ResponseExceptionMapper>>(1);
     private RequestPreprocessor requestPreprocessor;
+    private Set<Object> clonedProviders = new HashSet<Object>();
     
     private ProviderFactory() {
     }
     
     
-    private static void setDefaultProvider(ProviderFactory factory, String className) {
+    private static Object createProvider(String className) {
         
         try {
-            Object provider = ClassLoaderUtils.loadClass(className, ProviderFactory.class).newInstance();
-            factory.setProviders(provider);
+            return ClassLoaderUtils.loadClass(className, ProviderFactory.class).newInstance();
         } catch (Throwable ex) {
             String message = "Problem with setting the default provider " + className;
             if (ex.getMessage() != null) {
@@ -109,6 +113,7 @@ public final class ProviderFactory {
             }
             LOG.info(message);
         }
+        return null;
     }
     
     public static ProviderFactory getInstance() {
@@ -267,6 +272,7 @@ public final class ProviderFactory {
     
     
     
+    @SuppressWarnings("unchecked")
     public <T> MessageBodyReader<T> createMessageBodyReader(Class<T> bodyType,
                                                             Type parameterType,
                                                             Annotation[] parameterAnnotations,
@@ -283,8 +289,32 @@ public final class ProviderFactory {
         if (mr != null ||  this == SHARED_FACTORY) {
             return mr;
         }
-        return SHARED_FACTORY.createMessageBodyReader(bodyType, parameterType, 
+        mr = SHARED_FACTORY.createMessageBodyReader(bodyType, parameterType, 
                                                         parameterAnnotations, mediaType,
m);
+        return (MessageBodyReader)cloneSharedProviderIfNeeded(mr);
+    }
+    
+    private Object cloneSharedProviderIfNeeded(Object sharedProvider) {
+        if (sharedProvider != null) {
+            String clsName = sharedProvider.getClass().getName();
+            if (JAXB_PROVIDER_NAME.equals(clsName) || JSON_PROVIDER_NAME.equals(clsName))
{
+                try {
+                    synchronized (this) {
+                        for (Object cloned : clonedProviders) {
+                            if (cloned.getClass().getName().equals(clsName)) {
+                                return cloned;
+                            }
+                        }
+                        Object provider = sharedProvider.getClass().newInstance();
+                        setProviders(false, provider);
+                        return provider;
+                    }
+                } catch (Exception ex) {
+                    // won't happen at this stage
+                }
+            }
+        }
+        return sharedProvider;
     }
     
     public List<ProviderInfo<RequestHandler>> getRequestHandlers() {
@@ -315,6 +345,7 @@ public final class ProviderFactory {
         return Collections.unmodifiableList(responseHandlers);
     }
 
+    @SuppressWarnings("unchecked")
     public <T> MessageBodyWriter<T> createMessageBodyWriter(Class<T> bodyType,
                                                             Type parameterType,
                                                             Annotation[] parameterAnnotations,
@@ -331,12 +362,13 @@ public final class ProviderFactory {
         if (mw != null || this == SHARED_FACTORY) {
             return mw;
         }
-        return SHARED_FACTORY.createMessageBodyWriter(bodyType, parameterType, 
+        mw = SHARED_FACTORY.createMessageBodyWriter(bodyType, parameterType, 
                                                         parameterAnnotations, mediaType,
m);
+        return (MessageBodyWriter)cloneSharedProviderIfNeeded(mw);
     }
     
 //CHECKSTYLE:OFF       
-    private void setProviders(Object... providers) {
+    private void setProviders(boolean sort, Object... providers) {
         
         for (Object o : providers) {
             if (o == null) {
@@ -376,9 +408,10 @@ public final class ProviderFactory {
                 paramHandlers.add(new ProviderInfo<ParameterHandler>((ParameterHandler)o));

             }
         }
-        
-        sortReaders();
-        sortWriters();
+        if (sort) {
+            sortReaders();
+            sortWriters();
+        }
         
         injectContexts(messageReaders, messageWriters, contextResolvers, requestHandlers,
responseHandlers,
                        exceptionMappers);
@@ -537,7 +570,7 @@ public final class ProviderFactory {
      * @param entityProviders the entityProviders to set
      */
     public void setUserProviders(List<?> userProviders) {
-        setProviders(userProviders.toArray());
+        setProviders(true, userProviders.toArray());
     }
 
     private static class MessageBodyReaderComparator 

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=1084664&r1=1084663&r2=1084664&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
Wed Mar 23 18:21:54 2011
@@ -1019,7 +1019,7 @@ public class JAXBElementProviderTest ext
             assertNotNull(context2);
             assertSame(context, context2);
         } finally {
-            JAXBElementProvider.clearContexts();
+            p.clearContexts();
         }
         
     }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java?rev=1084664&r1=1084663&r2=1084664&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
Wed Mar 23 18:21:54 2011
@@ -128,6 +128,76 @@ public class ProviderFactoryTest extends
     }
     
     @Test
+    public void testDefaultJaxbProviderCloned() {
+        ProviderFactory pf = ProviderFactory.getInstance();
+        MessageBodyReader customJaxbReader = pf.createMessageBodyReader((Class<?>)Book.class,
null, null, 
+                                                              MediaType.TEXT_XML_TYPE, new
MessageImpl());
+        assertTrue(customJaxbReader instanceof JAXBElementProvider);
+        
+        MessageBodyReader customJaxbReader2 = pf.createMessageBodyReader((Class<?>)Book.class,
null, null, 
+                                                              MediaType.TEXT_XML_TYPE, new
MessageImpl());
+        assertSame(customJaxbReader, customJaxbReader2);
+        
+        MessageBodyWriter customJaxbWriter = pf.createMessageBodyWriter((Class<?>)Book.class,
null, null, 
+                                                              MediaType.TEXT_XML_TYPE, new
MessageImpl());
+        assertSame(customJaxbReader, customJaxbWriter);
+        
+        MessageBodyReader jaxbReader = ProviderFactory.getSharedInstance().createMessageBodyReader(
+            (Class<?>)Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
+        assertTrue(jaxbReader instanceof JAXBElementProvider);
+        assertNotSame(jaxbReader, customJaxbReader);
+    }
+    
+    @Test
+    public void testCustomJaxbProvider() {
+        ProviderFactory pf = ProviderFactory.getInstance();
+        JAXBElementProvider provider = new JAXBElementProvider();
+        pf.registerUserProvider(provider);
+        MessageBodyReader customJaxbReader = pf.createMessageBodyReader((Class<?>)Book.class,
null, null, 
+                                                              MediaType.TEXT_XML_TYPE, new
MessageImpl());
+        assertSame(customJaxbReader, provider);
+        
+        MessageBodyWriter customJaxbWriter = pf.createMessageBodyWriter((Class<?>)Book.class,
null, null, 
+                                                              MediaType.TEXT_XML_TYPE, new
MessageImpl());
+        assertSame(customJaxbWriter, provider);
+    }
+    
+    @Test
+    public void testCustomJsonProvider() {
+        ProviderFactory pf = ProviderFactory.getInstance();
+        JSONProvider provider = new JSONProvider();
+        pf.registerUserProvider(provider);
+        MessageBodyReader customJsonReader = pf.createMessageBodyReader((Class<?>)Book.class,
null, null, 
+                                               MediaType.APPLICATION_JSON_TYPE, new MessageImpl());
+        assertSame(customJsonReader, provider);
+        
+        MessageBodyWriter customJsonWriter = pf.createMessageBodyWriter((Class<?>)Book.class,
null, null, 
+                                               MediaType.APPLICATION_JSON_TYPE, new MessageImpl());
+        assertSame(customJsonWriter, provider);
+    }
+    
+    @Test
+    public void testDefaultJsonProviderCloned() {
+        ProviderFactory pf = ProviderFactory.getInstance();
+        MessageBodyReader customJsonReader = pf.createMessageBodyReader((Class<?>)Book.class,
null, null, 
+                                                MediaType.APPLICATION_JSON_TYPE, new MessageImpl());
+        assertTrue(customJsonReader instanceof JSONProvider);
+        
+        MessageBodyReader customJsonReader2 = pf.createMessageBodyReader((Class<?>)Book.class,
null, null, 
+                                                MediaType.APPLICATION_JSON_TYPE, new MessageImpl());
+        assertSame(customJsonReader, customJsonReader2);
+        
+        MessageBodyWriter customJsonWriter = pf.createMessageBodyWriter((Class<?>)Book.class,
null, null, 
+                                                MediaType.APPLICATION_JSON_TYPE, new MessageImpl());
+        assertSame(customJsonReader, customJsonWriter);
+        
+        MessageBodyReader jsonReader = ProviderFactory.getSharedInstance().createMessageBodyReader(
+            (Class<?>)Book.class, null, null, MediaType.APPLICATION_JSON_TYPE, new
MessageImpl());
+        assertTrue(jsonReader instanceof JSONProvider);
+        assertNotSame(jsonReader, customJsonReader);
+    }
+    
+    @Test
     public void testSchemaLocations() {
         ProviderFactory pf = ProviderFactory.getInstance();
         pf.setSchemaLocations(Collections.singletonList("classpath:/test.xsd"));

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanTest.java?rev=1084664&r1=1084663&r2=1084664&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanTest.java
Wed Mar 23 18:21:54 2011
@@ -91,6 +91,6 @@ public class JAXRSServerFactoryBeanTest 
         assertNotNull(c1);
         JAXBContext c2 = provider.getClassContext(SuperBook.class);
         assertSame(c1, c2);
-        JAXBElementProvider.clearContexts();
+        provider.clearContexts();
     }
 }

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1084664&r1=1084663&r2=1084664&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Wed Mar 23 18:21:54 2011
@@ -64,7 +64,7 @@ public class JAXRSClientServerBookTest e
     @BeforeClass
     public static void startServers() throws Exception {
         assertTrue("server did not launch correctly",
-                   launchServer(BookServer.class));
+                   launchServer(BookServer.class, true));
     }
     
     @Test



Mime
View raw message