001// Copyright 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.portlet;
016
017import java.util.ArrayList;
018import java.util.Collections;
019import java.util.Iterator;
020import java.util.List;
021
022import javax.portlet.PortletRequest;
023
024import org.apache.tapestry.IRequestCycle;
025import org.apache.tapestry.PageNotFoundException;
026
027/**
028 * Uses the tapestry.portlet.resolver.PageResolverRules configuration point to find a match.
029 * 
030 * @author Howard M. Lewis Ship
031 * @since 4.0
032 */
033public class MatchingPortletPageResolver implements PortletPageResolver
034{
035    private List _contributions;
036
037    private List _sortedContributions;
038
039    private PortletRequest _request;
040
041    public void initializeService()
042    {
043        _sortedContributions = new ArrayList(_contributions);
044        Collections.sort(_sortedContributions);
045    }
046
047    public String getPageNameForRequest(IRequestCycle cycle)
048    {
049        Iterator i = _sortedContributions.iterator();
050        while (i.hasNext())
051        {
052            PageResolverContribution c = (PageResolverContribution) i.next();
053
054            if (c.match(_request))
055            {
056                String pageName = c.getPageName();
057
058                try
059                {
060                    cycle.getPage(pageName);
061
062                    return pageName;
063                }
064                catch (PageNotFoundException ex)
065                {
066                    // Ignore.
067                }
068            }
069        }
070
071        // No match, or no matches where the page actually exists.
072
073        return null;
074    }
075
076    public void setContributions(List contributions)
077    {
078        _contributions = contributions;
079    }
080
081    public void setRequest(PortletRequest request)
082    {
083        _request = request;
084    }
085
086}