use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project cxf by apache.
the class CdiResourceProviderTest method assertSingleton.
private void assertSingleton() {
final ResourceProvider provider = new SingletonResourceProvider(new Lifecycle(beanManager, bean), Object.class);
assertTrue(new JAXRSCdiResourceExtension().isCxfSingleton(beanManager, bean));
assertTrue(provider.isSingleton());
final Object instance = provider.getInstance(new MessageImpl());
assertNotNull(instance);
assertEquals(instance, provider.getInstance(new MessageImpl()));
}
use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project cxf by apache.
the class JAXRSInvoker method getResourceProvider.
private ResourceProvider getResourceProvider(Exchange exchange) {
Object provider = exchange.remove(JAXRSUtils.ROOT_PROVIDER);
if (provider == null) {
OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);
ClassResourceInfo cri = ori.getClassResourceInfo();
return cri.getResourceProvider();
}
return (ResourceProvider) provider;
}
use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project cxf by apache.
the class JAXRSServerFactoryBean method setResourceProviders.
/**
* Sets the list of providers managing the life-cycle of the resource classes
*
* @param rps resource providers
*/
public void setResourceProviders(List<ResourceProvider> rps) {
for (ResourceProvider rp : rps) {
Class<?> c = rp.getResourceClass();
setServiceClass(c);
resourceProviders.put(c, rp);
}
}
use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project cxf by apache.
the class ResourceUtils method createApplication.
@SuppressWarnings("unchecked")
public static JAXRSServerFactoryBean createApplication(Application app, boolean ignoreAppPath, boolean staticSubresourceResolution, boolean useSingletonResourceProvider, Bus bus) {
Set<Object> singletons = app.getSingletons();
verifySingletons(singletons);
List<Class<?>> resourceClasses = new ArrayList<>();
List<Object> providers = new ArrayList<>();
List<Feature> features = new ArrayList<>();
Map<Class<?>, ResourceProvider> map = new HashMap<>();
// or singleton provider classes
for (Class<?> cls : app.getClasses()) {
if (isValidApplicationClass(cls, singletons)) {
if (isValidProvider(cls)) {
providers.add(createProviderInstance(cls));
} else if (Feature.class.isAssignableFrom(cls)) {
features.add(createFeatureInstance((Class<? extends Feature>) cls));
} else {
resourceClasses.add(cls);
if (useSingletonResourceProvider) {
map.put(cls, new SingletonResourceProvider(createProviderInstance(cls)));
} else {
map.put(cls, new PerRequestResourceProvider(cls));
}
}
}
}
// we can get either a provider or resource class here
for (Object o : singletons) {
if (isValidProvider(o.getClass())) {
providers.add(o);
} else if (o instanceof Feature) {
features.add((Feature) o);
} else {
resourceClasses.add(o.getClass());
map.put(o.getClass(), new SingletonResourceProvider(o));
}
}
JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
if (bus != null) {
bean.setBus(bus);
}
String address = "/";
if (!ignoreAppPath) {
ApplicationPath appPath = locateApplicationPath(app.getClass());
if (appPath != null) {
address = appPath.value();
}
}
if (!address.startsWith("/")) {
address = "/" + address;
}
bean.setAddress(address);
bean.setStaticSubresourceResolution(staticSubresourceResolution);
bean.setResourceClasses(resourceClasses);
bean.setProviders(providers);
bean.getFeatures().addAll(features);
for (Map.Entry<Class<?>, ResourceProvider> entry : map.entrySet()) {
bean.setResourceProvider(entry.getKey(), entry.getValue());
}
Map<String, Object> appProps = app.getProperties();
if (appProps != null) {
bean.getProperties(true).putAll(appProps);
}
bean.setApplication(app);
return bean;
}
use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project cxf by apache.
the class ValidationUtils method getResourceInstance.
public static Object getResourceInstance(Message message) {
final OperationResourceInfo ori = message.getExchange().get(OperationResourceInfo.class);
if (ori == null) {
return null;
}
if (!ori.getClassResourceInfo().isRoot()) {
return message.getExchange().get("org.apache.cxf.service.object.last");
}
final ResourceProvider resourceProvider = ori.getClassResourceInfo().getResourceProvider();
if (!resourceProvider.isSingleton()) {
String error = "Service object is not a singleton, use a custom invoker to validate";
LOG.warning(error);
return null;
}
return resourceProvider.getInstance(message);
}
Aggregations