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

com.sun.faces.lifecycle.LifecycleFactoryImpl 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
 */

// LifecycleFactoryImpl.java

package com.sun.faces.lifecycle;

import static com.sun.faces.util.MessageUtils.CANT_CREATE_LIFECYCLE_ERROR_MESSAGE_ID;
import static com.sun.faces.util.MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID;
import static java.util.logging.Level.FINE;

import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sun.faces.util.FacesLogger;
import com.sun.faces.util.MessageUtils;

import jakarta.faces.FacesException;
import jakarta.faces.context.FacesContext;
import jakarta.faces.lifecycle.Lifecycle;
import jakarta.faces.lifecycle.LifecycleFactory;

/**
 * LifecycleFactoryImpl is the stock implementation of Lifecycle in Mojarra.
 *
 * @see jakarta.faces.lifecycle.LifecycleFactory
 */
public class LifecycleFactoryImpl extends LifecycleFactory {

    // Log instance for this class
    private static Logger LOGGER = FacesLogger.LIFECYCLE.getLogger();

    protected ConcurrentHashMap lifecycleMap;

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

    public LifecycleFactoryImpl() {
        super(null);
        lifecycleMap = new ConcurrentHashMap<>();

        // We must have an implementation under this key.
        lifecycleMap.put(LifecycleFactory.DEFAULT_LIFECYCLE, new LifecycleImpl(FacesContext.getCurrentInstance()));
        LOGGER.fine("Created Default Lifecycle");
    }

    // -------------------------------------------------- Methods from Lifecycle

    @Override
    public void addLifecycle(String lifecycleId, Lifecycle lifecycle) {
        if (lifecycleId == null) {
            throw new NullPointerException(MessageUtils.getExceptionMessageString(MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "lifecycleId"));
        }
        if (lifecycle == null) {
            throw new NullPointerException(MessageUtils.getExceptionMessageString(MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "lifecycle"));
        }
        if (null != lifecycleMap.get(lifecycleId)) {
            Object params[] = { lifecycleId };
            String message = MessageUtils.getExceptionMessageString(MessageUtils.LIFECYCLE_ID_ALREADY_ADDED_ID, params);
            if (LOGGER.isLoggable(Level.WARNING)) {
                LOGGER.warning(MessageUtils.getExceptionMessageString(MessageUtils.LIFECYCLE_ID_ALREADY_ADDED_ID, params));
            }
            throw new IllegalArgumentException(message);
        }
        lifecycleMap.put(lifecycleId, lifecycle);
        if (LOGGER.isLoggable(FINE)) {
            LOGGER.fine("addedLifecycle: " + lifecycleId + " " + lifecycle);
        }
    }

    @Override
    public Lifecycle getLifecycle(String lifecycleId) throws FacesException {
        if (lifecycleId == null) {
            throw new NullPointerException(MessageUtils.getExceptionMessageString(NULL_PARAMETERS_ERROR_MESSAGE_ID, "lifecycleId"));
        }

        if (lifecycleMap.get(lifecycleId) == null) {
            Object[] params = { lifecycleId };
            String message = MessageUtils.getExceptionMessageString(CANT_CREATE_LIFECYCLE_ERROR_MESSAGE_ID, params);
            throw new IllegalArgumentException(message);
        }

        Lifecycle result = lifecycleMap.get(lifecycleId);

        if (LOGGER.isLoggable(FINE)) {
            LOGGER.fine("getLifecycle: " + lifecycleId + " " + result);
        }
        return result;
    }

    @Override
    public Iterator getLifecycleIds() {
        return lifecycleMap.keySet().iterator();
    }

// The testcase for this class is TestLifecycleFactoryImpl.java

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy