1 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 ; 19 import java.util.Map ; 20 import java.util.Collections ; 21 28 public class PortletMonitor implements PortletContainerMonitor { 29 30 public static final char SEPARATOR = '/'; 31 public static Map versionNumberMap = new HashMap (); 32 33 private Map runtimeDatas_; 34 private Map destroyedPortlets_; 35 private Map brokenPortlets_; 36 private Log log_; 37 private ExoCache globalCache_; 38 private CacheService cacheService_; 39 40 public PortletMonitor(LogService logService, CacheService cacheService) throws Exception { 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 ()); 45 brokenPortlets_ = Collections.synchronizedMap(new HashMap ()); 46 destroyedPortlets_ = Collections.synchronizedMap(new HashMap ()); 47 } 48 49 public Map getPortletRuntimeDataMap() { 50 return runtimeDatas_; 51 } 52 53 public PortletRuntimeDatasImpl getPortletRuntimeData(String appName, String portletName) { 54 return (PortletRuntimeDatasImpl) runtimeDatas_.get(appName + SEPARATOR + portletName); 55 } 56 57 public synchronized void registerPortletApp(String portletApplicationName) { 58 long versionNumber = 1; 59 if (versionNumberMap.get(portletApplicationName) != null) { 60 versionNumber = ((Long ) versionNumberMap.get(portletApplicationName)).longValue() + 1; 61 } 62 versionNumberMap.put(portletApplicationName, new Long (versionNumber)); 63 } 64 65 public long getPortletVersionNumber(String portletAppName) { 66 return ((Long ) versionNumberMap.get(portletAppName)).longValue(); 67 } 68 69 public synchronized void register(String portletApplicationName, String 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 portletAppName, String 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 portletAppName, String 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 portletAppName, String 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 portletAppName, String 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 portletAppName, String 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 portletApplicationName, String 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 portletApplicationName, String 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 portletApplicationName, String 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 portletApplicationName, String 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 portletApplicationName, String portletName) { 166 PortletRuntimeData datas = (PortletRuntimeData) runtimeDatas_. 167 get(portletApplicationName + SEPARATOR + portletName); 168 if (datas == null) { 169 return; } 171 runtimeDatas_.remove(portletApplicationName + SEPARATOR + portletName); 172 destroyedPortlets_.put(portletApplicationName + SEPARATOR + portletName, datas); 173 } 174 175 public void setLastAccessTime(String portletAppName, String portletName, long l) { 176 PortletRuntimeDatasImpl datas = (PortletRuntimeDatasImpl) runtimeDatas_. 177 get(portletAppName + SEPARATOR + portletName); 178 if (datas == null) { datas = (PortletRuntimeDatasImpl) brokenPortlets_.get(portletAppName + SEPARATOR + portletName); 180 } 181 if (datas == null) { datas = (PortletRuntimeDatasImpl) destroyedPortlets_.get(portletAppName + SEPARATOR + portletName); 183 } 184 datas.setLastAccessTime(l); 185 } 186 187 public void setLastInitFailureAccessTime(String portletAppName, String portletName, long l) { 188 PortletRuntimeData datas = (PortletRuntimeData) runtimeDatas_. 189 get(portletAppName + SEPARATOR + portletName); 190 datas.setLastInitFailureAccessTime(l); 191 } 192 193 public void setLastFailureAccessTime(String portletAppName, String portletName, long l) { 194 PortletRuntimeDatasImpl datas = 195 (PortletRuntimeDatasImpl) runtimeDatas_.get(portletAppName + SEPARATOR + portletName); 196 datas.setLastFailureAccessTime(l); 197 } 198 199 public void setUnavailabilityPeriod(String portletAppName, String 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 portletApplicationName, String portletName, 206 String 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 portletApplicationName, String portletName, 213 String 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 portletApplicationName, String portletName) { 220 PortletRuntimeData datas = 221 (PortletRuntimeData) runtimeDatas_.get(portletApplicationName + SEPARATOR + portletName); 222 return datas.getCacheExpirationPeriod(); 223 } 224 225 public void setCacheExpirationPeriod(String portletAppName, String portletName, int i) { 226 PortletRuntimeDatasImpl datas = 227 (PortletRuntimeDatasImpl) runtimeDatas_.get(portletAppName + SEPARATOR + portletName); 228 datas.setCacheExpirationPeriod(i); 229 } 230 231 public long getPortletLastAccessTime(String portletAppName, String portletName, 232 String 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 portletAppName, String portletName, 243 String 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 portletAppName, String portletName, 258 String key, String 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 getCachedTitle(String portletAppName, String portletName, 272 String 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 portletAppName, String portletName, 283 String 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 portletAppName, String portletName, 298 String 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 portletAppName, String portletName, 309 String 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 portletAppName, String portletName, 324 String 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 portletAppName, String portletName, 339 String 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 portletAppName, String portletName, long accessTime) { 355 PortletRuntimeDatasImpl datas = 356 (PortletRuntimeDatasImpl) runtimeDatas_.get(portletAppName + SEPARATOR + portletName); 357 datas.setInitializationTime(accessTime); 358 } 359 360 public long getInitializationTime(String portletAppName, String portletName) { 361 PortletRuntimeData datas = 362 (PortletRuntimeData) runtimeDatas_.get(portletAppName + SEPARATOR + portletName); 363 return datas.getInitializationTime(); 364 } 365 } | Popular Tags |