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.portlet; |
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.portlet.PortletContext; |
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.portlet.extractor.ApplicationScopeExtractor; |
38 | |
import org.apache.tiles.request.portlet.extractor.InitParameterExtractor; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public class PortletApplicationContext implements ApplicationContext { |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 6 | private Map<String, Object> applicationScope = null; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 6 | protected PortletContext context = null; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | 6 | private Map<String, String> initParam = null; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | 6 | public PortletApplicationContext(PortletContext context) { |
70 | 6 | initialize(context); |
71 | 6 | } |
72 | |
|
73 | |
|
74 | |
public Object getContext() { |
75 | 1 | return context; |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public void initialize(PortletContext context) { |
85 | |
|
86 | |
|
87 | 6 | this.context = context; |
88 | |
|
89 | 6 | } |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public PortletContext getPortletContext() { |
97 | 1 | return (this.context); |
98 | |
} |
99 | |
|
100 | |
|
101 | |
public Map<String, Object> getApplicationScope() { |
102 | 1 | if ((applicationScope == null) && (context != null)) { |
103 | 1 | applicationScope = new ScopeMap(new ApplicationScopeExtractor(context)); |
104 | |
} |
105 | 1 | return (applicationScope); |
106 | |
|
107 | |
} |
108 | |
|
109 | |
|
110 | |
public Map<String, String> getInitParams() { |
111 | 1 | if ((initParam == null) && (context != null)) { |
112 | 1 | initParam = new ReadOnlyEnumerationMap<String>(new InitParameterExtractor(context)); |
113 | |
} |
114 | 1 | return (initParam); |
115 | |
|
116 | |
} |
117 | |
|
118 | |
|
119 | |
public ApplicationResource getResource(String localePath) { |
120 | |
try { |
121 | 3 | URL url = context.getResource(localePath); |
122 | 3 | if (url != null) { |
123 | 2 | return new URLApplicationResource(localePath, url); |
124 | |
} else { |
125 | 1 | return null; |
126 | |
} |
127 | 0 | } catch (MalformedURLException e) { |
128 | 0 | return null; |
129 | |
} |
130 | |
} |
131 | |
|
132 | |
|
133 | |
public ApplicationResource getResource(ApplicationResource base, Locale locale) { |
134 | |
try { |
135 | 1 | URL url = context.getResource(base.getLocalePath(locale)); |
136 | 1 | if (url != null) { |
137 | 1 | return new URLApplicationResource(base.getPath(), locale, url); |
138 | |
} else { |
139 | 0 | return null; |
140 | |
} |
141 | 0 | } catch (MalformedURLException e) { |
142 | 0 | return null; |
143 | |
} |
144 | |
} |
145 | |
|
146 | |
|
147 | |
public Collection<ApplicationResource> getResources(String path) { |
148 | 1 | ArrayList<ApplicationResource> resources = new ArrayList<ApplicationResource>(); |
149 | 1 | resources.add(getResource(path)); |
150 | 1 | return resources; |
151 | |
} |
152 | |
} |