1 /*
2 * $Id: LifecycleImpl.java,v 1.83.4.1 2007/08/28 06:01:37 rlubke Exp $
3 */
4
5 /*
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
7 *
8 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
9 *
10 * The contents of this file are subject to the terms of either the GNU
11 * General Public License Version 2 only ("GPL") or the Common Development
12 * and Distribution License("CDDL") (collectively, the "License"). You
13 * may not use this file except in compliance with the License. You can obtain
14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
16 * language governing permissions and limitations under the License.
17 *
18 * When distributing the software, include this License Header Notice in each
19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
20 * Sun designates this particular file as subject to the "Classpath" exception
21 * as provided by Sun in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the License
23 * Header, with the fields enclosed by brackets [] replaced by your own
24 * identifying information: "Portions Copyrighted [year]
25 * [name of copyright owner]"
26 *
27 * Contributor(s):
28 *
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41 package com.sun.faces.lifecycle;
42
43 import java.util.List;
44 import java.util.concurrent.CopyOnWriteArrayList;
45 import java.util.logging.Level;
46 import java.util.logging.Logger;
47
48 import javax.faces.FacesException;
49 import javax.faces.context.FacesContext;
50 import javax.faces.event.PhaseListener;
51 import javax.faces.lifecycle.Lifecycle;
52
53 import com.sun.faces.util.FacesLogger;
54 import com.sun.faces.util.MessageUtils;
55
56
57 /**
58 * <p><b>LifecycleImpl</b> is the stock implementation of the standard
59 * Lifecycle in the JavaServer Faces RI.</p>
60 */
61
62 public class LifecycleImpl extends Lifecycle {
63
64
65 // -------------------------------------------------------- Static Variables
66
67
68 // Log instance for this class
69 private static Logger LOGGER = FacesLogger.LIFECYCLE.getLogger();
70
71
72 // ------------------------------------------------------ Instance Variables
73
74 // The Phase instance for the render() method
75 private Phase response = new RenderResponsePhase();
76
77 // The set of Phase instances that are executed by the execute() method
78 // in order by the ordinal property of each phase
79 private Phase[] phases = {
80 null, // ANY_PHASE placeholder, not a real Phase
81 new RestoreViewPhase(),
82 new ApplyRequestValuesPhase(),
83 new ProcessValidationsPhase(),
84 new UpdateModelValuesPhase(),
85 new InvokeApplicationPhase(),
86 response
87 };
88
89 // List for registered PhaseListeners
90 private List<PhaseListener> listeners =
91 new CopyOnWriteArrayList<PhaseListener>();
92
93
94
95 // ------------------------------------------------------- Lifecycle Methods
96
97
98 // Execute the phases up to but not including Render Response
99 public void execute(FacesContext context) throws FacesException {
100
101 if (context == null) {
102 throw new NullPointerException
103 (MessageUtils.getExceptionMessageString
104 (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "context"));
105 }
106
107 if (LOGGER.isLoggable(Level.FINE)) {
108 LOGGER.fine("execute(" + context + ")");
109 }
110
111 for (int i = 1, len = phases.length -1 ; i < len; i++) { // Skip ANY_PHASE placeholder
112
113 if (context.getRenderResponse() ||
114 context.getResponseComplete()) {
115 break;
116 }
117
118 phases[i].doPhase(context, this, listeners.listIterator());
119
120 }
121
122 }
123
124
125 // Execute the Render Response phase
126 public void render(FacesContext context) throws FacesException {
127
128 if (context == null) {
129 throw new NullPointerException
130 (MessageUtils.getExceptionMessageString
131 (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "context"));
132 }
133
134 if (LOGGER.isLoggable(Level.FINE)) {
135 LOGGER.fine("render(" + context + ")");
136 }
137
138 if (!context.getResponseComplete()) {
139 response.doPhase(context, this, listeners.listIterator());
140 }
141
142 }
143
144
145 // Add a new PhaseListener to the set of registered listeners
146 public void addPhaseListener(PhaseListener listener) {
147
148 if (listener == null) {
149 throw new NullPointerException
150 (MessageUtils.getExceptionMessageString
151 (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "listener"));
152 }
153
154 if (listeners == null) {
155 listeners = new CopyOnWriteArrayList<PhaseListener>();
156 }
157
158 if (listeners.contains(listener)) {
159 if (LOGGER.isLoggable(Level.FINE)) {
160 LOGGER.log(Level.FINE,
161 "jsf.lifecycle.duplicate_phase_listener_detected",
162 listener.getClass().getName());
163 }
164 } else {
165 if (LOGGER.isLoggable(Level.FINE)) {
166 LOGGER.log(Level.FINE,
167 "addPhaseListener({0},{1})",
168 new Object[]{
169 listener.getPhaseId().toString(),
170 listener.getClass().getName()});
171 }
172 listeners.add(listener);
173 }
174
175 }
176
177
178 // Return the set of PhaseListeners that have been registered
179 public PhaseListener[] getPhaseListeners() {
180
181 return listeners.toArray(new PhaseListener[listeners.size()]);
182
183 }
184
185
186 // Remove a registered PhaseListener from the set of registered listeners
187 public void removePhaseListener(PhaseListener listener) {
188
189 if (listener == null) {
190 throw new NullPointerException
191 (MessageUtils.getExceptionMessageString
192 (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "listener"));
193 }
194
195 if (listeners.remove(listener) && LOGGER.isLoggable(Level.FINE)) {
196 LOGGER.log(Level.FINE,
197 "removePhaseListener({0})",
198 new Object[]{listener.getClass().getName()});
199 }
200
201 }
202
203 }