This project has retired. For details please refer to its Attic page.
PublisherRenderer xref
View Javadoc

1   /*
2    * $Id: PublisherRenderer.java 1306435 2012-03-28 15:39:11Z nlebas $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.request.render;
22  
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.apache.tiles.request.Request;
29  
30  /**
31   * Provides a Publisher-Subscriber implementation around the provided renderer to delegate to.
32   *
33   * @version $Rev: 1035784 $ $Date: 2010-11-16 20:24:12 +0000 (Tue, 16 Nov 2010) $
34   */
35  public class PublisherRenderer implements Renderer {
36  
37      public interface RendererListener{
38          /** Called before the delegate's render method is called. */
39          void start(String template, Request request) throws IOException;
40          /** Called after the delegate's render method is called. */
41          void end(String template, Request request) throws IOException;
42          /** If the delegate render method throws an IOException it is passed through this. */
43          void handleIOException(IOException ex, Request request) throws IOException;
44      }
45  
46      private final Renderer renderer;
47      private final List<RendererListener> listeners = new ArrayList<RendererListener>();
48      private final List<RendererListener> listenersReversed = new ArrayList<RendererListener>();
49  
50      public PublisherRenderer(Renderer renderer){
51          this.renderer = renderer;
52      }
53  
54      @Override
55      public void render(String path, Request request) throws IOException {
56          if (path == null) {
57              throw new CannotRenderException("Cannot dispatch a null path");
58          }
59          try{
60              for(RendererListener listener : listeners){
61                  listener.start(path, request);
62              }
63              renderer.render(path, request);
64          }catch(IOException ex){
65              handleIOException(ex, request);
66          }finally{
67              for(RendererListener wrapper : listenersReversed){
68                  wrapper.end(path, request);
69              }
70          }
71      }
72  
73      @Override
74      public boolean isRenderable(String path, Request request) {
75          return renderer.isRenderable(path, request);
76      }
77  
78      public void addListener(RendererListener listener){
79          listeners.add(listener);
80          listenersReversed.clear();
81          listenersReversed.addAll(listeners);
82          Collections.reverse(listenersReversed);
83      }
84  
85      private void handleIOException(IOException exception, Request request) throws IOException{
86          IOException ex = exception;
87          boolean throwIt = listeners.isEmpty();
88          for(RendererListener listener : listenersReversed){
89              try{
90                  listener.handleIOException(ex, request);
91                  throwIt = false;
92              }catch(IOException newEx){
93                  ex = newEx;
94                  throwIt = true;
95              }
96          }
97          if(throwIt){
98              throw ex;
99          }
100     }
101 }