use of javax.enterprise.inject.spi.BeanManager in project meecrowave by apache.
the class JpaExtension method initBeans.
void initBeans(@Observes final AfterDeploymentValidation adv, final BeanManager bm) {
if (entityManagerBeans.isEmpty()) {
return;
}
// only not portable part is this config read, could be optional
final ServletContext sc = ServletContext.class.cast(bm.getReference(bm.resolve(bm.getBeans(ServletContext.class)), ServletContext.class, bm.createCreationalContext(null)));
final Meecrowave.Builder config = Meecrowave.Builder.class.cast(sc.getAttribute("meecrowave.configuration"));
final Map<String, String> props = new HashMap<>();
if (config != null) {
ofNullable(config.getProperties()).ifPresent(p -> p.stringPropertyNames().stream().filter(k -> k.startsWith("jpa.property.")).forEach(k -> props.put(k.substring("jpa.property.".length()), p.getProperty(k))));
}
final Map<String, PersistenceUnitInfo> infoIndex = unitBuilders.stream().map(bean -> {
final CreationalContext<?> cc = bm.createCreationalContext(null);
try {
final Bean<?> resolvedBean = bm.resolve(bm.getBeans(PersistenceUnitInfoBuilder.class, bean.getQualifiers().toArray(new Annotation[bean.getQualifiers().size()])));
final PersistenceUnitInfoBuilder builder = PersistenceUnitInfoBuilder.class.cast(bm.getReference(resolvedBean, PersistenceUnitInfoBuilder.class, cc));
if (builder.getManagedClasses().isEmpty()) {
builder.setManagedClassNames(jpaClasses).setExcludeUnlistedClasses(true);
}
props.forEach(builder::addProperty);
return builder.toInfo();
} finally {
cc.release();
}
}).collect(toMap(PersistenceUnitInfo::getPersistenceUnitName, identity()));
entityManagerBeans.forEach((k, e) -> {
PersistenceUnitInfo info = infoIndex.get(k.unitName);
if (info == null) {
info = tryCreateDefaultPersistenceUnit(k.unitName, bm, props);
}
if (info == null) {
// not valid
adv.addDeploymentProblem(new IllegalArgumentException("Didn't find any PersistenceUnitInfoBuilder for " + k));
} else {
e.init(info, bm);
}
});
}
use of javax.enterprise.inject.spi.BeanManager in project meecrowave by apache.
the class MeecrowaveRuleBase method doInject.
private static CreationalContext<Object> doInject(final Object instance) {
final BeanManager bm = CDI.current().getBeanManager();
final AnnotatedType<?> annotatedType = bm.createAnnotatedType(instance.getClass());
final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
final CreationalContext<Object> creationalContext = bm.createCreationalContext(null);
injectionTarget.inject(instance, creationalContext);
return creationalContext;
}
use of javax.enterprise.inject.spi.BeanManager in project cxf by apache.
the class ClassUnwrapperTest method getBeanReference.
@SuppressWarnings("unchecked")
private <T> T getBeanReference(Class<T> clazz) {
final BeanManager beanManager = lifecycle.getBeanManager();
final Set<Bean<?>> beans = beanManager.getBeans(clazz);
final Bean<?> bean = beanManager.resolve(beans);
return (T) beanManager.getReference(bean, clazz, beanManager.createCreationalContext(bean));
}
use of javax.enterprise.inject.spi.BeanManager in project cxf by apache.
the class CXFCdiServlet method loadBus.
@Override
protected void loadBus(ServletConfig servletConfig) {
Bus bus = null;
final BeanManager beanManager = CDI.current().getBeanManager();
if (beanManager != null) {
final Set<Bean<?>> candidates = beanManager.getBeans(CdiBusBean.CXF);
if (!candidates.isEmpty()) {
final Bean<?> candidate = beanManager.resolve(candidates);
bus = (Bus) beanManager.getReference(candidate, Bus.class, beanManager.createCreationalContext(candidate));
}
}
if (bus != null) {
setBus(bus);
} else {
busCreated = true;
setBus(BusFactory.newInstance().createBus());
}
}
use of javax.enterprise.inject.spi.BeanManager in project tomee by apache.
the class Assembler method deployMBean.
@SuppressWarnings("unchecked")
private void deployMBean(final WebBeansContext wc, final ClassLoader cl, final String mbeanClass, final Properties appMbeans, final String id) {
if (LocalMBeanServer.isJMXActive()) {
final Class<?> clazz;
try {
clazz = cl.loadClass(mbeanClass);
} catch (final ClassNotFoundException e) {
throw new OpenEJBRuntimeException(e);
}
// cdi can be off so init with null bean in this case
final Bean<?> bean;
final BeanManager bm;
if (wc == null) {
bm = null;
bean = null;
} else {
bm = wc.getBeanManagerImpl();
final Set<Bean<?>> beans = bm.getBeans(clazz);
bean = bm.resolve(beans);
}
// create the MBean instance with cdi if possible or manually otherwise
final Object instance;
final CreationalContext creationalContext;
if (bean == null) {
try {
instance = clazz.newInstance();
} catch (final InstantiationException e) {
logger.error("the mbean " + mbeanClass + " can't be registered because it can't be instantiated", e);
return;
} catch (final IllegalAccessException e) {
logger.error("the mbean " + mbeanClass + " can't be registered because it can't be accessed", e);
return;
}
creationalContext = null;
} else {
creationalContext = bm.createCreationalContext(bean);
instance = bm.getReference(bean, clazz, creationalContext);
}
final MBeanServer server = LocalMBeanServer.get();
try {
final MBean annotation = clazz.getAnnotation(MBean.class);
final ObjectName leaf = annotation == null || annotation.objectName().isEmpty() ? new ObjectNameBuilder("openejb.user.mbeans").set("application", id).set("group", clazz.getPackage().getName()).set("name", clazz.getSimpleName()).build() : new ObjectName(annotation.objectName());
server.registerMBean(new DynamicMBeanWrapper(wc, instance), leaf);
appMbeans.put(mbeanClass, leaf.getCanonicalName());
if (creationalContext != null && (bean.getScope() == null || Dependent.class.equals(bean.getScope()))) {
creationalContextForAppMbeans.put(leaf, creationalContext);
}
logger.info("Deployed MBean(" + leaf.getCanonicalName() + ")");
} catch (final Exception e) {
logger.error("the mbean " + mbeanClass + " can't be registered", e);
}
}
}
Aggregations