001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.action;
019:
020: import java.io.IOException;
021: import java.lang.reflect.Field;
022: import java.lang.reflect.InvocationTargetException;
023: import java.lang.reflect.Method;
024: import java.util.Collection;
025: import java.util.Collections;
026:
027: import de.finix.contelligent.CallData;
028: import de.finix.contelligent.ComponentManager;
029: import de.finix.contelligent.components.Folder;
030: import de.finix.contelligent.content.Content;
031: import de.finix.contelligent.content.ContentProvider;
032: import de.finix.contelligent.content.StringContent;
033: import de.finix.contelligent.exception.ContelligentException;
034: import de.finix.contelligent.logging.LoggingService;
035:
036: public class ActionResultProperty extends Folder implements Parameter,
037: ContentProvider, StringContent {
038:
039: final static org.apache.log4j.Logger log = LoggingService
040: .getLogger(ActionResultProperty.class);
041:
042: private String propertyName;
043:
044: private String methodName;
045:
046: public void setPropertyName(String value) {
047: propertyName = value;
048: methodName = createMethodName(value);
049: }
050:
051: public String getPropertyName() {
052: return propertyName;
053: }
054:
055: public Content getContent() {
056: return this ;
057: }
058:
059: public Object getContentValue(CallData callData)
060: throws IOException, ContelligentException {
061: return getString(callData);
062: }
063:
064: public long lastModified(CallData callData) throws IOException {
065: return -1;
066: }
067:
068: public Collection getSensitiveCategories() {
069: return Collections.EMPTY_SET;
070: }
071:
072: /**
073: * this method contains much implicit logic, to keep the fault tolerance as
074: * high as possible: If the element received from the parent through the
075: * getObject(callData) call is an Array, it is tried to get the first
076: * element. If this does not exist return empty string. on the non array
077: * element extracted or not a String clazz test is performed. in this case
078: * the string is returned. otherwise first the propertyName is checked to
079: * exist as a public accessible field. If it does the value is returned;
080: * otherwise it is tried to get the value from a bean style getter method
081: * matching the 'propertyName' definition. If no value can be extracted at
082: * all the empty string is returned. Today this method returns the
083: * toString() value on success. Later this should change to allow an
084: * independent usage of the toString() for logging, etc.
085: */
086: public String getString(CallData callData)
087: throws ContelligentException, IOException {
088: Object object = getObject(callData);
089:
090: if (object == null) {
091: return "";
092: } else {
093: if (log.isDebugEnabled()) {
094: log
095: .debug("getValue() - trying to extract from field of '"
096: + object + "'");
097: }
098: Class clazz = object.getClass();
099:
100: if (clazz.isArray()) {
101: Object _obj[] = (Object[]) object;
102:
103: if (_obj.length > 0) {
104: // TODO: build another instance for it
105: object = _obj[0];
106:
107: } else {
108: return "";
109: }
110: }
111:
112: // after this is surely no array return if it is String class
113: if (object instanceof String) {
114: return (String) object;
115: }
116:
117: // otherwise extract field or method from property 'propertyName'
118: Object value;
119:
120: value = extractFieldValue(object, clazz);
121: if (value == null) {
122: value = extractGetterValue(object, clazz);
123: }
124:
125: if (log.isDebugEnabled()) {
126: log
127: .debug("getValue() - received value='" + value
128: + "'");
129: }
130:
131: if (value == null) {
132: return "";
133: } else {
134: return value.toString();
135: }
136: }
137: }
138:
139: public Object getObject(CallData callData) {
140: ComponentManager manager = ctx.getCallManager();
141: Parameter parent = (Parameter) manager.parentContainer(this );
142:
143: if (parent == null) {
144: return null;
145: } else {
146: return parent.getObject(callData);
147: }
148: }
149:
150: protected Object extractFieldValue(Object object, Class clazz) {
151: try {
152: Field field = clazz.getField(getPropertyName());
153: return field.get(object);
154: } catch (NoSuchFieldException e) {
155: } catch (SecurityException e) {
156: } catch (IllegalArgumentException e) {
157: } catch (IllegalAccessException e) {
158: }
159: return null;
160: }
161:
162: protected Object extractGetterValue(Object object, Class clazz) {
163: try {
164: if (log.isDebugEnabled()) {
165: log
166: .debug("extractGetterValue() - extraction value by method. [methodName='"
167: + methodName
168: + "', class instance='"
169: + clazz + "']");
170: }
171: Method method = clazz.getMethod(methodName, new Class[0]);
172:
173: Object result = method.invoke(object, new Object[0]);
174:
175: return result;
176: } catch (NoSuchMethodException e) {
177: } catch (SecurityException e) {
178: } catch (IllegalArgumentException e) {
179: } catch (IllegalAccessException e) {
180: } catch (InvocationTargetException e) {
181: }
182: return null;
183: }
184:
185: /*
186: * protected Object extractGetterValueWithCallData(Object object, Class
187: * clazz, CallData callData) {
188: *
189: * try { if(log.isDebugEnabled()){
190: * log.debug("extractGetterValueWithCallData() - extraction value by method.
191: * [methodName='"+methodName+ "', class instance='"+clazz+"']"); }
192: *
193: * Method method = clazz.getMethod(methodName, new Class[]{CallData.class});
194: * Object result = method.invoke(object, new Object[]{callData}); return
195: * result; } catch (NoSuchMethodException e) { } catch (SecurityException e) { }
196: * catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { }
197: * catch (InvocationTargetException e) { } return null; }
198: */
199: private String createMethodName(String property) {
200: if (property.length() == 0) {
201: return "";
202: }
203:
204: StringBuffer buffer = new StringBuffer();
205:
206: buffer.append("get");
207: buffer.append(Character.toUpperCase(property.charAt(0)));
208: buffer.append(property.substring(1));
209: return buffer.toString();
210: }
211:
212: }
|