org.apache.cxf.common.util.ClassHelper Java Examples

The following examples show how to use org.apache.cxf.common.util.ClassHelper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SingletonResourceProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
public void init(Endpoint ep) {
    if (resourceInstance instanceof Constructor) {
        Constructor<?> c = (Constructor<?>)resourceInstance;
        Message m = new MessageImpl();
        ExchangeImpl exchange = new ExchangeImpl();
        exchange.put(Endpoint.class, ep);
        m.setExchange(exchange);
        Object[] values = 
            ResourceUtils.createConstructorArguments(c, m, false, Collections.emptyMap());
        try {
            resourceInstance = values.length > 0 ? c.newInstance(values) : c.newInstance(new Object[]{});
        } catch (Exception ex) {
            throw new ServiceConstructionException(ex);
        }
    }
    if (callPostConstruct) {
        InjectionUtils.invokeLifeCycleMethod(resourceInstance,
            ResourceUtils.findPostConstructMethod(ClassHelper.getRealClass(resourceInstance)));
    }    
}
 
Example #2
Source File: BlueprintResourceFactory.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void init() {
    Class<?> type = ClassHelper.getRealClassFromClass(blueprintContainer.getComponentInstance(beanId)
                                                      .getClass());
    if (Proxy.isProxyClass(type)) {
        type = ClassHelper.getRealClass(blueprintContainer.getComponentInstance(beanId));
    }
    c = ResourceUtils.findResourceConstructor(type, !isSingleton());
    if (c == null) {
        throw new RuntimeException("Resource class " + type + " has no valid constructor");
    }
    postConstructMethod = ResourceUtils.findPostConstructMethod(type);
    preDestroyMethod = ResourceUtils.findPreDestroyMethod(type);

    Object component = blueprintContainer.getComponentMetadata(beanId);
    if (component instanceof BeanMetadata) {
        BeanMetadata local = (BeanMetadata) component;
        isSingleton = BeanMetadata.SCOPE_SINGLETON.equals(local.getScope())
            || (local.getScope() == null && local.getId() != null);
    }
}
 
Example #3
Source File: ProviderFactory.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected static int compareClasses(Class<?> expectedCls, Object o1, Object o2) {
    Class<?> cl1 = ClassHelper.getRealClass(o1);
    Class<?> cl2 = ClassHelper.getRealClass(o2);
    Type[] types1 = getGenericInterfaces(cl1, expectedCls);
    Type[] types2 = getGenericInterfaces(cl2, expectedCls);
    if (types1.length == 0 && types2.length == 0) {
        return 0;
    } else if (types1.length == 0 && types2.length > 0) {
        return 1;
    } else if (types1.length > 0 && types2.length == 0) {
        return -1;
    }

    Class<?> realClass1 = InjectionUtils.getActualType(types1[0]);
    Class<?> realClass2 = InjectionUtils.getActualType(types2[0]);
    if (realClass1 == realClass2) {
        return 0;
    }
    if (realClass1.isAssignableFrom(realClass2)) {
        // subclass should go first
        return 1;
    }
    return -1;
}
 
Example #4
Source File: StratosAuthorizingHandler.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
/**
 * The instance of the secured bean get injected by the IOC framework
 *
 * @param securedObject
 */
public void setSecuredObject(Object securedObject) {
    Class<?> clazz = ClassHelper.getRealClass(securedObject);
    authorizationActionMap = getAuthorizationActionMap(clazz);
    superTenantServiceSet = getSuperTenantServiceSet(clazz);

}
 
Example #5
Source File: StratosAuthorizingHandler.java    From product-private-paas with Apache License 2.0 5 votes vote down vote up
/**
 * The instance of the secured bean get injected by the IOC framework
 *
 * @param securedObject
 */
public void setSecuredObject(Object securedObject) {
    Class<?> clazz = ClassHelper.getRealClass(securedObject);
    authorizationActionMap = getAuthorizationActionMap(clazz);
    superTenantServiceSet = getSuperTenantServiceSet(clazz);

}
 
