2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.shale.view.faces;
18  
19  import javax.faces.component.UIViewRoot;
20  import javax.faces.context.FacesContext;
21  
22  import org.apache.shale.view.Constants;
23  import org.apache.shale.view.ExceptionHandler;
24  
25  /***
26   * <p>Extending the UIViewRoot to provide specialized exception
27   * handling needed for the Shale view controller to enforce the contracts
28   * of the callbacks.</p>
29   */
30  public class ShaleViewRoot extends UIViewRoot  {
31  
32      /***
33       * <p>Override to catch exceptions raised by the action listeners
34       * in the invoke application phase.</p>
35       *
36       * @param context faces context
37       */
38      public void processApplication(FacesContext context) {
39          try {
40              super.processApplication(context);
41          } catch (Exception e) {
42              handleException(context, e);
43              context.responseComplete();
44          }
45      }
46  
47      /***
48       * <p>Override to catch and handle exceptions with immediate commands
49       * and value change listeners in the apply request values phase.</p>
50       *
51       * @param context faces context
52       */
53      public void processDecodes(FacesContext context) {
54          try {
55              super.processDecodes(context);
56          } catch (Exception e) {
57              handleException(context, e);
58              context.responseComplete();
59          }
60      }
61  
62      /***
63       * <p>Override to catch and handle exceptions raised in the 
64       * update model phase.</p>
65       *
66       * @param context faces context
67       */
68      public void processUpdates(FacesContext context) {
69          try {
70              super.processUpdates(context);
71          } catch (Exception e) {
72              handleException(context, e);
73              context.responseComplete();
74          }
75      }
76  
77      /***
78       * <p>Override to catch and handle exceptions in value change
79       * listeners that might have occured in the process validations
80       * phase.</p>
81       *
82       * @param context faces context
83       */
84      public void processValidators(FacesContext context) {
85          try {
86              super.processValidators(context);
87          } catch (Exception e) {
88              handleException(context, e);
89              context.responseComplete();           
90          }
91      }
92  
93  
94      /***
95       * <p>Handle the specified exception according to the strategy
96       * defined by our current {@link ExceptionHandler}.</p>
97       *
98       * @param context FacesContext for the current request
99       * @param exception Exception to be handled
100      */
101     private void handleException(FacesContext context, Exception exception) {
102 
103         if (context == null) {
104             exception.printStackTrace(System.out);
105             return;
106         }
107         ExceptionHandler handler = (ExceptionHandler)
108           context.getApplication().getVariableResolver().resolveVariable
109                 (context, Constants.EXCEPTION_HANDLER);
110         handler.handleException(exception);
111 
112     }
113 }