001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc.internal.services;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
018: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
019: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newSet;
020:
021: import java.util.Collections;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import org.apache.tapestry.ioc.internal.util.CollectionFactory;
027: import org.apache.tapestry.ioc.services.ClassPropertyAdapter;
028: import org.apache.tapestry.ioc.services.ExceptionAnalysis;
029: import org.apache.tapestry.ioc.services.ExceptionAnalyzer;
030: import org.apache.tapestry.ioc.services.ExceptionInfo;
031: import org.apache.tapestry.ioc.services.PropertyAccess;
032:
033: public class ExceptionAnalyzerImpl implements ExceptionAnalyzer {
034: private final PropertyAccess _propertyAccess;
035:
036: private final Set<String> _throwableProperties;
037:
038: public ExceptionAnalyzerImpl(PropertyAccess propertyAccess) {
039: _propertyAccess = propertyAccess;
040:
041: _throwableProperties = newSet(_propertyAccess.getAdapter(
042: Throwable.class).getPropertyNames());
043: }
044:
045: public ExceptionAnalysis analyze(Throwable rootException) {
046: List<ExceptionInfo> list = CollectionFactory.newList();
047:
048: Throwable t = rootException;
049:
050: ExceptionInfo previousInfo = null;
051:
052: while (t != null) {
053: ExceptionInfo info = extractInfo(t);
054:
055: if (addsValue(previousInfo, info)) {
056: list.add(info);
057: previousInfo = info;
058: }
059:
060: t = t.getCause();
061: }
062:
063: return new ExceptionAnalysisImpl(list);
064: }
065:
066: /**
067: * We want to filter out exceptions that do not provide any additional value. Additional value
068: * includes: an exception message not present in the containing exception or a property value
069: * not present in the containing exception. Also the first exception is always valued and the
070: * last exception (with the stack trace) is valued.
071: *
072: * @param previousInfo
073: * @param info
074: * @return
075: */
076: private boolean addsValue(ExceptionInfo previousInfo,
077: ExceptionInfo info) {
078: if (previousInfo == null)
079: return true;
080:
081: if (!info.getStackTrace().isEmpty())
082: return true;
083:
084: if (!previousInfo.getMessage().contains(info.getMessage()))
085: return true;
086:
087: for (String name : info.getPropertyNames()) {
088: if (info.getProperty(name).equals(
089: previousInfo.getProperty(name)))
090: continue;
091:
092: // Found something new and different at this level.
093:
094: return true;
095: }
096:
097: // This exception adds nothing that is not present at a higher level.
098:
099: return false;
100: }
101:
102: private ExceptionInfo extractInfo(Throwable t) {
103: Map<String, Object> properties = newMap();
104:
105: ClassPropertyAdapter adapter = _propertyAccess.getAdapter(t);
106:
107: for (String name : adapter.getPropertyNames()) {
108: if (_throwableProperties.contains(name))
109: continue;
110:
111: Object value = adapter.get(t, name);
112:
113: if (value == null)
114: continue;
115:
116: // An interesting property, let's save it for the analysis.
117:
118: properties.put(name, value);
119: }
120:
121: List<String> stackTrace = Collections.emptyList();
122:
123: // Usually, I'd use a terniary expression here, but Generics gets in
124: // the way here.
125:
126: if (t.getCause() == null)
127: stackTrace = extractStackTrace(t);
128:
129: return new ExceptionInfoImpl(t, properties, stackTrace);
130: }
131:
132: private List<String> extractStackTrace(Throwable t) {
133: List<String> trace = newList();
134:
135: for (StackTraceElement e : t.getStackTrace()) {
136: // Edit out IoC Proxy classes. They always start with a '$'
137: // and don't have any line number information.
138:
139: if (e.getClassName().startsWith("$")
140: && e.getLineNumber() < 0)
141: continue;
142:
143: trace.add(e.toString());
144: }
145:
146: return trace;
147: }
148: }
|