use of org.apache.openejb.assembler.classic.ServiceInfo in project tomee by apache.
the class WebServiceInjectionConfigurator method getServices.
private Collection<ServiceInfo> getServices(final Properties properties) {
final ConfigurationFactory cf = SystemInstance.get().getComponent(ConfigurationFactory.class);
if (cf == null || !ConfigurationFactory.class.isInstance(cf)) {
return Collections.emptyList();
}
final Openejb openejb = new Openejb();
ConfigurationFactory.fillOpenEjb(openejb, properties);
final List<Service> services = openejb.getServices();
if (services.isEmpty()) {
return Collections.emptyList();
}
final Collection<ServiceInfo> info = new ArrayList<>(services.size());
for (final Service s : services) {
final String prefix = s.getId() + ".";
for (final String key : properties.stringPropertyNames()) {
if (key.startsWith(prefix)) {
s.getProperties().put(key.substring(prefix.length()), properties.getProperty(key));
}
}
try {
info.add(cf.configureService(s, ServiceInfo.class));
} catch (final OpenEJBException e) {
throw new IllegalArgumentException(e);
}
}
return info;
}
use of org.apache.openejb.assembler.classic.ServiceInfo in project tomee by apache.
the class CxfEndpoint method configureService.
protected static JaxWsServiceFactoryBean configureService(final JaxWsServiceFactoryBean serviceFactory, final ServiceConfiguration configuration, final String prefix) {
final Properties beanConfig = configuration.getProperties();
if (beanConfig == null || beanConfig.isEmpty()) {
return serviceFactory;
}
final Collection<ServiceInfo> availableServices = configuration.getAvailableServices();
// databinding
final String databinding = beanConfig.getProperty(prefix + CxfUtil.DATABINDING);
if (databinding != null && !databinding.trim().isEmpty()) {
Object instance = ServiceInfos.resolve(availableServices, databinding);
if (instance == null) {
// maybe id == classname
try {
instance = Thread.currentThread().getContextClassLoader().loadClass(databinding).newInstance();
} catch (Exception e) {
// ignore
}
}
if (!DataBinding.class.isInstance(instance)) {
throw new OpenEJBRuntimeException(instance + " is not a " + DataBinding.class.getName() + ", please check configuration of service [id=" + databinding + "]");
}
serviceFactory.setDataBinding((DataBinding) instance);
}
final String wsFeatures = beanConfig.getProperty(prefix + "wsFeatures");
if (wsFeatures != null) {
final Collection<Object> instances = ServiceInfos.resolve(availableServices, wsFeatures.split(" *, *"));
if (instances != null && !instances.isEmpty()) {
final List<WebServiceFeature> features = new ArrayList<>(instances.size());
for (final Object i : instances) {
if (!WebServiceFeature.class.isInstance(i)) {
throw new IllegalArgumentException("Not a WebServiceFeature: " + i);
}
features.add(WebServiceFeature.class.cast(i));
}
serviceFactory.setWsFeatures(features);
}
}
return serviceFactory;
}
use of org.apache.openejb.assembler.classic.ServiceInfo in project tomee by apache.
the class CxfRsHttpListener method configureFactory.
private void configureFactory(final Collection<Object> givenAdditionalProviders, final ServiceConfiguration serviceConfiguration, final JAXRSServerFactoryBean factory, final WebBeansContext ctx) {
if (!"true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.cxf.rs.skip-provider-sorting", "false"))) {
final Comparator<?> providerComparator = findProviderComparator(serviceConfiguration, ctx);
if (providerComparator != null) {
factory.setProviderComparator(providerComparator);
}
}
CxfUtil.configureEndpoint(factory, serviceConfiguration, CXF_JAXRS_PREFIX);
boolean enforceCxfBvalMapper = false;
if (ctx == null || !ctx.getBeanManagerImpl().isInUse()) {
// activate bval
boolean bvalActive = Boolean.parseBoolean(SystemInstance.get().getProperty("openejb.cxf.rs.bval.active", serviceConfiguration.getProperties().getProperty(CXF_JAXRS_PREFIX + "bval.active", "true")));
if (factory.getFeatures() == null && bvalActive) {
factory.setFeatures(new ArrayList<Feature>());
} else if (bvalActive) {
// check we should activate it and user didn't configure it
for (final Feature f : factory.getFeatures()) {
if (BeanValidationFeature.class.isInstance(f)) {
bvalActive = false;
break;
}
}
for (final Interceptor<?> i : factory.getInInterceptors()) {
if (BeanValidationInInterceptor.class.isInstance(i)) {
bvalActive = false;
break;
}
}
for (final Interceptor<?> i : factory.getOutInterceptors()) {
if (BeanValidationOutInterceptor.class.isInstance(i)) {
bvalActive = false;
break;
}
}
}
if (bvalActive) {
// bval doesn't need the actual instance so faking it to avoid to lookup the bean
final BeanValidationProvider provider = new BeanValidationProvider();
final BeanValidationInInterceptor in = new JAXRSBeanValidationInInterceptor() {
@Override
protected Object getServiceObject(final Message message) {
return CxfRsHttpListener.this.getServiceObject(message);
}
};
in.setProvider(provider);
in.setServiceObject(FAKE_SERVICE_OBJECT);
factory.getInInterceptors().add(in);
final BeanValidationOutInterceptor out = new JAXRSBeanValidationOutInterceptor() {
@Override
protected Object getServiceObject(final Message message) {
return CxfRsHttpListener.this.getServiceObject(message);
}
};
out.setProvider(provider);
out.setServiceObject(FAKE_SERVICE_OBJECT);
factory.getOutInterceptors().add(out);
// and add a mapper to get back a 400 like for bval
enforceCxfBvalMapper = true;
}
}
final Collection<ServiceInfo> services = serviceConfiguration.getAvailableServices();
final String staticSubresourceResolution = serviceConfiguration.getProperties().getProperty(CXF_JAXRS_PREFIX + STATIC_SUB_RESOURCE_RESOLUTION_KEY);
if (staticSubresourceResolution != null) {
factory.setStaticSubresourceResolution("true".equalsIgnoreCase(staticSubresourceResolution));
}
// resource comparator
final String resourceComparator = serviceConfiguration.getProperties().getProperty(RESOURCE_COMPARATOR_KEY);
if (resourceComparator != null) {
try {
ResourceComparator instance = (ResourceComparator) ServiceInfos.resolve(services, resourceComparator);
if (instance == null) {
instance = (ResourceComparator) Thread.currentThread().getContextClassLoader().loadClass(resourceComparator).newInstance();
}
factory.setResourceComparator(instance);
} catch (final Exception e) {
LOGGER.error("Can't create the resource comparator " + resourceComparator, e);
}
}
// static resources
final String staticResources = serviceConfiguration.getProperties().getProperty(STATIC_RESOURCE_KEY);
if (staticResources != null) {
final String[] resources = staticResources.split(",");
for (final String r : resources) {
final String trimmed = r.trim();
if (!trimmed.isEmpty()) {
staticResourcesList.add(Pattern.compile(trimmed));
}
}
}
// providers
Set<String> providersConfig = null;
{
final String provider = serviceConfiguration.getProperties().getProperty(PROVIDERS_KEY);
if (provider != null) {
providersConfig = new HashSet<>();
for (final String p : Arrays.asList(provider.split(","))) {
providersConfig.add(p.trim());
}
}
{
if (GLOBAL_PROVIDERS != null) {
if (providersConfig == null) {
providersConfig = new HashSet<>();
}
providersConfig.addAll(Arrays.asList(GLOBAL_PROVIDERS.split(",")));
}
}
}
// another property to configure the scanning of providers but this one is consistent with current cxf config
// the other one is more generic but need another file
final String key = CXF_JAXRS_PREFIX + "skip-provider-scanning";
final boolean ignoreAutoProviders = "true".equalsIgnoreCase(SystemInstance.get().getProperty(key, serviceConfiguration.getProperties().getProperty(key, "false")));
final Collection<Object> additionalProviders = ignoreAutoProviders ? Collections.emptyList() : givenAdditionalProviders;
List<Object> providers = null;
if (providersConfig != null) {
providers = ServiceInfos.resolve(services, providersConfig.toArray(new String[providersConfig.size()]), OpenEJBProviderFactory.INSTANCE);
if (providers != null && additionalProviders != null && !additionalProviders.isEmpty()) {
providers.addAll(providers(serviceConfiguration.getAvailableServices(), additionalProviders, ctx));
}
}
if (providers == null) {
providers = new ArrayList<>(4);
if (additionalProviders != null && !additionalProviders.isEmpty()) {
providers.addAll(providers(serviceConfiguration.getAvailableServices(), additionalProviders, ctx));
}
}
if (!ignoreAutoProviders) {
addMandatoryProviders(providers, serviceConfiguration);
if (enforceCxfBvalMapper) {
if (!shouldSkipProvider(CxfResponseValidationExceptionMapper.class.getName())) {
providers.add(new CxfResponseValidationExceptionMapper());
}
}
}
SystemInstance.get().fireEvent(new ExtensionProviderRegistration(AppFinder.findAppContextOrWeb(Thread.currentThread().getContextClassLoader(), AppFinder.AppContextTransformer.INSTANCE), providers));
if (!providers.isEmpty()) {
factory.setProviders(providers);
}
}
use of org.apache.openejb.assembler.classic.ServiceInfo in project tomee by apache.
the class ServiceInfosTest method createServiceWithResourceAttribute.
@Test
public void createServiceWithResourceAttribute() {
final ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.id = "the-id";
serviceInfo.className = TheService.class.getName();
serviceInfo.properties = new Properties();
serviceInfo.properties.setProperty("dataSource", "@ds");
final TheService instance = TheService.class.cast(ServiceInfos.resolve(asList(serviceInfo), serviceInfo.id));
assertNotNull(instance.dataSource);
}
use of org.apache.openejb.assembler.classic.ServiceInfo in project tomee by apache.
the class CxfUtil method configureBus.
public static void configureBus() {
if (USER_COUNT.incrementAndGet() > 1) {
return;
}
final SystemInstance systemInstance = SystemInstance.get();
final Bus bus = getBus();
// ensure cxf classes are loaded from container to avoid conflicts with app
if ("true".equalsIgnoreCase(systemInstance.getProperty("openejb.cxf.CxfContainerClassLoader", "true"))) {
bus.setExtension(new CxfContainerClassLoader(), ClassLoader.class);
}
// activate jmx, by default isEnabled() == false in InstrumentationManagerImpl
final boolean hasMonitoring = hasMonitoring(systemInstance);
if (hasMonitoring || "true".equalsIgnoreCase(systemInstance.getProperty("openejb.cxf.jmx", "true"))) {
final InstrumentationManager mgr = bus.getExtension(InstrumentationManager.class);
if (InstrumentationManagerImpl.class.isInstance(mgr)) {
// just to keep everything consistent
bus.setExtension(LocalMBeanServer.get(), MBeanServer.class);
final InstrumentationManagerImpl manager = InstrumentationManagerImpl.class.cast(mgr);
manager.setEnabled(true);
manager.setServer(LocalMBeanServer.get());
manager.setDaemon(true);
try {
// avoid to bother our nice logs
LogUtils.getL7dLogger(InstrumentationManagerImpl.class).setLevel(Level.WARNING);
} catch (final Throwable th) {
// no-op
}
// failed when bus was constructed or even if passed we switch the MBeanServer
manager.init();
}
}
if (hasMonitoring) {
new CounterRepository().setBus(bus);
}
final ServiceConfiguration configuration = new ServiceConfiguration(systemInstance.getProperties(), systemInstance.getComponent(OpenEjbConfiguration.class).facilities.services);
final Collection<ServiceInfo> serviceInfos = configuration.getAvailableServices();
Properties properties = configuration.getProperties();
if (properties == null) {
properties = new Properties();
}
final String featuresIds = properties.getProperty(BUS_PREFIX + FEATURES);
if (featuresIds != null) {
final List<Feature> features = createFeatures(serviceInfos, featuresIds);
if (features != null) {
features.addAll(bus.getFeatures());
bus.setFeatures(features);
}
}
final Properties busProperties = ServiceInfos.serviceProperties(serviceInfos, properties.getProperty(BUS_PREFIX + ENDPOINT_PROPERTIES));
if (busProperties != null) {
bus.getProperties().putAll(PropertiesHelper.map(busProperties));
}
configureInterceptors(bus, BUS_PREFIX, serviceInfos, configuration.getProperties());
systemInstance.getProperties().setProperty(BUS_CONFIGURED_FLAG, "true");
systemInstance.fireEvent(new BusCreated(bus));
}
Aggregations