01: /*
02: * Copyright 2005 Joe Walker
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 org.directwebremoting.convert;
17:
18: import java.beans.IntrospectionException;
19: import java.beans.PropertyDescriptor;
20: import java.util.Map;
21:
22: import org.directwebremoting.extend.MarshallException;
23: import org.directwebremoting.extend.Property;
24: import org.directwebremoting.impl.PropertyDescriptorProperty;
25:
26: /**
27: * A special case of BeanConverter that doesn't convert StackTraces
28: * @author Joe Walker [joe at getahead dot ltd dot uk]
29: */
30: public class ExceptionConverter extends BeanConverter {
31: /* (non-Javadoc)
32: * @see org.directwebremoting.convert.BasicBeanConverter#getPropertyDescriptors(java.lang.Class, boolean, boolean)
33: */
34: @Override
35: public Map<String, Property> getPropertyMapFromClass(Class<?> type,
36: boolean readRequired, boolean writeRequired)
37: throws MarshallException {
38: Map<String, Property> descriptors = super
39: .getPropertyMapFromClass(type, readRequired,
40: writeRequired);
41: descriptors.put("javaClassName", new PlainProperty(
42: "javaClassName", type.getName()));
43:
44: // Make sure Throwable's standard properties are added
45: // (fix for Bean Introspector peculiarities)
46: try {
47: fixMissingThrowableProperty(descriptors, "message",
48: "getMessage");
49: fixMissingThrowableProperty(descriptors, "cause",
50: "getCause");
51: } catch (IntrospectionException ex) {
52: throw new MarshallException(type, ex);
53: }
54:
55: return descriptors;
56: }
57:
58: /**
59: * Make sure Throwable's standard properties are added
60: * (fix for Bean Introspector peculiarities)
61: * @param descriptors The Map of the known descriptors
62: * @param name The name of the property to add
63: * @param readMethodName A method name to use when getting said property
64: * @throws IntrospectionException If we fail to build a PropertyDescriptor
65: */
66: protected void fixMissingThrowableProperty(
67: Map<String, Property> descriptors, String name,
68: String readMethodName) throws IntrospectionException {
69: if (!descriptors.containsKey(name)
70: && isAllowedByIncludeExcludeRules(name)) {
71: PropertyDescriptor descriptor = new PropertyDescriptor(
72: name, Throwable.class, readMethodName, null);
73: descriptors.put(name, new PropertyDescriptorProperty(
74: descriptor));
75: }
76: }
77: }
|