001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: OutcookieValues.java 3716 2007-04-11 06:21:18Z gbevin $
007: */
008: package com.uwyn.rife.engine;
009:
010: import com.uwyn.rife.engine.exceptions.ElementOutjectionException;
011: import com.uwyn.rife.engine.exceptions.OutcookieOutjectionException;
012: import com.uwyn.rife.engine.exceptions.OutputOutjectionException;
013: import com.uwyn.rife.tools.BeanPropertyProcessor;
014: import com.uwyn.rife.tools.BeanUtils;
015: import com.uwyn.rife.tools.Convert;
016: import com.uwyn.rife.tools.exceptions.BeanUtilsException;
017: import java.beans.PropertyDescriptor;
018: import java.lang.reflect.InvocationTargetException;
019: import java.lang.reflect.Method;
020: import java.util.Collection;
021: import java.util.Collections;
022: import java.util.LinkedHashMap;
023: import java.util.Map;
024: import javax.servlet.http.Cookie;
025:
026: class OutcookieValues {
027: private ElementContext mContext = null;
028: private ElementAware mElement = null;
029:
030: private Map<String, String> mValues = null;
031: private Map<String, Method> mOutcookieGetters = null;
032:
033: OutcookieValues(ElementContext context) {
034: assert context != null;
035:
036: mContext = context;
037: mContext.getElementInfo().getSite();
038: mElement = mContext.getElementSupport().getElementAware();
039: mValues = new LinkedHashMap<String, String>();
040:
041: // try to obtain the outcookie getters from the cache
042: // if this wasn't possible, detect them and store them in the cache
043: final ElementInfo element_info = mContext.getElementInfo();
044: mOutcookieGetters = element_info.getSite()
045: .getCachedOutcookieGetters(element_info.getId());
046: if (null == mOutcookieGetters) {
047: final Collection<String> names_outcookies = element_info
048: .getOutcookieNames();
049: final Collection<String> names_globalcookies = element_info
050: .getGlobalCookieNames();
051: try {
052: BeanUtils.processProperties(BeanUtils.GETTERS, mElement
053: .getClass(), null, null, null,
054: new BeanPropertyProcessor() {
055: public boolean gotProperty(String name,
056: PropertyDescriptor descriptor)
057: throws IllegalAccessException,
058: IllegalArgumentException,
059: InvocationTargetException {
060: Method method = descriptor
061: .getReadMethod();
062: if (names_outcookies.contains(name)
063: || names_globalcookies
064: .contains(name))
065:
066: {
067: if (null == mOutcookieGetters) {
068: mOutcookieGetters = new LinkedHashMap<String, Method>();
069: }
070:
071: mOutcookieGetters.put(name, method);
072: }
073:
074: return true;
075: }
076: });
077:
078: if (null == mOutcookieGetters) {
079: mOutcookieGetters = Collections.EMPTY_MAP;
080: }
081: element_info.getSite().putCachedOutcookieGetters(
082: element_info.getId(), mOutcookieGetters);
083: } catch (BeanUtilsException e) {
084: throw new ElementOutjectionException(mContext
085: .getElementInfo().getDeclarationName(),
086: mElement.getClass(), e);
087: }
088: }
089: }
090:
091: void processGetters() {
092: Object value = null;
093: Cookie cookie = null;
094: if (mOutcookieGetters != null && mOutcookieGetters.size() > 0) {
095: for (Map.Entry<String, Method> outcookie_getter : mOutcookieGetters
096: .entrySet()) {
097: if (!mValues.containsKey(outcookie_getter.getKey())) {
098: value = getPropertyValue(outcookie_getter.getKey());
099: if (value != null) {
100: cookie = new Cookie(outcookie_getter.getKey(),
101: Convert.toString(value));
102: cookie.setPath("");
103: mContext.setCookieRaw(cookie);
104: }
105: }
106: }
107: }
108: }
109:
110: void processGetterChildTriggers() {
111: if (mContext.getElementState().inInheritanceStructure()) {
112: Collection<String> childtrigger_names = mContext
113: .getElementInfo().getChildTriggerNames();
114:
115: Object value = null;
116: if (mOutcookieGetters != null
117: && mOutcookieGetters.size() > 0) {
118: for (Map.Entry<String, Method> outcookie_getter : mOutcookieGetters
119: .entrySet()) {
120: if (childtrigger_names.contains(outcookie_getter
121: .getKey())) {
122: value = getPropertyValue(outcookie_getter
123: .getKey());
124: if (value != null) {
125: mContext.triggerChild(outcookie_getter
126: .getKey(), new String[] { Convert
127: .toString(value) });
128: }
129: }
130: }
131: }
132: }
133: }
134:
135: void put(String name, String value) {
136: if (null == name)
137: return;
138:
139: if (mOutcookieGetters != null) {
140: mOutcookieGetters.remove(name);
141: }
142:
143: mValues.put(name, value);
144: }
145:
146: String get(String name) {
147: if (null == name)
148: return null;
149:
150: String outcookie = mValues.get(name);
151: if (null == outcookie
152: && mContext.getElementInfo().hasOutcookieDefaults()) {
153: outcookie = mContext.getElementInfo()
154: .getOutcookieDefaultValue(name);
155: }
156:
157: if (null == outcookie) {
158: outcookie = getPropertyValue(name);
159: }
160:
161: return outcookie;
162: }
163:
164: private String getPropertyValue(String name)
165: throws OutputOutjectionException {
166: if (null == mOutcookieGetters) {
167: return null;
168: }
169:
170: Method method = mOutcookieGetters.get(name);
171: if (method != null) {
172: try {
173: Object value = method.invoke(mElement, (Object[]) null);
174: if (value != null) {
175: return Convert.toString(value);
176: }
177: } catch (Exception e) {
178: throw new OutcookieOutjectionException(mContext
179: .getElementInfo().getDeclarationName(),
180: mElement.getClass(), e);
181: }
182: }
183:
184: return null;
185: }
186:
187: boolean contains(String name) {
188: if (null == name)
189: return false;
190:
191: if (mValues.containsKey(name)) {
192: return true;
193: }
194:
195: if (getPropertyValue(name) != null) {
196: return true;
197: }
198:
199: return false;
200: }
201:
202: Map<String, String> aggregateValues() {
203: Map<String, String> entry_map = null;
204:
205: // handle default outcookie values
206: if (mContext.getElementInfo().hasOutcookieDefaults()) {
207: if (null == entry_map) {
208: entry_map = new LinkedHashMap<String, String>();
209: }
210:
211: for (Map.Entry<String, String> outcookie_defaults_entry : mContext
212: .getElementInfo().getOutcookieEntries()) {
213: if (outcookie_defaults_entry.getValue() != null) {
214: entry_map.put(outcookie_defaults_entry.getKey(),
215: outcookie_defaults_entry.getValue());
216: }
217: }
218: }
219:
220: // handle outcookie getter outjection
221: if (mOutcookieGetters != null && mOutcookieGetters.size() > 0) {
222: Object value = null;
223: try {
224: for (Map.Entry<String, Method> outcookie_getter : mOutcookieGetters
225: .entrySet()) {
226: value = outcookie_getter.getValue().invoke(
227: mElement, (Object[]) null);
228: if (value != null) {
229: if (null == entry_map) {
230: entry_map = new LinkedHashMap<String, String>();
231: }
232:
233: entry_map.put(outcookie_getter.getKey(),
234: Convert.toString(value));
235: }
236: }
237: } catch (Exception e) {
238: throw new OutcookieOutjectionException(mContext
239: .getElementInfo().getDeclarationName(),
240: mElement.getClass(), e);
241: }
242: }
243:
244: // handle outcookies that have been explicitly set
245: if (null == entry_map) {
246: return Collections.unmodifiableMap(mValues);
247: } else {
248: entry_map.putAll(mValues);
249: }
250:
251: return Collections.unmodifiableMap(entry_map);
252: }
253: }
|