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;
}
Aggregations