01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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.internal.services;
16:
17: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
18: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newStack;
19:
20: import java.net.MalformedURLException;
21: import java.net.URL;
22: import java.util.Collections;
23: import java.util.List;
24: import java.util.Set;
25:
26: import javax.servlet.ServletContext;
27:
28: import org.apache.tapestry.ioc.util.Stack;
29: import org.apache.tapestry.services.Context;
30:
31: public class ContextImpl implements Context {
32: private final ServletContext _servletContext;
33:
34: public ContextImpl(ServletContext servletContext) {
35: _servletContext = servletContext;
36: }
37:
38: public URL getResource(String path) {
39: try {
40: return _servletContext.getResource(path);
41: } catch (MalformedURLException ex) {
42: throw new RuntimeException(ex);
43: }
44: }
45:
46: public String getInitParameter(String name) {
47: return _servletContext.getInitParameter(name);
48: }
49:
50: @SuppressWarnings("unchecked")
51: public List<String> getResourcePaths(String path) {
52: List<String> result = newList();
53: Stack<String> queue = newStack();
54:
55: queue.push(path);
56:
57: while (!queue.isEmpty()) {
58: String current = queue.pop();
59:
60: Set<String> matches = (Set<String>) _servletContext
61: .getResourcePaths(current);
62:
63: // Tomcat 5.5.20 inside JBoss 4.0.2 has been observed to do this!
64: // Perhaps other servers do as well.
65:
66: if (matches == null)
67: continue;
68:
69: for (String match : matches) {
70: // Folders are queued up for further expansion.
71:
72: if (match.endsWith("/"))
73: queue.push(match);
74: else
75: result.add(match);
76: }
77:
78: }
79:
80: Collections.sort(result);
81:
82: return result;
83: }
84:
85: public Object getAttribute(String name) {
86: return _servletContext.getAttribute(name);
87: }
88:
89: }
|