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.injection;
 
 import static org.jboss.weld.util.collections.WeldCollections.immutableSet;
 
 import java.util.List;
 import java.util.Set;
 
 
Factory class that produces ResourceInjection instances for resource fields and setter methods.

Author(s):
Martin Kouba
 
 public final class ResourceInjectionFactory {
 
     private static final ResourceInjectionFactory INSTANCE = new ResourceInjectionFactory();
 
 
     private ResourceInjectionFactory() {
         super();
     }

    
Returns the default ResourceInjectionFactory singleton.

Returns:
the default ResourceInjectionFactory singleton
 
     public static ResourceInjectionFactory instance() {
         return ;
     }

    

Parameters:
declaringBean
type
manager
Returns:
resource injections for the given bean
 
     public List<Set<ResourceInjection<?>>> getResourceInjections(Bean<?> declaringBeanEnhancedAnnotatedType<?> type,
             BeanManagerImpl manager) {
 
         List<Set<ResourceInjection<?>>> result = new ArrayList<Set<ResourceInjection<?>>>();
 
         // Iterate through the type hierarchy
         for (EnhancedAnnotatedType<?> actualType = typeactualType != null && !actualType.getJavaClass().equals(Object.class); actualType = actualType
                 .getEnhancedSuperclass()) {
 
             Set<ResourceInjection<?>> resourceInjections = discoverType(declaringBeanactualTypemanager);
             if (!resourceInjections.isEmpty()) {
                 result.add(resourceInjections);
             }
         }
         Collections.reverse(result);
         return result;
     }

    

Parameters:
fieldInjectionPoint
beanManager
Returns:
the corresponding resource injection for the given field injection point, or null if no such resource injection exists
    public <T, X> ResourceInjection<T> getStaticProducerFieldResourceInjection(FieldInjectionPoint<T, X> fieldInjectionPoint,
            BeanManagerImpl beanManager) {
        ResourceInjection<T> resourceInjection = null;
        for (ResourceInjectionProcessor<?, ?> processor : ) {
            resourceInjection = processor.createStaticProducerFieldResourceInjection(fieldInjectionPointbeanManager);
            if (resourceInjection != null) {
                break;
            }
        }
        return resourceInjection;
    }
        processors.add(new EjbResourceInjectionProcessor());
        processors.add(new PersistenceUnitResourceInjectionProcessor());
        processors.add(new PersistenceContextResourceInjectionProcessor());
        processors.add(new ResourceResourceInjectionProcessor());
        processors.add(new WebServiceResourceInjectionProcessor());
        return processors;
    }

    
Abstract stateless resource injection processor.

