You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by GitBox <gi...@apache.org> on 2018/04/29 20:09:19 UTC

[GitHub] reta closed pull request #387: [CXF-7640] Create a form to set the use of Spring in the classHelper …

reta closed pull request #387: [CXF-7640] Create a form to set the use of Spring in the classHelper …
URL: https://github.com/apache/cxf/pull/387
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 4d7c9014270..91be9e8a0a9 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,6 +19,7 @@
 
 package org.apache.cxf.common.util;
 
+
 import java.lang.reflect.Proxy;
 
 import org.apache.cxf.Bus;
@@ -28,16 +29,21 @@
  *
  */
 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,7 +56,7 @@ private static ClassHelper getClassHelper() {
                 // ignore
             }
         }
-        return new ClassHelper();
+        return defaultHelper;
     }
 
     protected Class<?> getRealClassInternal(Object o) {
@@ -60,6 +66,7 @@ private static ClassHelper getClassHelper() {
     protected Class<?> getRealClassFromClassInternal(Class<?> cls) {
         return cls;
     }
+
     protected Object getRealObjectInternal(Object o) {
         return o instanceof Proxy ? Proxy.getInvocationHandler(o) : o;
     }
@@ -69,19 +76,38 @@ protected Object getRealObjectInternal(Object 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);
         }
-        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 00000000000..dc3e184b82d
--- /dev/null
+++ b/core/src/test/java/org/apache/cxf/common/util/ClassHelperTest.java
@@ -0,0 +1,169 @@
+/**
+ * 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.springframework.aop.AfterReturningAdvice;
+import org.springframework.aop.framework.ProxyFactory;
+
+import org.easymock.EasyMock;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+
+
+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


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services