com.freebox.engeneering.application.system.ApplicationScope.java Source code

Java tutorial

Introduction

Here is the source code for com.freebox.engeneering.application.system.ApplicationScope.java

Source

/*
 * Copyright 2012 Lexaden.com
 *
 *   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
 *
 *   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 com.freebox.engeneering.application.system;

import com.vaadin.server.VaadinSession;
import com.vaadin.ui.UI;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * This class defines a vaadin application scope for spring beans.
 */
public class ApplicationScope implements Scope, DisposableBean {

    public static final String NAME = "applicationScope";

    private static final String APPLICATION_SCOPE = NAME + ".";

    private final Map<String, Runnable> destructionCallbacks = new LinkedHashMap<String, Runnable>();

    @Override
    public Object get(String name, ObjectFactory<?> objectFactory) {

        final VaadinSession current = VaadinSession.getCurrent();
        String uiId = getCurrentUiId();
        Object bean = current.getAttribute(APPLICATION_SCOPE + uiId + name);
        if (bean == null) {
            bean = objectFactory.getObject();
            current.setAttribute(APPLICATION_SCOPE + uiId + name, bean);
        }
        return bean;
    }

    @Override
    public Object remove(String name) {
        final VaadinSession current = VaadinSession.getCurrent();

        String uiId = getCurrentUiId();
        Object bean = current.getAttribute(APPLICATION_SCOPE + uiId + name);
        if (bean != null) {
            current.setAttribute(APPLICATION_SCOPE + uiId + name, null);
            this.destructionCallbacks.remove(name);
        }
        return bean;
    }

    private String getCurrentUiId() {
        final UI currentUI = UI.getCurrent();
        String uiId;
        if (currentUI == null || currentUI.getUIId() == -1l) {
            uiId = VaadinSession.getCurrent().getAttribute("applicationScope.UiId").toString();

        } else {
            uiId = currentUI.getUIId() + "";
        }
        return uiId;
    }

    @Override
    public void registerDestructionCallback(String name, Runnable callback) {
        this.destructionCallbacks.put(name, callback);
    }

    @Override
    public Object resolveContextualObject(String key) {
        return null;
    }

    @Override
    public String getConversationId() {
        return null;
    }

    @Override
    public void destroy() throws Exception {
        for (Runnable runnable : this.destructionCallbacks.values()) {
            runnable.run();
        }
        this.destructionCallbacks.clear();
    }
}