001: /*
002: * $Id: ComponentContext.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.tiles;
023:
024: import java.io.Serializable;
025: import java.util.Collections;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.Set;
030:
031: import javax.servlet.ServletRequest;
032: import javax.servlet.jsp.PageContext;
033:
034: import org.apache.struts.tiles.taglib.ComponentConstants;
035:
036: /**
037: * Component context.
038: */
039: public class ComponentContext implements Serializable {
040:
041: /**
042: * Component attributes.
043: */
044: private Map attributes = null;
045:
046: /**
047: * Constructor.
048: */
049: public ComponentContext() {
050: super ();
051: }
052:
053: /**
054: * Constructor.
055: * Create a context and set specified attributes.
056: * @param attributes Attributes to initialize context.
057: */
058: public ComponentContext(Map attributes) {
059: if (attributes != null) {
060: this .attributes = new HashMap(attributes);
061: }
062: }
063:
064: /**
065: * Add all attributes to this context.
066: * Copies all of the mappings from the specified attribute map to this context.
067: * New attribute mappings will replace any mappings that this context had for any of the keys
068: * currently in the specified attribute map.
069: * @param newAttributes Attributes to add.
070: */
071: public void addAll(Map newAttributes) {
072: if (attributes == null) {
073: attributes = new HashMap(newAttributes);
074: return;
075: }
076:
077: attributes.putAll(newAttributes);
078: }
079:
080: /**
081: * Add all missing attributes to this context.
082: * Copies all of the mappings from the specified attributes map to this context.
083: * New attribute mappings will be added only if they don't already exist in
084: * this context.
085: * @param defaultAttributes Attributes to add.
086: */
087: public void addMissing(Map defaultAttributes) {
088: if (defaultAttributes == null) {
089: return;
090: }
091:
092: if (attributes == null) {
093: attributes = new HashMap(defaultAttributes);
094: return;
095: }
096:
097: Set entries = defaultAttributes.entrySet();
098: Iterator iterator = entries.iterator();
099: while (iterator.hasNext()) {
100: Map.Entry entry = (Map.Entry) iterator.next();
101: if (!attributes.containsKey(entry.getKey())) {
102: attributes.put(entry.getKey(), entry.getValue());
103: }
104: }
105: }
106:
107: /**
108: * Get an attribute from context.
109: * @param name Name of the attribute.
110: * @return <{Object}>
111: */
112: public Object getAttribute(String name) {
113: if (attributes == null) {
114: return null;
115: }
116:
117: return attributes.get(name);
118: }
119:
120: /**
121: * Get names of all attributes.
122: * @return <{Object}>
123: */
124: public Iterator getAttributeNames() {
125: if (attributes == null) {
126: return Collections.EMPTY_LIST.iterator();
127: }
128:
129: return attributes.keySet().iterator();
130: }
131:
132: /**
133: * Put a new attribute to context.
134: * @param name Name of the attribute.
135: * @param value Value of the attribute.
136: */
137: public void putAttribute(String name, Object value) {
138: if (attributes == null) {
139: attributes = new HashMap();
140: }
141:
142: attributes.put(name, value);
143: }
144:
145: /**
146: * Find object in one of the contexts.
147: * Order : component then pageContext.findAttribute()
148: * @param beanName Name of the bean to find.
149: * @param pageContext Page context.
150: * @return Requested bean or <code>null</code> if not found.
151: */
152: public Object findAttribute(String beanName, PageContext pageContext) {
153: Object attribute = getAttribute(beanName);
154: if (attribute == null) {
155: attribute = pageContext.findAttribute(beanName);
156: }
157:
158: return attribute;
159: }
160:
161: /**
162: * Get object from requested context.
163: * Context can be 'component'.
164: * @param beanName Name of the bean to find.
165: * @param scope Search scope (see {@link PageContext}).
166: * @param pageContext Page context.
167: * @return requested bean or <code>null</code> if not found.
168: */
169: public Object getAttribute(String beanName, int scope,
170: PageContext pageContext) {
171:
172: if (scope == ComponentConstants.COMPONENT_SCOPE) {
173: return getAttribute(beanName);
174: }
175:
176: return pageContext.getAttribute(beanName, scope);
177: }
178:
179: /**
180: * Get component context from request.
181: * @param request ServletRequest.
182: * @return ComponentContext or null if context is not found or an
183: * jspException is present in the request.
184: */
185: static public ComponentContext getContext(ServletRequest request) {
186: if (request.getAttribute("javax.servlet.jsp.jspException") != null) {
187: return null;
188: }
189: return (ComponentContext) request
190: .getAttribute(ComponentConstants.COMPONENT_CONTEXT);
191: }
192:
193: /**
194: * Store component context into request.
195: * @param context ComponentContext to store.
196: * @param request Request to store ComponentContext.
197: */
198: static public void setContext(ComponentContext context,
199: ServletRequest request) {
200:
201: request.setAttribute(ComponentConstants.COMPONENT_CONTEXT,
202: context);
203: }
204: }
|