1 /*
2 * $Id: ContainerTagSupport.java 527536 2007-04-11 15:44:51Z 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.jsp.taglib;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.tiles.AttributeContext;
26 import org.apache.tiles.TilesContainer;
27 import org.apache.tiles.access.TilesAccess;
28
29 import javax.servlet.jsp.JspException;
30 import javax.servlet.jsp.PageContext;
31
32 /**
33 * Base tag for the tiles tags which interact with the container.
34 * Provides standard support for security, and provides access
35 * to the container and attribute context.
36 *
37 * @since Tiles 2.0
38 * @version $Rev: 527536 $ $Date: 2007-04-11 17:44:51 +0200 (Wed, 11 Apr 2007) $
39 */
40 public abstract class ContainerTagSupport extends RoleSecurityTagSupport {
41
42 /**
43 * The log instance for this tag.
44 */
45 @SuppressWarnings("unused")
46 private static final Log LOG = LogFactory.getLog(ContainerTagSupport.class);
47
48 /**
49 * The Tiles container that can be used inside the tag.
50 */
51 protected TilesContainer container;
52
53 /**
54 * The attribute context to use to store and read attribute values.
55 */
56 protected AttributeContext attributeContext;
57
58 /**
59 * By default, all ContainerTags evaluate their body. Subclasses may choose to be more selective.
60 * In any case, children can rely upon the container and attributeContext being initialized if they
61 * call <code>super.doStartTag()</code>
62 *
63 * @return <code>EVAL_BODY_BUFFERED</code>.
64 * @throws JspException If the container has not been initialized.
65 */
66 public int doStartTag() throws JspException {
67 container = TilesAccess.getContainer(pageContext.getServletContext());
68 if (container != null) {
69 startContext(pageContext);
70 return EVAL_BODY_BUFFERED;
71 } else {
72 throw new JspException("TilesContainer not initialized");
73 }
74 }
75
76
77 /** {@inheritDoc} */
78 public int doEndTag() throws JspException {
79 try {
80 return super.doEndTag();
81 } finally {
82 endContext(pageContext);
83 }
84 }
85
86
87
88 /** {@inheritDoc} */
89 public void release() {
90 super.release();
91 this.container = null;
92 this.attributeContext = null;
93 }
94
95 /**
96 * Starts the context when entering the tag.
97 *
98 * @param context The page context to use.
99 */
100 protected void startContext(PageContext context) {
101 if (container != null) {
102 attributeContext = container.startContext(pageContext);
103 }
104 }
105
106 /**
107 * Ends the context when exiting the tag.
108 *
109 * @param context The page context to use.
110 */
111 protected void endContext(PageContext context) {
112 if (attributeContext != null && container != null) {
113 container.endContext(pageContext);
114 }
115 }
116 }