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.portlet.context;
22
23 import org.apache.tiles.TilesApplicationContext;
24 import org.apache.tiles.context.TilesRequestContext;
25
26 import javax.portlet.PortletContext;
27 import javax.portlet.PortletRequest;
28 import javax.portlet.PortletResponse;
29 import java.net.MalformedURLException;
30 import java.net.URL;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.HashSet;
34
35 /***
36 * Portlet-based TilesApplicationContext implementation.
37 *
38 * @version $Rev: 531864 $ $Date: 2007-04-24 12:24:30 +0200 (Tue, 24 Apr 2007) $
39 */
40 public class PortletTilesApplicationContext implements TilesApplicationContext {
41
42 /***
43 * <p>The lazily instantiated <code>Map</code> of application scope
44 * attributes.</p>
45 */
46 private Map<String, Object> applicationScope = null;
47
48
49 /***
50 * <p>The <code>PortletContext</code> for this web application.</p>
51 */
52 protected PortletContext context = null;
53
54
55 /***
56 * <p>The lazily instantiated <code>Map</code> of context initialization
57 * parameters.</p>
58 */
59 private Map<String, String> initParam = null;
60
61
62 /***
63 * Creates a new instance of PortletTilesApplicationContext.
64 *
65 * @param context The portlet context to use.
66 */
67 public PortletTilesApplicationContext(PortletContext context) {
68 initialize(context);
69 }
70
71
72 /***
73 * <p>Initialize (or reinitialize) this {@link PortletTilesApplicationContext} instance
74 * for the specified Portlet API objects.</p>
75 *
76 * @param context The <code>PortletContext</code> for this web application
77 */
78 public void initialize(PortletContext context) {
79
80
81 this.context = context;
82
83 }
84
85 /***
86 * <p>Release references to allocated resources acquired in
87 * <code>initialize()</code> of via subsequent processing. After this
88 * method is called, subsequent calls to any other method than
89 * <code>initialize()</code> will return undefined results.</p>
90 */
91 public void release() {
92
93
94 applicationScope = null;
95 initParam = null;
96
97
98 context = null;
99
100 }
101
102 /***
103 * <p>Return the {@link PortletContext} for this context.</p>
104 *
105 * @return The original portlet context.
106 */
107 public PortletContext getPortletContext() {
108 return (this.context);
109 }
110
111
112 /*** {@inheritDoc} */
113 public Map<String, Object> getApplicationScope() {
114 if ((applicationScope == null) && (context != null)) {
115 applicationScope = new PortletApplicationScopeMap(context);
116 }
117 return (applicationScope);
118
119 }
120
121 /*** {@inheritDoc} */
122 public Map<String, String> getInitParams() {
123 if ((initParam == null) && (context != null)) {
124 initParam = new PortletInitParamMap(context);
125 }
126 return (initParam);
127
128 }
129
130
131 /*** {@inheritDoc} */
132 public URL getResource(String path) throws MalformedURLException {
133 return context.getResource(path);
134 }
135
136 /*** {@inheritDoc} */
137 public Set<URL> getResources(String path) throws MalformedURLException {
138 HashSet<URL> set = new HashSet<URL>();
139 set.add(getResource(path));
140 return set;
141 }
142
143 /***
144 * Creates a portlet context for a given request/response pair.
145 *
146 * @param request The request object.
147 * @param response The response object.
148 * @return The corresponding Tiles request context.
149 */
150 public TilesRequestContext createRequestContext(Object request, Object response) {
151 if (request instanceof PortletRequest && response instanceof PortletResponse) {
152 return new PortletTilesRequestContext(
153 context,
154 (PortletRequest) request,
155 (PortletResponse) response);
156 } else {
157 throw new IllegalArgumentException("Invalid context specified. "
158 + context.getClass().getName());
159 }
160 }
161
162
163 }