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

org.apache.myfaces.application.ApplicationFactoryImpl Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
 *
 *   http://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.apache.myfaces.application;

import java.util.logging.Level;
import java.util.logging.Logger;

import jakarta.faces.application.Application;
import jakarta.faces.application.ApplicationFactory;
import jakarta.faces.context.ExternalContext;
import jakarta.faces.context.FacesContext;
import org.apache.myfaces.core.api.shared.lang.Assert;
import org.apache.myfaces.util.Purgeable;

/**
 * @author Manfred Geiler (latest modification by $Author$)
 * @author Thomas Spiegl
 * @version $Revision$ $Date$
 */
public class ApplicationFactoryImpl extends ApplicationFactory implements Purgeable
{
    private static final Logger log = Logger.getLogger(ApplicationFactoryImpl.class.getName());

    /**
     * Application is thread-safe (see Application javadoc)
     * "Application represents a per-web-application singleton object..." FactoryFinder has a ClassLoader-Factory Map.
     * Since each webapp has it's own ClassLoader, each webapp will have it's own private factory instances.
     */
    private Application _application;
    
    private boolean _myfacesInstanceAddedToApplicationMap = false;

    public ApplicationFactoryImpl()
    {
        createAndLogNewApplication();
    }

    private void createAndLogNewApplication()
    {
        _application = new ApplicationImpl();
        putApplicationOnMap();
        if (log.isLoggable(Level.FINEST))
        {
            log.finest("New ApplicationFactory instance created");
        }
    }

    @Override
    public void purge()
    {
        createAndLogNewApplication();
    }

    @Override
    public Application getApplication()
    {
        //Put it on ApplicationMap, so jakarta.faces.application.Application
        //class can find it. This allows wrapped jsf 1.1 application instances
        //to work correctly in jsf 1.2 as ri does.
        if (_application != null && !_myfacesInstanceAddedToApplicationMap)
        {
            putApplicationOnMap();
        }

        return _application;
    }

    @Override
    public void setApplication(Application application)
    {
        Assert.notNull(application, "application");
        
        _application = application;
        putApplicationOnMap();
    }
    
    private void putApplicationOnMap()
    {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        if (facesContext != null)
        {
            ExternalContext externalContext = facesContext.getExternalContext();
            if (externalContext != null)
            {
                externalContext.
                    getApplicationMap().put("org.apache.myfaces.application.ApplicationImpl", _application);
                _myfacesInstanceAddedToApplicationMap = true;
            }
        }        
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy