This project has retired. For details please refer to its Attic page.
ServletTilesApplicationContext xref
View Javadoc

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