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.remoting.faces;
19  
20  import java.io.IOException;
21  import java.util.Iterator;
22  import java.util.ResourceBundle;
23  
24  import javax.faces.FacesException;
25  import javax.faces.context.FacesContext;
26  import javax.faces.event.PhaseEvent;
27  import javax.faces.event.PhaseId;
28  import javax.faces.event.PhaseListener;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.shale.remoting.Mapping;
33  import org.apache.shale.remoting.Mappings;
34  import org.apache.shale.remoting.Processor;
35  
36  /***
37   * <p>A JavaServer Faces <code>PhaseListener</code> that provides support for
38   * intercepting some incoming requests, and processing them separately from the
39   * standard Request Processing Lifecycle, without requiring explicit
40   * configuration (for example, of a Servlet or a Filter).</p>
41   */
42  public class RemotingPhaseListener implements PhaseListener {
43  
44  
45      // ------------------------------------------------------------ Constructors
46  
47  
48      /***
49       * Serial version UID.
50       */
51      private static final long serialVersionUID = 9188955342844983587L;
52  
53  
54      /***
55       * <p>Creates a new instance of RemotingPhaseListener.</p>
56       */
57      public RemotingPhaseListener() {
58      }
59  
60  
61      // ------------------------------------------------------ Instance Variables
62  
63  
64      /***
65       * <p><code>ResourceBundle</code> containing our localized messages.</p>
66       */
67      private ResourceBundle bundle =
68              ResourceBundle.getBundle("org.apache.shale.remoting.Bundle");
69  
70  
71      /***
72       * <p>Helper object to acquire a reference to the {@link Mappings}
73       * instance for this web application.</p>
74       */
75      private MappingsHelper helper = new MappingsHelper();
76  
77  
78      /***
79       * <p>Log instance for this class.</p>
80       */
81      private transient Log log = null;
82  
83  
84      // --------------------------------------------------- PhaseListener Methods
85  
86  
87      /***
88       * <p>Perform interception processing activities <em>after</em> the
89       * specified phase processing has been performed.</p>
90       *
91       * @param event <code>PhaseEvent</code> to be processed
92       */
93      public void afterPhase(PhaseEvent event) {
94  
95          // Acquire a reference to the FacesContext for this request
96          FacesContext context = event.getFacesContext();
97          if (log().isDebugEnabled()) {
98              log().debug("Checking view identifier '" + context.getViewRoot().getViewId() + "'");
99          }
100 
101         // Match this view identifier against our configured patterns
102         Iterator mappings = helper.getMappings(context).getMappings().iterator();
103         while (mappings.hasNext()) {
104             Mapping mapping = (Mapping) mappings.next();
105             String resourceId = mapping.mapViewId(context);
106             if (resourceId != null) {
107                 if (log().isTraceEnabled()) {
108                     log().trace("View identifier '" + context.getViewRoot().getViewId()
109                                 + "' matched pattern '" + mapping.getPattern()
110                                 + "' with resource id '" + resourceId + "'");
111                 }
112                 try {
113                     Processor processor = mapping.getProcessor();
114                     processor.process(context, resourceId);
115                 } catch (IOException e) {
116                     throw new FacesException(e);
117                 }
118                 break;
119             }
120         }
121 
122     }
123 
124 
125     /***
126      * <p>Perform interception processing activities <em>before</em> the
127      * specified phase processing has been performed.</p>
128      *
129      * @param event <code>PhaseEvent</code> to be processed
130      */
131     public void beforePhase(PhaseEvent event) {
132 
133         // No processing required
134 
135     }
136 
137 
138     /***
139      * <p>Return the identifier of the JavaServer Faces request processing
140      * lifecycle phase(s) that we are interested in.</p>
141      */
142     public PhaseId getPhaseId() {
143 
144         return PhaseId.RESTORE_VIEW;
145 
146     }
147 
148 
149     // --------------------------------------------------------- Support Methods
150 
151 
152     /***
153      * <p>Return the <code>Log</code> instance to use, creating one if needed.</p>
154      */
155     private Log log() {
156 
157         if (this.log == null) {
158             log = LogFactory.getLog(RemotingPhaseListener.class);
159         }
160         return log;
161 
162     }
163 
164 
165 }