KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > portlet > PortletWebContext


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.portlet;
16
17 import java.net.MalformedURLException JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.portlet.PortletContext;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hivemind.util.Defense;
26 import org.apache.tapestry.describe.DescriptionReceiver;
27 import org.apache.tapestry.web.WebContext;
28 import org.apache.tapestry.web.WebUtils;
29
30 /**
31  * Adapts {@link javax.portlet.PortletContext}as {@link org.apache.tapestry.web.WebContext}.
32  *
33  * @author Howard M. Lewis Ship
34  * @since 4.0
35  */

36 public class PortletWebContext implements WebContext
37 {
38     private static final Log LOG = LogFactory.getLog(PortletWebContext.class);
39
40     private final PortletContext _portletContext;
41
42     public PortletWebContext(PortletContext portletContext)
43     {
44         Defense.notNull(portletContext, "portletContext");
45
46         _portletContext = portletContext;
47     }
48
49     public URL JavaDoc getResource(String JavaDoc path)
50     {
51         try
52         {
53             return _portletContext.getResource(path);
54         }
55         catch (MalformedURLException JavaDoc ex)
56         {
57             LOG.error(PortletMessages.errorGettingResource(path, ex), ex);
58
59             return null;
60         }
61     }
62
63     public List JavaDoc getAttributeNames()
64     {
65         return WebUtils.toSortedList(_portletContext.getAttributeNames());
66     }
67
68     public Object JavaDoc getAttribute(String JavaDoc name)
69     {
70         return _portletContext.getAttribute(name);
71     }
72
73     public void setAttribute(String JavaDoc name, Object JavaDoc attribute)
74     {
75         if (attribute == null)
76             _portletContext.removeAttribute(name);
77         else
78             _portletContext.setAttribute(name, attribute);
79     }
80
81     public List JavaDoc getInitParameterNames()
82     {
83         return WebUtils.toSortedList(_portletContext.getInitParameterNames());
84     }
85
86     public String JavaDoc getInitParameterValue(String JavaDoc name)
87     {
88         return _portletContext.getInitParameter(name);
89     }
90
91     public String JavaDoc getMimeType(String JavaDoc resourcePath)
92     {
93         return _portletContext.getMimeType(resourcePath);
94     }
95
96     public void describeTo(DescriptionReceiver receiver)
97     {
98         receiver.describeAlternate(_portletContext);
99     }
100 }
Popular Tags