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

com.sun.faces.cdi.CdiProducer Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

package com.sun.faces.cdi;

import static java.util.Arrays.asList;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static java.util.Collections.unmodifiableSet;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;

import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.enterprise.inject.spi.InjectionPoint;
import jakarta.enterprise.inject.spi.PassivationCapable;
import jakarta.faces.context.FacesContext;

/**
 * An abstract base class used by the CDI producers for some common functionality.
 *
 * @since 2.3
 */
abstract class CdiProducer implements Bean, PassivationCapable, Serializable {

    /**
     * Serialization version
     */
    private static final long serialVersionUID = 1L;

    private String id = this.getClass().getName();
    private String name;
    private Class beanClass = Object.class;
    private Set types = singleton(Object.class);
    private Set qualifiers = unmodifiableSet(asSet(new DefaultAnnotationLiteral(), new AnyAnnotationLiteral()));
    private Class scope = Dependent.class;
    private Function, T> create;

    /**
     * Get the ID of this particular instantiation of the producer.
     * 

* This is an implementation detail of CDI, where it wants to relocate a particular producer in order to re-inject a * value. This is typically used in combination with passivation. Note that this is NOT about the value we're producing, * but about the producer itself. * * @return the ID of this particular instantiation of the producer */ @Override public String getId() { return id; } @Override public String getName() { return name; } @Override public Class getBeanClass() { return beanClass; } @Override public Set getTypes() { return types; } /** * Get the default qualifier. * * @return the qualifiers, which in the default case only contains the Default */ @Override public Set getQualifiers() { return qualifiers; } @Override public Class getScope() { return scope; } @Override public T create(CreationalContext creationalContext) { return FacesContext.getCurrentInstance() != null ? create.apply(creationalContext) : null; } /** * Destroy the instance. * *

* Since most artifact that the sub classes are producing are artifacts that the Faces runtime really is managing the * destroy method here does not need to do anything. *

* * @param instance the instance. * @param creationalContext the creational context. */ @Override public void destroy(T instance, CreationalContext creationalContext) { } /** * Get the injection points. * * @return the injection points. */ @Override public Set getInjectionPoints() { return emptySet(); } /** * Get the stereotypes. * * @return the stereotypes. */ @Override public Set> getStereotypes() { return emptySet(); } /** * Is this an alternative. * * @return false. */ @Override public boolean isAlternative() { return false; } // TODO to be removed once using CDI API 4.x public boolean isNullable() { return false; } protected CdiProducer name(String name) { this.name = name; return this; } protected CdiProducer create(Function, T> create) { this.create = create; return this; } protected CdiProducer beanClass(BeanManager beanManager, Class beanClass) { if (CdiUtils.isWeld(beanManager)) { this.beanClass = CdiExtension.class; // See #5457 and #5157 } else { this.beanClass = beanClass; } return this; } protected CdiProducer types(Type... types) { this.types = asSet(types); this.types.add(getClass()); // Add producer class as well so it can at least be filtered from BeanManager#getBeans(). return this; } protected CdiProducer qualifiers(Annotation... qualifiers) { this.qualifiers = asSet(qualifiers); return this; } protected CdiProducer scope(Class scope) { this.scope = scope; return this; } protected CdiProducer addToId(Object object) { id = id + " " + object.toString(); return this; } @SafeVarargs private static Set asSet(T... a) { return new HashSet<>(asList(a)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy