use of javax.enterprise.context.spi.CreationalContext in project tomee by apache.
the class WebContext method newWeakableInstance.
public <T> Instance newWeakableInstance(final Class<T> beanClass) throws OpenEJBException {
final WebBeansContext webBeansContext = getWebBeansContext();
final ConstructorInjectionBean<Object> beanDefinition = getConstructorInjectionBean(beanClass, webBeansContext);
CreationalContext<Object> creationalContext;
final Object o;
if (webBeansContext == null) {
creationalContext = null;
try {
o = beanClass.newInstance();
} catch (final InstantiationException | IllegalAccessException e) {
throw new OpenEJBException(e);
}
} else {
creationalContext = webBeansContext.getBeanManagerImpl().createCreationalContext(beanDefinition);
o = beanDefinition.create(creationalContext);
}
// Create bean instance
final Context unwrap = InjectionProcessor.unwrap(getInitialContext());
final InjectionProcessor injectionProcessor = new InjectionProcessor(o, injections, unwrap);
final Object beanInstance;
try {
beanInstance = injectionProcessor.createInstance();
if (webBeansContext != null) {
final InjectionTargetBean<Object> bean = InjectionTargetBean.class.cast(beanDefinition);
bean.getInjectionTarget().inject(beanInstance, creationalContext);
if (shouldBeReleased(bean.getScope())) {
creationalContexts.put(beanInstance, creationalContext);
}
}
} catch (final OpenEJBException oejbe) {
if (creationalContext != null) {
creationalContext.release();
}
throw oejbe;
}
return new Instance(beanInstance, creationalContext);
}
use of javax.enterprise.context.spi.CreationalContext in project tomee by apache.
the class WebContext method inject.
public Object inject(final Object o) throws OpenEJBException {
try {
final WebBeansContext webBeansContext = getWebBeansContext();
// Create bean instance
final Context initialContext = (Context) new InitialContext().lookup("java:");
final Context unwrap = InjectionProcessor.unwrap(initialContext);
final InjectionProcessor injectionProcessor = new InjectionProcessor(o, injections, unwrap);
final Object beanInstance = injectionProcessor.createInstance();
if (webBeansContext != null) {
final ConstructorInjectionBean<Object> beanDefinition = getConstructorInjectionBean(o.getClass(), webBeansContext);
final CreationalContext<Object> creationalContext = webBeansContext.getBeanManagerImpl().createCreationalContext(beanDefinition);
final InjectionTargetBean<Object> bean = InjectionTargetBean.class.cast(beanDefinition);
bean.getInjectionTarget().inject(beanInstance, creationalContext);
if (shouldBeReleased(beanDefinition.getScope())) {
creationalContexts.put(beanInstance, creationalContext);
}
}
return beanInstance;
} catch (final NamingException | OpenEJBException e) {
throw new OpenEJBException(e);
}
}
use of javax.enterprise.context.spi.CreationalContext in project Payara by payara.
the class TransactionScopedBeanTest method testAllMethods.
@Test
@SuppressWarnings("unchecked")
public void testAllMethods() {
LocalBean localBean = new LocalBean();
EasyMockSupport mockSupport = new EasyMockSupport();
Contextual<LocalBean> contextual = (Contextual<LocalBean>) mockSupport.createMock(Contextual.class);
CreationalContext<LocalBean> creationalContext = (CreationalContext<LocalBean>) mockSupport.createMock(CreationalContext.class);
TransactionScopedContextImpl transactionScopedContext = mockSupport.createMock(TransactionScopedContextImpl.class);
transactionScopedContext.beansPerTransaction = new ConcurrentHashMap<>();
// test getContextualInstance
TransactionScopedBean<LocalBean> transactionScopedBean = getTransactionScopedBean(mockSupport, localBean, contextual, creationalContext, transactionScopedContext);
assertSame(localBean, transactionScopedBean.getContextualInstance());
// test afterCompletion
contextual.destroy(localBean, creationalContext);
mockSupport.replayAll();
transactionScopedBean.afterCompletion(0);
mockSupport.verifyAll();
mockSupport.resetAll();
}
use of javax.enterprise.context.spi.CreationalContext 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.context.spi.CreationalContext in project kie-wb-common by kiegroup.
the class ResourceChangeIncrementalBuilderWithoutFullBuildTest method testBatchResourceChanges.
@Test
public void testBatchResourceChanges() throws Exception {
final Bean buildChangeListenerBean = (Bean) beanManager.getBeans(org.guvnor.common.services.builder.ResourceChangeIncrementalBuilder.class).iterator().next();
final CreationalContext cc = beanManager.createCreationalContext(buildChangeListenerBean);
final org.guvnor.common.services.builder.ResourceChangeIncrementalBuilder buildChangeListener = (org.guvnor.common.services.builder.ResourceChangeIncrementalBuilder) beanManager.getReference(buildChangeListenerBean, org.guvnor.common.services.builder.ResourceChangeIncrementalBuilder.class, cc);
final URL resourceUrl1 = this.getClass().getResource("/BuildChangeListenerRepo/src/main/resources/add.drl");
final org.uberfire.java.nio.file.Path nioResourcePath1 = fs.getPath(resourceUrl1.toURI());
final Path resourcePath1 = paths.convert(nioResourcePath1);
final URL resourceUrl2 = this.getClass().getResource("/BuildChangeListenerRepo/src/main/resources/update.drl");
final org.uberfire.java.nio.file.Path nioResourcePath2 = fs.getPath(resourceUrl2.toURI());
final Path resourcePath2 = paths.convert(nioResourcePath2);
final URL resourceUrl3 = this.getClass().getResource("/BuildChangeListenerRepo/src/main/resources/delete.drl");
final org.uberfire.java.nio.file.Path nioResourcePath3 = fs.getPath(resourceUrl3.toURI());
final Path resourcePath3 = paths.convert(nioResourcePath3);
final Map<Path, Collection<ResourceChange>> batch = new HashMap<Path, Collection<ResourceChange>>();
batch.put(resourcePath1, new ArrayList<ResourceChange>() {
{
add(new ResourceAdded(""));
}
});
batch.put(resourcePath2, new ArrayList<ResourceChange>() {
{
add(new ResourceUpdated(""));
}
});
batch.put(resourcePath3, new ArrayList<ResourceChange>() {
{
add(new ResourceUpdated(""));
}
});
// batch.add( new ResourceChange( ResourceChangeType.ADD,
// resourcePath1,
// new SessionInfoImpl( "id",
// new IdentityImpl( "user",
// Collections.<Role>emptyList() ) ) ) );
// batch.add( new ResourceChange( ResourceChangeType.UPDATE,
// resourcePath2,
// new SessionInfoImpl( "id",
// new IdentityImpl( "user",
// Collections.<Role>emptyList() ) ) ) );
// batch.add( new ResourceChange( ResourceChangeType.DELETE,
// resourcePath3,
// new SessionInfoImpl( "id",
// new IdentityImpl( "user",
// Collections.<Role>emptyList() ) ) ) );
// Perform incremental build (Without a full Build first)
buildChangeListener.batchResourceChanges(batch);
waitForBuildResults(buildResultsObserver);
final BuildResults buildResults = buildResultsObserver.getBuildResults();
assertNotNull(buildResults);
assertEquals(0, buildResults.getErrorMessages().size());
assertEquals(1, buildResults.getInformationMessages().size());
final IncrementalBuildResults incrementalBuildResults = buildResultsObserver.getIncrementalBuildResults();
assertNull(incrementalBuildResults);
}
Aggregations