KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portletcontainer > impl > monitor > PortletMonitor


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5
6
7 package org.exoplatform.services.portletcontainer.impl.monitor;
8
9 import org.apache.commons.logging.Log;
10 import org.exoplatform.services.cache.CacheService;
11 import org.exoplatform.services.cache.ExoCache;
12 import org.exoplatform.services.log.LogService;
13 import org.exoplatform.services.portletcontainer.PortletContainerConstants;
14 import org.exoplatform.services.portletcontainer.monitor.*;
15
16 import javax.portlet.PortletMode;
17 import javax.portlet.WindowState;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Collections JavaDoc;
21 /**
22  * Created by The eXo Platform SARL
23  * Author : Mestrallet Benjamin
24  * benjmestrallet@users.sourceforge.net
25  * Date: Sep 10, 2003
26  * Time: 2:36:19 PM
27  */

28 public class PortletMonitor implements PortletContainerMonitor {
29
30   public static final char SEPARATOR = '/';
31   public static Map JavaDoc versionNumberMap = new HashMap JavaDoc();
32
33   private Map JavaDoc runtimeDatas_;
34   private Map JavaDoc destroyedPortlets_;
35   private Map JavaDoc brokenPortlets_;
36   private Log log_;
37   private ExoCache globalCache_;
38   private CacheService cacheService_;
39
40   public PortletMonitor(LogService logService, CacheService cacheService) throws Exception JavaDoc {
41     this.log_ = logService.getLog("org.exoplatform.services.portletcontainer");
42     this.cacheService_ = cacheService;
43     globalCache_ = cacheService.getCacheInstance(PortletContainerConstants.GLOBAL_SCOPE_CACHE);
44     runtimeDatas_ = Collections.synchronizedMap(new HashMap JavaDoc());
45     brokenPortlets_ = Collections.synchronizedMap(new HashMap JavaDoc());
46     destroyedPortlets_ = Collections.synchronizedMap(new HashMap JavaDoc());
47   }
48
49   public Map JavaDoc getPortletRuntimeDataMap() {
50     return runtimeDatas_;
51   }
52
53   public PortletRuntimeDatasImpl getPortletRuntimeData(String JavaDoc appName, String JavaDoc portletName) {
54     return (PortletRuntimeDatasImpl) runtimeDatas_.get(appName + SEPARATOR + portletName);
55   }
56
57   public synchronized void registerPortletApp(String JavaDoc portletApplicationName) {
58     long versionNumber = 1;
59     if (versionNumberMap.get(portletApplicationName) != null) {
60       versionNumber = ((Long JavaDoc) versionNumberMap.get(portletApplicationName)).longValue() + 1;
61     }
62     versionNumberMap.put(portletApplicationName, new Long JavaDoc(versionNumber));
63   }
64
65   public long getPortletVersionNumber(String JavaDoc portletAppName) {
66     return ((Long JavaDoc) versionNumberMap.get(portletAppName)).longValue();
67   }
68
69   public synchronized void register(String JavaDoc portletApplicationName, String JavaDoc portletName) {
70     PortletRuntimeData rD =
71       new PortletRuntimeDatasImpl(portletApplicationName, portletName,
72                                   cacheService_, globalCache_, log_);
73     runtimeDatas_.put(portletApplicationName + SEPARATOR + portletName, rD);
74     brokenPortlets_.remove(portletApplicationName + SEPARATOR + portletName);
75     destroyedPortlets_.remove(portletApplicationName + SEPARATOR + portletName);
76   }
77
78   public boolean isInitialized(String JavaDoc portletAppName, String JavaDoc portletName) {
79     PortletRuntimeData datas = (PortletRuntimeData) runtimeDatas_.get(portletAppName +
80         SEPARATOR + portletName);
81     if (datas == null) {
82       return false;
83     }
84     if (datas.isInitialized()) {
85       return true;
86     }
87     return false;
88   }
89
90   public synchronized void init(String JavaDoc portletAppName, String JavaDoc portletName, int cacheExpirationTime) {
91     PortletRuntimeDatasImpl datas = (PortletRuntimeDatasImpl) runtimeDatas_.
92         get(portletAppName + SEPARATOR + portletName);
93     datas.setInitialized(true);
94     datas.setCacheExpirationPeriod(cacheExpirationTime);
95   }
96
97   public synchronized void brokePortlet(String JavaDoc portletAppName, String JavaDoc portletName) {
98     PortletRuntimeData datas = (PortletRuntimeData) runtimeDatas_.get(portletAppName +
99         SEPARATOR + portletName);
100     if (datas == null) {
101       datas = (PortletRuntimeData) destroyedPortlets_.get(portletAppName + SEPARATOR + portletName);
102       destroyedPortlets_.remove(portletAppName + SEPARATOR + portletName);
103     }
104     runtimeDatas_.remove(portletAppName + SEPARATOR + portletName);
105     brokenPortlets_.put(portletAppName + SEPARATOR + portletName, datas);
106   }
107
108   public boolean isBroken(String JavaDoc portletAppName, String JavaDoc portletName) {
109     PortletRuntimeData datas = (PortletRuntimeData) brokenPortlets_.get(portletAppName + SEPARATOR +
110         portletName);
111     if (datas != null) {
112       return true;
113     }
114     return false ;
115   }
116
117   public boolean isDestroyed(String JavaDoc portletAppName, String JavaDoc portletName) {
118     PortletRuntimeData datas = (PortletRuntimeData) destroyedPortlets_.get(portletAppName + SEPARATOR +
119         portletName);
120     if (datas != null) {
121       return true;
122     }
123     return false;
124   }
125
126   public boolean isAvailable(String JavaDoc portletApplicationName, String JavaDoc portletName, long l) {
127     PortletRuntimeDatasImpl datas = (PortletRuntimeDatasImpl) runtimeDatas_.
128         get(portletApplicationName + SEPARATOR + portletName);
129     if (datas == null) {
130       return false;
131     }
132     return datas.isAvailable(l);
133   }
134   
135   public boolean isAvailable(String JavaDoc portletApplicationName, String JavaDoc portletName) {
136     PortletRuntimeDatasImpl datas = (PortletRuntimeDatasImpl) runtimeDatas_.
137         get(portletApplicationName + SEPARATOR + portletName);
138     if (datas == null) {
139       return false;
140     }
141     return datas.isAvailable(System.currentTimeMillis());
142   }
143   
144   public long whenAvailable(String JavaDoc portletApplicationName, String JavaDoc portletName){
145     PortletRuntimeDatasImpl datas = (PortletRuntimeDatasImpl) runtimeDatas_.
146     get(portletApplicationName + SEPARATOR + portletName);
147     if (datas == null) {
148       return -1;
149     }
150     if(datas.isAvailable(System.currentTimeMillis()))
151       return 0;
152     else
153       return datas.whenAvailable();
154   }
155
156   public boolean isInitialisationAllowed(String JavaDoc portletApplicationName, String JavaDoc portletName, long l) {
157     PortletRuntimeDatasImpl datas = (PortletRuntimeDatasImpl) runtimeDatas_.
158         get(portletApplicationName + SEPARATOR + portletName);
159     if (datas == null) {
160       return false;
161     }
162     return datas.isInitialisationAllowed(l);
163   }
164
165   public synchronized void destroy(String JavaDoc portletApplicationName, String JavaDoc portletName) {
166     PortletRuntimeData datas = (PortletRuntimeData) runtimeDatas_.
167         get(portletApplicationName + SEPARATOR + portletName);
168     if (datas == null) {
169       return;//already destroyed or broke
170
}
171     runtimeDatas_.remove(portletApplicationName + SEPARATOR + portletName);
172     destroyedPortlets_.put(portletApplicationName + SEPARATOR + portletName, datas);
173   }
174
175   public void setLastAccessTime(String JavaDoc portletAppName, String JavaDoc portletName, long l) {
176     PortletRuntimeDatasImpl datas = (PortletRuntimeDatasImpl) runtimeDatas_.
177         get(portletAppName + SEPARATOR + portletName);
178     if (datas == null) {//look in broken portlets
179
datas = (PortletRuntimeDatasImpl) brokenPortlets_.get(portletAppName + SEPARATOR + portletName);
180     }
181     if (datas == null) {//look in destroyed portlets
182
datas = (PortletRuntimeDatasImpl) destroyedPortlets_.get(portletAppName + SEPARATOR + portletName);
183     }
184     datas.setLastAccessTime(l);
185   }
186
187   public void setLastInitFailureAccessTime(String JavaDoc portletAppName, String JavaDoc portletName, long l) {
188     PortletRuntimeData datas = (PortletRuntimeData) runtimeDatas_.
189         get(portletAppName + SEPARATOR + portletName);
190     datas.setLastInitFailureAccessTime(l);
191   }
192
193   public void setLastFailureAccessTime(String JavaDoc portletAppName, String JavaDoc portletName, long l) {
194     PortletRuntimeDatasImpl datas =
195       (PortletRuntimeDatasImpl) runtimeDatas_.get(portletAppName + SEPARATOR + portletName);
196     datas.setLastFailureAccessTime(l);
197   }
198
199   public void setUnavailabilityPeriod(String JavaDoc portletAppName, String JavaDoc portletName, int unavailableSeconds) {
200     PortletRuntimeDatasImpl datas = (PortletRuntimeDatasImpl) runtimeDatas_.
201         get(portletAppName + SEPARATOR + portletName);
202     datas.setUnavailabilityPeriod(unavailableSeconds * 1000);
203   }
204
205   public boolean isDataCached(String JavaDoc portletApplicationName, String JavaDoc portletName,
206                               String JavaDoc key, boolean isCacheGlobal) {
207     PortletRuntimeData datas =
208       (PortletRuntimeData) runtimeDatas_.get(portletApplicationName + SEPARATOR + portletName);
209     return datas.isDataCached(key, isCacheGlobal);
210   }
211
212   public void removeCachedData(String JavaDoc portletApplicationName, String JavaDoc portletName,
213                                String JavaDoc key, boolean isCacheGlobal) {
214     PortletRuntimeDatasImpl datas =
215       (PortletRuntimeDatasImpl) runtimeDatas_.get(portletApplicationName + SEPARATOR + portletName);
216     datas.removeCachedData(key, isCacheGlobal);
217   }
218
219   public int getCacheExpirationPeriod(String JavaDoc portletApplicationName, String JavaDoc portletName) {
220     PortletRuntimeData datas =
221       (PortletRuntimeData) runtimeDatas_.get(portletApplicationName + SEPARATOR + portletName);
222     return datas.getCacheExpirationPeriod();
223   }
224
225   public void setCacheExpirationPeriod(String JavaDoc portletAppName, String JavaDoc portletName, int i) {
226     PortletRuntimeDatasImpl datas =
227       (PortletRuntimeDatasImpl) runtimeDatas_.get(portletAppName + SEPARATOR + portletName);
228     datas.setCacheExpirationPeriod(i);
229   }
230
231   public long getPortletLastAccessTime(String JavaDoc portletAppName, String JavaDoc portletName,
232                                        String JavaDoc key, boolean isCacheGlobal) {
233     PortletRuntimeDatasImpl datas =
234       (PortletRuntimeDatasImpl) runtimeDatas_.get(portletAppName + SEPARATOR + portletName);
235     CachedData cachedData = datas.getCachedData(key, isCacheGlobal);
236     if (cachedData != null) {
237       return ((CachedDataImpl) cachedData).getLastAccessTime();
238     }
239     return 0;
240   }
241
242   public void setPortletLastAccessTime(String JavaDoc portletAppName, String JavaDoc portletName,
243                                        String JavaDoc key, long lastAccessTime,
244                                        boolean isCacheGlobal) {
245     PortletRuntimeDatasImpl datas =
246       (PortletRuntimeDatasImpl) runtimeDatas_.get(portletAppName + SEPARATOR + portletName);
247     CachedDataImpl cachedData = (CachedDataImpl) datas.getCachedData(key, isCacheGlobal);
248     if (cachedData == null) {
249       cachedData = new CachedDataImpl();
250       cachedData.setLastAccessTime(lastAccessTime);
251       datas.setCachedData(key, cachedData, isCacheGlobal);
252     } else {
253       cachedData.setLastAccessTime(lastAccessTime);
254     }
255   }
256
257   public void setCachedTitle(String JavaDoc portletAppName, String JavaDoc portletName,
258                              String JavaDoc key, String JavaDoc title, boolean isCacheGlobal) {
259     PortletRuntimeDatasImpl datas =
260       (PortletRuntimeDatasImpl) runtimeDatas_.get(portletAppName + SEPARATOR + portletName);
261     CachedDataImpl cachedData = (CachedDataImpl) datas.getCachedData(key, isCacheGlobal);
262     if (cachedData == null) {
263       cachedData = new CachedDataImpl();
264       cachedData.setTitle(title);
265       datas.setCachedData(key, cachedData, isCacheGlobal);
266     } else {
267       cachedData.setTitle(title);
268     }
269   }
270
271   public String JavaDoc getCachedTitle(String JavaDoc portletAppName, String JavaDoc portletName,
272                                String JavaDoc key, boolean isCacheGlobal) {
273     PortletRuntimeData datas =
274       (PortletRuntimeData) runtimeDatas_.get(portletAppName + SEPARATOR + portletName);
275     CachedData cachedData = datas.getCachedData(key, isCacheGlobal);
276     if (cachedData != null) {
277       return cachedData.getTitle();
278     }
279     return null;
280   }
281
282   public void setCachedContent(String JavaDoc portletAppName, String JavaDoc portletName,
283                                String JavaDoc key, char[] content,
284                                boolean isCacheGlobal) {
285     PortletRuntimeDatasImpl datas =
286       (PortletRuntimeDatasImpl) runtimeDatas_.get(portletAppName + SEPARATOR + portletName);
287     CachedDataImpl cachedData = (CachedDataImpl) datas.getCachedData(key, isCacheGlobal);
288     if (cachedData == null) {
289       cachedData = new CachedDataImpl();
290       cachedData.setContent(content);
291       datas.setCachedData(key, cachedData, isCacheGlobal);
292     } else {
293       cachedData.setContent(content);
294     }
295   }
296
297   public char[] getCachedContent(String JavaDoc portletAppName, String JavaDoc portletName,
298                                  String JavaDoc key, boolean isCacheGlobal) {
299     PortletRuntimeData datas =
300       (PortletRuntimeData) runtimeDatas_.get(portletAppName + SEPARATOR + portletName);
301     CachedData cachedData = datas.getCachedData(key, isCacheGlobal);
302     if (cachedData != null) {
303       return cachedData.getContent();
304     }
305     return null;
306   }
307
308   public void setCachedMode(String JavaDoc portletAppName, String JavaDoc portletName,
309                             String JavaDoc key, PortletMode mode,
310                             boolean isCacheGlobal) {
311     PortletRuntimeDatasImpl datas =
312       (PortletRuntimeDatasImpl) runtimeDatas_.get(portletAppName + SEPARATOR + portletName);
313     CachedDataImpl cachedData = (CachedDataImpl) datas.getCachedData(key, isCacheGlobal);
314     if (cachedData == null) {
315       cachedData = new CachedDataImpl();
316       cachedData.setMode(mode);
317       datas.setCachedData(key, cachedData, isCacheGlobal);
318     } else {
319       cachedData.setMode(mode);
320     }
321   }
322
323   public void setCachedWindowState(String JavaDoc portletAppName, String JavaDoc portletName,
324                                    String JavaDoc key, WindowState window,
325                                    boolean isCacheGlobal) {
326     PortletRuntimeDatasImpl datas =
327       (PortletRuntimeDatasImpl) runtimeDatas_.get(portletAppName + SEPARATOR + portletName);
328     CachedDataImpl cachedData = (CachedDataImpl) datas.getCachedData(key, isCacheGlobal);
329     if (cachedData == null) {
330       cachedData = new CachedDataImpl();
331       cachedData.setWindowState(window);
332       datas.setCachedData(key, cachedData, isCacheGlobal);
333     } else {
334       cachedData.setWindowState(window);
335     }
336   }
337
338   public boolean needsCacheInvalidation(String JavaDoc portletAppName, String JavaDoc portletName,
339                                         String JavaDoc key, PortletMode mode,
340                                         WindowState window,
341                                         boolean isCacheGlobal) {
342     PortletRuntimeData datas =
343       (PortletRuntimeData) runtimeDatas_.get(portletAppName + SEPARATOR + portletName);
344     CachedData cachedData = datas.getCachedData(key, isCacheGlobal);
345     if (cachedData == null) {
346       return false;
347     }
348     if (cachedData.getMode() != mode || cachedData.getWindowState() != window) {
349       return true;
350     }
351     return false;
352   }
353
354   public void setInitializationTime(String JavaDoc portletAppName, String JavaDoc portletName, long accessTime) {
355     PortletRuntimeDatasImpl datas =
356       (PortletRuntimeDatasImpl) runtimeDatas_.get(portletAppName + SEPARATOR + portletName);
357     datas.setInitializationTime(accessTime);
358   }
359
360   public long getInitializationTime(String JavaDoc portletAppName, String JavaDoc portletName) {
361     PortletRuntimeData datas =
362       (PortletRuntimeData) runtimeDatas_.get(portletAppName + SEPARATOR + portletName);
363     return datas.getInitializationTime();
364   }
365 }
Popular Tags