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