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 | 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 | |
} |