View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
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  
17  package org.apache.struts.faces.application;
18  
19  
20  import java.io.IOException;
21  import java.util.Locale;
22  import javax.faces.FacesException;
23  import javax.faces.application.ViewHandler;
24  import javax.faces.component.UIViewRoot;
25  import javax.faces.context.ExternalContext;
26  import javax.faces.context.FacesContext;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.struts.Globals;
31  
32  
33  /***
34   * <p>Custom <code>ViewHandler</code> implementation that adds features
35   * specific to the Struts-Faces Integration Library.  It leverages the
36   * "decorator pattern" customization strategy that JSF supports, by
37   * delegating most processing to the <code>ViewHandler</code> instance
38   * handed to our constructor.</p>
39   */
40  
41  public class ViewHandlerImpl extends ViewHandler {
42  
43  
44      // ------------------------------------------------------------ Constructors
45  
46  
47      /***
48       * <p>Construct a <code>ViewHandlerImpl</code> decorating the
49       * specified <code>ViewHandler</code> instance.</p>
50       *
51       * @param handler <code>ViewHandler</code> to be decorated
52       */
53      public ViewHandlerImpl(ViewHandler handler) {
54          if (log.isDebugEnabled()) {
55              log.debug("Creating ViewHandler instance, wrapping handler " +
56                        handler);
57          }
58          this.handler = handler;
59      }
60  
61  
62      // ------------------------------------------------------ Instance Variables
63  
64  
65      /***
66       * <p>The <code>ViewHandler</code> instance that we are decorating.</p>
67       */
68      private ViewHandler handler = null;
69  
70  
71      // -------------------------------------------------------- Static Variables
72  
73  
74      /***
75       * <p>The <code>Log</code> instance for this class.</p>
76       */
77      private static final Log log =
78          LogFactory.getLog(ViewHandlerImpl.class);
79  
80  
81  
82      // -------------------------------------------------------------- Properties
83  
84  
85      /***
86       * <p>Return the <code>ViewHandler</code> instance we are decorating.</p>
87       */
88      public ViewHandler getHandler() {
89          return this.handler;
90      }
91  
92  
93      /***
94       * <p>Set the <code>ViewHandler</code> instance we are decorating.</p>
95       *
96       * @param handler <code>ViewHandler</code> instance to decorate
97       */
98      public void setHandler(ViewHandler handler) {
99          this.handler = handler;
100     }
101 
102 
103     // ----------------------------------------------------- Specialized Methods
104 
105 
106     /***
107      * <p>If the Struts application has set a <code>Locale</code>, pass it
108      * on to JSF prior to delegating the actual rendering.</p>
109      *
110      * @param context <code>FacesContext</code> for the current request
111      * @param view <code>UIViewRoot</code> to be rendered
112      */
113     public void renderView(FacesContext context, UIViewRoot view)
114         throws IOException, FacesException {
115 
116         if (log.isDebugEnabled()) {
117             log.debug("renderView(" + view.getViewId() + ")");
118         }
119         ExternalContext econtext = context.getExternalContext();
120         if (econtext.getSession(false) != null) {
121             Locale locale = (Locale)
122                 econtext.getSessionMap().get(Globals.LOCALE_KEY);
123             if (locale != null) {
124                 if (log.isTraceEnabled()) {
125                     log.trace("Setting view locale to " + locale);
126                 }
127                 view.setLocale(locale);
128             }
129         }
130         handler.renderView(context, view);
131 
132     }
133 
134 
135     // ------------------------------------------------------- Delegated Methods
136 
137 
138     // See ViewHandler JavaDocs for method descriptions
139 
140 
141     public Locale calculateLocale(FacesContext context) {
142         return handler.calculateLocale(context);
143     }
144 
145 
146     public String calculateRenderKitId(FacesContext context) {
147         return handler.calculateRenderKitId(context);
148     }
149 
150 
151     public UIViewRoot createView(FacesContext context, String viewId) {
152         return handler.createView(context, viewId);
153     }
154 
155 
156     public String getActionURL(FacesContext context, String viewId) {
157         return handler.getActionURL(context, viewId);
158     }
159 
160 
161     public String getResourceURL(FacesContext context, String viewId) {
162         return handler.getResourceURL(context, viewId);
163     }
164 
165 
166     public UIViewRoot restoreView(FacesContext context, String viewId) {
167         return handler.restoreView(context, viewId);
168     }
169 
170 
171     public void writeState(FacesContext context) throws IOException {
172         handler.writeState(context);
173     }
174 
175 
176 }