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

org.omnifaces.cdi.BeanStorage Maven / Gradle / Ivy

There is a newer version: 4.5.1
Show newest version
/*
 * Copyright OmniFaces
 *
 * 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
 *
 *     https://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.omnifaces.cdi;

import static org.omnifaces.util.Beans.destroy;

import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;

import jakarta.enterprise.context.spi.Contextual;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.PassivationCapable;

/**
 * CDI bean storage. This class is theoretically reusable for multiple CDI scopes. It's currently however only used by
 * the OmniFaces CDI view scope.
 *
 * @author Radu Creanga {@literal }
 * @author Bauke Scholtz
 * @since 1.6
 */
public class BeanStorage implements Serializable {

	// Constants ------------------------------------------------------------------------------------------------------

	private static final long serialVersionUID = 1L;

	// Properties -----------------------------------------------------------------------------------------------------

	private final ConcurrentHashMap beans;

	// Constructors ---------------------------------------------------------------------------------------------------

	/**
	 * Construct a new CDI bean storage with the given initial capacity of the map holding all beans.
	 * @param initialCapacity The initial capacity of the map holding all beans.
	 */
	public BeanStorage(int initialCapacity) {
		beans = new ConcurrentHashMap<>(initialCapacity);
	}

	// Actions --------------------------------------------------------------------------------------------------------

	/**
	 * Create and return the bean associated with given context and creational context.
	 * @param  The generic bean type.
	 * @param type The contextual type of the CDI managed bean.
	 * @param context The context to create the bean in.
	 * @return The bean associated with given context and creational context.
	 * @throws ClassCastException When the bean doesn't implement serializable.
	 */
	public  T createBean(Contextual type, CreationalContext context) {
		T bean = type.create(context);
		beans.put(getBeanId(type), (Serializable) bean);
		return bean;
	}

	/**
	 * Returns the bean associated with the given context, or null if there is none.
	 * @param  The generic bean type.
	 * @param type The contextual type of the CDI managed bean.
	 * @return The bean associated with the given context, or null if there is none.
	 */
	@SuppressWarnings("unchecked")
	public  T getBean(Contextual type) {
		return (T) beans.get(getBeanId(type));
	}

	/**
	 * Returns the bean identifier of the given type.
	 */
	private static String getBeanId(Contextual type) {
		return (type instanceof PassivationCapable) ? ((PassivationCapable) type).getId() : type.getClass().getName();
	}

	/**
	 * Destroy all beans managed so far.
	 */
	public synchronized void destroyBeans() { // Not sure if synchronization is absolutely necessary. Just to be on safe side.
		for (Object bean : beans.values()) {
			destroy(bean);
		}

		beans.clear();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy