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);
}
}
}
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;
}
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;
}
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());
}
}
}
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;
}
Aggregations