use of javax.enterprise.inject.spi.BeanManager in project tomee by apache.
the class LazyRealm method instance.
private Realm instance() {
if (delegate == null) {
synchronized (this) {
if (delegate == null) {
final Object instance;
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (container != null && container.getLoader() != null && container.getLoader().getClassLoader() != null) {
cl = container.getLoader().getClassLoader();
}
final Class<?> clazz;
try {
clazz = cl.loadClass(realmClass);
} catch (final ClassNotFoundException e) {
throw new TomEERuntimeException(e);
}
if (!cdi) {
try {
final ObjectRecipe recipe = new ObjectRecipe(clazz);
recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
recipe.allow(Option.FIELD_INJECTION);
recipe.allow(Option.PRIVATE_PROPERTIES);
if (properties != null) {
final Properties props = new PropertiesAdapter().unmarshal(properties.trim().replaceAll("\\p{Space}*(\\p{Alnum}*)=", "\n$1="));
recipe.setAllProperties(props);
}
instance = recipe.create();
} catch (final Exception e) {
throw new TomEERuntimeException(e);
}
} else {
final WebBeansContext webBeansContext;
try {
webBeansContext = WebBeansContext.currentInstance();
if (webBeansContext == null) {
return null;
}
} catch (final IllegalStateException ise) {
// too early to have a cdi bean, skip these methods - mainly init() but @PostConstruct works then
return null;
}
final BeanManager bm = webBeansContext.getBeanManagerImpl();
final Set<Bean<?>> beans = bm.getBeans(clazz);
final Bean<?> bean = bm.resolve(beans);
if (bean == null) {
return null;
}
creationalContext = bm.createCreationalContext(null);
instance = bm.getReference(bean, clazz, creationalContext);
}
if (instance == null) {
throw new TomEERuntimeException("realm can't be retrieved from cdi");
}
if (instance instanceof Realm) {
delegate = (Realm) instance;
delegate.setContainer(container);
delegate.setCredentialHandler(credentialHandler);
if (Lifecycle.class.isInstance(delegate)) {
if (init) {
try {
final Lifecycle lifecycle = Lifecycle.class.cast(delegate);
lifecycle.init();
if (start) {
lifecycle.start();
}
} catch (final LifecycleException e) {
// no-op
}
}
}
} else {
delegate = new LowTypedRealm(instance);
delegate.setContainer(container);
delegate.setCredentialHandler(credentialHandler);
}
for (final PropertyChangeListener listener : support.getPropertyChangeListeners()) {
delegate.addPropertyChangeListener(listener);
}
}
}
}
return delegate;
}
use of javax.enterprise.inject.spi.BeanManager in project meecrowave by apache.
the class NoConnectorTest method run.
@Test
public void run() {
final Meecrowave.Builder config = new Meecrowave.Builder();
config.setSkipHttp(true);
try (final Meecrowave meecrowave = new Meecrowave(config.includePackages(NoConnectorTest.class.getName())).bake()) {
final BeanManager beanManager = CDI.current().getBeanManager();
assertEquals("yeah", SomeBean.class.cast(beanManager.getReference(beanManager.resolve(beanManager.getBeans(SomeBean.class)), SomeBean.class, beanManager.createCreationalContext(null))).get());
IntStream.of(config.getHttpPort(), config.getHttpsPort()).forEach(port -> {
try (final Socket socket = new Socket("localhost", port)) {
fail("port " + port + " is opened");
} catch (final IOException e) {
// ok
}
});
assertEquals(0, meecrowave.getTomcat().getService().findConnectors().length);
}
}
use of javax.enterprise.inject.spi.BeanManager in project meecrowave by apache.
the class MeecrowaveClientLifecycleListenerTest method autoClose.
@Test
public void autoClose() throws IOException, NoSuchFieldException, IllegalAccessException {
try (final Meecrowave meecrowave = new Meecrowave(new Meecrowave.Builder().randomHttpPort().includePackages(MeecrowaveClientLifecycleListenerTest.class.getName())).bake()) {
final JohnzonCdiExtension johnzonCdiExtension = CDI.current().select(JohnzonCdiExtension.class).get();
final Field jsonbs = JohnzonCdiExtension.class.getDeclaredField("jsonbs");
jsonbs.setAccessible(true);
final BeanManager beanManager = CDI.current().getBeanManager();
final JohnzonCdiExtension extensionInstance = JohnzonCdiExtension.class.cast(beanManager.getContext(ApplicationScoped.class).get(beanManager.resolve(beanManager.getBeans(JohnzonCdiExtension.class))));
final Collection<?> o = Collection.class.cast(jsonbs.get(extensionInstance));
{
// ensure server is init whatever test suite we run in
final Client client = ClientBuilder.newClient();
get(meecrowave, client);
client.close();
}
final int origin = o.size();
final Client client = ClientBuilder.newClient();
final JsonbJaxrsProvider<?> provider = new JsonbJaxrsProvider<>();
client.register(provider);
get(meecrowave, client);
assertEquals(origin + 1, o.size());
client.close();
assertEquals(origin, o.size());
}
}
use of javax.enterprise.inject.spi.BeanManager in project meecrowave by apache.
the class EntityManagerBean method init.
void init(final PersistenceUnitInfo info, final BeanManager bm) {
final PersistenceProvider provider;
try {
provider = PersistenceProvider.class.cast(Thread.currentThread().getContextClassLoader().loadClass(info.getPersistenceProviderClassName()).newInstance());
} catch (final InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new IllegalArgumentException("Bad provider: " + info.getPersistenceProviderClassName());
}
final EntityManagerFactory factory = provider.createContainerEntityManagerFactory(info, new HashMap() {
{
put("javax.persistence.bean.manager", bm);
if (ValidationMode.NONE != info.getValidationMode()) {
ofNullable(findValidatorFactory(bm)).ifPresent(factory -> put("javax.persistence.validation.factory", factory));
}
}
});
instanceFactory = synchronization == SynchronizationType.SYNCHRONIZED ? factory::createEntityManager : () -> factory.createEntityManager(synchronization);
}
use of javax.enterprise.inject.spi.BeanManager in project meecrowave by apache.
the class Injector method inject.
public static CreationalContext<?> inject(final Object testInstance) {
if (testInstance == null) {
return null;
}
final BeanManager bm = CDI.current().getBeanManager();
final AnnotatedType<?> annotatedType = bm.createAnnotatedType(testInstance.getClass());
final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
final CreationalContext<?> creationalContext = bm.createCreationalContext(null);
injectionTarget.inject(testInstance, creationalContext);
return creationalContext;
}
Aggregations