View Javadoc

1   /*
2    * $Id: TilesAccess.java 1044659 2010-12-11 14:16:04Z 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.access;
22  
23  import java.util.Map;
24  
25  import org.apache.tiles.NoSuchContainerException;
26  import org.apache.tiles.TilesContainer;
27  import org.apache.tiles.request.ApplicationContext;
28  import org.apache.tiles.request.Request;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  
33  /**
34   * Provides static access to the tiles container.
35   *
36   * @version $Rev: 1044659 $ $Date: 2010-12-12 01:16:04 +1100 (Sun, 12 Dec 2010) $
37   */
38  public final class TilesAccess {
39  
40      /**
41       * Name of the attribute used to store the current used container.
42       */
43      public static final String CURRENT_CONTAINER_ATTRIBUTE_NAME =
44          "org.apache.tiles.servlet.context.ServletTilesRequestContext.CURRENT_CONTAINER_KEY";
45  
46      /**
47       * Constructor, private to avoid instantiation.
48       */
49      private TilesAccess() {
50      }
51  
52      /**
53       * The name of the attribute to use when getting and setting the container
54       * object in a context.
55       */
56      public static final String CONTAINER_ATTRIBUTE =
57          "org.apache.tiles.CONTAINER";
58  
59      /**
60       * Configures the default container to be used in the application.
61       *
62       * @param context The Tiles application context object to use.
63       * @param container The container object to set.
64       * @since 2.1.2
65       */
66      public static void setContainer(ApplicationContext context,
67              TilesContainer container) {
68          setContainer(context, container, CONTAINER_ATTRIBUTE);
69      }
70  
71      /**
72       * Configures the container to be used in the application.
73       *
74       * @param context The Tiles application context object to use.
75       * @param container The container object to set.
76       * @param key The key under which the container will be stored.
77       * @since 2.1.2
78       */
79      public static void setContainer(ApplicationContext context,
80              TilesContainer container, String key) {
81          Logger log = LoggerFactory.getLogger(TilesAccess.class);
82          if (key == null) {
83              key = CONTAINER_ATTRIBUTE;
84          }
85  
86          if (container == null) {
87              if (log.isInfoEnabled()) {
88                  log.info("Removing TilesContext for context: " + context.getClass().getName());
89              }
90              context.getApplicationScope().remove(key);
91          } else {
92              if (log.isInfoEnabled()) {
93                  log.info("Publishing TilesContext for context: " + context.getClass().getName());
94              }
95              context.getApplicationScope().put(key, container);
96          }
97      }
98  
99      /**
100      * Returns default the container to be used in the application.
101      *
102      * @param context The Tiles application context object to use.
103      * @return The default container object.
104      * @since 3.0.0
105      */
106     public static TilesContainer getContainer(ApplicationContext context) {
107         return getContainer(context, CONTAINER_ATTRIBUTE);
108     }
109 
110     /**
111      * Returns the container to be used in the application registered under a specific key.
112      *
113      * @param context The Tiles application context object to use.
114      * @param key The key under which the container will be stored.
115      * @return The container object.
116      * @since 3.0.0
117      */
118     public static TilesContainer getContainer(ApplicationContext context,
119             String key) {
120         if (key == null) {
121             key = CONTAINER_ATTRIBUTE;
122         }
123 
124         return (TilesContainer) context.getApplicationScope().get(key);
125     }
126 
127     /**
128      * Sets the current container to use in web pages.
129      *
130      * @param request The request to use.
131      * @param key The key under which the container is stored.
132      * @since 2.1.0
133      */
134     public static void setCurrentContainer(Request request,
135             String key) {
136         ApplicationContext applicationContext = request.getApplicationContext();
137         TilesContainer container = getContainer(applicationContext, key);
138         if (container != null) {
139             request.getContext("request").put(CURRENT_CONTAINER_ATTRIBUTE_NAME, container);
140         } else {
141             throw new NoSuchContainerException("The container with the key '"
142                     + key + "' cannot be found");
143         }
144     }
145 
146     /**
147      * Sets the current container to use in web pages.
148      *
149      * @param request The request to use.
150      * @param container The container to use as the current container.
151      * @since 2.1.0
152      */
153     public static void setCurrentContainer(Request request,
154             TilesContainer container) {
155         if (container != null) {
156             request.getContext("request").put(CURRENT_CONTAINER_ATTRIBUTE_NAME, container);
157         } else {
158             throw new NullPointerException("The container cannot be null");
159         }
160     }
161 
162     /**
163      * Returns the current container that has been set, or the default one.
164      *
165      * @param request The request to use.
166      * @return The current Tiles container to use in web pages.
167      * @since 2.1.0
168      */
169     public static TilesContainer getCurrentContainer(Request request) {
170         ApplicationContext context = request.getApplicationContext();
171         Map<String, Object> requestScope = request.getContext("request");
172         TilesContainer container = (TilesContainer) requestScope.get(CURRENT_CONTAINER_ATTRIBUTE_NAME);
173         if (container == null) {
174             container = getContainer(context);
175             requestScope.put(CURRENT_CONTAINER_ATTRIBUTE_NAME, container);
176         }
177 
178         return container;
179     }
180 }