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