001: /*
002: * $Id: ViewHandlerImpl.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.faces.application;
023:
024: import java.io.IOException;
025: import java.util.Locale;
026: import javax.faces.FacesException;
027: import javax.faces.application.ViewHandler;
028: import javax.faces.component.UIViewRoot;
029: import javax.faces.context.ExternalContext;
030: import javax.faces.context.FacesContext;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.struts.Globals;
035:
036: /**
037: * <p>Custom <code>ViewHandler</code> implementation that adds features
038: * specific to the Struts-Faces Integration Library. It leverages the
039: * "decorator pattern" customization strategy that JSF supports, by
040: * delegating most processing to the <code>ViewHandler</code> instance
041: * handed to our constructor.</p>
042: */
043:
044: public class ViewHandlerImpl extends ViewHandler {
045:
046: // ------------------------------------------------------------ Constructors
047:
048: /**
049: * <p>Construct a <code>ViewHandlerImpl</code> decorating the
050: * specified <code>ViewHandler</code> instance.</p>
051: *
052: * @param handler <code>ViewHandler</code> to be decorated
053: */
054: public ViewHandlerImpl(ViewHandler handler) {
055: if (log.isDebugEnabled()) {
056: log
057: .debug("Creating ViewHandler instance, wrapping handler "
058: + handler);
059: }
060: this .handler = handler;
061: }
062:
063: // ------------------------------------------------------ Instance Variables
064:
065: /**
066: * <p>The <code>ViewHandler</code> instance that we are decorating.</p>
067: */
068: private ViewHandler handler = null;
069:
070: // -------------------------------------------------------- Static Variables
071:
072: /**
073: * <p>The <code>Log</code> instance for this class.</p>
074: */
075: private static final Log log = LogFactory
076: .getLog(ViewHandlerImpl.class);
077:
078: // -------------------------------------------------------------- Properties
079:
080: /**
081: * <p>Return the <code>ViewHandler</code> instance we are decorating.</p>
082: */
083: public ViewHandler getHandler() {
084: return this .handler;
085: }
086:
087: /**
088: * <p>Set the <code>ViewHandler</code> instance we are decorating.</p>
089: *
090: * @param handler <code>ViewHandler</code> instance to decorate
091: */
092: public void setHandler(ViewHandler handler) {
093: this .handler = handler;
094: }
095:
096: // ----------------------------------------------------- Specialized Methods
097:
098: /**
099: * <p>If the Struts application has set a <code>Locale</code>, pass it
100: * on to JSF prior to delegating the actual rendering.</p>
101: *
102: * @param context <code>FacesContext</code> for the current request
103: * @param view <code>UIViewRoot</code> to be rendered
104: */
105: public void renderView(FacesContext context, UIViewRoot view)
106: throws IOException, FacesException {
107:
108: if (log.isDebugEnabled()) {
109: log.debug("renderView(" + view.getViewId() + ")");
110: }
111: ExternalContext econtext = context.getExternalContext();
112: if (econtext.getSession(false) != null) {
113: Locale locale = (Locale) econtext.getSessionMap().get(
114: Globals.LOCALE_KEY);
115: if (locale != null) {
116: if (log.isTraceEnabled()) {
117: log.trace("Setting view locale to " + locale);
118: }
119: view.setLocale(locale);
120: }
121: }
122: handler.renderView(context, view);
123:
124: }
125:
126: // ------------------------------------------------------- Delegated Methods
127:
128: // See ViewHandler JavaDocs for method descriptions
129:
130: public Locale calculateLocale(FacesContext context) {
131: return handler.calculateLocale(context);
132: }
133:
134: public String calculateRenderKitId(FacesContext context) {
135: return handler.calculateRenderKitId(context);
136: }
137:
138: public UIViewRoot createView(FacesContext context, String viewId) {
139: return handler.createView(context, viewId);
140: }
141:
142: public String getActionURL(FacesContext context, String viewId) {
143: return handler.getActionURL(context, viewId);
144: }
145:
146: public String getResourceURL(FacesContext context, String viewId) {
147: return handler.getResourceURL(context, viewId);
148: }
149:
150: public UIViewRoot restoreView(FacesContext context, String viewId) {
151: return handler.restoreView(context, viewId);
152: }
153:
154: public void writeState(FacesContext context) throws IOException {
155: handler.writeState(context);
156: }
157:
158: }
|