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

org.apache.tapestry5.internal.services.ApplicationStateManagerImpl Maven / Gradle / Ivy

Go to download

Central module for Tapestry, containing interfaces to the Java Servlet API and all core services and components.

There is a newer version: 5.8.6
Show newest version
// Copyright 2007, 2008, 2010 The Apache Software Foundation
//
// 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 org.apache.tapestry5.internal.services;

import static org.apache.tapestry5.ioc.internal.util.CollectionFactory.newConcurrentMap;

import java.util.Map;

import org.apache.tapestry5.ioc.ObjectLocator;
import org.apache.tapestry5.services.ApplicationStateContribution;
import org.apache.tapestry5.services.ApplicationStateCreator;
import org.apache.tapestry5.services.ApplicationStateManager;
import org.apache.tapestry5.services.ApplicationStatePersistenceStrategy;
import org.apache.tapestry5.services.ApplicationStatePersistenceStrategySource;

public class ApplicationStateManagerImpl implements ApplicationStateManager
{
    static final String DEFAULT_STRATEGY = "session";

    static class ApplicationStateAdapter
    {
        private final Class ssoClass;

        private final ApplicationStatePersistenceStrategy strategy;

        private final ApplicationStateCreator creator;

        ApplicationStateAdapter(Class ssoClass, ApplicationStatePersistenceStrategy strategy,
                ApplicationStateCreator creator)
        {
            this.ssoClass = ssoClass;
            this.strategy = strategy;
            this.creator = creator;
        }

        T getOrCreate()
        {
            return strategy.get(ssoClass, creator);
        }

        void set(T sso)
        {
            strategy.set(ssoClass, sso);
        }

        boolean exists()
        {
            return strategy.exists(ssoClass);
        }
    }

    /**
     * The map will be extended periodically as new ASOs, not in the configuration, are encountered.
     * Thut is is thread
     * safe.
     */
    private final Map classToAdapter = newConcurrentMap();

    private final ApplicationStatePersistenceStrategySource source;

    private final ObjectLocator locator;

    @SuppressWarnings("unchecked")
    public ApplicationStateManagerImpl(Map configuration,
            ApplicationStatePersistenceStrategySource source, ObjectLocator locator)
    {
        this.source = source;
        this.locator = locator;

        for (Class asoClass : configuration.keySet())
        {
            ApplicationStateContribution contribution = configuration.get(asoClass);

            ApplicationStateAdapter adapter = newAdapter(asoClass, contribution.getStrategy(),
                    contribution.getCreator());

            classToAdapter.put(asoClass, adapter);
        }

    }

    @SuppressWarnings("unchecked")
    private  ApplicationStateAdapter newAdapter(final Class ssoClass, String strategyName,
            ApplicationStateCreator creator)
    {
        if (creator == null)
        {
            creator = new ApplicationStateCreator()
            {
                public T create()
                {
                    return locator.autobuild("Instantiating instance of SSO class "
                            + ssoClass.getName(), ssoClass);
                }
            };
        }

        ApplicationStatePersistenceStrategy strategy = source.get(strategyName);

        return new ApplicationStateAdapter(ssoClass, strategy, creator);
    }

    @SuppressWarnings("unchecked")
    private  ApplicationStateAdapter getAdapter(Class ssoClass)
    {
        ApplicationStateAdapter result = classToAdapter.get(ssoClass);

        // Not found is completely OK, we'll define it on the fly.

        if (result == null)
        {
            result = newAdapter(ssoClass, DEFAULT_STRATEGY, null);
            classToAdapter.put(ssoClass, result);
        }

        return result;
    }

    public  T get(Class ssoClass)
    {
        return getAdapter(ssoClass).getOrCreate();
    }

    public  T getIfExists(Class ssoClass)
    {
        ApplicationStateAdapter adapter = getAdapter(ssoClass);

        return adapter.exists() ? adapter.getOrCreate() : null;
    }

    public  void set(Class ssoClass, T sso)
    {
        getAdapter(ssoClass).set(sso);
    }

    public  boolean exists(Class ssoClass)
    {
        return getAdapter(ssoClass).exists();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy