1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.tiles.request.jakarta.servlet;
17
18 import java.net.MalformedURLException;
19 import java.net.URL;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Locale;
23 import java.util.Map;
24
25 import org.apache.tiles.request.ApplicationContext;
26 import org.apache.tiles.request.ApplicationResource;
27 import org.apache.tiles.request.collection.ReadOnlyEnumerationMap;
28 import org.apache.tiles.request.collection.ScopeMap;
29 import org.apache.tiles.request.jakarta.servlet.extractor.ApplicationScopeExtractor;
30 import org.apache.tiles.request.jakarta.servlet.extractor.InitParameterExtractor;
31 import org.apache.tiles.request.locale.URLApplicationResource;
32
33 import jakarta.servlet.ServletContext;
34
35
36
37
38
39
40
41 public class ServletApplicationContext implements ApplicationContext {
42
43
44
45
46 private ServletContext servletContext;
47
48
49
50
51 private Map<String, Object> applicationScope = null;
52
53
54
55
56 private Map<String, String> initParam = null;
57
58
59
60
61
62
63 public ServletApplicationContext(ServletContext servletContext) {
64 this.servletContext = servletContext;
65 }
66
67
68
69
70
71
72 public Object getContext() {
73 return servletContext;
74 }
75
76
77
78
79
80
81 public Map<String, Object> getApplicationScope() {
82 if (applicationScope == null && servletContext != null) {
83 applicationScope = new ScopeMap(
84 new ApplicationScopeExtractor(servletContext));
85 }
86
87 return applicationScope;
88 }
89
90
91
92
93
94
95
96 public Map<String, String> getInitParams() {
97 if (initParam == null && servletContext != null) {
98 initParam = new ReadOnlyEnumerationMap<String>(
99 new InitParameterExtractor(servletContext));
100 }
101
102 return initParam;
103 }
104
105
106
107
108
109
110
111
112
113
114 public ApplicationResource getResource(String localePath) {
115 try {
116 URL url = servletContext.getResource(localePath);
117 if (url != null) {
118 return new URLApplicationResource(localePath, url);
119 } else {
120 return null;
121 }
122 } catch (MalformedURLException e) {
123 return null;
124 }
125 }
126
127
128
129
130
131
132
133
134
135
136 public ApplicationResource getResource(ApplicationResource base, Locale locale) {
137 try {
138 URL url = servletContext.getResource(base.getLocalePath(locale));
139 if (url != null) {
140 return new URLApplicationResource(base.getPath(), locale, url);
141 } else {
142 return null;
143 }
144 } catch (MalformedURLException e) {
145 return null;
146 }
147 }
148
149
150
151
152
153
154
155
156 public Collection<ApplicationResource> getResources(String path) {
157 ArrayList<ApplicationResource> resources = new ArrayList<ApplicationResource>();
158 resources.add(getResource(path));
159 return resources;
160 }
161 }