Example #6
Source File: SecureAnnotationsInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void setSecuredObject(Object object) {
    Class<?> cls = ClassHelper.getRealClass(object);
    Map<String, String> rolesMap = new HashMap<>();
    findRoles(cls, rolesMap);
    if (rolesMap.isEmpty()) {
        LOG.warning("The roles map is empty, the service object is not protected");
    } else if (LOG.isLoggable(Level.FINE)) {
        for (Map.Entry<String, String> entry : rolesMap.entrySet()) {
            LOG.fine("Method: " + entry.getKey() + ", roles: " + entry.getValue());
        }
    }
    super.setMethodRolesMap(rolesMap);
}
 
Example #7
Source File: ClientProviderFactory.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
protected void setProviders(boolean custom, boolean busGlobal, Object... providers) {
    List<ProviderInfo<? extends Object>> theProviders =
        prepareProviders(custom, busGlobal, providers, null);
    super.setCommonProviders(theProviders);
    for (ProviderInfo<? extends Object> provider : theProviders) {
        Class<?> providerCls = ClassHelper.getRealClass(getBus(), provider.getProvider());
        if (providerCls == Object.class) {
            // If the provider is a lambda, ClassHelper.getRealClass returns Object.class
            providerCls = provider.getProvider().getClass();
        }
        if (filterContractSupported(provider, providerCls, ClientRequestFilter.class)) {
            addProviderToList(clientRequestFilters, provider);
        }

        if (filterContractSupported(provider, providerCls, ClientResponseFilter.class)) {
            addProviderToList(clientResponseFilters, provider);
        }

        if (ResponseExceptionMapper.class.isAssignableFrom(providerCls)) {
            addProviderToList(responseExceptionMappers, provider);
        }

        if (RxInvokerProvider.class.isAssignableFrom(providerCls)) {
            this.rxInvokerProvider = RxInvokerProvider.class.cast(provider.getProvider());
        }
    }
    Collections.sort(clientRequestFilters,
                     new BindingPriorityComparator(ClientRequestFilter.class, true));
    Collections.sort(clientResponseFilters,
                     new BindingPriorityComparator(ClientResponseFilter.class, false));

    injectContextProxies(responseExceptionMappers, clientRequestFilters, clientResponseFilters);
}
 
Example #8
Source File: WebClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static AbstractClient toAbstractClient(Object client) {

        if (client instanceof AbstractClient) {
            return (AbstractClient)client;
        } else if (client instanceof InvocationHandlerAware) {
            return (AbstractClient)((InvocationHandlerAware)client).getInvocationHandler();
        } else {
            Object realObject = ClassHelper.getRealObject(client);
            if (realObject instanceof AbstractClient) {
                return (AbstractClient)realObject;
            }
        }
        return null;
    }
 
Example #9
Source File: MicroProfileClientFactoryBean.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Set<Object> processProviders() {
    Set<Object> providers = new LinkedHashSet<>();
    for (Object provider : configuration.getInstances()) {
        Class<?> providerCls = ClassHelper.getRealClass(getBus(), provider);
        if (provider instanceof ClientRequestFilter || provider instanceof ClientResponseFilter) {
            FilterProviderInfo<Object> filter = new FilterProviderInfo<>(providerCls, providerCls,
                    provider, getBus(), configuration.getContracts(providerCls));
            providers.add(filter);
        } else {
            providers.add(provider);
        }
    }
    return providers;
}
 
Example #10
Source File: MicroProfileClientProviderFactory.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
protected void setProviders(boolean custom, boolean busGlobal, Object... providers) {
    List<ProviderInfo<?>> theProviders =
            prepareProviders(custom, busGlobal, providers, null);
    super.setCommonProviders(theProviders);
    for (ProviderInfo<?> provider : theProviders) {
        Class<?> providerCls = ClassHelper.getRealClass(getBus(), provider.getProvider());

        if (ResponseExceptionMapper.class.isAssignableFrom(providerCls)) {
            addProviderToList(responseExceptionMappers, provider);
        }
        String className = "org.eclipse.microprofile.rest.client.ext.AsyncInvocationInterceptorFactory";
        try {
            
            Class<?> asyncIIFactoryClass = ClassLoaderUtils.loadClass(className,
                                                                      MicroProfileClientProviderFactory.class);
            if (asyncIIFactoryClass.isAssignableFrom(providerCls)) {
                addProviderToList(asyncInvocationInterceptorFactories, provider);
            }
        } catch (ClassNotFoundException ex) {
            // expected if using the MP Rest Client 1.0 APIs
            LOG.log(Level.FINEST, ex, () -> {
                return "Caught ClassNotFoundException - expected if using MP Rest Client 1.0 APIs"; });
        }

    }
    responseExceptionMappers.sort(comparator);
    asyncInvocationInterceptorFactories.sort(comparator);

    injectContextProxies(responseExceptionMappers);
    injectContextProxies(asyncInvocationInterceptorFactories);
}
 
