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

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

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2017 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

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 javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.PassivationCapable;

/**
 * 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 create.apply(creationalContext); } /** * Destroy the instance. * *

* Since most artifact that the sub classes are producing * are artifacts that the JSF 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; } /** * Is this nullable. * * @return false. */ @Override 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(Class beanClass) { this.beanClass = beanClass; return this; } protected CdiProducer types(Type... types) { this.types = asSet(types); return this; } protected CdiProducer beanClassAndType(Class beanClass) { beanClass(beanClass); types(beanClass); 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 protected static Set asSet(T... a) { return new HashSet(asList(a)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy