01: /*
02: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package de.odysseus.calyxo.base.access;
17:
18: import javax.servlet.http.HttpServletRequest;
19:
20: import de.odysseus.calyxo.base.ModuleContext;
21: import de.odysseus.calyxo.base.util.PropertyUtils;
22:
23: /**
24: * This accessor serves a bean property value of an attribute.
25: *
26: * @author Christoph Beck
27: */
28: public class PropertyAccessor extends AttributeAccessor {
29:
30: private String property;
31:
32: /**
33: * Constructor.
34: * @param context The module context we're in
35: * @param name attribute name
36: * @param property property name
37: * @param scope attribute scope
38: * @param cachable true iff attribute value cannot change within request
39: */
40: public PropertyAccessor(ModuleContext context, String name,
41: String property, int scope, boolean cachable) {
42: super (context, name, scope, cachable);
43: this .property = property;
44: }
45:
46: /**
47: * Constructor (not cachable).
48: * @param context The module context we're in
49: * @param name attribute name
50: * @param property property name
51: * @param scope attribute scope
52: */
53: public PropertyAccessor(ModuleContext context, String name,
54: String property, int scope) {
55: this (context, name, property, scope, false);
56: }
57:
58: /**
59: * Constructor (search all scopes).
60: * @param context The module context we're in
61: * @param name attribute name
62: * @param property property name
63: * @param cachable true iff attribute value cannot change within request
64: */
65: public PropertyAccessor(ModuleContext context, String name,
66: String property, boolean cachable) {
67: this (context, name, property, ALL_SCOPES, false);
68: }
69:
70: /**
71: * Constructor (not cachable, search all scopes).
72: * @param context The module context we're in
73: * @param name attribute name
74: * @param property property name
75: */
76: public PropertyAccessor(ModuleContext context, String name,
77: String property) {
78: this (context, name, property, false);
79: }
80:
81: /**
82: * Lookup attribute and get property
83: */
84: public final Object get(HttpServletRequest request) {
85: Object bean = super .get(request);
86: if (bean == null) {
87: return null;
88: }
89: try {
90: return PropertyUtils.getNestedProperty(bean, property);
91: } catch (Exception e) {
92: return null;
93: }
94: }
95: }
|