Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2013, 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.ejb;
 
 import java.util.Set;
 
 

The spec requires that an javax.enterprise.inject.spi.InjectionPoint of a session bean returns:

  • a javax.enterprise.inject.spi.Bean object representing the session bean if the session bean is a contextual instance (obtained using javax.inject.Inject)
  • null if the session bean is a non-contextual instance obtained using

    Author(s):
    Jozef Hartinger
    EJB:
    or from JNDI

Each time a contextual instance of a session bean is created we add its bean class to a thread local collection. The class is removed from the collection after instance creation.

If an javax.enterprise.inject.spi.InjectionPoint of a session bean is about to be injected using org.jboss.weld.bean.builtin.InjectionPointBean and the session bean class is present in the thread local collection (indicating that it is a contextual instance), we leave the javax.enterprise.inject.spi.InjectionPoint untouched and inject it. Otherwise, we wrap the javax.enterprise.inject.spi.InjectionPoint within SessionBeanInjectionPoint.NonContextualSessionBeanInjectionPoint which always returns null from getBean().

The only known limitation of this approach is that if the same session bean is used both in its contextual and non-contextual form within a single dependency chain (non-contextual instance of Foo injecting another Foo instance using javax.inject.Inject), the behavior will not be reliable.

 
 public class SessionBeanInjectionPoint {
 
     private static final ThreadLocal<Set<Class<?>>> CONTEXTUAL_SESSION_BEANS = new ThreadLocal<Set<Class<?>>>() {
         @Override
         protected Set<Class<?>> initialValue() {
             return new HashSet<Class<?>>();
         }
     };
 
     private SessionBeanInjectionPoint() {
     }

    
Indicates that a contextual instance of a session bean is about to be constructed.
 
     public static void registerContextualInstance(EjbDescriptor<?> descriptor) {
         .get().add(descriptor.getBeanClass());
     }

    
Indicates that contextual session bean instance has been constructed.
 
     public static void unregisterContextualInstance(EjbDescriptor<?> descriptor) {
         Set<Class<?>> classes = .get();
         classes.remove(descriptor.getBeanClass());
         if (classes.isEmpty()) {
             .remove();
         }
     }

    
Returns the javax.enterprise.inject.spi.InjectionPoint passed in as a parameter if this javax.enterprise.inject.spi.InjectionPoint belongs to a contextual instance of a session bean. Otherwise, the method wraps the javax.enterprise.inject.spi.InjectionPoint to guarantee that getBean() always returns null.
 
     public static InjectionPoint wrapIfNecessary(InjectionPoint ip) {
        if (ip.getBean() instanceof SessionBean<?>) {
            if (!.get().contains(ip.getBean().getBeanClass())) {
                return new NonContextualSessionBeanInjectionPoint(ip);
            }
        }
        return ip;
    }
    private static class NonContextualSessionBeanInjectionPoint extends ForwardingInjectionPoint implements Serializable {
        private static final long serialVersionUID = 6338875301221129389L;
        private final InjectionPoint delegate;
        public NonContextualSessionBeanInjectionPoint(InjectionPoint delegate) {
            this. = delegate;
        }
        @Override
        protected InjectionPoint delegate() {
            return ;
        }
        @Override
        public Bean<?> getBean() {
            return null;
        }
    }
New to GrepCode? Check out our FAQ X