Parameters:
<S>
<A>
Author(s):
Martin Kouba
    private abstract class ResourceInjectionProcessor<S extends Service, A extends ApiAbstraction> {

        

Parameters:
fieldInjectionPoint
beanManager
Returns:
ResourceInjection for static producer field or null if required services are not supported or the field is not annotated with the specific marker annotation
See also:
StaticEEResourceProducerField
        protected <T, X> ResourceInjection<T> createStaticProducerFieldResourceInjection(
                FieldInjectionPoint<T, X> fieldInjectionPointBeanManagerImpl beanManager) {
            S injectionServices = getInjectionServices(beanManager);
            A apiAbstraction = getApiAbstraction(beanManager);
            if (injectionServices != null && apiAbstraction != null
                    && fieldInjectionPoint.getAnnotated().isAnnotationPresent(getMarkerAnnotation(apiAbstraction))) {
                return createFieldResourceInjection(fieldInjectionPointinjectionServicesapiAbstraction);
            }
            return null;
        }

        

Parameters:
declaringBean
type
manager
Returns:
the set of ResourceInjections for the specified bean and type
        protected Set<ResourceInjection<?>> createResourceInjections(Bean<?> declaringBeanEnhancedAnnotatedType<?> type,
                BeanManagerImpl manager) {
            S injectionServices = getInjectionServices(manager);
            A apiAbstraction = getApiAbstraction(manager);
            if (injectionServices == null || apiAbstraction == null) {
                return Collections.emptySet();
            }
            Class<? extends Annotationmarker = getMarkerAnnotation(apiAbstraction);
            Set<ResourceInjection<?>> resourceInjections = new ArraySet<ResourceInjection<?>>();
            for (EnhancedAnnotatedField<?, ?> field : type.getDeclaredEnhancedFields(marker)) {
                resourceInjections.add(createFieldResourceInjection(InjectionPointFactory.silentInstance()
                        .createFieldInjectionPoint(fielddeclaringBeantype.getJavaClass(), manager), injectionServices,
                        apiAbstraction));
            }
            for (EnhancedAnnotatedMethod<?, ?> method : type.getDeclaredEnhancedMethods(marker)) {
                if (method.getParameters().size() != 1) {
                    throw ..resourceSetterInjectionNotAJavabean(method);
                }
                resourceInjections.add(createSetterResourceInjection(
                        InjectionPointFactory.silentInstance().createParameterInjectionPoint(
                                method.getEnhancedParameters().get(0), declaringBeantype.getJavaClass(), manager),
                        injectionServicesapiAbstraction));
            }
            return immutableSet(resourceInjections);
        }

        

Parameters:
fieldInjectionPoint
beanManager
Returns:
ResourceInjection for the given field
        private <T, X> ResourceInjection<T> createFieldResourceInjection(FieldInjectionPoint<T, X> fieldInjectionPoint,
                S injectionServices, A apiAbstraction) {
            return new FieldResourceInjection<T, X>(fieldInjectionPoint,
                    Reflections.<ResourceReferenceFactory<T>> cast(getResourceReferenceFactory(fieldInjectionPoint,
                            injectionServicesapiAbstraction)));
        }

        

Parameters:
methodInjectionPoint
beanManager
Returns:
ResourceInjection for the given setter method
        private <T, X> ResourceInjection<T> createSetterResourceInjection(
                ParameterInjectionPoint<T, X> parameterInjectionPoint, S injectionServices, A apiAbstraction) {
            return new SetterResourceInjection<T, X>(parameterInjectionPoint,
                    Reflections.<ResourceReferenceFactory<T>> cast(getResourceReferenceFactory(parameterInjectionPoint,
                            injectionServicesapiAbstraction)));
        }
        protected abstract <T> ResourceReferenceFactory<T> getResourceReferenceFactory(InjectionPoint injectionPoint,
                S injectionServices, A apiAbstraction);
        protected abstract Class<? extends AnnotationgetMarkerAnnotation(A apiAbstraction);
        protected abstract A getApiAbstraction(BeanManagerImpl manager);
        protected abstract S getInjectionServices(BeanManagerImpl manager);
    }

    
EJB resource injection processor.
        @Override
        protected <T> ResourceReferenceFactory<T> getResourceReferenceFactory(InjectionPoint injectionPoint,
                EjbInjectionServices injectionServicesEJBApiAbstraction apiAbstraction) {
            return Reflections.<ResourceReferenceFactory<T>> cast(injectionServices.registerEjbInjectionPoint(injectionPoint));
        }
        @Override
        protected Class<? extends AnnotationgetMarkerAnnotation(EJBApiAbstraction apiAbstraction) {
            return apiAbstraction.EJB_ANNOTATION_CLASS;
        }
        @Override
        protected EJBApiAbstraction getApiAbstraction(BeanManagerImpl manager) {
            return manager.getServices().get(EJBApiAbstraction.class);
        }
        @Override
        protected EjbInjectionServices getInjectionServices(BeanManagerImpl manager) {
            return manager.getServices().get(EjbInjectionServices.class);
        }
    }

    
JPA persistence unit resource injection processor.
    private class PersistenceUnitResourceInjectionProcessor extends
        @Override
        protected <T> ResourceReferenceFactory<T> getResourceReferenceFactory(InjectionPoint injectionPoint,
                JpaInjectionServices injectionServicesPersistenceApiAbstraction apiAbstraction) {
            if (!injectionPoint.getType().equals(apiAbstraction.ENTITY_MANAGER_FACTORY_CLASS)) {
                throw ..invalidResourceProducerType(injectionPoint.getAnnotated(),
                        apiAbstraction.ENTITY_MANAGER_FACTORY_CLASS);
            }
            return Reflections.<ResourceReferenceFactory<T>> cast(injectionServices
                    .registerPersistenceUnitInjectionPoint(injectionPoint));
        }
        @Override
        protected Class<? extends AnnotationgetMarkerAnnotation(PersistenceApiAbstraction apiAbstraction) {
            return apiAbstraction.PERSISTENCE_UNIT_ANNOTATION_CLASS;
        }
        @Override
        protected PersistenceApiAbstraction getApiAbstraction(BeanManagerImpl manager) {
            return manager.getServices().get(PersistenceApiAbstraction.class);
        }
        @Override
        protected JpaInjectionServices getInjectionServices(BeanManagerImpl manager) {
            return manager.getServices().get(JpaInjectionServices.class);
        }
    }

    
