KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.exoplatform.services.portletcontainer.impl.monitor;
7
8 import org.apache.commons.logging.Log;
9 import org.exoplatform.Constants;
10 import org.exoplatform.services.cache.CacheService;
11 import org.exoplatform.services.cache.ExoCache;
12 import org.exoplatform.services.portletcontainer.monitor.*;
13
14 /**
15  * Created y the eXo platform team
16  * User: Benjamin Mestrallet
17  * Date: 6 mai 2004
18  */

19 public class PortletRuntimeDatasImpl implements PortletRuntimeData {
20   static int NUMBER_OF_REQUEST_MONITOR = 10;
21   static long TIME_RANGE = 100; // 200ms
22

23   private String JavaDoc portletAppName;
24   private String JavaDoc portletName;
25   private boolean initialized;
26   private long initializationTime;
27   private long lastAccessTime;
28   private long lastFailureAccessTime;
29   private long lastInitFailureAccessTime;
30   private long unavailabilityPeriod = 0;
31   private int cacheExpirationPeriod = 0;
32   private String JavaDoc globalKey;
33   private ExoCache userCache;
34   private Log log;
35   private ExoCache globalCache;
36   private PortletRequestMonitorData[] portletRequestMonitors_;
37
38   public PortletRuntimeDatasImpl(String JavaDoc portletAppName, String JavaDoc portletName,
39                                  CacheService cacheService, ExoCache globalCache,
40                                  Log log) {
41     this.log = log;
42     this.globalCache = globalCache;
43     this.portletAppName = portletAppName;
44     this.portletName = portletName;
45     this.globalKey = portletAppName + Constants.PORTLET_HANDLE_ENCODER + portletName;
46     try {
47       userCache = cacheService.getCacheInstance(globalKey);
48     } catch (Exception JavaDoc e) {
49       log.error("Can not lookup user cache", e);
50     }
51
52     portletRequestMonitors_ = new PortletRequestMonitorData[NUMBER_OF_REQUEST_MONITOR];
53     long min = 0;
54     long max = TIME_RANGE - 1;
55     for (int i = 0; i < NUMBER_OF_REQUEST_MONITOR; i++) {
56       portletRequestMonitors_[i] = new PortletRequestMonitorData(min, max);
57       min += TIME_RANGE;
58       max += TIME_RANGE;
59     }
60   }
61
62   public boolean isInitialized() {
63     return initialized;
64   }
65
66   public synchronized void setInitialized(boolean initialized) {
67     this.initialized = initialized;
68   }
69
70   public String JavaDoc getPortletAppName() {
71     return portletAppName;
72   }
73
74   public synchronized void setPortletAppName(String JavaDoc portletAppName) {
75     this.portletAppName = portletAppName;
76   }
77
78   public String JavaDoc getPortletName() {
79     return portletName;
80   }
81
82   public synchronized void setPortletName(String JavaDoc portletName) {
83     this.portletName = portletName;
84   }
85
86   public long getLastAccessTime() {
87     return lastAccessTime;
88   }
89
90   public synchronized void setLastAccessTime(long lastAccessTime) {
91     this.lastAccessTime = lastAccessTime;
92   }
93
94   public long getLastFailureAccessTime() {
95     return lastFailureAccessTime;
96   }
97
98   public long getLastInitFailureAccessTime() {
99     return lastInitFailureAccessTime;
100   }
101
102   public synchronized void setLastInitFailureAccessTime(long lastInitFailureAccessTime) {
103     this.lastInitFailureAccessTime = lastInitFailureAccessTime;
104   }
105
106   public synchronized void setLastFailureAccessTime(long lastSuccessfullAccessTime) {
107     this.lastFailureAccessTime = lastSuccessfullAccessTime;
108   }
109
110   public long getUnavailabilityPeriod() {
111     return unavailabilityPeriod;
112   }
113
114   public synchronized void setUnavailabilityPeriod(long unavailabilityPeriod) {
115     this.unavailabilityPeriod = unavailabilityPeriod;
116   }
117
118   public boolean isDataCached(String JavaDoc key, boolean isCacheGlobal) {
119     try {
120       if (isCacheGlobal) {
121         if (globalCache.get(key) != null) {
122           return true;
123         }
124       } else {
125         if (userCache.get(key) != null) {
126           return true;
127         }
128       }
129     } catch (Exception JavaDoc e) {
130       log.error("Unable to load data from user cache", e);
131     }
132     return false;
133   }
134
135   public synchronized void setCachedData(String JavaDoc key, CachedData cachedData, boolean isCacheGlobal) {
136     try {
137       if (isCacheGlobal) {
138         globalCache.put(key, cachedData);
139       } else {
140         userCache.put(key, cachedData);
141       }
142     } catch (Exception JavaDoc e) {
143       log.error("Unable to store data in user cache", e);
144     }
145   }
146
147   public CachedData getCachedData(String JavaDoc key, boolean isCacheGlobal) {
148     try {
149       if (isCacheGlobal) {
150         return (CachedData) globalCache.get(key);
151       }
152       return (CachedData) userCache.get(key);
153     } catch (Exception JavaDoc e) {
154       log.error("Unable to load data from user cache", e);
155     }
156     return null;
157   }
158
159   public synchronized void removeCachedData(String JavaDoc key, boolean isCacheGlobal) {
160     try {
161       if (isCacheGlobal) {
162         globalCache.remove(key);
163       } else {
164         userCache.remove(key);
165       }
166     } catch (Exception JavaDoc e) {
167       log.error("Unable to remove data from user cache", e);
168     }
169   }
170
171   public int getCacheExpirationPeriod() {
172     return cacheExpirationPeriod;
173   }
174
175   public synchronized void setCacheExpirationPeriod(int cacheExpirationPeriod) {
176     this.cacheExpirationPeriod = cacheExpirationPeriod;
177   }
178
179   public long getInitializationTime() {
180     return initializationTime;
181   }
182
183   public synchronized void setInitializationTime(long initializationTime) {
184     this.initializationTime = initializationTime;
185   }
186
187   final public void logProcessActionRequest(long startTime, long endTime) {
188     long executionTime = endTime - startTime;
189     int index = (int) (executionTime / TIME_RANGE);
190     if (index >= NUMBER_OF_REQUEST_MONITOR) index = NUMBER_OF_REQUEST_MONITOR - 1;
191     portletRequestMonitors_[index].logActionRequest(executionTime);
192   }
193
194   final public void logRenderRequest(long startTime, long endTime, boolean cacheHit) {
195     long executionTime = endTime - startTime;
196     int index = (int) (executionTime / TIME_RANGE);
197     if (index >= NUMBER_OF_REQUEST_MONITOR) index = NUMBER_OF_REQUEST_MONITOR - 1;
198     portletRequestMonitors_[index].logRenderRequest(executionTime, cacheHit);
199   }
200
201   public PortletRequestMonitorData[] getPortletRequestMonitorData() {
202     return portletRequestMonitors_;
203   }
204
205   public synchronized boolean isAvailable(long l) {
206     if ((l - getLastFailureAccessTime() > getUnavailabilityPeriod())) {
207       setUnavailabilityPeriod(0);
208       return true;
209     } else {
210       return false;
211     }
212   }
213
214   public synchronized boolean isInitialisationAllowed(long l) {
215     if ((l - getLastInitFailureAccessTime() > getUnavailabilityPeriod())) {
216       setUnavailabilityPeriod(0);
217       return true;
218     } else {
219       return false;
220     }
221   }
222
223   public long whenAvailable() {
224     long period = getUnavailabilityPeriod() - (System.currentTimeMillis() - getLastInitFailureAccessTime());
225     if(period < 0)
226       return -1;
227     else
228       return period;
229   }
230 }
Popular Tags