injections.remove();
}
}
}
ResourceInjector injector = null;
// instantiate and inject resources into service using the app classloader to be sure to get the right InitialContext
implementor = null;
final ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(loader);
try {
final WebBeansContext webBeansContext = WebBeansContext.currentInstance();
final BeanManagerImpl bm = webBeansContext.getBeanManagerImpl();
if (bm.isInUse()) { // try cdi bean
if (JAXWS_AS_CDI_BEANS) {
try {
final Set<Bean<?>> beans = bm.getBeans(instance);
final Bean<?> bean = bm.resolve(beans);
final CreationalContextImpl creationalContext = bm.createCreationalContext(bean);
if (bean != null) {
final Bean<?> oldBean = creationalContext.putBean(bean);
try {
if (AbstractOwbBean.class.isInstance(bean)) {
final AbstractOwbBean<?> aob = AbstractOwbBean.class.cast(bean);
final Producer producer = aob.getProducer();
implementor = producer.produce(creationalContext);
if (producer instanceof InjectionTarget) {
final InjectionTarget injectionTarget = (InjectionTarget) producer;
injectionTarget.inject(implementor, creationalContext);
injector = injectCxfResources(implementor); // we need it before postconstruct
injectionTarget.postConstruct(implementor);
}
if (aob.getScope().equals(Dependent.class)) {
creationalContext.addDependent(aob, instance);
}
}
} finally {
creationalContext.putBean(oldBean);
}
} else {
implementor = bm.getReference(bean, instance, creationalContext);
injector = injectCxfResources(implementor);
}
if (WebBeansUtil.isDependent(bean)) { // should be isPseudoScope but should be ok for jaxws
toClean = creationalContext;
}
} catch (final Exception ie) {
LOGGER.info("Can't use cdi to create " + instance + " webservice: " + ie.getMessage());
}
}
}
if (implementor == null) { // old pojo style
final InjectionProcessor<Object> injectionProcessor = new InjectionProcessor<Object>(instance, port.getInjections(), null, null, unwrap(context), bindings);
injectionProcessor.createInstance();
implementor = injectionProcessor.getInstance();
injector = injectCxfResources(implementor);
if (!JAXWS_AS_CDI_BEANS && bm.isInUse()) {
final CreationalContextImpl creationalContext = bm.createCreationalContext(null);
OWBInjector.inject(bm, implementor, null);
toClean = creationalContext;
}
injector.invokePostConstruct();
}
} catch (final Exception e) {
throw new WebServiceException("Service resource injection failed", e);
} finally {
Thread.currentThread().setContextClassLoader(old);