Example #11
Source File: OAuthScopesFilter.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void setSecuredObject(Object object) {
    Class<?> cls = ClassHelper.getRealClass(object);
    checkSecureClass(cls);
    if (scopesMap.isEmpty()) {
        LOG.warning("The scopes map is empty");
    } else if (LOG.isLoggable(Level.FINE)) {
        for (Map.Entry<String, List<String>> entry : scopesMap.entrySet()) {
            LOG.fine("Method: " + entry.getKey() + ", scopes: " + entry.getValue());
        }
    }
}
 
Example #12
Source File: ServerProviderFactory.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Integer getPriority(Collection<?> list, Class<?> cls, Class<?> filterClass) {
    Collection<ProviderInfo<?>> list2 = CastUtils.cast(list);
    for (ProviderInfo<?> p : list2) {
        if (p instanceof FilterProviderInfo) {
            Class<?> pClass = ClassHelper.getRealClass(p.getBus(), p.getProvider());
            if (cls.isAssignableFrom(pClass)) {
                return ((FilterProviderInfo<?>)p).getPriority(filterClass);
            }
        }
    }
    return Priorities.USER;
}
 
Example #13
Source File: ServerProviderFactory.java    From cxf with Apache License 2.0 5 votes vote down vote up
private boolean isRegistered(Collection<?> list, Class<?> cls) {
    Collection<ProviderInfo<?>> list2 = CastUtils.cast(list);
    for (ProviderInfo<?> p : list2) {
        Class<?> pClass = ClassHelper.getRealClass(p.getBus(), p.getProvider());
        if (cls.isAssignableFrom(pClass)) {
            return true;
        }
    }
    return false;
}
 
Example #14
Source File: ServerProviderFactory.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void doApplyDynamicFeatures(ClassResourceInfo cri) {
    Set<OperationResourceInfo> oris = cri.getMethodDispatcher().getOperationResourceInfos();
    for (OperationResourceInfo ori : oris) {
        String nameBinding = DEFAULT_FILTER_NAME_BINDING
            + ori.getClassResourceInfo().getServiceClass().getName()
            + "."
            + ori.getMethodToInvoke().toString();
        for (DynamicFeature feature : dynamicFeatures) {
            FeatureContext featureContext = createServerFeatureContext();
            feature.configure(new ResourceInfoImpl(ori), featureContext);
            Configuration cfg = featureContext.getConfiguration();
            for (Object provider : cfg.getInstances()) {
                Map<Class<?>, Integer> contracts = cfg.getContracts(provider.getClass());
                if (contracts != null && !contracts.isEmpty()) {
                    Class<?> providerCls = ClassHelper.getRealClass(getBus(), provider);
                    registerUserProvider(new FilterProviderInfo<Object>(provider.getClass(),
                        providerCls,
                        provider,
                        getBus(),
                        Collections.singleton(nameBinding),
                        true,
                        contracts));
                    ori.addNameBindings(Collections.singletonList(nameBinding));
                }
            }
        }
    }
    Collection<ClassResourceInfo> subs = cri.getSubResources();
    for (ClassResourceInfo sub : subs) {
        if (sub != cri) {
            doApplyDynamicFeatures(sub);
        }
    }
}
 
Example #15
Source File: ServerProviderFactory.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void injectApplicationIntoFeature(Feature feature) {
    if (application != null) {
        AbstractResourceInfo info = new AbstractResourceInfo(feature.getClass(),
                                                             ClassHelper.getRealClass(feature),
                                                             true,
                                                             true,
                                                             getBus()) {
            @Override
            public boolean isSingleton() {
                return false;
            }
        };
        Method contextMethod = info.getContextMethods().get(Application.class);
        if (contextMethod != null) {
            InjectionUtils.injectThroughMethod(feature, contextMethod, application.getProvider());
            return;
        }
        for (Field contextField : info.getContextFields()) {
            if (Application.class == contextField.getType()) {
                InjectionUtils.injectContextField(info, contextField, feature, application.getProvider());
                break;
            }
        }
        
    }
    
}
 
