001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.framework.container;
016:
017: import org.apache.commons.lang.exception.ExceptionUtils;
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.araneaframework.InputData;
021: import org.araneaframework.Message;
022: import org.araneaframework.OutputData;
023: import org.araneaframework.Path;
024: import org.araneaframework.Widget;
025: import org.araneaframework.core.ProxyEventListener;
026: import org.araneaframework.core.util.ExceptionUtil;
027: import org.araneaframework.http.UpdateRegionContext;
028: import org.araneaframework.http.util.AtomicResponseHelper;
029:
030: /**
031: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
032: */
033: public abstract class ExceptionHandlingFlowContainerWidget extends
034: StandardFlowContainerWidget {
035: private static final Log log = LogFactory
036: .getLog(ExceptionHandlingFlowContainerWidget.class);
037:
038: protected Exception exception;
039:
040: public ExceptionHandlingFlowContainerWidget() {
041: super ();
042: }
043:
044: public ExceptionHandlingFlowContainerWidget(Widget topWidget) {
045: super (topWidget);
046: }
047:
048: protected void init() throws Exception {
049: super .init();
050:
051: addEventListener("retry", new ProxyEventListener(this ));
052: addEventListener("cancel", new ProxyEventListener(this ));
053: addEventListener("reset", new ProxyEventListener(this ));
054: }
055:
056: public void handleEventRetry() throws Exception {
057: //noop
058: }
059:
060: public void handleEventCancel() throws Exception {
061: cancel();
062: }
063:
064: public void handleEventReset() throws Exception {
065: reset(null);
066: }
067:
068: protected void handleWidgetException(Exception e) throws Exception {
069: this .exception = e;
070:
071: if (ExceptionUtils.getRootCause(e) != null)
072: log.error("Critical exception occured: ", ExceptionUtils
073: .getRootCause(e));
074: else
075: log.error("Critical exception occured: ", e);
076:
077: UpdateRegionContext updateRegionContext = (UpdateRegionContext) getEnvironment()
078: .getEntry(UpdateRegionContext.class);
079: if (updateRegionContext != null) {
080: updateRegionContext.disableOnce();
081: }
082: }
083:
084: protected void update(InputData input) throws Exception {
085: if (exception == null)
086: super .update(input);
087: else
088: handleUpdate(input);
089: }
090:
091: protected void event(Path path, InputData input) throws Exception {
092: if (exception == null)
093: super .event(path, input);
094: else if (path != null && !path.hasNext())
095: handleEvent(input);
096: }
097:
098: protected void propagate(Message message) throws Exception {
099: try {
100: super .propagate(message);
101: } catch (Exception e) {
102: try {
103: handleWidgetException(e);
104: } catch (Exception e2) {
105: ExceptionUtil.uncheckException(e2);
106: }
107: }
108: }
109:
110: protected void render(OutputData output) throws Exception {
111: AtomicResponseHelper arUtil = new AtomicResponseHelper(output);
112:
113: try {
114: if (exception != null)
115: throw exception;
116:
117: super .render(output);
118: } catch (Exception e) {
119: arUtil.rollback();
120:
121: log.error("Handling error:", e);
122:
123: renderExceptionHandler(output, e);
124:
125: exception = null;
126: }
127:
128: arUtil.commit();
129: }
130:
131: protected abstract void renderExceptionHandler(OutputData output,
132: Exception e) throws Exception;
133: }
|