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

org.jboss.weld.bootstrap.events.builder.BeanBuilderImpl Maven / Gradle / Ivy

There is a newer version: 6.0.2.Final
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2016, 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.bootstrap.events.builder;

import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanAttributes;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.PassivationCapable;
import javax.enterprise.inject.spi.builder.BeanConfigurator;

import org.jboss.weld.bean.BeanIdentifiers;
import org.jboss.weld.bootstrap.events.builder.BeanConfiguratorImpl.CreateCallback;
import org.jboss.weld.bootstrap.events.builder.BeanConfiguratorImpl.DestroyCallback;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.bean.ForwardingBeanAttributes;
import org.jboss.weld.util.collections.ImmutableSet;
import org.jboss.weld.util.reflection.Formats;

/**
 *
 * @author Martin Kouba
 *
 * @param 
 */
public class BeanBuilderImpl {
    // implements BeanBuilder {

    private final BeanConfiguratorImpl configurator;

    /**
     *
     * @param configurator
     */
    public BeanBuilderImpl(BeanConfiguratorImpl configurator) {
        this.configurator = configurator;
    }

    // @Override
    public BeanConfigurator configure() {
        return configurator;
    }

    // @Override
    public Bean build() {
        // TODO validate?
        return new ImmutableBean<>(configurator);
    }

    public BeanManagerImpl getBeanManager() {
        return configurator.getBeanManager();
    }

    /**
     *
     * @author Martin Kouba
     *
     * @param  the class of the bean instance
     */
    static class ImmutableBean extends ForwardingBeanAttributes implements Bean, PassivationCapable {

        private final String id;

        private final BeanManagerImpl beanManager;

        private final Class beanClass;

        private final BeanAttributes attributes;

        private final Set injectionPoints;

        private final CreateCallback createCallback;

        private final DestroyCallback destroyCallback;

        /**
         *
         * @param configurator
         */
        ImmutableBean(BeanConfiguratorImpl configurator) {
            this.beanManager = configurator.getBeanManager();
            this.beanClass = configurator.getBeanClass();
            this.attributes = new BeanAttributesBuilderImpl<>(configurator.getAttributes()).build();
            this.injectionPoints = ImmutableSet.copyOf(configurator.getInjectionPoints());
            this.createCallback = configurator.getCreateCallback();
            this.destroyCallback = configurator.getDestroyCallback();
            if (configurator.getId() != null) {
                this.id = configurator.getId();
            } else {
                this.id = BeanIdentifiers.forBuilderBean(attributes, beanClass);
            }
        }

        @Override
        public T create(CreationalContext creationalContext) {
            return createCallback.create(creationalContext, beanManager);
        }

        @Override
        public void destroy(T instance, CreationalContext creationalContext) {
            if (destroyCallback != null) {
                destroyCallback.destroy(instance, creationalContext);
            }
        }

        @Override
        public Class getBeanClass() {
            return beanClass;
        }

        @Override
        public Set getInjectionPoints() {
            return injectionPoints;
        }

        @Override
        public boolean isNullable() {
            return false;
        }

        @Override
        protected BeanAttributes attributes() {
            return attributes;
        }

        @Override
        public String getId() {
            return id;
        }

        @Override
        public String toString() {
            return "Immutable Builder Bean [" + getBeanClass().toString() + ", types: " + Formats.formatTypes(getTypes()) + ", qualifiers: "
                    + Formats.formatAnnotations(getQualifiers()) + "]";
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy