use of org.apache.aries.jax.rs.whiteboard.internal.utils.ServiceTuple in project aries-jax-rs-whiteboard by apache.
the class Whiteboard method createBus.
private ExtensionManagerBus createBus(Map<String, ServiceTuple<Object>> extensions) {
BundleWiring wiring = _bundleContext.getBundle().adapt(BundleWiring.class);
Map<String, Object> properties = new HashMap<>(_configurationMap);
properties.putIfAbsent("replace.loopback.address.with.localhost", "false");
HashMap<Class<?>, Object> cxfExtensions = new HashMap<>();
if (extensions.isEmpty()) {
cxfExtensions = null;
} else {
for (Map.Entry<String, ServiceTuple<Object>> entry : extensions.entrySet()) {
String className = entry.getKey();
ServiceTuple<Object> serviceTuple = entry.getValue();
ClassLoader classLoader = getClassLoader(serviceTuple);
try {
Class<?> clazz = classLoader.loadClass(className);
cxfExtensions.put(clazz, serviceTuple.getService());
} catch (Exception e) {
if (_log.isErrorEnabled()) {
_log.error("Could not load extension for CXF bus", e);
}
}
}
}
if (_log.isDebugEnabled()) {
_log.debug("Creating CXF Bus with extensions {} and properties {}", extensions, properties);
}
ExtensionManagerBus bus = new ExtensionManagerBus(cxfExtensions, properties, wiring.getClassLoader());
bus.initialize();
if (_log.isDebugEnabled()) {
_log.debug("Created CXF Bus with extensions {} and properties {}", extensions, properties);
}
return bus;
}
use of org.apache.aries.jax.rs.whiteboard.internal.utils.ServiceTuple in project aries-jax-rs-whiteboard by apache.
the class Whiteboard method getResourceProvider.
private static <T> OSGi<? extends ResourceProvider> getResourceProvider(ServiceTuple<T> serviceTuple) {
CachingServiceReference<T> cachingServiceReference = serviceTuple.getCachingServiceReference();
String scope = Utils.getString(cachingServiceReference.getProperty("service.scope"));
if (Objects.equals(scope, "prototype")) {
boolean applicationScoped = Boolean.parseBoolean(getString(cachingServiceReference.getProperty("org.apache.aries.jax.rs.whiteboard.application.scoped")));
if (applicationScoped) {
return just(serviceTuple.getServiceObjects().getService()).effects(__ -> {
}, serviceTuple.getServiceObjects()::ungetService).map(service -> new SingletonServiceReferenceResourceProvider(cachingServiceReference, service));
} else {
return just(new PrototypeServiceReferenceResourceProvider(cachingServiceReference, serviceTuple.getService().getClass(), serviceTuple.getServiceObjects())).effects(__ -> {
}, __ -> serviceTuple.dispose(), __ -> {
}, __ -> {
});
}
} else {
return just(new SingletonServiceReferenceResourceProvider(serviceTuple.getCachingServiceReference(), serviceTuple.getService()));
}
}
use of org.apache.aries.jax.rs.whiteboard.internal.utils.ServiceTuple in project aries-jax-rs-whiteboard by apache.
the class CxfJaxrsServiceRegistrator method doRewire.
private synchronized void doRewire(Collection<ServiceTuple<?>> providers, Collection<ResourceProvider> services) {
if (!_enabled) {
return;
}
if (!_applicationTuple.isAvailable()) {
_applicationTuple.dispose();
return;
}
if (_server != null) {
_server.destroy();
_applicationTuple.refresh();
for (ServiceTuple<?> provider : providers) {
provider.refresh();
}
}
Application application = _applicationTuple.getService();
if (application == null) {
return;
}
if (_services.isEmpty() && application.getSingletons().isEmpty() && application.getClasses().isEmpty()) {
return;
}
_jaxRsServerFactoryBean = createEndpoint(application, JAXRSServerFactoryBean.class);
_jaxRsServerFactoryBean.setInvoker(new PromiseAwareJAXRSInvoker());
_jaxRsServerFactoryBean.setBus(_bus);
_bus.setExtension(context -> {
ConfigurableImpl<FeatureContext> configurable = new ConfigurableImpl<>(context, RuntimeType.SERVER);
configurable.property("osgi.jaxrs.application.serviceProperties", _properties);
return configurable;
}, ServerConfigurableFactory.class);
_jaxRsServerFactoryBean.setStart(false);
List<org.apache.cxf.feature.Feature> features = new ArrayList<>();
for (ServiceTuple<?> provider : providers) {
CachingServiceReference<?> cachingServiceReference = provider.getCachingServiceReference();
if (!provider.isAvailable()) {
continue;
}
Object service = provider.getService();
if (service == null) {
continue;
}
if (service instanceof Feature || service instanceof DynamicFeature) {
_jaxRsServerFactoryBean.setProvider(service);
continue;
} else if (service instanceof org.apache.cxf.feature.Feature) {
features.add((org.apache.cxf.feature.Feature) service);
continue;
}
Class<?> realClass = ClassHelper.getRealClass(getBus(), service);
Class<?>[] interfaces = Arrays.stream(canonicalize(cachingServiceReference.getProperty("objectClass"))).filter(SUPPORTED_EXTENSION_INTERFACES::containsKey).map(SUPPORTED_EXTENSION_INTERFACES::get).toArray(Class[]::new);
Map<Class<?>, Integer> classesWithPriorities = Arrays.stream(interfaces).collect(toMap(c -> c, __ -> AnnotationUtils.getBindingPriority(realClass)));
_jaxRsServerFactoryBean.setProvider(new ServiceReferenceFilterProviderInfo<>(cachingServiceReference, realClass, realClass, service, getBus(), getFilterNameBindings(getBus(), service), false, classesWithPriorities));
}
_jaxRsServerFactoryBean.setProvider(new SseEventSinkContextProvider());
_jaxRsServerFactoryBean.setProvider(new SseContextProvider());
_jaxRsServerFactoryBean.setProvider(new ContextProvider<ApplicationClasses>() {
@Override
public ApplicationClasses createContext(Message message) {
return () -> {
return Stream.concat(StreamSupport.stream(getStaticResourceClasses().spliterator(), false), _services.stream().map(ResourceProvider::getResourceClass)).collect(toSet());
};
}
});
if (!features.isEmpty()) {
features.addAll(_jaxRsServerFactoryBean.getFeatures());
_jaxRsServerFactoryBean.setFeatures(features);
}
for (ResourceProvider resourceProvider : services) {
if (resourceProvider instanceof PrototypeServiceReferenceResourceProvider) {
PrototypeServiceReferenceResourceProvider provider = (PrototypeServiceReferenceResourceProvider) resourceProvider;
if (!provider.isAvailable()) {
continue;
}
}
_jaxRsServerFactoryBean.setResourceProvider(resourceProvider);
}
if (_jaxRsServerFactoryBean.getResourceClasses().isEmpty()) {
return;
}
ComparableResourceComparator comparableResourceComparator = new ComparableResourceComparator();
_jaxRsServerFactoryBean.setResourceComparator(comparableResourceComparator);
ProviderInfoClassComparator providerInfoClassComparator = new ProviderInfoClassComparator(Object.class);
_jaxRsServerFactoryBean.setProviderComparator(new ServiceReferenceProviderInfoComparator(providerInfoClassComparator));
_server = _jaxRsServerFactoryBean.create();
Endpoint endpoint = _server.getEndpoint();
ApplicationInfo applicationInfo = (ApplicationInfo) endpoint.get(Application.class.getName());
applicationInfo.setOverridingProps(new HashMap<String, Object>() {
{
put("osgi.jaxrs.application.serviceProperties", _properties);
}
});
endpoint.put("org.apache.cxf.jaxrs.resource.context.provider", createResourceContextProvider(_jaxRsServerFactoryBean.getServiceFactory()));
_server.start();
}
Aggregations