1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.aggregator.impl;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.jetspeed.aggregator.PortletTrackingManager;
27 import org.apache.jetspeed.aggregator.RenderTrackable;
28 import org.apache.jetspeed.container.window.PortletWindowAccessor;
29 import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
30 import org.apache.pluto.om.window.PortletWindow;
31
32 /***
33 * Tracks out of service status for portlets
34 *
35 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
36 * @version $Id: $
37 */
38 public class PortletTrackingManagerImpl implements PortletTrackingManager
39 {
40 protected Map outOfService = Collections.synchronizedMap(new HashMap());
41
42 /***
43 * when rendering a portlet, the default timeout period in milliseconds
44 * setting to zero will disable (no timeout) the timeout
45 *
46 */
47 protected long defaultPortletTimeout;
48
49 /***
50 * Out of service limit, if a portlet entity times out past its limit (or default limit) n consecutive times,
51 * it is taken out of service
52 */
53 protected int outOfServiceLimit;
54
55 protected PortletWindowAccessor windowAccessor;
56
57 public PortletTrackingManagerImpl(PortletWindowAccessor windowAccessor, long defaultPortletTimeout, int outOfServiceLimit)
58 {
59 this.windowAccessor = windowAccessor;
60 this.defaultPortletTimeout = defaultPortletTimeout;
61 this.outOfServiceLimit = outOfServiceLimit;
62 }
63
64 public long getDefaultPortletTimeout()
65 {
66 return this.defaultPortletTimeout;
67 }
68
69 public boolean exceededTimeout(long renderTime, PortletWindow window)
70 {
71 RenderTrackable trackInfo = (RenderTrackable)window.getPortletEntity();
72 long defaultTimeout = this.getDefaultPortletTimeout();
73 if (trackInfo.getExpiration() > 0)
74 {
75 return (renderTime > trackInfo.getExpiration());
76 }
77 else if (defaultTimeout > 0)
78 {
79 return (renderTime > defaultTimeout);
80 }
81 return false;
82 }
83
84 public boolean isOutOfService(PortletWindow window)
85 {
86 RenderTrackable trackable = (RenderTrackable)window.getPortletEntity();
87 if (trackable.getRenderTimeoutCount() > this.outOfServiceLimit)
88 {
89 return true;
90 }
91 return false;
92 }
93
94 public int getOutOfServiceLimit()
95 {
96 return this.outOfServiceLimit;
97 }
98
99 public void incrementRenderTimeoutCount(PortletWindow window)
100 {
101 RenderTrackable trackable = (RenderTrackable)window.getPortletEntity();
102 trackable.incrementRenderTimeoutCount();
103 }
104
105 public void success(PortletWindow window)
106 {
107 RenderTrackable trackable = (RenderTrackable)window.getPortletEntity();
108 trackable.success();
109 }
110
111 public void setExpiration(PortletWindow window, long expiration)
112 {
113 RenderTrackable trackable = (RenderTrackable)window.getPortletEntity();
114 trackable.setExpiration(expiration);
115 }
116
117 public void takeOutOfService(PortletWindow window)
118 {
119 RenderTrackable trackable = (RenderTrackable)window.getPortletEntity();
120 trackable.setRenderTimeoutCount((int)this.defaultPortletTimeout + 1);
121 }
122
123 public void putIntoService(PortletWindow window)
124 {
125 RenderTrackable trackable = (RenderTrackable)window.getPortletEntity();
126 trackable.setRenderTimeoutCount(0);
127 }
128
129 public void putIntoService(List fullPortletNames)
130 {
131 Iterator windows = this.windowAccessor.getPortletWindows().iterator();
132 while (windows.hasNext())
133 {
134 PortletWindow window = (PortletWindow)windows.next();
135 PortletDefinitionComposite pd = (PortletDefinitionComposite)window.getPortletEntity().getPortletDefinition();
136 for (int ix = 0; ix < fullPortletNames.size(); ix++)
137 {
138 if (pd.getUniqueName().equals(fullPortletNames.get(ix)))
139 {
140 putIntoService(window);
141 }
142 }
143 }
144 }
145
146 public List getOutOfServiceList(String fullPortletName)
147 {
148 List outs = new ArrayList();
149 Iterator windows = this.windowAccessor.getPortletWindows().iterator();
150 while (windows.hasNext())
151 {
152 PortletWindow window = (PortletWindow)windows.next();
153 PortletDefinitionComposite pd = (PortletDefinitionComposite)window.getPortletEntity().getPortletDefinition();
154 if (pd.getUniqueName().equals(fullPortletName) && isOutOfService(window))
155 {
156 outs.add(window);
157 }
158 }
159 return outs;
160 }
161
162 public List getOutOfServiceList()
163 {
164 List outs = new ArrayList();
165 Iterator windows = this.windowAccessor.getPortletWindows().iterator();
166 while (windows.hasNext())
167 {
168 PortletWindow window = (PortletWindow)windows.next();
169 if (isOutOfService(window))
170 {
171 outs.add(window);
172 }
173 }
174 return outs;
175 }
176 }