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

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