de.inetsource.jsfforum.config.ViewScope.java Source code

Java tutorial

Introduction

Here is the source code for de.inetsource.jsfforum.config.ViewScope.java

Source

/*
 * Copyright (C) 2014 Joerg Wiesmann joerg.wiesmann@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.inetsource.jsfforum.config;

import java.util.Map;
import javax.faces.context.FacesContext;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;
import org.springframework.web.context.request.FacesRequestAttributes;

/**
 *
 * @author Joerg Wiesmann joerg.wiesmann@gmail.com
 */
public class ViewScope implements Scope {

    public static final String VIEW_SCOPE_CALLBACKS = "viewScope.callbacks";

    @Override
    public synchronized Object get(String name, ObjectFactory<?> objectFactory) {
        Object instance = this.getViewMap().get(name);
        if (instance == null) {
            instance = objectFactory.getObject();
            this.getViewMap().put(name, instance);
        }
        return instance;
    }

    @Override
    public Object remove(String name) {
        Object instance = this.getViewMap().remove(name);
        if (instance == null) {
            Map<String, Runnable> callbacks = (Map<String, Runnable>) this.getViewMap().get(VIEW_SCOPE_CALLBACKS);
            if (callbacks != null) {
                callbacks.remove(name);
            }
        }
        return instance;
    }

    @Override
    public void registerDestructionCallback(String name, Runnable runnable) {
        Map<String, Runnable> callbacks = (Map<String, Runnable>) this.getViewMap().get(VIEW_SCOPE_CALLBACKS);
        if (callbacks != null) {
            callbacks.put(name, runnable);
        }
    }

    @Override
    public Object resolveContextualObject(String key) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        FacesRequestAttributes facesResquestAttributes = new FacesRequestAttributes(facesContext);
        return facesResquestAttributes.resolveReference(key);
    }

    @Override
    public String getConversationId() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        FacesRequestAttributes facesResquestAttributes = new FacesRequestAttributes(facesContext);
        return facesResquestAttributes.getSessionId() + "-" + facesContext.getViewRoot().getViewId();
    }

    private Map<String, Object> getViewMap() {
        return FacesContext.getCurrentInstance().getViewRoot().getViewMap();
    }

}