KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > web > ServletWebContext


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.web;
16
17 import java.net.MalformedURLException JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.servlet.ServletContext JavaDoc;
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
28 /**
29  * Adapts {@link javax.servlet.ServletContext} as {@link org.apache.tapestry.web.WebContext}.
30  *
31  * @author Howard M. Lewis Ship
32  * @since 4.0
33  */

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