Repository: deltaspike Updated Branches: refs/heads/master e3ce1da69 -> 444d3e7a9
DELTASPIKE-769 enable deltaspike interceptors globally per default Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/444d3e7a Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/444d3e7a Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/444d3e7a Branch: refs/heads/master Commit: 444d3e7a9c20bd5a57bd8d64e199267879415fae Parents: e3ce1da Author: gpetracek <[email protected]> Authored: Sun Nov 16 01:21:49 2014 +0100 Committer: gpetracek <[email protected]> Committed: Sun Nov 16 01:21:49 2014 +0100 ---------------------------------------------------------------------- .../interceptor/GlobalInterceptorExtension.java | 94 ++++++++++++++++ .../interceptor/GlobalInterceptorWrapper.java | 106 +++++++++++++++++++ .../javax.enterprise.inject.spi.Extension | 1 + 3 files changed, 201 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/444d3e7a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/interceptor/GlobalInterceptorExtension.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/interceptor/GlobalInterceptorExtension.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/interceptor/GlobalInterceptorExtension.java new file mode 100644 index 0000000..1e4e2eb --- /dev/null +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/interceptor/GlobalInterceptorExtension.java @@ -0,0 +1,94 @@ +/* + * 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.deltaspike.core.impl.interceptor; + +import org.apache.deltaspike.core.spi.activation.Deactivatable; +import org.apache.deltaspike.core.util.ClassDeactivationUtils; +import org.apache.deltaspike.core.util.ClassUtils; +import org.apache.deltaspike.core.util.metadata.AnnotationInstanceProvider; + +import javax.enterprise.event.Observes; +import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.BeforeBeanDiscovery; +import javax.enterprise.inject.spi.Extension; +import javax.enterprise.inject.spi.ProcessAnnotatedType; +import javax.interceptor.Interceptor; +import java.lang.annotation.Annotation; +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Logger; + +//promotes deltaspike interceptors to global interceptors in case of cdi 1.1+ +public class GlobalInterceptorExtension implements Deactivatable, Extension +{ + private static final Logger LOG = Logger.getLogger(GlobalInterceptorExtension.class.getName()); + private static final String DS_PACKAGE_NAME = "org.apache.deltaspike."; + private Annotation priorityAnnotationInstance; + + @SuppressWarnings("UnusedDeclaration") + protected void init(@Observes BeforeBeanDiscovery beforeBeanDiscovery, BeanManager beanManager) + { + if (!ClassDeactivationUtils.isActivated(getClass())) + { + return; + } + + Class<? extends Annotation> priorityAnnotationClass = + ClassUtils.tryToLoadClassForName("javax.annotation.Priority"); + + //check for @Priority and CDI v1.1+ + if (priorityAnnotationClass != null && + ClassUtils.tryToLoadClassForName("javax.enterprise.inject.spi.AfterTypeDiscovery") != null) + { + Map<String, Object> defaultValueMap = new HashMap<String, Object>(); + defaultValueMap.put("value", 0); + priorityAnnotationInstance = AnnotationInstanceProvider.of(priorityAnnotationClass, defaultValueMap); + } + } + + protected void promoteInterceptors(@Observes ProcessAnnotatedType pat, BeanManager beanManager) + { + if (priorityAnnotationInstance == null) //not CDI 1.1 or the extension is deactivated + { + return; + } + + String beanClassName = pat.getAnnotatedType().getJavaClass().getName(); + if (beanClassName.startsWith(DS_PACKAGE_NAME)) + { + if (pat.getAnnotatedType().isAnnotationPresent(Interceptor.class)) + { + //noinspection unchecked + pat.setAnnotatedType(new GlobalInterceptorWrapper(pat.getAnnotatedType(), priorityAnnotationInstance)); + } + //currently not needed, because we don't use our interceptors internally -> check for the future + else if (!beanClassName.contains(".test.")) + { + for (Annotation annotation : pat.getAnnotatedType().getAnnotations()) + { + if (beanManager.isInterceptorBinding(annotation.annotationType())) + { + //once we see this warning we need to introduce double-call prevention logic due to WELD-1780 + LOG.warning(beanClassName + " is an bean from DeltaSpike which is intercepted."); + } + } + } + } + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/444d3e7a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/interceptor/GlobalInterceptorWrapper.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/interceptor/GlobalInterceptorWrapper.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/interceptor/GlobalInterceptorWrapper.java new file mode 100644 index 0000000..17c59c9 --- /dev/null +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/interceptor/GlobalInterceptorWrapper.java @@ -0,0 +1,106 @@ +/* + * 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.deltaspike.core.impl.interceptor; + +import javax.enterprise.inject.spi.AnnotatedType; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +class GlobalInterceptorWrapper implements AnnotatedType<Object> +{ + private final AnnotatedType wrapped; + private Map<Class<? extends Annotation>, Annotation> annotations; + private Set<Annotation> annotationSet; + + GlobalInterceptorWrapper(AnnotatedType wrapped, + Annotation priorityAnnotation) + { + this.wrapped = wrapped; + Set<Annotation> originalAnnotationSet = wrapped.getAnnotations(); + this.annotations = new HashMap<Class<? extends Annotation>, Annotation>(originalAnnotationSet.size()); + + for (Annotation originalAnnotation : originalAnnotationSet) + { + this.annotations.put(originalAnnotation.annotationType(), originalAnnotation); + } + + this.annotations.put(priorityAnnotation.annotationType(), priorityAnnotation); + + this.annotationSet = new HashSet<Annotation>(this.annotations.size()); + this.annotationSet.addAll(this.annotations.values()); + } + + @Override + public Class getJavaClass() + { + return wrapped.getJavaClass(); + } + + @Override + public Set getConstructors() + { + return wrapped.getConstructors(); + } + + @Override + public Set getMethods() + { + return wrapped.getMethods(); + } + + @Override + public Set getFields() + { + return wrapped.getFields(); + } + + @Override + public Type getBaseType() + { + return wrapped.getBaseType(); + } + + @Override + public Set<Type> getTypeClosure() + { + return wrapped.getTypeClosure(); + } + + @Override + public <T extends Annotation> T getAnnotation(Class<T> targetClass) + { + return (T) this.annotations.get(targetClass); + } + + @Override + public Set<Annotation> getAnnotations() + { + return this.annotationSet; + } + + @Override + public boolean isAnnotationPresent(Class<? extends Annotation> targetClass) + { + return this.annotations.containsKey(targetClass); + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/444d3e7a/deltaspike/core/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension b/deltaspike/core/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension index a943098..1c2c7df 100644 --- a/deltaspike/core/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension +++ b/deltaspike/core/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension @@ -23,3 +23,4 @@ org.apache.deltaspike.core.impl.exception.control.extension.ExceptionControlExte org.apache.deltaspike.core.impl.config.ConfigurationExtension org.apache.deltaspike.core.impl.jmx.MBeanExtension org.apache.deltaspike.core.impl.scope.DeltaSpikeContextExtension +org.apache.deltaspike.core.impl.interceptor.GlobalInterceptorExtension \ No newline at end of file
