View Javadoc

1   /*
2    * $Id: PortletTilesApplicationContext.java 531864 2007-04-24 10:24:30Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          // Save the specified Portlet API object references
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          // Release references to allocated collections
94          applicationScope = null;
95          initParam = null;
96  
97          // Release references to Portlet API objects
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 }