org.jboss.weld.bean.InterceptorImpl Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.jboss.weld.bean;
import org.jboss.weld.bean.interceptor.WeldInterceptorClassMetadata;
import org.jboss.weld.bootstrap.api.ServiceRegistry;
import org.jboss.weld.exceptions.DeploymentException;
import org.jboss.weld.exceptions.WeldException;
import org.jboss.weld.interceptor.InterceptorBindingType;
import org.jboss.weld.interceptor.proxy.InterceptorInvocation;
import org.jboss.weld.interceptor.proxy.SimpleInterceptionChain;
import org.jboss.weld.interceptor.reader.ClassMetadataInterceptorReference;
import org.jboss.weld.interceptor.spi.metadata.InterceptorMetadata;
import org.jboss.weld.introspector.WeldClass;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.reflection.Formats;
import javax.enterprise.inject.spi.InterceptionType;
import javax.enterprise.inject.spi.Interceptor;
import javax.interceptor.InvocationContext;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import static org.jboss.weld.logging.messages.BeanMessage.CONFLICTING_INTERCEPTOR_BINDINGS;
import static org.jboss.weld.logging.messages.BeanMessage.MISSING_BINDING_ON_INTERCEPTOR;
/**
* @author Marius Bogoevici
* @author Marko Luksa
*/
public class InterceptorImpl extends ManagedBean implements Interceptor {
private final InterceptorMetadata> interceptorMetadata;
private final Set interceptorBindingTypes;
private final boolean serializable;
public static InterceptorImpl of(WeldClass type, BeanManagerImpl beanManager, ServiceRegistry services) {
return new InterceptorImpl(type, beanManager, services);
}
protected InterceptorImpl(WeldClass type, BeanManagerImpl beanManager, ServiceRegistry services) {
super(type, new StringBuilder().append(Interceptor.class.getSimpleName()).append(BEAN_ID_SEPARATOR).append(type.getName()).toString(), beanManager, services);
this.interceptorMetadata = beanManager.getInterceptorMetadataReader().getInterceptorMetadata(ClassMetadataInterceptorReference.of(WeldInterceptorClassMetadata.of(type)));
this.serializable = type.isSerializable();
this.interceptorBindingTypes = new HashSet();
interceptorBindingTypes.addAll(beanManager.extractAndFlattenInterceptorBindings(getWeldAnnotated().getAnnotations()));
for (Class extends Annotation> annotation : getStereotypes()) {
interceptorBindingTypes.addAll(beanManager.extractAndFlattenInterceptorBindings(beanManager.getStereotypeDefinition(annotation)));
}
if (this.interceptorBindingTypes.size() == 0) {
throw new DeploymentException(MISSING_BINDING_ON_INTERCEPTOR, type.getName());
}
if (Beans.findInterceptorBindingConflicts(interceptorBindingTypes)) {
throw new DeploymentException(CONFLICTING_INTERCEPTOR_BINDINGS, getType());
}
}
public Set getInterceptorBindings() {
return InterceptorBindingType.unwrap(interceptorBindingTypes);
}
public InterceptorMetadata> getInterceptorMetadata() {
return interceptorMetadata;
}
public Object intercept(InterceptionType type, T instance, InvocationContext ctx) {
try {
org.jboss.weld.interceptor.spi.model.InterceptionType interceptionType = org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(type.name());
Collection invocations = new ArrayList();
invocations.add(interceptorMetadata.getInterceptorInvocation(instance, interceptorMetadata, interceptionType));
return new SimpleInterceptionChain(invocations, instance, ctx.getMethod()).invokeNextInterceptor(ctx);
} catch (RuntimeException e) {
throw e;
} catch (Throwable e) {
throw new WeldException(e);
}
}
public boolean intercepts(InterceptionType type) {
return interceptorMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(type.name()));
}
public boolean isSerializable() {
return serializable;
}
@Override
protected void defaultPostConstruct(T instance) {
// Lifecycle callbacks not supported
}
@Override
protected void defaultPreDestroy(T instance) {
// Lifecycle callbacks not supported
}
@Override
public String toString() {
return "Interceptor [" + getBeanClass() + " intercepts " + Formats.formatAnnotations(getInterceptorBindings()) + "]";
}
}