Example #16
Source File: ProviderFactory.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected static Set<String> getFilterNameBindings(Bus bus, Object provider) {
    Class<?> pClass = ClassHelper.getRealClass(bus, provider);
    Set<String> names = AnnotationUtils.getNameBindings(pClass.getAnnotations());
    if (names.isEmpty()) {
        names = Collections.singleton(DEFAULT_FILTER_NAME_BINDING);
    }
    return names;
}
 
Example #17
Source File: ClassResourceInfo.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void injectContexts(Object resourceObject, OperationResourceInfo ori, Message inMessage) {
    final boolean contextsAvailable = contextsAvailable();
    final boolean paramsAvailable = paramsAvailable();
    if (contextsAvailable || paramsAvailable) {
        Object realResourceObject = ClassHelper.getRealObject(resourceObject);
        if (paramsAvailable) {
            JAXRSUtils.injectParameters(ori, this, realResourceObject, inMessage);
        }
        if (contextsAvailable) {
            InjectionUtils.injectContexts(realResourceObject, this, inMessage);
        }
    }
}
 
Example #18
Source File: InjectionUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static Class<?> getRawResponseClass(Object targetObject) {
    if (targetObject != null) {
        Class<?> targetClass = targetObject.getClass();
        return ClassHelper.getRealClassFromClass(targetClass);
    }
    return null;
}
 
Example #19
Source File: SpringResourceFactory.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void init() {
    type = ClassHelper.getRealClassFromClass(ac.getType(beanId));
    if (Proxy.isProxyClass(type)) {
        type = ClassHelper.getRealClass(ac.getBean(beanId));
    }
    isSingleton = ac.isSingleton(beanId);
    postConstructMethod = ResourceUtils.findPostConstructMethod(type, postConstructMethodName);
    preDestroyMethod = ResourceUtils.findPreDestroyMethod(type, preDestroyMethodName);

    if (isSingleton()) {
        try {
            singletonInstance = ac.getBean(beanId);
        } catch (BeansException ex) {
            // ignore for now, try resolving resource constructor later
        }
        if (singletonInstance != null) {
            return;
        }
    } else {
        isPrototype = ac.isPrototype(beanId);
    }
    c = ResourceUtils.findResourceConstructor(type, !isSingleton());
    if (c == null) {
        throw new RuntimeException("Resource class " + type
                                   + " has no valid constructor");
    }

}
 
Example #20
Source File: AbstractSpringComponentScanServer.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected boolean nonProxyClass(String beanName) {
    // JAX-RS runtime needs to be able to access the real component class to introspect it for
    // JAX-RS annotations; the following check ensures that the valid proxified components
    // are accepted while the client proxies are ignored.
    Class<?> type = ClassHelper.getRealClassFromClass(applicationContext.getType(beanName));
    if (Proxy.isProxyClass(type) && applicationContext.isSingleton(beanName)) {
        type = ClassHelper.getRealClass(applicationContext.getBean(beanName));
    }
    if (Proxy.isProxyClass(type)) {
        LOG.fine("Can not determine the real class of the component '" + beanName + "'");
        return false;
    }
    return true;
}
 
Example #21
Source File: ClaimsAuthorizingInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void setSecuredObject(Object object) {
    Class<?> cls = ClassHelper.getRealClass(object);
    findClaims(cls);
    if (claims.isEmpty()) {
        LOG.warning("The claims list is empty, the service object is not protected");
    }
}
 
Example #22
Source File: CustomJAXRSInvoker.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(Exchange exchange, Object requestParams, Object resourceObject) {

    OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);
    Method m = ori.getMethodToInvoke();
    Class<?> realClass = ClassHelper.getRealClass(exchange.getBus(), resourceObject);

    Principal p = new SecurityContextImpl(exchange.getInMessage()).getUserPrincipal();
    if (realClass == SecureBookStore.class && "getThatBook".equals(m.getName())
        && "baddy".equals(p.getName())) {
        return new MessageContentsList(Response.status(Response.Status.FORBIDDEN).build());
    }

    return super.invoke(exchange, requestParams, resourceObject);
}
 
Example #23
Source File: StratosAuthorizingHandler.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
/**
 * The instance of the secured bean get injected by the IOC framework
 *
 * @param securedObject
 */
