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