Search in sources :

Example 1 with Invoker

use of org.apache.cxf.service.invoker.Invoker in project cxf by apache.

the class AnnotationsFactoryBeanListener method setScope.

private void setScope(Server server, Class<?> cls) {
    FactoryType scope = cls.getAnnotation(FactoryType.class);
    if (scope != null) {
        Invoker i = server.getEndpoint().getService().getInvoker();
        if (i instanceof FactoryInvoker) {
            Factory f;
            if (scope.factoryClass() == FactoryType.DEFAULT.class) {
                switch(scope.value()) {
                    case Session:
                        if (scope.args().length > 0) {
                            f = new SessionFactory(cls, Boolean.parseBoolean(scope.args()[0]));
                        } else {
                            f = new SessionFactory(cls);
                        }
                        break;
                    case PerRequest:
                        f = new PerRequestFactory(cls);
                        break;
                    case Pooled:
                        f = new PooledFactory(cls, Integer.parseInt(scope.args()[0]));
                        break;
                    default:
                        f = new SingletonFactory(cls);
                        break;
                }
            } else {
                try {
                    f = scope.factoryClass().getConstructor(Class.class, String[].class).newInstance(cls, scope.args());
                } catch (Throwable t) {
                    throw new ServiceConstructionException(t);
                }
            }
            ((FactoryInvoker) i).setFactory(f);
        }
    }
}
Also used : SessionFactory(org.apache.cxf.service.invoker.SessionFactory) FactoryInvoker(org.apache.cxf.service.invoker.FactoryInvoker) Invoker(org.apache.cxf.service.invoker.Invoker) PooledFactory(org.apache.cxf.service.invoker.PooledFactory) Factory(org.apache.cxf.service.invoker.Factory) PooledFactory(org.apache.cxf.service.invoker.PooledFactory) PerRequestFactory(org.apache.cxf.service.invoker.PerRequestFactory) SessionFactory(org.apache.cxf.service.invoker.SessionFactory) SingletonFactory(org.apache.cxf.service.invoker.SingletonFactory) FactoryType(org.apache.cxf.annotations.FactoryType) FactoryInvoker(org.apache.cxf.service.invoker.FactoryInvoker) SingletonFactory(org.apache.cxf.service.invoker.SingletonFactory) PerRequestFactory(org.apache.cxf.service.invoker.PerRequestFactory)

Example 2 with Invoker

use of org.apache.cxf.service.invoker.Invoker in project tomee by apache.

the class JAXRSServerFactoryBean method create.

/**
 * Creates the JAX-RS Server instance
 * @return the server
 */
public Server create() {
    ClassLoaderHolder origLoader = null;
    try {
        Bus bus = getBus();
        ClassLoader loader = bus.getExtension(ClassLoader.class);
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        serviceFactory.setBus(bus);
        checkResources(true);
        if (serviceFactory.getService() == null) {
            serviceFactory.create();
        }
        Endpoint ep = createEndpoint();
        getServiceFactory().sendEvent(FactoryBeanListener.Event.PRE_SERVER_CREATE, server);
        server = new ServerImpl(getBus(), ep, getDestinationFactory(), getBindingFactory());
        Invoker invoker = serviceFactory.getInvoker();
        if (invoker == null) {
            ep.getService().setInvoker(createInvoker());
        } else {
            ep.getService().setInvoker(invoker);
        }
        ServerProviderFactory factory = setupFactory(ep);
        ep.put(Application.class.getName(), appProvider);
        factory.setRequestPreprocessor(new RequestPreprocessor(languageMappings, extensionMappings));
        ep.put(Bus.class.getName(), getBus());
        if (documentLocation != null) {
            ep.put(JAXRSUtils.DOC_LOCATION, documentLocation);
        }
        if (rc != null) {
            ep.put("org.apache.cxf.jaxrs.comparator", rc);
        }
        checkPrivateEndpoint(ep);
        applyBusFeatures(getBus());
        applyFeatures();
        updateClassResourceProviders(ep);
        injectContexts(factory, (ApplicationInfo) ep.get(Application.class.getName()));
        factory.applyDynamicFeatures(getServiceFactory().getClassResourceInfo());
        getServiceFactory().sendEvent(FactoryBeanListener.Event.SERVER_CREATED, server, null, null);
        if (start) {
            try {
                server.start();
            } catch (RuntimeException re) {
                if (!(re instanceof ServiceConstructionException && re.getMessage().startsWith("There is an endpoint already running on"))) {
                    // avoid destroying another server on the same endpoint url
                    // prevent resource leak if server really started by itself
                    server.destroy();
                }
                throw re;
            }
        }
    } catch (Exception e) {
        throw new ServiceConstructionException(e);
    } finally {
        if (origLoader != null) {
            origLoader.reset();
        }
    }
    return server;
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) Bus(org.apache.cxf.Bus) RequestPreprocessor(org.apache.cxf.jaxrs.impl.RequestPreprocessor) Endpoint(org.apache.cxf.endpoint.Endpoint) ServerImpl(org.apache.cxf.endpoint.ServerImpl) Invoker(org.apache.cxf.service.invoker.Invoker) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) Application(javax.ws.rs.core.Application) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException)

Example 3 with Invoker

use of org.apache.cxf.service.invoker.Invoker in project cxf by apache.

the class JAXRSServerFactoryBean method create.

/**
 * Creates the JAX-RS Server instance
 * @return the server
 */
public Server create() {
    ClassLoaderHolder origLoader = null;
    try {
        Bus bus = getBus();
        ClassLoader loader = bus.getExtension(ClassLoader.class);
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        serviceFactory.setBus(bus);
        checkResources(true);
        if (serviceFactory.getService() == null) {
            serviceFactory.create();
        }
        Endpoint ep = createEndpoint();
        getServiceFactory().sendEvent(FactoryBeanListener.Event.PRE_SERVER_CREATE, server);
        server = new ServerImpl(getBus(), ep, getDestinationFactory(), getBindingFactory());
        Invoker invoker = serviceFactory.getInvoker();
        if (invoker == null) {
            ep.getService().setInvoker(createInvoker());
        } else {
            ep.getService().setInvoker(invoker);
        }
        ServerProviderFactory factory = setupFactory(ep);
        ep.put(Application.class.getName(), appProvider);
        factory.setRequestPreprocessor(new RequestPreprocessor(languageMappings, extensionMappings));
        ep.put(Bus.class.getName(), getBus());
        if (documentLocation != null) {
            ep.put(JAXRSUtils.DOC_LOCATION, documentLocation);
        }
        if (rc != null) {
            ep.put("org.apache.cxf.jaxrs.comparator", rc);
        }
        checkPrivateEndpoint(ep);
        applyBusFeatures(getBus());
        applyFeatures();
        updateClassResourceProviders(ep);
        injectContexts(factory, (ApplicationInfo) ep.get(Application.class.getName()));
        factory.applyDynamicFeatures(getServiceFactory().getClassResourceInfo());
        getServiceFactory().sendEvent(FactoryBeanListener.Event.SERVER_CREATED, server, null, null);
        if (start) {
            try {
                server.start();
            } catch (RuntimeException re) {
                if (!(re instanceof ServiceConstructionException && re.getMessage().startsWith("There is an endpoint already running on"))) {
                    // avoid destroying another server on the same endpoint url
                    // prevent resource leak if server really started by itself
                    server.destroy();
                }
                throw re;
            }
        }
    } catch (Exception e) {
        throw new ServiceConstructionException(e);
    } finally {
        if (origLoader != null) {
            origLoader.reset();
        }
    }
    return server;
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) Bus(org.apache.cxf.Bus) RequestPreprocessor(org.apache.cxf.jaxrs.impl.RequestPreprocessor) Endpoint(org.apache.cxf.endpoint.Endpoint) ServerImpl(org.apache.cxf.endpoint.ServerImpl) Invoker(org.apache.cxf.service.invoker.Invoker) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) Application(javax.ws.rs.core.Application) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException)

Example 4 with Invoker

use of org.apache.cxf.service.invoker.Invoker in project cxf by apache.

the class ServiceInvokerInterceptor method handleMessage.

public void handleMessage(final Message message) {
    final Exchange exchange = message.getExchange();
    final Endpoint endpoint = exchange.getEndpoint();
    final Service service = endpoint.getService();
    final Invoker invoker = service.getInvoker();
    Runnable invocation = new Runnable() {

        public void run() {
            Exchange runableEx = message.getExchange();
            Object result = invoker.invoke(runableEx, getInvokee(message));
            if (!exchange.isOneWay()) {
                Endpoint ep = exchange.getEndpoint();
                Message outMessage = runableEx.getOutMessage();
                if (outMessage == null) {
                    // perf: size 16 / factor 1 to avoid resize operation
                    outMessage = new MessageImpl(16, 1);
                    outMessage.setExchange(exchange);
                    outMessage = ep.getBinding().createMessage(outMessage);
                    exchange.setOutMessage(outMessage);
                }
                copyJaxwsProperties(message, outMessage);
                if (result != null) {
                    MessageContentsList resList = null;
                    if (result instanceof MessageContentsList) {
                        resList = (MessageContentsList) result;
                    } else if (result instanceof List) {
                        resList = new MessageContentsList((List<?>) result);
                    } else if (result.getClass().isArray()) {
                        resList = new MessageContentsList((Object[]) result);
                    } else {
                        outMessage.setContent(Object.class, result);
                    }
                    if (resList != null) {
                        outMessage.setContent(List.class, resList);
                    }
                }
            }
        }
    };
    Executor executor = getExecutor(endpoint);
    Executor executor2 = exchange.get(Executor.class);
    if (executor2 == executor || executor == null || !(message.getInterceptorChain() instanceof PhaseInterceptorChain)) {
        // already executing on the appropriate executor
        invocation.run();
    } else {
        exchange.put(Executor.class, executor);
        // The current thread holds the lock on PhaseInterceptorChain.
        // In order to avoid the executor threads deadlocking on any of
        // synchronized PhaseInterceptorChain methods the current thread
        // needs to release the chain lock and re-acquire it after the
        // executor thread is done
        final PhaseInterceptorChain chain = (PhaseInterceptorChain) message.getInterceptorChain();
        final AtomicBoolean contextSwitched = new AtomicBoolean();
        final FutureTask<Object> o = new FutureTask<Object>(invocation, null) {

            @Override
            protected void done() {
                super.done();
                if (contextSwitched.get()) {
                    PhaseInterceptorChain.setCurrentMessage(chain, null);
                    message.remove(Message.THREAD_CONTEXT_SWITCHED);
                }
                chain.releaseChain();
            }

            @Override
            public void run() {
                if (PhaseInterceptorChain.setCurrentMessage(chain, message)) {
                    contextSwitched.set(true);
                    message.put(Message.THREAD_CONTEXT_SWITCHED, true);
                }
                synchronized (chain) {
                    super.run();
                }
            }
        };
        synchronized (chain) {
            executor.execute(o);
            // the task will already be done if the executor uses the current thread
            // but the chain lock status still needs to be re-set
            chain.releaseAndAcquireChain();
        }
        try {
            o.get();
        } catch (InterruptedException e) {
            throw new Fault(e);
        } catch (ExecutionException e) {
            if (e.getCause() instanceof RuntimeException) {
                throw (RuntimeException) e.getCause();
            }
            throw new Fault(e.getCause());
        }
    }
}
Also used : PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) Message(org.apache.cxf.message.Message) MessageContentsList(org.apache.cxf.message.MessageContentsList) Service(org.apache.cxf.service.Service) Exchange(org.apache.cxf.message.Exchange) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Executor(java.util.concurrent.Executor) Endpoint(org.apache.cxf.endpoint.Endpoint) Invoker(org.apache.cxf.service.invoker.Invoker) FutureTask(java.util.concurrent.FutureTask) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) MessageImpl(org.apache.cxf.message.MessageImpl)

Example 5 with Invoker

use of org.apache.cxf.service.invoker.Invoker in project cxf by apache.

the class AbstractValidationInterceptor method getServiceObject.

protected Object getServiceObject(Message message) {
    if (serviceObject != null) {
        return serviceObject;
    }
    Object current = message.getExchange().get(Message.SERVICE_OBJECT);
    if (current != null) {
        return current;
    }
    Endpoint e = message.getExchange().getEndpoint();
    if (e != null && e.getService() != null) {
        Invoker invoker = e.getService().getInvoker();
        if (invoker instanceof FactoryInvoker) {
            FactoryInvoker factoryInvoker = (FactoryInvoker) invoker;
            if (factoryInvoker.isSingletonFactory()) {
                return factoryInvoker.getServiceObject(message.getExchange());
            }
        }
    }
    return null;
}
Also used : Endpoint(org.apache.cxf.endpoint.Endpoint) FactoryInvoker(org.apache.cxf.service.invoker.FactoryInvoker) Invoker(org.apache.cxf.service.invoker.Invoker) FactoryInvoker(org.apache.cxf.service.invoker.FactoryInvoker)

Aggregations

Invoker (org.apache.cxf.service.invoker.Invoker)6 Endpoint (org.apache.cxf.endpoint.Endpoint)4 Application (javax.ws.rs.core.Application)2 Bus (org.apache.cxf.Bus)2 ClassLoaderHolder (org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder)2 ServerImpl (org.apache.cxf.endpoint.ServerImpl)2 RequestPreprocessor (org.apache.cxf.jaxrs.impl.RequestPreprocessor)2 ServerProviderFactory (org.apache.cxf.jaxrs.provider.ServerProviderFactory)2 ServiceConstructionException (org.apache.cxf.service.factory.ServiceConstructionException)2 FactoryInvoker (org.apache.cxf.service.invoker.FactoryInvoker)2 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 Executor (java.util.concurrent.Executor)1 FutureTask (java.util.concurrent.FutureTask)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 FactoryType (org.apache.cxf.annotations.FactoryType)1 Exchange (org.apache.cxf.message.Exchange)1 Message (org.apache.cxf.message.Message)1 MessageContentsList (org.apache.cxf.message.MessageContentsList)1 MessageImpl (org.apache.cxf.message.MessageImpl)1