public void setSecuredObject(Object securedObject) {
    Class<?> clazz = ClassHelper.getRealClass(securedObject);
    authorizationActionMap = getAuthorizationActionMap(clazz);
    superTenantServiceSet = getSuperTenantServiceSet(clazz);

}
 
Example #24
Source File: CxfJaxrsServiceRegistrator.java    From aries-jax-rs-whiteboard with Apache License 2.0 5 votes vote down vote up
private static Set<String> getFilterNameBindings(
    Bus bus, Object provider) {
    Class<?> pClass = ClassHelper.getRealClass(bus, provider);
    Set<String> names = AnnotationUtils.getNameBindings(
        pClass.getAnnotations());
    if (names.isEmpty()) {
        names = Collections.singleton(DEFAULT_FILTER_NAME_BINDING);
    }
    return names;
}
 
Example #25
Source File: ProviderFactory.java    From cxf with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
protected void setCommonProviders(List<ProviderInfo<? extends Object>> theProviders) {
    List<ProviderInfo<ReaderInterceptor>> readInts =
        new LinkedList<>();
    List<ProviderInfo<WriterInterceptor>> writeInts =
        new LinkedList<>();
    for (ProviderInfo<? extends Object> provider : theProviders) {
        Class<?> providerCls = ClassHelper.getRealClass(bus, provider.getProvider());

        if (filterContractSupported(provider, providerCls, MessageBodyReader.class)) {
            addProviderToList(messageReaders, provider);
        }

        if (filterContractSupported(provider, providerCls, MessageBodyWriter.class)) {
            addProviderToList(messageWriters, provider);
        }

        if (filterContractSupported(provider, providerCls, ContextResolver.class)) {
            addProviderToList(contextResolvers, provider);
        }

        if (ContextProvider.class.isAssignableFrom(providerCls)) {
            addProviderToList(contextProviders, provider);
        }

        if (filterContractSupported(provider, providerCls, ReaderInterceptor.class)) {
            readInts.add((ProviderInfo<ReaderInterceptor>)provider);
        }

        if (filterContractSupported(provider, providerCls, WriterInterceptor.class)) {
            writeInts.add((ProviderInfo<WriterInterceptor>)provider);
        }

        if (filterContractSupported(provider, providerCls, ParamConverterProvider.class)) {
            paramConverters.add((ProviderInfo<ParamConverterProvider>)provider);
        }
    }
    sortReaders();
    sortWriters();
    sortContextResolvers();

    mapInterceptorFilters(readerInterceptors, readInts, ReaderInterceptor.class, true);
    mapInterceptorFilters(writerInterceptors, writeInts, WriterInterceptor.class, true);

    injectContextProxies(messageReaders, messageWriters, contextResolvers, paramConverters,
        readerInterceptors.values(), writerInterceptors.values());
    checkParamConverterContexts();
}
 
Example #26
Source File: ProviderFactory.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected <T> boolean handleMapper(ProviderInfo<T> em,
                                   Class<?> expectedType,
                                   Message m,
                                   Class<?> providerClass,
                                   Class<?> commonBaseClass,
                                   boolean injectContext) {

    Class<?> mapperClass = ClassHelper.getRealClass(bus, em.getProvider());
    Type[] types;
    if (m != null && MessageUtils.getContextualBoolean(m, IGNORE_TYPE_VARIABLES)) {
        types = new Type[]{mapperClass};
    } else {
        types = getGenericInterfaces(mapperClass, expectedType, commonBaseClass);
    }
    for (Type t : types) {
        if (t instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType)t;
            Type[] args = pt.getActualTypeArguments();
            for (Type arg : args) {
                if (arg instanceof TypeVariable) {
                    TypeVariable<?> var = (TypeVariable<?>) arg;
                    Type[] bounds = var.getBounds();
                    boolean isResolved = false;
                    for (Type bound : bounds) {
                        Class<?> cls = InjectionUtils.getRawType(bound);
                        if (cls != null && (cls == Object.class || cls.isAssignableFrom(expectedType))) {
                            isResolved = true;
                            break;
                        }
                    }
                    if (!isResolved) {
                        return false;
                    }
                    if (injectContext) {
                        injectContextValues(em, m);
                    }
                    return true;
                }
                Class<?> actualClass = InjectionUtils.getRawType(arg);
                if (actualClass == null) {
                    continue;
                }
                if (expectedType.isArray() && !actualClass.isArray()) {
                    expectedType = expectedType.getComponentType();
                }
                if (actualClass.isAssignableFrom(expectedType) || actualClass == Object.class) {
                    if (injectContext) {
                        injectContextValues(em, m);
                    }
                    return true;
                }
            }
        } else if (t instanceof Class && providerClass.isAssignableFrom((Class<?>)t)) {
            if (injectContext) {
                injectContextValues(em, m);
            }
            return true;
        }
    }
    return false;
}
 