JPA persistence context resource injection processor.
    private class PersistenceContextResourceInjectionProcessor extends
        @Override
        protected <T> ResourceReferenceFactory<T> getResourceReferenceFactory(InjectionPoint injectionPoint,
                JpaInjectionServices injectionServicesPersistenceApiAbstraction apiAbstraction) {
            if (!injectionPoint.getType().equals(apiAbstraction.ENTITY_MANAGER_CLASS)) {
                throw ..invalidResourceProducerType(injectionPoint.getAnnotated(),
                        apiAbstraction.ENTITY_MANAGER_CLASS);
            }
            return Reflections.<ResourceReferenceFactory<T>> cast(injectionServices
                    .registerPersistenceContextInjectionPoint(injectionPoint));
        }
        @Override
        protected Class<? extends AnnotationgetMarkerAnnotation(PersistenceApiAbstraction apiAbstraction) {
            return apiAbstraction.PERSISTENCE_CONTEXT_ANNOTATION_CLASS;
        }
        @Override
        protected PersistenceApiAbstraction getApiAbstraction(BeanManagerImpl manager) {
            return manager.getServices().get(PersistenceApiAbstraction.class);
        }
        @Override
        protected JpaInjectionServices getInjectionServices(BeanManagerImpl manager) {
            return manager.getServices().get(JpaInjectionServices.class);
        }
    }

    
Resource injection processor.
    private class ResourceResourceInjectionProcessor extends
        @Override
        protected <T> ResourceReferenceFactory<T> getResourceReferenceFactory(InjectionPoint injectionPoint,
                ResourceInjectionServices injectionServicesEJBApiAbstraction apiAbstraction) {
            return Reflections.<ResourceReferenceFactory<T>> cast(injectionServices
                    .registerResourceInjectionPoint(injectionPoint));
        }
        @Override
        protected Class<? extends AnnotationgetMarkerAnnotation(EJBApiAbstraction apiAbstraction) {
            return apiAbstraction.RESOURCE_ANNOTATION_CLASS;
        }
        @Override
        protected EJBApiAbstraction getApiAbstraction(BeanManagerImpl manager) {
            return manager.getServices().get(EJBApiAbstraction.class);
        }
        @Override
        protected ResourceInjectionServices getInjectionServices(BeanManagerImpl manager) {
            return manager.getServices().get(ResourceInjectionServices.class);
        }
    }

    
JAX-WS resource injection processor.
    private class WebServiceResourceInjectionProcessor extends
        @Override
        protected <T> ResourceReferenceFactory<T> getResourceReferenceFactory(InjectionPoint injectionPoint,
                JaxwsInjectionServices injectionServicesWSApiAbstraction apiAbstraction) {
            return Reflections.<ResourceReferenceFactory<T>> cast(injectionServices
                    .registerWebServiceRefInjectionPoint(injectionPoint));
        }
        @Override
        protected Class<? extends AnnotationgetMarkerAnnotation(WSApiAbstraction apiAbstraction) {
            return apiAbstraction.WEB_SERVICE_REF_ANNOTATION_CLASS;
        }
        @Override
        protected WSApiAbstraction getApiAbstraction(BeanManagerImpl manager) {
            return manager.getServices().get(WSApiAbstraction.class);
        }
        @Override
        protected JaxwsInjectionServices getInjectionServices(BeanManagerImpl manager) {
            return manager.getServices().get(JaxwsInjectionServices.class);
        }
    }

    

Parameters:
bean
type
manager
Returns:
found resource injections for the given bean and type
    private Set<ResourceInjection<?>> discoverType(Bean<?> beanEnhancedAnnotatedType<?> typeBeanManagerImpl manager) {
        Set<ResourceInjection<?>> resourceInjections = new HashSet<ResourceInjection<?>>();
        for (ResourceInjectionProcessor<?, ?> processor : ) {
            resourceInjections.addAll(processor.createResourceInjections(beantypemanager));
        }
        return resourceInjections;
    }
New to GrepCode? Check out our FAQ X