KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > wsrp > producer > impl > TransientStateManagerImpl


1 /*
2 * Copyright 2001-2004 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.wsrp.producer.impl;
7
8 import org.apache.commons.logging.Log;
9 import org.exoplatform.commons.utils.IdentifierUtil;
10 import org.exoplatform.container.PortalContainer;
11 import org.exoplatform.container.SessionContainer;
12 import org.exoplatform.services.cache.CacheService;
13 import org.exoplatform.services.cache.ExoCache;
14 import org.exoplatform.services.log.LogService;
15 import org.exoplatform.services.portletcontainer.pci.PortletData;
16 import org.exoplatform.services.wsrp.WSRPConstants;
17 import org.exoplatform.services.wsrp.exceptions.Faults;
18 import org.exoplatform.services.wsrp.exceptions.WSRPException;
19 import org.exoplatform.services.wsrp.producer.TransientStateManager;
20 import org.exoplatform.services.wsrp.producer.impl.helpers.CacheControlProxy;
21 import org.exoplatform.services.wsrp.producer.impl.helpers.WSRPHttpSession;
22 import org.exoplatform.services.wsrp.type.CacheControl;
23 import org.exoplatform.services.wsrp.type.Templates;
24 import org.exoplatform.services.wsrp.type.UserContext;
25
26 /*
27  * @author Mestrallet Benjamin
28  * benjmestrallet@users.sourceforge.net
29  * Date: 25 janv. 2004
30  * Time: 17:53:18
31  */

32
33 public class TransientStateManagerImpl implements TransientStateManager {
34
35   private static final String JavaDoc TEMPLATE_KEY = "org.exoplatform.services.wsrp.templates.key";
36   private static final String JavaDoc USER_CONTEXT_KEY = "org.exoplatform.services.wsrp.user.context.key";
37   private Log log;
38   private ExoCache cache;
39   private WSRPConfiguration conf;
40
41   public TransientStateManagerImpl(LogService logService, CacheService cacheService,
42                                    WSRPConfiguration conf) {
43     this.log = logService.getLog("org.exoplatform.services.wsrp");
44     this.conf = conf;
45     try {
46       cache = cacheService.getCacheInstance(WSRPConstants.WSRP_CACHE_REGION);
47     } catch (Exception JavaDoc e) {
48       log.debug("Can not lookup cache : " + WSRPConstants.WSRP_CACHE_REGION, e);
49     }
50   }
51
52   public WSRPHttpSession resolveSession(String JavaDoc sessionID, String JavaDoc user) throws WSRPException {
53     WSRPHttpSession session = null;
54     log.debug("Try to lookup session with ID : " + sessionID);
55     try {
56       session = (WSRPHttpSession) cache.get(sessionID);
57       SessionContainer scontainer = null;
58       if (sessionID != null) {
59         if (session.isInvalidated()) {
60           session = new WSRPHttpSession(sessionID, SESSION_TIME_PERIOD);
61           scontainer = PortalContainer.getInstance().createSessionContainer(sessionID, user);
62         } else {
63           session.setLastAccessTime(System.currentTimeMillis());
64           scontainer = (SessionContainer)PortalContainer.getInstance().
65             getComponentInstance(sessionID) ;
66         }
67         log.debug("Lookup session success");
68       } else {
69         sessionID = IdentifierUtil.generateUUID(this);
70         session = new WSRPHttpSession(sessionID, SESSION_TIME_PERIOD);
71         cache.put(sessionID, session);
72         scontainer = PortalContainer.getInstance().createSessionContainer(sessionID, user);
73         log.debug("Create new session with ID : " + sessionID);
74       }
75       SessionContainer.setInstance(scontainer) ;
76       return session;
77     } catch (Exception JavaDoc e) {
78       throw new WSRPException(Faults.INVALID_SESSION_FAULT, e);
79     }
80   }
81
82   public void releaseSession(String JavaDoc sessionID) {
83     try {
84       cache.remove(sessionID);
85       PortalContainer.getInstance().removeSessionContainer(sessionID) ;
86     } catch (Exception JavaDoc e) {
87       log.debug("Can not release session : " + sessionID, e);
88     }
89   }
90
91   public CacheControl getCacheControl(PortletData portletDatas) throws WSRPException {
92     log.debug("Fill a CacheControl object for the portlet");
93     CacheControl cacheControl = null;
94     try {
95       cacheControl = new CacheControl();
96       String JavaDoc key = IdentifierUtil.generateUUID(cacheControl);
97       cacheControl.setExpires(Integer.parseInt(portletDatas.getExpirationCache()));
98       if(portletDatas.isCacheGlobal()){
99         cacheControl.setUserScope(WSRPConstants.WSRP_GLOBAL_SCOPE_CACHE);
100       } else {
101         cacheControl.setUserScope(WSRPConstants.WSRP_USER_SCOPE_CACHE);
102       }
103       log.debug("Use Cache key : " + key);
104       cacheControl.setValidateTag(key);
105       CacheControlProxy proxy = new CacheControlProxy(cacheControl);
106       cache.put(key, proxy);
107     } catch (Exception JavaDoc e) {
108       log.debug("Unable to cache CacheControlProxy", e);
109       throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
110     }
111     return cacheControl;
112   }
113
114   public boolean validateCache(String JavaDoc validateTag) throws WSRPException {
115     log.debug("Validate a CacheControl object : " + validateTag);
116     //TODO find a better way to validate a cache
117
try {
118       CacheControlProxy cacheControlProxy = (CacheControlProxy) cache.get(validateTag);
119       if(cacheControlProxy != null && cacheControlProxy.isValid()){
120         log.debug("Consumer cache validated");
121         return true;
122       }
123     } catch (Exception JavaDoc e) {
124       log.debug("Unable to lookup CacheControlProxy", e);
125       throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
126     }
127     return false;
128   }
129
130   public Templates getTemplates(WSRPHttpSession session) {
131     return (Templates) session.getAttribute(TEMPLATE_KEY);
132   }
133
134   public void storeTemplates(Templates templates, WSRPHttpSession session) {
135     session.setAttribute(TEMPLATE_KEY, templates);
136   }
137
138   public UserContext reolveUserContext(UserContext userContext, WSRPHttpSession session) {
139     if(conf.isUserContextStoredInSession()){
140       log.debug("Optimized mode : user context store in session");
141       if(userContext == null){
142         log.debug("Optimized mode : retrieve the user context from session");
143         return (UserContext) session.getAttribute(USER_CONTEXT_KEY);
144       } else {
145         log.debug("Optimized mode : store the user context in session");
146         session.setAttribute(USER_CONTEXT_KEY, userContext);
147       }
148     }
149     return userContext;
150   }
151 }
152
Popular Tags