Search in sources :

Example 1 with OwbInterceptorProxy

use of org.apache.webbeans.proxy.OwbInterceptorProxy in project tomee by apache.

the class CxfUtil method initDefaultBus.

private static Bus initDefaultBus() {
    final ClassLoader cl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(CxfUtil.class.getClassLoader());
    try {
        // create the bus reusing cxf logic but skipping factory lookup
        final Bus bus = BusFactory.newInstance(CXFBusFactory.class.getName()).createBus();
        bus.setId(SystemInstance.get().getProperty("openejb.cxf.bus.id", "openejb.cxf.bus"));
        final BindingFactoryManager bfm = bus.getExtension(BindingFactoryManager.class);
        bindingFactoryMap = (Map<String, BindingFactory>) Reflections.get(bfm, "bindingFactories");
        bus.setExtension(new OpenEJBHttpDestinationFactory(), HttpDestinationFactory.class);
        // ensure client proxies can use app classes
        CXFBusFactory.setDefaultBus(Bus.class.cast(Proxy.newProxyInstance(CxfUtil.class.getClassLoader(), new Class<?>[] { Bus.class }, new ClientAwareBusHandler())));
        bus.setProperty(ClassUnwrapper.class.getName(), new ClassUnwrapper() {

            @Override
            public Class<?> getRealClass(final Object o) {
                final Class<?> aClass = o.getClass();
                Class<?> c = aClass;
                while (c.getName().contains("$$")) {
                    c = c.getSuperclass();
                }
                return c == Object.class ? aClass : c;
            }

            @Override
            public Class<?> getRealClassFromClass(Class<?> aClass) {
                return aClass;
            }

            @Override
            public Object getRealObject(Object o) {
                // straight on the proxy
                if (o instanceof OwbInterceptorProxy) {
                    return getProxiedInstance(o);
                }
                return o;
            }

            private Object getProxiedInstance(Object o) {
                try {
                    final Field field = o.getClass().getDeclaredField(FIELD_INTERCEPTOR_HANDLER);
                    field.setAccessible(true);
                    final Object fieldValue = field.get(o);
                    if (fieldValue instanceof DefaultInterceptorHandler) {
                        final DefaultInterceptorHandler handler = (DefaultInterceptorHandler) fieldValue;
                        return handler.getTarget();
                    } else {
                        throw new IllegalStateException("Expected OwbInterceptorProxy handler to be an instance of Default Interceptor Handler.");
                    }
                } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        SystemInstance.get().addObserver(new LifecycleManager());
        initAuthenticators();
        // we keep as internal the real bus and just expose to cxf the client aware bus to be able to cast it easily
        return bus;
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
    }
}
Also used : Bus(org.apache.cxf.Bus) BindingFactoryManager(org.apache.cxf.binding.BindingFactoryManager) ClassUnwrapper(org.apache.cxf.common.util.ClassUnwrapper) Field(java.lang.reflect.Field) DefaultInterceptorHandler(org.apache.webbeans.intercept.DefaultInterceptorHandler) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) OpenEJBHttpDestinationFactory(org.apache.openejb.server.cxf.transport.OpenEJBHttpDestinationFactory) OwbInterceptorProxy(org.apache.webbeans.proxy.OwbInterceptorProxy) BindingFactory(org.apache.cxf.binding.BindingFactory)

Aggregations

Field (java.lang.reflect.Field)1 Bus (org.apache.cxf.Bus)1 BindingFactory (org.apache.cxf.binding.BindingFactory)1 BindingFactoryManager (org.apache.cxf.binding.BindingFactoryManager)1 ClassUnwrapper (org.apache.cxf.common.util.ClassUnwrapper)1 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)1 OpenEJBHttpDestinationFactory (org.apache.openejb.server.cxf.transport.OpenEJBHttpDestinationFactory)1 DefaultInterceptorHandler (org.apache.webbeans.intercept.DefaultInterceptorHandler)1 OwbInterceptorProxy (org.apache.webbeans.proxy.OwbInterceptorProxy)1