cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject [cxf] branch 3.1.x-fixes updated: [CXF-7640] Create a form to set the use of Spring in the classHelper … (#387)
Date Fri, 11 May 2018 00:11:15 GMT
This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch 3.1.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/3.1.x-fixes by this push:
     new efd9e91  [CXF-7640] Create a form to set the use of Spring in the classHelper …
(#387)
efd9e91 is described below

commit efd9e916b3bf6b5efe537f07c15f78d41d65edf1
Author: Osvaldo Pina <osvaldo.pina@gmail.com>
AuthorDate: Sun Apr 29 17:09:17 2018 -0300

    [CXF-7640] Create a form to set the use of Spring in the classHelper … (#387)
    
    * [CXF-7640] Create a form to set the use of Spring in the classHelper on a per client
way
    
    * [CXF-7640] Create a form to set the use of Spring in the classHelper on a per client
way
---
 .../org/apache/cxf/common/util/ClassHelper.java    |  61 +++++---
 .../apache/cxf/common/util/ClassHelperTest.java    | 168 +++++++++++++++++++++
 2 files changed, 211 insertions(+), 18 deletions(-)

diff --git a/core/src/main/java/org/apache/cxf/common/util/ClassHelper.java b/core/src/main/java/org/apache/cxf/common/util/ClassHelper.java
index 6b50ef1..91be9e8 100644
--- a/core/src/main/java/org/apache/cxf/common/util/ClassHelper.java
+++ b/core/src/main/java/org/apache/cxf/common/util/ClassHelper.java
@@ -19,25 +19,31 @@
 
 package org.apache.cxf.common.util;
 
+
 import java.lang.reflect.Proxy;
 
 import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
 
 /**
- * 
+ *
  */
 public class ClassHelper {
+
+    public static final String USE_DEFAULT_CLASS_HELPER = "org.apache.cxf.useDefaultClassHelpers";
+
     static final ClassHelper HELPER;
+    static final ClassHelper DEFAULT_HELPER;
+
     static {
-        HELPER = getClassHelper();
+        DEFAULT_HELPER = new ClassHelper();
+        HELPER = getClassHelper(DEFAULT_HELPER);
     }
-    
-    
+
     protected ClassHelper() {
     }
-    
-    private static ClassHelper getClassHelper() { 
+
+    private static ClassHelper getClassHelper(ClassHelper defaultHelper) {
         boolean useSpring = true;
         String s = SystemPropertyAction.getPropertyOrNull("org.apache.cxf.useSpringClassHelpers");
         if (!StringUtils.isEmpty(s)) {
@@ -50,39 +56,58 @@ public class ClassHelper {
                 // ignore
             }
         }
-        return new ClassHelper();
+        return defaultHelper;
     }
-    
+
     protected Class<?> getRealClassInternal(Object o) {
         return getRealObjectInternal(o).getClass();
     }
-    
+
     protected Class<?> getRealClassFromClassInternal(Class<?> cls) {
         return cls;
     }
+
     protected Object getRealObjectInternal(Object o) {
         return o instanceof Proxy ? Proxy.getInvocationHandler(o) : o;
     }
-    
+
     public static Class<?> getRealClass(Object o) {
         return getRealClass(null, o);
     }
-    
+
     public static Class<?> getRealClassFromClass(Class<?> cls) {
-        return HELPER.getRealClassFromClassInternal(cls);
+        return getRealClassFromClass(null, cls);
+    }
+
+    public static Class<?> getRealClassFromClass(Bus bus, Class<?> cls) {
+        bus = getBus(bus);
+        return getContextClassHelper(bus).getRealClassFromClassInternal(cls);
     }
-    
+
     public static Object getRealObject(Object o) {
-        return HELPER.getRealObjectInternal(o);
+        Bus bus = getBus(null);
+        return getContextClassHelper(bus).getRealObjectInternal(o);
     }
 
     public static Class<?> getRealClass(Bus bus, Object o) {
-        bus = bus == null ? BusFactory.getThreadDefaultBus() : bus;
+        bus = getBus(bus);
         if (bus != null && bus.getProperty(ClassUnwrapper.class.getName()) != null)
{
-            ClassUnwrapper unwrapper = (ClassUnwrapper)bus.getProperty(ClassUnwrapper.class.getName());
+            ClassUnwrapper unwrapper = (ClassUnwrapper) bus.getProperty(ClassUnwrapper.class.getName());
             return unwrapper.getRealClass(o);
-        } else {
-            return HELPER.getRealClassInternal(o);
         }
+        return getContextClassHelper(bus).getRealClassInternal(o);
+    }
+
+    private static ClassHelper getContextClassHelper(Bus bus) {
+        return (DEFAULT_HELPER == HELPER || checkUseDefaultClassHelper(bus)) ? DEFAULT_HELPER
: HELPER;
     }
+
+    private static Bus getBus(Bus bus) {
+        return bus == null ? BusFactory.getThreadDefaultBus() : bus;
+    }
+
+    private static boolean checkUseDefaultClassHelper(Bus bus) {
+        return bus != null && Boolean.TRUE.equals(bus.getProperty(USE_DEFAULT_CLASS_HELPER));
+    }
+
 }
diff --git a/core/src/test/java/org/apache/cxf/common/util/ClassHelperTest.java b/core/src/test/java/org/apache/cxf/common/util/ClassHelperTest.java
new file mode 100644
index 0000000..6df5084
--- /dev/null
+++ b/core/src/test/java/org/apache/cxf/common/util/ClassHelperTest.java
@@ -0,0 +1,168 @@
+/**
+ * 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.common.util;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.easymock.EasyMock;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import org.springframework.aop.AfterReturningAdvice;
+import org.springframework.aop.framework.ProxyFactory;
+
+public class ClassHelperTest extends Assert {
+
+    private Object proxiedObject;
+
+    private Object springAopObject;
+
+    private InvocationHandler realObjectInternalProxy;
+
+    private Object realObjectInternalSpring;
+
+    private Bus bus;
+
+    private Bus currentThreadBus;
+
+    @Before
+    public void setUp() {
+        realObjectInternalProxy = new InvocationHandler() {
+            @Override
+            public Object invoke(Object o, Method method, Object[] objects) throws Throwable
{
+                return null;
+            }
+        };
+
+        proxiedObject = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
+                new Class[]{AnyInterface.class}, realObjectInternalProxy);
+
+        realObjectInternalSpring = new Object();
+
+        ProxyFactory proxyFactory = new ProxyFactory(realObjectInternalSpring);
+        proxyFactory.addAdvice(new AfterReturningAdvice() {
+
+            @Override
+            public void afterReturning(Object o, Method method, Object[] objects, Object
o1) throws Throwable {
+
+            }
+        });
+
+        springAopObject = proxyFactory.getProxy();
+
+        currentThreadBus = BusFactory.getThreadDefaultBus();
+
+        bus = EasyMock.mock(Bus.class);
+
+        BusFactory.setThreadDefaultBus(bus);
+    }
+
+    @After
+    public void tearDown() {
+        BusFactory.setThreadDefaultBus(currentThreadBus);
+    }
+
+    @Test
+    public void getRealClassPropertyWasSetInBus() {
+
+        EasyMock.expect(bus.getProperty(ClassHelper.USE_DEFAULT_CLASS_HELPER)).andReturn(true);
+        EasyMock.expect(bus.getProperty(ClassUnwrapper.class.getName())).andReturn(null);
+        EasyMock.replay(bus);
+
+        assertSame(realObjectInternalProxy.getClass(), ClassHelper.getRealClass(proxiedObject));
+
+        EasyMock.verify(bus);
+
+    }
+
+    @Test
+    public void getRealClassPropertyWasNotSetInBus() {
+
+        EasyMock.expect(bus.getProperty(ClassHelper.USE_DEFAULT_CLASS_HELPER)).andReturn(false);
+        EasyMock.expect(bus.getProperty(ClassUnwrapper.class.getName())).andReturn(null);
+        EasyMock.replay(bus);
+
+        assertSame(realObjectInternalSpring.getClass(), ClassHelper.getRealClass(springAopObject));
+
+        EasyMock.verify(bus);
+
+    }
+
+    @Test
+    public void getRealClassFromClassPropertyWasSetInBus() {
+
+        EasyMock.expect(bus.getProperty(ClassHelper.USE_DEFAULT_CLASS_HELPER)).andReturn(true);
+        EasyMock.replay(bus);
+
+        assertSame(proxiedObject.getClass(), ClassHelper.getRealClassFromClass(proxiedObject.getClass()));
+
+        EasyMock.verify(bus);
+
+    }
+
+    @Test
+    public void getRealClassFromClassPropertyWasNotSetInBus() {
+
+        EasyMock.expect(bus.getProperty(ClassHelper.USE_DEFAULT_CLASS_HELPER)).andReturn(false);
+        EasyMock.replay(bus);
+
+        assertSame(realObjectInternalSpring.getClass(), ClassHelper.getRealClassFromClass(springAopObject.getClass()));
+
+        EasyMock.verify(bus);
+
+    }
+
+
+    @Test
+    public void getRealObjectPropertyWasSetInBus() {
+
+        EasyMock.expect(bus.getProperty(ClassHelper.USE_DEFAULT_CLASS_HELPER)).andReturn(true);
+        EasyMock.replay(bus);
+
+        assertSame(realObjectInternalProxy, ClassHelper.getRealObject(proxiedObject));
+
+        EasyMock.verify(bus);
+
+    }
+
+    @Test
+    public void getRealObjectPropertyWasNotSetInBus() {
+
+        EasyMock.expect(bus.getProperty(ClassHelper.USE_DEFAULT_CLASS_HELPER)).andReturn(false);
+        EasyMock.replay(bus);
+
+        assertSame(realObjectInternalSpring, ClassHelper.getRealObject(springAopObject));
+
+        EasyMock.verify(bus);
+
+    }
+
+    public interface AnyInterface {
+        void anyMethod();
+    }
+
+
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
reta@apache.org.

Mime
View raw message