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

org.jboss.weld.manager.api.WeldManager Maven / Gradle / Ivy

Go to download

This jar bundles all the bits of Weld and CDI required for running in a Servlet container.

There is a newer version: 6.0.0.Beta4
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, 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.api;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.stream.Collectors;

import javax.enterprise.context.spi.Context;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionTarget;

import org.jboss.weld.bootstrap.api.ServiceRegistry;
import org.jboss.weld.construction.api.WeldCreationalContext;
import org.jboss.weld.context.WeldAlterableContext;
import org.jboss.weld.ejb.spi.EjbDescriptor;
import org.jboss.weld.serialization.spi.BeanIdentifier;

/**
 * Functionality provided by the Weld Manager over and above the {@link BeanManager}.
 *
 * @author Pete Muir
 */
public interface WeldManager extends BeanManager, Serializable {

    /**
     * The injection target for the given EJB, or null if Weld was not given this descriptor in the deployment.
     *
     * This should be used to create and inject contextual EJBs by the EJB container.
     * Can also be used for message driven beans, even though they are non-contextual.
     * {@link #fireProcessInjectionTarget(AnnotatedType)} and
     * {@link #fireProcessInjectionTarget(AnnotatedType, InjectionTarget)}
     * must be used to obtain an {@link InjectionTarget} for non-contextual EJB injection.
     *
     * @param  the type of the EJB
     * @param descriptor the given EJB descriptor
     * @return the injection target for the given EJB
     */
     InjectionTarget createInjectionTarget(EjbDescriptor descriptor);

    /**
     * Get the Bean object for the given EJB, or null if Weld was not given this descriptor in the deployment.
     *
     * @param  the type of the bean
     * @param descriptor the given EJB descriptor
     * @return the Bean object for the given EJB
     */
     Bean getBean(EjbDescriptor descriptor);

    /**
     * Get the EjbDescriptor for the given EJB name
     *
     * @param  the type of the EJB
     * @param ejbName the given EJB name
     * @return the EjbDescriptor for the given EJB name
     */
     EjbDescriptor getEjbDescriptor(String ejbName);

    /**
     * Get the services registered for this manager
     *
     * @return the services registered for this manager
     */
    ServiceRegistry getServices();

    /**
     * Fire a ProcessInjectionTarget event for the given type.
     *
     * A helper method to allow integration code to easily fire the ProcessInjectionTarget for Java EE component classes
     * supporting injection
     *
     * The container must use the returned InjectionTarget to inject the Java EE component.
     *
     * @param  the type
     * @param type the type
     * @return final version of InjectionTarget after all observers have been invoked
     */
     InjectionTarget fireProcessInjectionTarget(AnnotatedType type);

    /**
     * Fire a ProcessInjectionTarget event for the given type.
     *
     * A helper method to allow integration code to easily fire the ProcessInjectionTarget for Java EE component classes
     * supporting injection
     *
     * The container must use the returned InjectionTarget to inject the Java EE component.
     *
     * @param  the type
     * @param annotatedType the type
     * @param injectionTarget the injection target
     * @return final version of InjectionTarget after all observers have been invoked
     */
     InjectionTarget fireProcessInjectionTarget(AnnotatedType annotatedType, InjectionTarget injectionTarget);

    /**
     * The ID of the manager, identical to the ID provided by the BDA
     *
     * @return the ID of the manager
     */
    String getId();

    /**
     * @seeIssue CDI-671
     * @return a new {@link Instance} with required type {@link Object} and no required qualifiers ({@link javax.enterprise.inject.Default} is added automatically during resolution if
     *         no qualifiers are selected)
     */
    Instance instance();

    @Override
     WeldInjectionTargetFactory getInjectionTargetFactory(AnnotatedType type);

    @Override
     WeldCreationalContext createCreationalContext(Contextual contextual);

    Bean getPassivationCapableBean(BeanIdentifier identifier);

    /**
     * Returns a new instance of {@link WeldInjectionTargetBuilder} which can be used to create a new {@link WeldInjectionTarget} for the specified type.
     * @param  the type
     * @param type the specified type
     * @return a new {@link WeldInjectionTargetBuilder} instance for the specified type
     */
     WeldInjectionTargetBuilder createInjectionTargetBuilder(AnnotatedType type);

    /**
     *
     * @return an unwrapped instance (e.g. the delegate in the case of forwarding implementation)
     */
    WeldManager unwrap();

    /**
     * Obtain an {@link AnnotatedType} that may be used to read the annotations of the given class or interface.
     * 

* Allows multiple annotated types, based on the same underlying type, to be created. {@link AnnotatedType}s discovered by the container use the fully * qualified class name to identify the type. *

* This method must only be used when creating non-contextual instances, such as Java EE components. It's not designed to work with contextual instances. *

* If called after the container bootstrap finished, the client code is required to explicitly call {@link #disposeAnnotatedType(Class, String)} * as soon as the specified type should be garbage-collected (to avoid memory leaks). * * @param type * @param id * @return the {@link AnnotatedType} */ AnnotatedType createAnnotatedType(Class type, String id); /** * Dispose the {@link AnnotatedType} created for the identified type. *

* This method should be explicitly called for each result of {@link #createAnnotatedType(Class, String)} created after the container bootstrap finished. *

* It's not necessary to call this method unless the identified type should be a subject of garbage collection. * * @param type * @param id * @see #createAnnotatedType(Class, String) */ void disposeAnnotatedType(Class type, String id); /** * Indicates whether there is an active context for a given scope. * * @throws IllegalStateException if there are multiple active contexts for a given scope * @param scopeType * @return true if there is an active context for a given scope, false otherwise */ boolean isContextActive(Class scopeType); /** * Returns an unmodifiable collection of all registered scopes, both built-in and custom. You can then use {@code BeanManager#getContext()} * to retrieve the {@link Context}. * The method returns scopes regardless of whether their respective contexts are active or otherwise. * * @return All scopes present in the application */ Collection> getScopes(); /** * Returns an unmodifiable collection of all currently active {@link Context}s. This is just a convenient method. * * Note that for each scope, there might be more than one {@link Context}, but there can be at most one active at a time. * * @return Collection of all currently active {@link Context}s */ default Collection getActiveContexts() { return getScopes().stream() .filter(this::isContextActive) .map(this::getContext) .collect(Collectors.toSet()); } /** * Returns an unmodifiable collection of all currently active {@link Context}s that implement {@link WeldAlterableContext}. * This is just a convenient method. * * Note that for each scope, there might be more than one {@link Context}, but there can be at most one active at a time. * This method can therefore return an incomplete view of all active contexts as not every context implements {@link WeldAlterableContext}. * * @return Collection of all active contexts implementing {@link WeldAlterableContext} */ default Collection getActiveWeldAlterableContexts() { return getScopes().stream() .filter(this::isContextActive) .map(this::getContext) .filter(t -> t instanceof WeldAlterableContext) .map(t -> (WeldAlterableContext) t) .collect(Collectors.toSet()); } }