    001// Copyright 2006, 2007, 2008, 2009 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.tapestry5.internal.services;
    016
    017import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
    018import org.apache.tapestry5.ioc.internal.util.InternalUtils;
    019import org.apache.tapestry5.ioc.util.Stack;
    020import org.apache.tapestry5.services.Context;
    021
    022import javax.servlet.ServletContext;
    023import java.io.File;
    024import java.net.MalformedURLException;
    025import java.net.URL;
    026import java.util.Collections;
    027import java.util.List;
    028import java.util.Set;
    029
    030public class ContextImpl implements Context
    031{
    032    private final ServletContext servletContext;
    033
    034    public ContextImpl(ServletContext servletContext)
    035    {
    036        this.servletContext = servletContext;
    037    }
    038
    039    public URL getResource(String path)
    040    {
    041        try
    042        {
    043            return servletContext.getResource(path);
    044        }
    045        catch (MalformedURLException ex)
    046        {
    047            throw new RuntimeException(ex);
    048        }
    049    }
    050
    051    public File getRealFile(String path)
    052    {
    053        String realPath = servletContext.getRealPath(path);
    054
    055        return realPath == null ? null : new File(realPath);
    056    }
    057
    058    public String getInitParameter(String name)
    059    {
    060        return servletContext.getInitParameter(name);
    061    }
    062
    063    @SuppressWarnings("unchecked")
    064    public List<String> getResourcePaths(String path)
    065    {
    066        List<String> result = CollectionFactory.newList();
    067        Stack<String> queue = CollectionFactory.newStack();
    068
    069        queue.push(path);
    070
    071        while (!queue.isEmpty())
    072        {
    073            String current = queue.pop();
    074
    075            Set<String> matches = servletContext.getResourcePaths(current);
    076
    077            // Tomcat 5.5.20 inside JBoss 4.0.2 has been observed to do this!
    078            // Perhaps other servers do as well.
    079
    080            if (matches == null) continue;
    081
    082            for (String match : matches)
    083            {
    084                // Folders are queued up for further expansion.
    085
    086                if (match.endsWith("/")) queue.push(match);
    087                else result.add(match);
    088            }
    089        }
    090
    091        Collections.sort(result);
    092
    093        return result;
    094    }
    095
    096    public Object getAttribute(String name)
    097    {
    098        return servletContext.getAttribute(name);
    099    }
    100
    101    public List<String> getAttributeNames()
    102    {
    103        return InternalUtils.toList(servletContext.getAttributeNames());
    104    }
    105
    106    public String getMimeType(String file)
    107    {
    108        return servletContext.getMimeType(file);
    109    }
    110}