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