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    
015    package org.apache.tapestry.web;
016    
017    import java.io.InputStream;
018    import java.net.MalformedURLException;
019    import java.net.URL;
020    import java.util.List;
021    import java.util.Set;
022    
023    import javax.servlet.ServletContext;
024    
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.apache.hivemind.util.Defense;
028    import org.apache.tapestry.describe.DescriptionReceiver;
029    
030    /**
031     * Adapts {@link javax.servlet.ServletContext} as
032     * {@link org.apache.tapestry.web.WebContext}.
033     * 
034     * @author Howard M. Lewis Ship
035     * @since 4.0
036     */
037    public class ServletWebContext implements WebContext
038    {
039    
040        private static final Log LOG = LogFactory.getLog(ServletWebContext.class);
041    
042        private final ServletContext _servletContext;
043    
044        public ServletWebContext(ServletContext context)
045        {
046            Defense.notNull(context, "context");
047    
048            _servletContext = context;
049        }
050    
051        public void describeTo(DescriptionReceiver receiver)
052        {
053            receiver.describeAlternate(_servletContext);
054        }
055    
056        public List getAttributeNames()
057        {
058            return WebUtils.toSortedList(_servletContext.getAttributeNames());
059        }
060    
061        public Object getAttribute(String name)
062        {
063            return _servletContext.getAttribute(name);
064        }
065    
066        public void setAttribute(String name, Object attribute)
067        {
068            if (attribute == null)
069                _servletContext.removeAttribute(name);
070            else _servletContext.setAttribute(name, attribute);
071    
072        }
073    
074        public URL getResource(String path)
075        {
076            try
077            {
078                return _servletContext.getResource(path);
079            }
080            catch (MalformedURLException ex)
081            {
082                LOG.error(WebMessages.errorGettingResource(path, ex), ex);
083    
084                return null;
085            }
086        }
087    
088        public String getInitParameterValue(String name)
089        {
090            return _servletContext.getInitParameter(name);
091        }
092    
093        public List getInitParameterNames()
094        {
095            return WebUtils.toSortedList(_servletContext.getInitParameterNames());
096        }
097    
098        public String getMimeType(String resourcePath)
099        {
100            return _servletContext.getMimeType(resourcePath);
101        }
102    
103        public String getRealPath(String path)
104        {
105            return _servletContext.getRealPath(path);
106        }
107    
108        public InputStream getResourceAsStream(String path)
109        {
110            return _servletContext.getResourceAsStream(path);
111        }
112    
113        public Set getResourcePaths(String path)
114        {
115            return _servletContext.getResourcePaths(path);
116        }
117    }