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

org.apache.tapestry.ioc.internal.util.Orderer Maven / Gradle / Ivy

// Copyright 2006, 2007 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.tapestry.ioc.internal.util;

import org.apache.tapestry.ioc.IdMatcher;
import org.apache.tapestry.ioc.Orderable;
import org.apache.tapestry.ioc.internal.IdMatcherImpl;
import org.apache.tapestry.ioc.internal.OrIdMatcher;
import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newCaseInsensitiveMap;
import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
import org.slf4j.Logger;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Used to order objects into an "execution" order. Each object must have a unique id. It may
 * specify a list of constraints which identify the ordering of the objects.
 */
public class Orderer
{
    private final OneShotLock _lock = new OneShotLock();

    private final Logger _logger;

    private final List _orderables = newList();

    private final Map> _orderablesById = newCaseInsensitiveMap();

    private final Map> _dependencyNodesById = newCaseInsensitiveMap();

    // Special node that is always dead last: all other nodes are a dependency
    // of the trailer.

    private DependencyNode _trailer;

    interface DependencyLinker
    {
        void link(DependencyNode source, DependencyNode target);
    }

    // before: source is added as a dependency of target, so source will
    // appear before target.

    final DependencyLinker _before = new DependencyLinker()
    {
        public void link(DependencyNode source, DependencyNode target)
        {
            target.addDependency(source);
        }
    };

    // after: target is added as a dependency of source, so source will appear
    // after target.

    final DependencyLinker _after = new DependencyLinker()
    {
        public void link(DependencyNode source, DependencyNode target)
        {
            source.addDependency(target);
        }
    };

    public Orderer(Logger logger)
    {
        _logger = logger;
    }

    /**
     * Adds an object to be ordered.
     *
     * @param orderable
     */
    public void add(Orderable orderable)
    {
        _lock.check();

        String id = orderable.getId();

        if (_orderablesById.containsKey(id))
        {
            _logger.warn(UtilMessages.duplicateOrderer(id));
            return;
        }

        _orderables.add(orderable);

        _orderablesById.put(id, orderable);
    }

    /**
     * Adds an object to be ordered.
     *
     * @param id          unique, qualified id for the target
     * @param target      the object to be ordered (or null as a placeholder)
     * @param constraints optional, variable constraints
     * @see #add(Orderable)
     */

    public void add(String id, T target, String... constraints)
    {
        _lock.check();

        add(new Orderable(id, target, constraints));
    }

    public List getOrdered()
    {
        _lock.lock();

        initializeGraph();

        List result = newList();

        for (Orderable orderable : _trailer.getOrdered())
        {
            T target = orderable.getTarget();

            // Nulls are placeholders that are skipped.

            if (target != null) result.add(target);
        }

        return result;
    }

    private void initializeGraph()
    {
        _trailer = new DependencyNode(_logger, new Orderable("*-trailer-*", null));

        addNodes();

        addDependencies();
    }

    private void addNodes()
    {
        for (Orderable orderable : _orderables)
        {
            DependencyNode node = new DependencyNode(_logger, orderable);

            _dependencyNodesById.put(orderable.getId(), node);

            _trailer.addDependency(node);
        }
    }

    private void addDependencies()
    {
        for (Orderable orderable : _orderables)
        {
            addDependencies(orderable);
        }
    }

    private void addDependencies(Orderable orderable)
    {
        String sourceId = orderable.getId();

        for (String constraint : orderable.getConstraints())
        {
            addDependencies(sourceId, constraint);
        }
    }

    private void addDependencies(String sourceId, String constraint)
    {
        int colonx = constraint.indexOf(':');

        String type = colonx > 0 ? constraint.substring(0, colonx) : null;

        DependencyLinker linker = null;

        if ("after".equals(type))
            linker = _after;
        else if ("before".equals(type)) linker = _before;

        if (linker == null)
        {
            _logger.warn(UtilMessages.constraintFormat(constraint, sourceId));
            return;
        }

        String patternList = constraint.substring(colonx + 1);

        linkNodes(sourceId, patternList, linker);
    }

    private void linkNodes(String sourceId, String patternList, DependencyLinker linker)
    {
        Collection> nodes = findDependencies(sourceId, patternList);

        DependencyNode source = _dependencyNodesById.get(sourceId);

        for (DependencyNode target : nodes)
        {
            linker.link(source, target);
        }
    }

    private Collection> findDependencies(String sourceId, String patternList)
    {
        IdMatcher matcher = buildMatcherForPattern(patternList);

        Collection> result = newList();

        for (String id : _dependencyNodesById.keySet())
        {
            if (sourceId.equals(id)) continue;

            if (matcher.matches(id)) result.add(_dependencyNodesById.get(id));
        }

        return result;
    }

    private IdMatcher buildMatcherForPattern(String patternList)
    {
        List matchers = newList();

        for (String pattern : patternList.split(","))
        {
            IdMatcher matcher = new IdMatcherImpl(pattern.trim());

            matchers.add(matcher);
        }

        return matchers.size() == 1 ? matchers.get(0) : new OrIdMatcher(matchers);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy