org.apache.openejb.cdi.WebappBeanManager Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.openejb.cdi;
import org.apache.openejb.util.reflection.Reflections;
import org.apache.webbeans.component.BuiltInOwbBean;
import org.apache.webbeans.component.ExtensionBean;
import org.apache.webbeans.component.OwbBean;
import org.apache.webbeans.config.WebBeansContext;
import org.apache.webbeans.container.BeanManagerImpl;
import org.apache.webbeans.context.creational.CreationalContextImpl;
import org.apache.webbeans.event.EventMetadataImpl;
import org.apache.webbeans.util.Asserts;
import org.apache.webbeans.util.WebBeansUtil;
import javax.el.ELResolver;
import javax.el.ExpressionFactory;
import javax.enterprise.context.spi.Context;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.UnsatisfiedResolutionException;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.enterprise.inject.spi.InterceptionType;
import javax.enterprise.inject.spi.Interceptor;
import javax.enterprise.inject.spi.ObserverMethod;
import javax.enterprise.inject.spi.PassivationCapable;
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
public class WebappBeanManager extends BeanManagerImpl {
private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
private static final ThreadLocal USE_PARENT_BM = new ThreadLocal<>();
private final WebappWebBeansContext webappCtx;
private final InheritedBeanFilter filter;
private Set> deploymentBeans;
private boolean started/* = false*/;
public WebappBeanManager(final WebappWebBeansContext ctx) {
super(ctx);
webappCtx = ctx;
deploymentBeans = super.getBeans(); // use the parent one while starting
Reflections.set(this, "injectionResolver", new WebAppInjectionResolver(ctx));
filter = new InheritedBeanFilter(this);
}
@Override
public void fireEvent(final Object event, final EventMetadataImpl metadata, final boolean isLifecycleEvent) {
getNotificationManager().fireEvent(event, metadata, isLifecycleEvent);
if (isEvent(event)) {
final BeanManagerImpl parentBm = getParentBm();
if (parentBm != null) {
parentBm.getNotificationManager().fireEvent(event, metadata, isLifecycleEvent);
}
}
}
@Override
public List> resolveInterceptors(final InterceptionType type, final Annotation... interceptorBindings) {
final List> interceptors = super.resolveInterceptors(type, interceptorBindings);
final List> parentInterceptors = getParentBm().resolveInterceptors(type, interceptorBindings);
for (final Interceptor> i : parentInterceptors) {
if (!interceptors.contains(i)) {
interceptors.add(i);
}
}
return interceptors;
}
@Override
public Set> resolveObserverMethods(final T event, final EventMetadataImpl metadata) {
final Set> set = new HashSet<>();
set.addAll(getNotificationManager().resolveObservers(event, metadata, false));
if (isEvent(event)) {
final BeanManagerImpl parentBm = getParentBm();
if (parentBm != null) {
set.addAll(parentBm.getNotificationManager().resolveObservers(event, metadata, false));
}
} // else nothing since extensions are loaded by classloader so we already have it
return set;
}
@Override
public Object getInjectableReference(final InjectionPoint injectionPoint, final CreationalContext> ctx) {
Asserts.assertNotNull(injectionPoint, "injectionPoint parameter");
if(injectionPoint == null) {
return null;
}
final BeanManagerImpl parentBm = getParentBm();
final Boolean existing = USE_PARENT_BM.get();
if (existing != null && existing) { // shortcut the whole logic to keep the threadlocal set up correctly
if (parentBm == null) {
return null;
}
return parentBm.getInjectableReference(injectionPoint, ctx);
}
// we can do it cause there is caching but we shouldn't - easy way to overide OWB actually
final Bean
© 2015 - 2025 Weber Informatics LLC | Privacy Policy