Example #27
Source File: SpringResourceFactory.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected void initInstance(Message m, Object instance) {
    if (isCallPostConstruct()) {
        InjectionUtils.invokeLifeCycleMethod(ClassHelper.getRealObject(instance), postConstructMethod);
    }
}
 
Example #28
Source File: SingletonResourceProvider.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Class<?> getResourceClass() {
    return ClassHelper.getRealClass(resourceInstance);
}
 
Example #29
Source File: ServerFactoryBean.java    From cxf with Apache License 2.0 4 votes vote down vote up
public Class<?> getServiceBeanClass() {
    if (serviceBean != null) {
        return ClassHelper.getRealClass(getBus(), serviceBean);
    }
    return getServiceFactory().getServiceClass();
}
 
Example #30
Source File: ServerFactoryBean.java    From cxf with Apache License 2.0 4 votes vote down vote up
public Server create() {
    ClassLoaderHolder orig = null;
    try {
        Server server = null;
        try {
            if (bus != null) {
                ClassLoader loader = bus.getExtension(ClassLoader.class);
                if (loader != null) {
                    orig = ClassLoaderUtils.setThreadContextClassloader(loader);
                }
            }

            if (getServiceFactory().getProperties() == null) {
                getServiceFactory().setProperties(getProperties());
            } else if (getProperties() != null) {
                getServiceFactory().getProperties().putAll(getProperties());
            }
            if (serviceBean != null && getServiceClass() == null) {
                setServiceClass(ClassHelper.getRealClass(bus, serviceBean));
            }
            if (invoker != null) {
                getServiceFactory().setInvoker(invoker);
            } else if (serviceBean != null) {
                invoker = createInvoker();
                getServiceFactory().setInvoker(invoker);
            }

            Endpoint ep = createEndpoint();

            getServiceFactory().sendEvent(FactoryBeanListener.Event.PRE_SERVER_CREATE, server, serviceBean,
                                          serviceBean == null
                                          ? getServiceClass() == null
                                              ? getServiceFactory().getServiceClass()
                                              : getServiceClass()
                                          : getServiceClass() == null
                                              ? ClassHelper.getRealClass(getBus(), getServiceBean())
                                              : getServiceClass());

            server = new ServerImpl(getBus(),
                                    ep,
                                    getDestinationFactory(),
                                    getBindingFactory());

            if (ep.getService().getInvoker() == null) {
                if (invoker == null) {
                    ep.getService().setInvoker(createInvoker());
                } else {
                    ep.getService().setInvoker(invoker);
                }
            }

        } catch (EndpointException | BusException | IOException e) {
            throw new ServiceConstructionException(e);
        }

        if (serviceBean != null) {
            Class<?> cls = ClassHelper.getRealClass(getServiceBean());
            if (getServiceClass() == null || cls.equals(getServiceClass())) {
                initializeAnnotationInterceptors(server.getEndpoint(), cls);
            } else {
                initializeAnnotationInterceptors(server.getEndpoint(), cls, getServiceClass());
            }
        } else if (getServiceClass() != null) {
            initializeAnnotationInterceptors(server.getEndpoint(), getServiceClass());
        }

        applyFeatures(server);

        getServiceFactory().sendEvent(FactoryBeanListener.Event.SERVER_CREATED, server, serviceBean,
                                      serviceBean == null
                                      ? getServiceClass() == null
                                          ? getServiceFactory().getServiceClass()
                                          : getServiceClass()
                                      : getServiceClass() == null
                                          ? ClassHelper.getRealClass(getServiceBean())
                                          : getServiceClass());

        if (start) {
            try {
                server.start();
            } catch (RuntimeException re) {
                server.destroy(); // prevent resource leak
                throw re;
            }
        }
        getServiceFactory().reset();

        return server;
    } finally {
        if (orig != null) {
            orig.reset();
        }
    }
}