Coverage Report - org.apache.tiles.request.render.PublisherRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
PublisherRenderer
56%
21/37
50%
5/10
2.125
PublisherRenderer$RendererListener
N/A
N/A
2.125
 
 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  2
     private final List<RendererListener> listeners = new ArrayList<RendererListener>();
 48  2
     private final List<RendererListener> listenersReversed = new ArrayList<RendererListener>();
 49  
 
 50  2
     public PublisherRenderer(Renderer renderer){
 51  2
         this.renderer = renderer;
 52  2
     }
 53  
 
 54  
     @Override
 55  
     public void render(String path, Request request) throws IOException {
 56  1
         if (path == null) {
 57  0
             throw new CannotRenderException("Cannot dispatch a null path");
 58  
         }
 59  
         try{
 60  1
             for(RendererListener listener : listeners){
 61  1
                 listener.start(path, request);
 62  1
             }
 63  1
             renderer.render(path, request);
 64  0
         }catch(IOException ex){
 65  0
             handleIOException(ex, request);
 66  
         }finally{
 67  1
             for(RendererListener wrapper : listenersReversed){
 68  1
                 wrapper.end(path, request);
 69  1
             }
 70  1
         }
 71  1
     }
 72  
 
 73  
     @Override
 74  
     public boolean isRenderable(String path, Request request) {
 75  1
         return renderer.isRenderable(path, request);
 76  
     }
 77  
 
 78  
     public void addListener(RendererListener listener){
 79  2
         listeners.add(listener);
 80  2
         listenersReversed.clear();
 81  2
         listenersReversed.addAll(listeners);
 82  2
         Collections.reverse(listenersReversed);
 83  2
     }
 84  
 
 85  
     private void handleIOException(IOException exception, Request request) throws IOException{
 86  0
         IOException ex = exception;
 87  0
         boolean throwIt = listeners.isEmpty();
 88  0
         for(RendererListener listener : listenersReversed){
 89  
             try{
 90  0
                 listener.handleIOException(ex, request);
 91  0
                 throwIt = false;
 92  0
             }catch(IOException newEx){
 93  0
                 ex = newEx;
 94  0
                 throwIt = true;
 95  0
             }
 96  0
         }
 97  0
         if(throwIt){
 98  0
             throw ex;
 99  
         }
 100  0
     }
 101  
 }