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