This project has retired. For details please refer to its
Attic page.
PublisherRenderer xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
32
33
34
35 public class PublisherRenderer implements Renderer {
36
37 public interface RendererListener{
38
39 void start(String template, Request request) throws IOException;
40
41 void end(String template, Request request) throws IOException;
42
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 }