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

org.apache.tapestry5.internal.services.EnvironmentImpl 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 2006, 2007, 2008 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 org.apache.tapestry5.ioc.internal.util.CollectionFactory;
import org.apache.tapestry5.ioc.internal.util.OneShotLock;
import org.apache.tapestry5.ioc.services.ThreadCleanupListener;
import org.apache.tapestry5.services.Environment;
import org.apache.tapestry5.services.EnvironmentalAccess;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * A non-threadsafe implementation (expects to use the "perthread" service lifecyle).
 */
public class EnvironmentImpl implements Environment, ThreadCleanupListener
{

    // My generics mojo breaks down when we talk about the key and the value being related
    // types.

    private final Map typeToStack = CollectionFactory.newMap();

    private final Map typeToAccess = CollectionFactory.newMap();

    private final OneShotLock lock = new OneShotLock();

    @SuppressWarnings("unchecked")
    private  LinkedList stackFor(Class type)
    {
        lock.check();

        LinkedList result = typeToStack.get(type);

        if (result == null)
        {
            result = CollectionFactory.newLinkedList();
            typeToStack.put(type, result);
        }

        return result;
    }

    public  T peek(Class type)
    {
        LinkedList stack = stackFor(type);

        return stack.isEmpty() ? null : stack.getFirst();
    }

    public  T peekRequired(Class type)
    {
        T result = peek(type);

        if (result == null)
        {
            List types = CollectionFactory.newList();
            for (Map.Entry e : typeToStack.entrySet())
            {
                LinkedList list = e.getValue();

                if (list != null && !list.isEmpty()) types.add(e.getKey());
            }

            throw new RuntimeException(ServicesMessages.missingFromEnvironment(type, types));
        }

        return result;
    }

    public  T pop(Class type)
    {
        LinkedList stack = stackFor(type);

        invalidate(type);

        return stack.removeFirst();
    }

    public  T push(Class type, T instance)
    {
        LinkedList stack = stackFor(type);

        T result = stack.isEmpty() ? null : stack.getFirst();

        stack.addFirst(instance);

        invalidate(type);

        return result;
    }

    public void clear()
    {
        lock.check();

        typeToStack.clear();

        for (EnvironmentalAccessImpl closure : typeToAccess.values())
        {
            closure.invalidate();
        }
    }

    public  EnvironmentalAccess getAccess(Class type)
    {
        lock.check();

        EnvironmentalAccessImpl access = typeToAccess.get(type);

        if (access == null)
        {
            access = new EnvironmentalAccessImpl(this, type);
            typeToAccess.put(type, access);
        }

        return access;
    }

    public void threadDidCleanup()
    {
        lock.lock();
    }

    void invalidate(Class type)
    {
        EnvironmentalAccessImpl access = typeToAccess.get(type);

        if (access != null) access.invalidate();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy