use of org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider in project cxf by apache.
the class RxJava2ObservableServer method run.
protected void run() {
Bus bus = BusFactory.getDefaultBus();
// Make sure default JSONProvider is not loaded
bus.setProperty("skip.default.json.provider.registration", true);
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setInvoker(new ReactiveIOInvoker());
sf.setProvider(new JacksonJsonProvider());
sf.getOutInterceptors().add(new LoggingOutInterceptor());
sf.setResourceClasses(RxJava2ObservableService.class);
sf.setResourceProvider(RxJava2ObservableService.class, new SingletonResourceProvider(new RxJava2ObservableService(), true));
sf.setAddress("http://localhost:" + PORT + "/");
server = sf.create();
}
use of org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider in project cxf by apache.
the class RxJavaObservableServer method run.
protected void run() {
Bus bus = BusFactory.getDefaultBus();
// Make sure default JSONProvider is not loaded
bus.setProperty("skip.default.json.provider.registration", true);
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setProvider(new JacksonJsonProvider());
new ObservableCustomizer().customize(sf);
sf.getOutInterceptors().add(new LoggingOutInterceptor());
sf.setResourceClasses(RxJavaObservableService.class);
sf.setResourceProvider(RxJavaObservableService.class, new SingletonResourceProvider(new RxJavaObservableService(), true));
sf.setAddress("http://localhost:" + PORT + "/");
server = sf.create();
}
use of org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider in project tutorials by eugenp.
the class RestfulServer method main.
public static void main(String[] args) throws Exception {
JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
factoryBean.setResourceClasses(CourseRepository.class);
factoryBean.setResourceProvider(new SingletonResourceProvider(new CourseRepository()));
factoryBean.setAddress("http://localhost:8080/");
Server server = factoryBean.create();
System.out.println("Server ready...");
Thread.sleep(60 * 1000);
System.out.println("Server exiting");
server.destroy();
System.exit(0);
}
use of org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider in project opencast by opencast.
the class RestPublisher method createEndpoint.
/**
* Creates a REST endpoint for the JAX-RS annotated service.
*
* @param ref
* the osgi service reference
* @param service
* The service itself
*/
protected void createEndpoint(ServiceReference<?> ref, Object service) {
String serviceType = (String) ref.getProperty(SERVICE_TYPE_PROPERTY);
String servicePath = (String) ref.getProperty(SERVICE_PATH_PROPERTY);
boolean servicePublishFlag = ref.getProperty(SERVICE_PUBLISH_PROPERTY) == null || Boolean.parseBoolean((String) ref.getProperty(SERVICE_PUBLISH_PROPERTY));
boolean jobProducer = Boolean.parseBoolean((String) ref.getProperty(SERVICE_JOBPRODUCER_PROPERTY));
ServiceRegistration<?> reg = servletRegistrationMap.get(servicePath);
if (reg != null) {
logger.debug("Rest endpoint {} is still registred, skip registering again", servicePath);
return;
}
RestServlet cxf = new RestServlet();
try {
Dictionary<String, Object> props = new Hashtable<>();
props.put(SharedHttpContext.ALIAS, servicePath);
props.put(SharedHttpContext.SERVLET_NAME, service.toString());
props.put(SharedHttpContext.CONTEXT_ID, RestConstants.HTTP_CONTEXT_ID);
props.put(SharedHttpContext.SHARED, "true");
props.put(SERVICE_TYPE_PROPERTY, serviceType);
props.put(SERVICE_PATH_PROPERTY, servicePath);
props.put(SERVICE_PUBLISH_PROPERTY, servicePublishFlag);
props.put(SERVICE_JOBPRODUCER_PROPERTY, jobProducer);
reg = componentContext.getBundleContext().registerService(Servlet.class.getName(), cxf, props);
} catch (Exception e) {
logger.info("Problem registering REST endpoint {} : {}", servicePath, e.getMessage());
return;
}
servletRegistrationMap.put(servicePath, reg);
// Wait for the servlet to be initialized as long as one minute. Since the servlet is published via the whiteboard,
// this may happen asynchronously. However, after 30 seconds we expect the HTTP service and the whiteboard
// implementation to be loaded and active.
int count = 0;
while (!cxf.isInitialized() && count < 300) {
logger.debug("Waiting for the servlet at '{}' to be initialized", servicePath);
try {
Thread.sleep(100);
count++;
} catch (InterruptedException e) {
logger.warn("Interrupt while waiting for RestServlet initialization");
break;
}
}
// Was initialization successful
if (!cxf.isInitialized()) {
logger.error("Whiteboard implemenation failed to pick up REST endpoint declaration {}", serviceType);
return;
}
resourceProviders.put(ref, new SingletonResourceProvider(service));
// Set up cxf
Bus bus = cxf.getBus();
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setBus(bus);
sf.setProviders(providers);
// Set the service class
sf.setResourceProviders(new ArrayList<>(resourceProviders.values()));
sf.setResourceComparator(new OsgiCxfEndpointComparator());
sf.setAddress("/");
BindingFactoryManager manager = sf.getBus().getExtension(BindingFactoryManager.class);
JAXRSBindingFactory factory = new JAXRSBindingFactory();
factory.setBus(sf.getBus());
manager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, factory);
if (server != null) {
logger.debug("Destroying JAX-RS server");
server.stop();
server.destroy();
}
server = sf.create();
logger.info("Registered REST endpoint at " + servicePath);
if (service instanceof RestEndpoint) {
((RestEndpoint) service).endpointPublished();
}
}
use of org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider in project tomee 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;
}
Aggregations