All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.weld.manager.SimpleInjectionTarget Maven / Gradle / Ivy

There is a newer version: 3.0.0.Alpha1
Show newest version
/*
 * 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.manager;

import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.exceptions.IllegalStateException;
import org.jboss.weld.exceptions.WeldException;
import org.jboss.weld.injection.ConstructorInjectionPoint;
import org.jboss.weld.injection.FieldInjectionPoint;
import org.jboss.weld.injection.InjectionContextImpl;
import org.jboss.weld.injection.MethodInjectionPoint;
import org.jboss.weld.injection.WeldInjectionPoint;
import org.jboss.weld.introspector.WeldClass;
import org.jboss.weld.introspector.WeldMethod;
import org.jboss.weld.logging.messages.BeanMessage;
import org.jboss.weld.util.Beans;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.jboss.weld.logging.messages.BeanManagerMessage.MISSING_BEAN_CONSTRUCTOR_FOUND;
import static org.jboss.weld.logging.messages.BeanMessage.INVOCATION_ERROR;

/**
 * @author pmuir
 */
public class SimpleInjectionTarget implements InjectionTarget {

    private final BeanManagerImpl beanManager;
    private final WeldClass type;
    private final ConstructorInjectionPoint constructor;
    private final List>> injectableFields;
    private final List>> initializerMethods;
    private final List> postConstructMethods;
    private final List> preDestroyMethods;
    private final Set injectionPoints;
    private final Set> ejbInjectionPoints;
    private final Set> persistenceContextInjectionPoints;
    private final Set> persistenceUnitInjectionPoints;
    private final Set> resourceInjectionPoints;

    public SimpleInjectionTarget(WeldClass type, BeanManagerImpl beanManager) {
        this.beanManager = beanManager;
        this.type = type;
        this.injectionPoints = new HashSet();
        if (type.getJavaClass().isInterface()) {
            throw new DefinitionException(BeanMessage.INJECTION_TARGET_CANNOT_BE_CREATED_FOR_INTERFACE, type);
        }
        ConstructorInjectionPoint constructor = null;
        try {
            constructor = Beans.getBeanConstructor(null, type);
            this.injectionPoints.addAll(Beans.getParameterInjectionPoints(null, constructor));
        } catch (Exception e) {
            // this means the bean of a type that cannot be produce()d, but that is
            // non-fatal
            // unless someone calls produce()
        }
        this.constructor = constructor;
        this.injectableFields = Beans.getFieldInjectionPoints(null, type);
        this.injectionPoints.addAll(Beans.getFieldInjectionPoints(null, this.injectableFields));
        this.initializerMethods = Beans.getInitializerMethods(null, type);
        this.injectionPoints.addAll(Beans.getParameterInjectionPoints(null, initializerMethods));
        this.postConstructMethods = Beans.getPostConstructMethods(type);
        this.preDestroyMethods = Beans.getPreDestroyMethods(type);
        this.ejbInjectionPoints = Beans.getEjbInjectionPoints(null, type, beanManager);
        this.persistenceContextInjectionPoints = Beans.getPersistenceContextInjectionPoints(null, type, beanManager);
        this.persistenceUnitInjectionPoints = Beans.getPersistenceUnitInjectionPoints(null, type, beanManager);
        this.resourceInjectionPoints = Beans.getResourceInjectionPoints(null, type, beanManager);
    }

    public T produce(CreationalContext ctx) {
        if (constructor == null) {
            // this means we couldn't find a constructor on instantiation, which
            // means there isn't one that's spec-compliant
            // try again so the correct DefinitionException is thrown
            Beans.getBeanConstructor(null, type);
            // should not be reached
            throw new IllegalStateException(MISSING_BEAN_CONSTRUCTOR_FOUND);
        }
        return constructor.newInstance(beanManager, ctx);
    }

    public void inject(final T instance, final CreationalContext ctx) {
        new InjectionContextImpl(beanManager, this, getType(), instance) {

            public void proceed() {
                Beans.injectEEFields(instance, beanManager, ejbInjectionPoints, persistenceContextInjectionPoints, persistenceUnitInjectionPoints, resourceInjectionPoints);
                Beans.injectFieldsAndInitializers(instance, ctx, beanManager, injectableFields, initializerMethods);
            }

        }.run();

    }

    public void postConstruct(T instance) {
        for (WeldMethod method : postConstructMethods) {
            if (method != null) {
                try {
                    // note: RI supports injection into @PreDestroy
                    method.invoke(instance);
                } catch (Exception e) {
                    throw new WeldException(INVOCATION_ERROR, e, method, instance);
                }
            }
        }
    }

    public void preDestroy(T instance) {
        for (WeldMethod method : preDestroyMethods) {
            if (method != null) {
                try {
                    // note: RI supports injection into @PreDestroy
                    method.invoke(instance);
                } catch (Exception e) {
                    throw new WeldException(INVOCATION_ERROR, e, method, instance);
                }
            }
        }
    }

    public void dispose(T instance) {
        // No-op
    }

    public Set getInjectionPoints() {
        return injectionPoints;
    }

    protected WeldClass getType() {
        return type;
    }

    protected BeanManagerImpl getBeanManager() {
        return beanManager;
    }

    protected List>> getInjectableFields() {
        return injectableFields;
    }

    protected List>> getInitializerMethods() {
        return initializerMethods;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy