View Javadoc

1   /*
2    * $Id: ComposeStackUtil.java 1305937 2012-03-27 18:15:15Z nlebas $
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  
22  package org.apache.tiles.template;
23  
24  import java.util.Deque;
25  import java.util.LinkedList;
26  import java.util.Map;
27  
28  import org.apache.tiles.request.Request;
29  
30  /**
31   * Utilities to work with compose stacks.
32   *
33   * @version $Rev: 1305937 $ $Date: 2012-03-28 05:15:15 +1100 (Wed, 28 Mar 2012) $
34   * @since 3.0.0
35   */
36  public final class ComposeStackUtil {
37  
38      /**
39       * The name of the attribute that holds the compose stack.
40       */
41      public static final String COMPOSE_STACK_ATTRIBUTE_NAME = "org.apache.tiles.template.COMPOSE_STACK";
42  
43      /**
44       * Private constructor to avoid instantiation.
45       */
46      private ComposeStackUtil() {
47  
48      }
49  
50      /**
51       * Finds the first ancestor in the stack, that is assignable to the given class.
52       *
53       * @param composeStack The compose stack to evaluate.
54       * @param clazz The class to check.
55       * @return The first ancestor that is assignable to the class, or null if not found.
56       * @since 3.0.0
57       */
58      public static Object findAncestorWithClass(Deque<Object> composeStack, Class<?> clazz) {
59          for (Object obj : composeStack) {
60              if (clazz.isAssignableFrom(obj.getClass())) {
61                  return obj;
62              }
63          }
64  
65          return null;
66      }
67  
68      /**
69       * Returns the current compose stack, or creates a new one if not present.
70       *
71       * @param request The request.
72       * @return The compose stack.
73       * @since 3.0.0
74       */
75      @SuppressWarnings("unchecked")
76      public static Deque<Object> getComposeStack(Request request) {
77          Map<String, Object> requestScope = request.getContext("request");
78          Deque<Object> composeStack = (Deque<Object>) requestScope
79                  .get(COMPOSE_STACK_ATTRIBUTE_NAME);
80          if (composeStack == null) {
81              composeStack = new LinkedList<Object>();
82              requestScope.put(ComposeStackUtil.COMPOSE_STACK_ATTRIBUTE_NAME, composeStack);
83          }
84          return composeStack;
85      }
86  }