View Javadoc

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