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  
18  package org.apache.shale.view.faces;
19  
20  import javax.faces.context.FacesContext;
21  import org.apache.shale.view.Constants;
22  import org.apache.shale.view.ExceptionHandler;
23  import org.apache.shale.view.ViewController;
24  
25  /***
26   * <p>Utility class to perform the event callbacks specified by the
27   * {@link ViewController} interface.  The method signatures make it
28   * possible to specialize the behavior for annotated callbacks in the
29   * <em>Shale Tiger Extensions</em> module.</p>
30   *
31   * @since 1.0.1
32   *
33   * $Id: ViewControllerCallbacks.java 464373 2006-10-16 04:21:54Z rahul $
34   */
35  public class ViewControllerCallbacks {
36  
37  
38      // ------------------------------------------------------------ Constructors
39  
40  
41      // ------------------------------------------------------ Instance Variables
42  
43  
44      // ---------------------------------------------------------- Public Methods
45  
46  
47      /***
48       * <p>Perform the <code>preprocess</code> callback on the specified
49       * instance.</p>
50       *
51       * @param instance Bean instance on which to perform this callback
52       */
53      public void preprocess(Object instance) {
54  
55          if (instance instanceof ViewController) {
56              try {
57                  ((ViewController) instance).preprocess();
58              } catch (Exception e) {
59                  handleException(FacesContext.getCurrentInstance(), e);
60              }
61          }
62  
63      }
64  
65  
66      /***
67       * <p>Perform the <code>prerender</code> callback on the specified
68       * instance.</p>
69       *
70       * @param instance Bean instance on which to perform this callback
71       */
72      public void prerender(Object instance) {
73  
74          if (instance instanceof ViewController) {
75              try {
76                  ((ViewController) instance).prerender();
77              } catch (Exception e) {
78                  handleException(FacesContext.getCurrentInstance(), e);
79              }
80          }
81  
82      }
83  
84  
85      // ------------------------------------------------------- Protected Methods
86  
87  
88      /***
89       * <p>Handle the specified exception according to the strategy
90       * defined by our current {@link ExceptionHandler}.</p>
91       *
92       * @param context FacesContext for the current request
93       * @param exception Exception to be handled
94       */
95      protected void handleException(FacesContext context, Exception exception) {
96  
97          if (context == null) {
98              exception.printStackTrace(System.out);
99              return;
100         }
101         ExceptionHandler handler = (ExceptionHandler)
102           context.getApplication().getVariableResolver().resolveVariable
103                 (context, Constants.EXCEPTION_HANDLER);
104         handler.handleException(exception);
105 
106     }
107 
108 
109 }