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

org.omnifaces.cdi.viewscope.ViewScopeContext Maven / Gradle / Ivy

There is a newer version: 4.4.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.viewscope;

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

import java.lang.annotation.Annotation;

import jakarta.enterprise.context.ContextNotActiveException;
import jakarta.enterprise.context.spi.Context;
import jakarta.enterprise.context.spi.Contextual;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.faces.component.UIViewRoot;
import jakarta.faces.context.FacesContext;

import org.omnifaces.cdi.ViewScoped;

/**
 * Provide a context for the @{@link ViewScoped} annotation wherein beans are managed by
 * {@link ViewScopeManager}.
 *
 * @author Radu Creanga {@literal }
 * @author Bauke Scholtz
 * @see ViewScoped
 * @see ViewScopeManager
 * @since 1.6
 */
public class ViewScopeContext implements Context {

	// Variables ------------------------------------------------------------------------------------------------------

	private ViewScopeManager viewScopeManager;

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

	/**
	 * Returns {@link ViewScoped} class.
	 */
	@Override
	public Class getScope() {
		return ViewScoped.class;
	}

	/**
	 * Returns true if there is a {@link FacesContext}, and it has a {@link UIViewRoot}, and
	 * {@link #isInitialized()} has returned true.
	 */
	@Override
	public boolean isActive() {
		FacesContext context = FacesContext.getCurrentInstance();
		return context != null && context.getViewRoot() != null && isInitialized();
	}

	@Override
	public  T get(Contextual type) {
		checkActive();
		return viewScopeManager.getBean(type);
	}

	@Override
	public  T get(Contextual type, CreationalContext context) {
		T instance = get(type);
		return (instance != null) ? instance : viewScopeManager.createBean(type, context);
	}

	// Helpers --------------------------------------------------------------------------------------------------------

	/**
	 * Check and initialize the view scope manager.
	 * @return true if it has been initialized.
	 */
	private boolean isInitialized() {
		if (viewScopeManager == null) {
			viewScopeManager = getReference(ViewScopeManager.class);
		}

		return viewScopeManager != null;
	}

	/**
	 * Throws {@link ContextNotActiveException} when {@link #isActive()} returns false.
	 * @throws ContextNotActiveException When context is not active.
	 */
	private void checkActive() {
		if (!isActive()) {
			throw new ContextNotActiveException();
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy