+ * If necessary, gets it via the MBean. If gets via MBean, then also sets up the cache listener at the same time, if
+ * required.
+ */
+ private Cache getCache() {
+ if (cache == null) {
+
+ try {
+ if (cacheObjectName == null) {
+ cacheObjectName = DEFAULT_CACHE_OBJECT_NAME;
}
- return treeCache;
+ /*
+ * When migrating to JBoss 5, the way the Cache is accessed had to be changed. Also, currently Cache is
+ * not exposed by JMX, so it is also unavailable for Cache Manager. Trying to retrieve it causes an
+ * error. This will be fixed in the future.
+ */
+ MBeanServer server = JMXUtil.getMBeanServer();
+ CacheJmxWrapperMBean wrapper = (CacheJmxWrapperMBean) MBeanServerInvocationHandler.newProxyInstance(
+ server, ObjectName.getInstance(cacheObjectName), CacheJmxWrapperMBean.class, false);
+ cache = wrapper.getCache();
+
+ // cache = (Cache) server.getObjectInstance(ObjectName.getInstance(cacheObjectName));
+ // cache = (Cache) server.createMBean(Cache.class.getName(), );
+
+ if (Configuration.getAsBoolean(ConfigurationKeys.USE_CACHE_DEBUG_LISTENER)) {
+ if (listener != null) {
+ cache.removeCacheListener(listener);
+ }
+ listener = new CacheDebugListener();
+ cache.addCacheListener(listener);
+ log.info("Added tree cache listener.");
+ }
+ } catch (Exception e) {
+ log.error("Unable to access the JBOSS cache mbean " + cacheObjectName + ". Cache not available.", e);
+ }
}
-
- /** Get the String[] version of the objects class name. */
- public String[] getPartsFromClass(Class clasz) {
- return clasz.getName().split("\\.");
+ return cache;
+ }
+
+ /** Get the String[] version of the objects class name. */
+ public String[] getPartsFromClass(Class clasz) {
+ return clasz.getName().split("\\.");
+ }
+
+ /**
+ * Get the Fqn for this object, based on the class name. The Fqn is used as the part of the key to the cached
+ * object.
+ */
+ private Fqn getFqn(Class clasz) {
+ return Fqn.fromElements(getPartsFromClass(clasz));
+ }
+
+ /**
+ * Get the Fqn for this object, based on classNameParts. The Fqn is used as the part of the key to the cached
+ * object.
+ */
+ private Fqn getFqn(String[] classNameParts) {
+ return Fqn.fromElements(classNameParts);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.lamsfoundation.lams.cache.ICacheManager#getItem(java.lang.String[], java.lang.Object)
+ */
+ public Object getItem(String[] classNameParts, Object key) {
+ if (key == null || classNameParts == null) {
+ return null;
}
- /** Get the Fqn for this object, based on the class name. The Fqn is used as the part of the key to the cached object. */
- private Fqn getFqn(Class clasz) {
- return new Fqn(getPartsFromClass(clasz));
+ return getItem(getFqn(classNameParts), key);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.lamsfoundation.lams.cache.ICacheManager#getItem(java.lang.Class, java.lang.Object)
+ */
+ public Object getItem(Class clasz, Object key) {
+ if (key == null || clasz == null) {
+ return null;
}
-
- /** Get the Fqn for this object, based on classNameParts. The Fqn is used as the part of the key to the cached object. */
- private Fqn getFqn(String[] classNameParts) {
- return new Fqn(classNameParts);
- }
- /* (non-Javadoc)
- * @see org.lamsfoundation.lams.cache.ICacheManager#getItem(java.lang.String[], java.lang.Object)
- */
- public Object getItem(String[] classNameParts, Object key){
- if ( key == null || classNameParts == null )
- return null;
+ return getItem(getFqn(clasz), key);
+ }
- return getItem(getFqn(classNameParts),key);
+ /** Does the "real" get from the cache. Key and fqn must not be null or an exception may be thrown. */
+ private Object getItem(Fqn fqn, Object key) {
+ Cache cache = getCache();
+ if (cache == null) {
+ log.warn("Unable to get item with fqn " + fqn + " key " + key + " as we can't get the JBOSS Cache mbean.");
+ return null;
}
-
- /* (non-Javadoc)
- * @see org.lamsfoundation.lams.cache.ICacheManager#getItem(java.lang.Class, java.lang.Object)
- */
- public Object getItem(Class clasz, Object key) {
- if ( key == null || clasz == null )
- return null;
- return getItem(getFqn(clasz), key);
+ Object obj = null;
+ try {
+ obj = cache.get(fqn, key);
+ if (obj != null) {
+ log.debug("Retrieved object from cache fqn " + fqn + " key " + key);
+ }
+ } catch (CacheException e) {
+ log.error("JBOSS Cache exception occured getting object from cache. fqn " + fqn + " key " + key, e);
}
-
- /** Does the "real" get from the cache. Key and fqn must not be null or an exception may be thrown. */
- private Object getItem(Fqn fqn, Object key) {
- TreeCacheMBean cache = getCache();
- if (cache==null) {
- log.warn("Unable to get item with fqn "+fqn+" key "+key+" as we can't get the JBOSS Cache mbean.");
- return null;
- }
-
- Object obj = null;
- try {
- obj = (Object) cache.get(fqn, key);
- if ( obj != null ) {
- log.debug("Retrieved object from cache fqn "+fqn+" key "+key);
- }
- } catch (CacheException e) {
- log.error("JBOSS Cache exception occured getting object from cache. fqn "+fqn+" key "+key, e);
- }
- return obj;
+ return obj;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.lamsfoundation.lams.cache.ICacheManager#addItem(java.lang.String[], java.lang.Object, java.lang.Object)
+ */
+ public void addItem(String[] classNameParts, Object key, Object item) {
+ if (item != null && key != null && classNameParts != null) {
+ addItem(getFqn(classNameParts), key, item);
}
+ }
- /* (non-Javadoc)
- * @see org.lamsfoundation.lams.cache.ICacheManager#addItem(java.lang.String[], java.lang.Object, java.lang.Object)
- */
- public void addItem(String[] classNameParts, Object key, Object item){
- if ( item != null && key != null && classNameParts != null )
- addItem(getFqn(classNameParts), key, item);
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.lamsfoundation.lams.cache.ICacheManager#addItem(java.lang.Class, java.lang.Object, java.lang.Object)
+ */
+ public void addItem(Class clasz, Object key, Object item) {
+ if (item != null && key != null && clasz != null) {
+ addItem(getFqn(clasz), key, item);
}
-
- /* (non-Javadoc)
- * @see org.lamsfoundation.lams.cache.ICacheManager#addItem(java.lang.Class, java.lang.Object, java.lang.Object)
- */
- public void addItem(Class clasz, Object key, Object item){
- if ( item != null && key != null && clasz != null )
- addItem(getFqn(clasz),key,item);
- }
+ }
- /** Does the "real" put in the cache. Key, fqn and item must not be null or an exception may be thrown. */
- private void addItem(Fqn fqn, Object key, Object item){
- TreeCacheMBean cache = getCache();
- if (cache==null) {
- log.warn("Unable to get cache item with fqn "+fqn+" key "+key+" as we can't get the JBOSS Cache mbean.");
- return;
- }
-
- try {
- cache.put(fqn, key, item);
- } catch (CacheException e) {
- log.error("JBOSS Cache exception occured putting object in cache. fqn "+fqn+" key "+key,e);
- }
+ /** Does the "real" put in the cache. Key, fqn and item must not be null or an exception may be thrown. */
+ private void addItem(Fqn fqn, Object key, Object item) {
+ Cache cache = getCache();
+ if (cache == null) {
+ log.warn("Unable to get cache item with fqn " + fqn + " key " + key
+ + " as we can't get the JBOSS Cache mbean.");
+ return;
}
- public Map getCachedItems() {
- TreeCacheMBean cache = getCache();
- Map allChildNames = new TreeMap();
- if (cache==null) {
- log.warn("Unable to get cache items as we can't get the JBOSS Cache mbean.");
- } else {
- addChildren("/", cache, allChildNames);
- }
- return allChildNames;
+ try {
+ cache.put(fqn, key, item);
+ } catch (CacheException e) {
+ log.error("JBOSS Cache exception occured putting object in cache. fqn " + fqn + " key " + key, e);
}
+ }
- /* Recursively add all the child nodes to the map. This is where the format of FQNs is important -
- * this code will hardcode in "/" between each step. */
- private void addChildren(String node, TreeCacheMBean cache, Map allChildNames ) {
- try {
- Set childNames = cache.getChildrenNames(node);
- if ( childNames != null ) {
- allChildNames.put(node, childNames);
- Iterator iter = childNames.iterator();
- while ( iter.hasNext() ) {
- String childNode = (String) iter.next();
- if ( node.endsWith("/") ) {
- addChildren(node+childNode,cache,allChildNames);
- } else {
- addChildren(node+"/"+childNode,cache,allChildNames);
- }
- }
- }
- } catch (CacheException e) {
- log.error("JBOSS Cache exception occured getting child names from cache",e);
- }
+ public Map getCachedItems() {
+ Cache cache = getCache();
+ Map allChildNames = new TreeMap();
+ if (cache == null) {
+ log.warn("Unable to get cache items as we can't get the JBOSS Cache mbean.");
+ } else {
+ addChildren("/", cache, allChildNames);
}
+ return allChildNames;
+ }
- /** Clear all the nodes in the cache with the given key.
- * Works on nodes starting with /org, /com and /net */
- public void clearCache(String node) {
- TreeCacheMBean cache = getCache();
- if (cache==null) {
- log.warn("Unable to clear cache node "+node+" as we can't get the JBOSS Cache mbean.");
- } else {
- try {
- cache.remove(node);
- } catch (CacheException e) {
- log.error("JBOSS Cache exception occured getting child names from cache",e);
- }
+ /*
+ * Recursively add all the child nodes to the map. This is where the format of FQNs is important - this code will
+ * hardcode in "/" between each step.
+ */
+ private void addChildren(String node, Cache cache, Map allChildNames) {
+ try {
+ Set childNames = cache.getChildrenNames(node);
+ if (childNames != null) {
+ allChildNames.put(node, childNames);
+ Iterator iter = childNames.iterator();
+ while (iter.hasNext()) {
+ String childNode = (String) iter.next();
+ if (node.endsWith("/")) {
+ addChildren(node + childNode, cache, allChildNames);
+ } else {
+ addChildren(node + "/" + childNode, cache, allChildNames);
+ }
}
+ }
+ } catch (CacheException e) {
+ log.error("JBOSS Cache exception occured getting child names from cache", e);
}
-
- /** Remove a particular item from the cache. */
- public void removeItem(String[] classNameParts, Object key) {
- TreeCacheMBean cache = getCache();
- if (cache==null) {
- log.warn("Unable to remove cache item "+classNameParts+":"+key+"as we can't get the JBOSS Cache mbean.");
- } else {
- try {
- cache.remove(getFqn(classNameParts), key);
- } catch (CacheException e) {
- log.error("JBOSS Cache exception occured getting child names from cache",e);
- }
- }
- }
+ }
- /* **** Spring initialisation methods */
-
- public String getCacheObjectName() {
- return cacheObjectName;
+ /**
+ * Clear all the nodes in the cache with the given key. Works on nodes starting with /org, /com and /net
+ */
+ public void clearCache(String node) {
+ Cache cache = getCache();
+
+ if (cache == null) {
+ log.warn("Unable to clear cache node " + node + " as we can't get the JBOSS Cache mbean.");
+ } else {
+ try {
+ cache.removeNode(node);
+ } catch (CacheException e) {
+ log.error("JBOSS Cache exception occured getting child names from cache", e);
+ }
}
+ }
- public void setCacheObjectName(String cacheObjectName) {
- this.cacheObjectName = cacheObjectName;
+ /** Remove a particular item from the cache. */
+ public void removeItem(String[] classNameParts, Object key) {
+ Cache cache = getCache();
+ if (cache == null) {
+ log.warn("Unable to remove cache item " + classNameParts + ":" + key
+ + "as we can't get the JBOSS Cache mbean.");
+ } else {
+ try {
+ cache.remove(getFqn(classNameParts), key);
+ } catch (CacheException e) {
+ log.error("JBOSS Cache exception occured getting child names from cache", e);
+ }
}
-
+ }
+
+ /* **** Spring initialisation methods */
+
+ public String getCacheObjectName() {
+ return cacheObjectName;
+ }
+
+ public void setCacheObjectName(String cacheObjectName) {
+ this.cacheObjectName = cacheObjectName;
+ }
+
}
Index: lams_common/src/java/org/lamsfoundation/lams/events/dao/hibernate/EventDAOHibernate.java
===================================================================
RCS file: /usr/local/cvsroot/lams_common/src/java/org/lamsfoundation/lams/events/dao/hibernate/EventDAOHibernate.java,v
diff -u -r1.1 -r1.1.8.1
--- lams_common/src/java/org/lamsfoundation/lams/events/dao/hibernate/EventDAOHibernate.java 25 Aug 2008 08:15:43 -0000 1.1
+++ lams_common/src/java/org/lamsfoundation/lams/events/dao/hibernate/EventDAOHibernate.java 13 May 2009 10:05:21 -0000 1.1.8.1
@@ -9,34 +9,34 @@
class EventDAOHibernate extends HibernateDaoSupport implements EventDAO {
- protected static final String GET_EVENT_QUERY = "FROM " + Event.class.getName()
- + " AS e WHERE e.scope=? AND e.name=? AND e.eventSessionId=? AND e.failTime IS NULL";
+ protected static final String GET_EVENT_QUERY = "FROM " + Event.class.getName()
+ + " AS e WHERE e.scope=? AND e.name=? AND e.eventSessionId=? AND e.failTime IS NULL";
- protected static final String GET_EVENTS_TO_RESEND_QUERY = "SELECT DISTINCT e FROM " + Event.class.getName()
- + " AS e LEFT JOIN FETCH e.subscriptions WHERE e.failTime IS NOT NULL OR "
- + "(e.subscriptions.periodicity > 0 AND (NOW()- e.subscriptions.lastOperationTime >= e.subscriptions.periodicity))";
+ protected static final String GET_EVENTS_TO_RESEND_QUERY = "SELECT DISTINCT e FROM " + Event.class.getName()
+ + " AS e LEFT JOIN FETCH e.subscriptions AS s WHERE e.failTime IS NOT NULL OR "
+ + "(s.periodicity > 0 AND (NOW()- s.lastOperationTime >= s.periodicity))";
- public Event getEvent(String scope, String name, Long sessionId) throws InvalidParameterException {
- List
- * NOTICE: This filter must set before
+ * NOTICE: This filter must set before SessionVisitor
of currentSessionId
. An internal method, only
+ * available in package.
+ *
+ * @return
+ */
+ static SessionVisitor getSessionVisitor() {
+ return (SessionVisitor) getSession();
+ }
+
+ /**
+ * An internal method, only available in package.
+ *
+ * @param currentSessionId
+ */
+ static void setCurrentSessionId(String currentSessionId) {
+ getInstance().currentSessionIdContainer.set(currentSessionId);
+ }
+
+ /**
+ * This class initialize method called by Spring framework.
+ */
+ public void init() {
+ if (SessionManager.sessionMgr == null) {
+ // only start once
+ SessionManager.sessionMgr = this;
+ if (monitorPeriod > 0) {
+ monitor = new Monitor();
+ monitor.start();
+ }
+ }
+ }
+
+ /**
+ * This class destroy method called by Spring framework.
+ */
+ public void destroy() {
+ if (monitor != null) {
+ SessionManager.sessionMgr = null;
+ monitor.stop();
+ monitor = null;
+ }
+
+ }
+
+ public short getMonitorPeriod() {
+ return monitorPeriod;
+ }
+
+ public void setMonitorPeriod(short monitorPeriod) {
+ this.monitorPeriod = monitorPeriod;
+ }
+
+ /**
+ * Start a session for current ServletRequest and SerlvetResponse. If session does not exist, then create a new
+ * session. If it exists, just using current session.
+ *
+ *
+ * @param req
+ * @param res
+ */
+ public static void startSession(ServletRequest req, ServletResponse res) {
+ Cookie cookie = findCookie((HttpServletRequest) req, SystemSessionFilter.SSO_SESSION_COOKIE);
+
+ String currentSessionId = null;
+ if (cookie != null) {
+ currentSessionId = cookie.getValue();
+ Object obj = getSession(currentSessionId);
+ // if cookie exist, but session does not. This usually menas seesion expired.
+ // then delete the cookie first and set it null in order to create a new one
+ if (obj == null) {
+ createSession(currentSessionId);
+ /*
+ * After changing cookie name to JSESSIONID, left cookie lifecycle management to Server
+ * LDEV-2071: SSO session cookie is used to indicate shared session, as there were
+ * problems with JBoss JSESSIONID cookie management. For each WAR a new cookie was
+ * created by the server itself. Setting path and domain to "/" in createCookie() did
+ * not help - the cookie was not found by tool modules and a new was created by the
+ * server, resulting in a new session.
+ *
+ * removeCookie((HttpServletResponse) res,SystemSessionFilter.SYS_SESSION_COOKIE);
+ * cookie = null;
+ *
+ */
+
+ }
+ }
+ // can not be in else!
+ if (cookie == null) {
+ // TODO remove this debugging - only put in to diagnose Ozgur's session problem
+ if (SessionManager.log.isDebugEnabled()) {
+ SessionManager.log.debug("SessionManager: SSO cookie does not exist, generating a new session");
+ }
+ // create new session and set it into cookie
+ currentSessionId = (String) new UUIDHexGenerator().generate(null, null);
+ createSession(currentSessionId);
+ cookie = createCookie((HttpServletResponse) res, SystemSessionFilter.SSO_SESSION_COOKIE, currentSessionId);
+ }
+
+ setCurrentSessionId(currentSessionId);
+ // reset session last access time
+ SessionVisitor sessionVisitor = getSessionVisitor();
+ sessionVisitor.accessed();
+ }
+
+ /**
+ * This method will reset current session id, so programs can not use getSession()
to get current
+ * session after this method is called.
+ */
+ public static void endSession() {
+ setCurrentSessionId(null);
+ }
+
+ /**
+ * Find a cookie by given cookie name from request.
+ *
+ * @param req
+ * @param name
+ * The cookie name
+ * @return The cookie of this name in the request, or null if not found.
+ */
+ private static Cookie findCookie(HttpServletRequest req, String name) {
+ Cookie[] cookies = req.getCookies();
+ if (cookies != null) {
+ for (int i = 0; i < cookies.length; i++) {
+ if (cookies[i].getName().equals(name)) {
+ return cookies[i];
+ }
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Remove cookie by given name from request
+ *
+ * @param res
+ * @param name
+ * @return the removed cookies
+ */
+ private static Cookie removeCookie(HttpServletResponse res, String name) {
+ Cookie cookie = new Cookie(name, "");
+ cookie.setPath("/");
+ cookie.setMaxAge(0);
+ res.addCookie(cookie);
+
+ return cookie;
+ }
+
+ /**
+ * Create a new cookie for request.
+ *
+ * @param res
+ * @param name
+ * cookie name
+ * @param value
+ * cookie value
+ * @return the created cookie.
+ */
+ private static Cookie createCookie(HttpServletResponse res, String name, String value) {
+ Cookie cookie = new Cookie(name, value);
+ cookie.setPath("/");
+ cookie.setMaxAge(-1);
+ res.addCookie(cookie);
+
+ return cookie;
+ }
+
+ // ************************************************************************
+ // SYSTEM SESSION MONITOR CLASS
+ // ************************************************************************
+ class Monitor implements Runnable {
+ private static final String THREAD_NAME = "LAMS SYSTEM SESSION MONITOR";
+ private Thread monitoringThread;
+ private boolean stopSign = false;
+
+ public void start() {
+ monitoringThread = new Thread(this, Monitor.THREAD_NAME);
+ stopSign = false;
+ monitoringThread.start();
+ }
+
+ public void run() {
+ while (!stopSign) {
+ try {
+ // check whether session is expired
+ Iterator iter = sessionContainer.values().iterator();
+ while (iter.hasNext()) {
+ SessionImpl session = (SessionImpl) iter.next();
+ if (session.getMaxInactiveInterval() > 0) {
+ if (System.currentTimeMillis() - session.getLastAccessedTime()
+ - session.getMaxInactiveInterval() * 1000L > 0) {
+ session.invalidate();
+ }
+ }
+ }
+ } catch (Throwable e) {
+ SessionManager.log.warn("Monitor thread exception: " + e);
+ }
+ if (!stopSign) {
+ try {
+ Thread.sleep(monitorPeriod * 1000L);
+ } catch (Exception e) {
+ // do nothing
+ }
+ }
+ }
+ }
+
+ public void stop() {
+
+ if (monitoringThread != null) {
+ stopSign = true;
+ monitoringThread.interrupt();
+ try {
+ monitoringThread.join();
+ } catch (InterruptedException ignore) {
+ SessionManager.log.error("Exception when interruptting Session Monitoring Thread");
+ }
+ monitoringThread = null;
+ }
+ }
+ }
+
+ // ************************************************************************
+ // SYSTEM SESSION IMPLEMENTAION CLASS
+ // ************************************************************************
+ class SessionImpl implements HttpSession, SessionVisitor {
+
+ private String sessionId;
+ private long createTime;
+ private long accessTime;
+ private int timeout;
+
+ private Map valueMap;
+
+ public SessionImpl(String sessionId) {
+ this.sessionId = sessionId;
+ createTime = System.currentTimeMillis();
+ accessTime = createTime;
+ timeout = Configuration.getAsInt(ConfigurationKeys.INACTIVE_TIME);
+ valueMap = new ConcurrentReaderHashMap();
+ }
+
/**
- * Get the singleton instance of this class.
- * @return
+ * {@inheritDoc}
*/
- private static SessionManager getInstance(){
- if(sessionMgr == null)
- log.error("init SessionManager failed");
-
- return sessionMgr;
+ public long getCreationTime() {
+ return createTime;
}
+
/**
- * Get system level HttpSession by current session id.
- * @return HttpSession instanceof org.lamsfoundation.lams.systemsession.SessionManager#SessionImpl
+ * {@inheritDoc}
*/
- public static HttpSession getSession(){
- String sessionId = (String) getInstance().currentSessionIdContainer.get();
- return getSession(sessionId);
+ public String getId() {
+ return sessionId;
}
+
/**
- * Get system session by given session id.
- * @param sessionId
- * @return system session. Return an null if the given sessionid can not map to an existed session.
+ * {@inheritDoc}
*/
- public static HttpSession getSession(String sessionId){
- if(sessionId == null){
- log.debug("Failed on finding current system session with null sessionId");
- return null;
- }
- return (HttpSession) getInstance().sessionContainer.get(sessionId);
-
+ public long getLastAccessedTime() {
+ return accessTime;
}
-
- static void createSession(String sessionId){
- //initialize a new one
- HttpSession session = getInstance().new SessionImpl(sessionId);
- getInstance().sessionContainer.put(sessionId,session);
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setMaxInactiveInterval(int timeout) {
+ this.timeout = timeout;
}
-
+
/**
- * Return SessionVisitor
of currentSessionId
.
- * An internal method, only available in package.
- * @return
+ * {@inheritDoc}
*/
- static SessionVisitor getSessionVisitor() {
- return (SessionVisitor)getSession();
+ public int getMaxInactiveInterval() {
+ return timeout;
}
+
/**
- * An internal method, only available in package.
- * @param currentSessionId
+ * {@inheritDoc}
*/
- static void setCurrentSessionId(String currentSessionId) {
- getInstance().currentSessionIdContainer.set(currentSessionId);
+ public Object getAttribute(String name) {
+ return valueMap.get(name);
}
+
/**
- * This class initialize method called by Spring framework.
+ * {@inheritDoc}
*/
- public void init(){
- if(sessionMgr == null){
- //only start once
- sessionMgr = this;
- if (monitorPeriod > 0){
- monitor = new Monitor();
- monitor.start();
- }
+ public Enumeration getAttributeNames() {
+
+ return new Enumeration() {
+ Iterator iter = valueMap.keySet().iterator();
+
+ public boolean hasMoreElements() {
+ return iter.hasNext();
}
+
+ public Object nextElement() {
+ return iter.next();
+ }
+
+ };
}
+
/**
- * This class destroy method called by Spring framework.
+ * {@inheritDoc}
*/
- public void destroy(){
- if(monitor != null){
- sessionMgr = null;
- monitor.stop();
- monitor = null;
- }
-
+ public void setAttribute(String name, Object value) {
+ if (value == null) {
+ removeAttribute(name);
+ }
+
+ Object old = valueMap.put(name, value);
+
+ fireBound(name, value);
+
+ if (old != null) {
+ fireUnbound(name, old);
+ }
}
-
- public short getMonitorPeriod() {
- return monitorPeriod;
+
+ /**
+ * {@inheritDoc}
+ */
+ public void removeAttribute(String name) {
+ Object value = valueMap.remove(name);
+ if (value != null) {
+ fireUnbound(name, value);
+ }
}
- public void setMonitorPeriod(short monitorPeriod) {
- this.monitorPeriod = monitorPeriod;
- }
/**
- * Start a session for current ServletRequest and SerlvetResponse.
- * If session does not exist, then create a new session. If it exists, just using current session.
- *
- * @param req
- * @param res
+ * {@inheritDoc}
*/
- public static void startSession(ServletRequest req, ServletResponse res) {
- Cookie ssoCookie = findCookie((HttpServletRequest) req,SystemSessionFilter.SSO_SESSION_COOKIE);
- if(ssoCookie == null){
- log.debug("==>Couldn't find the sso cookie");
- String value = (String) new UUIDHexGenerator().generate(null,null);
- ssoCookie = createCookie((HttpServletResponse) res,SystemSessionFilter.SSO_SESSION_COOKIE,value);
- log.debug("==>Created one - "+ssoCookie.getValue());
- }
- Cookie cookie = findCookie((HttpServletRequest) req,SystemSessionFilter.SYS_SESSION_COOKIE);
- String currentSessionId = null;
- if(cookie != null){
- currentSessionId = cookie.getValue();
- Object obj = getSession(currentSessionId);
- //if cookie exist, but session does not. This usually menas seesion expired.
- //then delete the cookie first and set it null in order to create a new one
- if(obj == null){
- createSession(currentSessionId);
- //After changing cookie name to JSESSIONID, left cookie lifecycle management to Server
-// removeCookie((HttpServletResponse) res,SystemSessionFilter.SYS_SESSION_COOKIE);
-// cookie = null;
- }
- }
- //can not be in else!
- if(cookie == null){
- // TODO remove this debugging - only put in to diagnose Ozgur's session problem
- if ( log.isDebugEnabled() ) {
- log.debug("SessionManager: cookie is null, generating a new session");
- }
- //create new session and set it into cookie
- currentSessionId = (String) new UUIDHexGenerator().generate(null,null);
- createSession(currentSessionId);
- cookie = createCookie((HttpServletResponse) res,SystemSessionFilter.SYS_SESSION_COOKIE,currentSessionId);
- }
-
- setCurrentSessionId(currentSessionId);
- //reset session last access time
- SessionVisitor sessionVisitor = getSessionVisitor();
- sessionVisitor.accessed();
+ public void invalidate() {
+
+ Iterator iter = valueMap.entrySet().iterator();
+ while (iter.hasNext()) {
+ Map.Entry entry = (Map.Entry) iter.next();
+ fireUnbound((String) entry.getKey(), entry.getValue());
+ }
+ valueMap.clear();
}
+
/**
- * This method will reset current session id, so programs can not use getSession()
to get current
- * session after this method is called.
+ * Notice: This method always return false {@inheritDoc}
*/
- public static void endSession() {
- setCurrentSessionId(null);
+ public boolean isNew() {
+ return false;
}
/**
- * Find a cookie by given cookie name from request.
- *
- * @param req
- * @param name The cookie name
- * @return The cookie of this name in the request, or null if not found.
+ * {@inheritDoc}
*/
- private static Cookie findCookie(HttpServletRequest req, String name)
- {
- Cookie[] cookies = req.getCookies();
- if (cookies != null) {
- for (int i = 0; i < cookies.length; i++) {
- if (cookies[i].getName().equals(name)) {
- return cookies[i];
- }
- }
- }
-
- return null;
+ public void putValue(String name, Object value) {
+ setAttribute(name, value);
}
+
/**
- * Remove cookie by given name from request
- * @param res
- * @param name
- * @return the removed cookies
+ * {@inheritDoc}
*/
- private static Cookie removeCookie(HttpServletResponse res, String name){
- Cookie cookie = new Cookie(name, "");
- cookie.setPath("/");
- cookie.setMaxAge(0);
- res.addCookie(cookie);
-
- return cookie;
+ public void removeValue(String name) {
+ removeAttribute(name);
}
+
/**
- * Create a new cookie for request.
- * @param res
- * @param name cookie name
- * @param value cookie value
- * @return the created cookie.
+ * {@inheritDoc}
*/
- private static Cookie createCookie(HttpServletResponse res, String name, String value){
- Cookie cookie = new Cookie(name, value);
- cookie.setPath("/");
- cookie.setMaxAge(-1);
- res.addCookie(cookie);
-
- return cookie;
+ public Object getValue(String name) {
+ return getAttribute(name);
}
- //************************************************************************
- // SYSTEM SESSION MONITOR CLASS
- //************************************************************************
- class Monitor implements Runnable{
- private static final String THREAD_NAME = "LAMS SYSTEM SESSION MONITOR";
- private Thread monitoringThread;
- private boolean stopSign = false;
- public void start(){
- monitoringThread = new Thread(this,THREAD_NAME);
- stopSign = false;
- monitoringThread.start();
- }
- public void run() {
- while (!stopSign) {
- try {
- //check whether session is expired
- Iterator iter = sessionContainer.values().iterator();
- while(iter.hasNext()) {
- SessionImpl session = (SessionImpl) iter.next();
- if(session.getMaxInactiveInterval() > 0){
- if ((System.currentTimeMillis() - session.getLastAccessedTime() -
- session.getMaxInactiveInterval() * 1000L) > 0)
- session.invalidate();
- }
- }
- } catch (Throwable e) {
- log.warn("Monitor thread exception: " + e);
- }
- if (!stopSign) {
- try {
- Thread.sleep(monitorPeriod * 1000L);
- } catch (Exception e) {
- // do nothing
- }
- }
- }
- }
-
- public void stop(){
-
- if (monitoringThread != null){
- stopSign = true;
- monitoringThread.interrupt();
- try{
- monitoringThread.join();
- }catch (InterruptedException ignore){
- log.error("Exception when interruptting Session Monitoring Thread");
- }
- monitoringThread = null;
- }
- }
+ /**
+ * {@inheritDoc}
+ */
+ public String[] getValueNames() {
+ return (String[]) valueMap.keySet().toArray(new String[valueMap.size()]);
}
- //************************************************************************
- // SYSTEM SESSION IMPLEMENTAION CLASS
- //************************************************************************
- class SessionImpl implements HttpSession,SessionVisitor {
- private String sessionId;
- private long createTime;
- private long accessTime;
- private int timeout;
-
- private Map valueMap;
-
- public SessionImpl(String sessionId){
- this.sessionId = sessionId;
- createTime = System.currentTimeMillis();
- accessTime = createTime;
- timeout = Configuration.getAsInt(ConfigurationKeys.INACTIVE_TIME);
- valueMap = new ConcurrentReaderHashMap();
- }
- /**
- * {@inheritDoc}
- */
- public long getCreationTime() {
- return createTime;
- }
- /**
- * {@inheritDoc}
- */
- public String getId() {
- return sessionId;
- }
- /**
- * {@inheritDoc}
- */
- public long getLastAccessedTime() {
- return accessTime;
- }
- /**
- * {@inheritDoc}
- */
- public void setMaxInactiveInterval(int timeout) {
- this.timeout = timeout;
- }
- /**
- * {@inheritDoc}
- */
- public int getMaxInactiveInterval() {
- return timeout;
- }
+ /**
+ * {@inheritDoc}
+ */
+ public HttpSessionContext getSessionContext() {
+ return new HttpSessionContext() {
- /**
- * {@inheritDoc}
- */
- public Object getAttribute(String name) {
- return valueMap.get(name);
+ public HttpSession getSession(String sessionId) {
+ return SessionImpl.this;
}
- /**
- * {@inheritDoc}
- */
- public Enumeration getAttributeNames() {
-
- return new Enumeration(){
- Iterator iter = valueMap.keySet().iterator();
- public boolean hasMoreElements() {
- return iter.hasNext();
- }
- public Object nextElement() {
- return iter.next();
- }
-
- };
- }
- /**
- * {@inheritDoc}
- */
- public void setAttribute(String name, Object value) {
- if(value == null)
- removeAttribute(name);
-
- Object old = valueMap.put(name, value);
+ public Enumeration getIds() {
+ return new Enumeration() {
+ public boolean hasMoreElements() {
+ return false;
+ }
- fireBound(name, value);
-
- if (old != null){
- fireUnbound(name, old);
+ public Object nextElement() {
+ return null;
}
+ };
}
- /**
- * {@inheritDoc}
- */
- public void removeAttribute(String name) {
- Object value = valueMap.remove(name);
- if(value != null)
- fireUnbound(name, value);
- }
- /**
- * {@inheritDoc}
- */
- public void invalidate() {
-
- Iterator iter = valueMap.entrySet().iterator();
- while(iter.hasNext()){
- Map.Entry entry = (Map.Entry) iter.next();
- fireUnbound((String) entry.getKey(),entry.getValue());
- }
- valueMap.clear();
- }
- /**
- * Notice: This method always return false
- * {@inheritDoc}
- */
- public boolean isNew() {
- return false;
- }
-
- /**
- * {@inheritDoc}
- */
- public void putValue(String name, Object value) {
- setAttribute(name,value);
- }
- /**
- * {@inheritDoc}
- */
- public void removeValue(String name) {
- removeAttribute(name);
- }
- /**
- * {@inheritDoc}
- */
- public Object getValue(String name) {
- return getAttribute(name);
- }
- /**
- * {@inheritDoc}
- */
- public String[] getValueNames() {
- return (String[])valueMap.keySet().toArray(new String[valueMap.size()]);
- }
- /**
- * {@inheritDoc}
- */
- public HttpSessionContext getSessionContext() {
- return new HttpSessionContext(){
- public HttpSession getSession(String sessionId) {
- return SessionImpl.this;
- }
+ };
+ }
- public Enumeration getIds() {
- return new Enumeration(){
- public boolean hasMoreElements() {
- return false;
- }
- public Object nextElement() {
- return null;
- }
- };
- }
-
- };
- }
- /**
- * Notice: This method always return null.
- * {@inheritDoc}
- */
- public ServletContext getServletContext() {
- return null;
- }
- //**********************************************************
- // SessionVisitor method
- public void accessed() {
- accessTime = System.currentTimeMillis();
- }
- //**********************************************************
- // private method
- private void fireUnbound(String name, Object value) {
- if(value instanceof HttpSessionBindingListener){
- HttpSessionBindingEvent event = new HttpSessionBindingEvent(this,name,value);
- ((HttpSessionBindingListener)value).valueUnbound(event);
- }
- }
- private void fireBound(String name, Object value) {
- if(value instanceof HttpSessionBindingListener){
- HttpSessionBindingEvent event = new HttpSessionBindingEvent(this,name,value);
- ((HttpSessionBindingListener)value).valueBound(event);
- }
- }
+ /**
+ * Notice: This method always return null. {@inheritDoc}
+ */
+ public ServletContext getServletContext() {
+ return null;
+ }
+ // **********************************************************
+ // SessionVisitor method
+ public void accessed() {
+ accessTime = System.currentTimeMillis();
}
+ // **********************************************************
+ // private method
+ private void fireUnbound(String name, Object value) {
+ if (value instanceof HttpSessionBindingListener) {
+ HttpSessionBindingEvent event = new HttpSessionBindingEvent(this, name, value);
+ ((HttpSessionBindingListener) value).valueUnbound(event);
+ }
+ }
+
+ private void fireBound(String name, Object value) {
+ if (value instanceof HttpSessionBindingListener) {
+ HttpSessionBindingEvent event = new HttpSessionBindingEvent(this, name, value);
+ ((HttpSessionBindingListener) value).valueBound(event);
+ }
+ }
+
+ }
+
}
Index: lams_common/src/java/org/lamsfoundation/lams/web/session/SystemSessionFilter.java
===================================================================
RCS file: /usr/local/cvsroot/lams_common/src/java/org/lamsfoundation/lams/web/session/SystemSessionFilter.java,v
diff -u -r1.11 -r1.11.18.1
--- lams_common/src/java/org/lamsfoundation/lams/web/session/SystemSessionFilter.java 11 Oct 2006 05:05:12 -0000 1.11
+++ lams_common/src/java/org/lamsfoundation/lams/web/session/SystemSessionFilter.java 13 May 2009 10:05:21 -0000 1.11.18.1
@@ -34,47 +34,50 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-
/**
*
- * @author Steve.Ni
- * Create and manage system wide (across multiple webapps in JBOSS) session.
- *
- * org.lamsfoundation.lams.web.filter.LocaleFilter
- * in web.xml because LocaleFilter need get value from SystemSession .
- *
+ * @author Steve.Ni Create and manage system wide (across multiple webapps in JBOSS) session.
+ *
+ * org.lamsfoundation.lams.web.filter.LocaleFilter
in web.xml
+ * because LocaleFilter need get value from SystemSession .
+ *
* @version $Revision$
*/
public class SystemSessionFilter implements Filter {
-
- //The session name to trace shared session
- public static final String SYS_SESSION_COOKIE = "JSESSIONID";
-
- public static final String SSO_SESSION_COOKIE = "JSESSIONIDSSO";
- public void init(FilterConfig config) throws ServletException {
- }
+ /*
+ * The session name to trace shared session LDEV-2071: Removed because JBoss 5 uses this cookie differently. It is
+ * not shared among LAMS WARs like JSESSIONIDSSO. Setting path and domain parameters to root "/" does not help.
+ * JSESSIONIDSSO is used instead, as it is the same for all WARs and makes a good shared session indicator. public
+ *
+ * static final String SYS_SESSION_COOKIE = "JSESSIONID";
+ */
- public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
- throws IOException, ServletException {
+ public static final String SSO_SESSION_COOKIE = "JSESSIONIDSSO";
- // Skip non-http request/response
- if (!((req instanceof HttpServletRequest) && (res instanceof HttpServletResponse))){
- chain.doFilter(req, res);
- return;
- }
-
- SessionManager.startSession(req, res);
-
- //do following part of chain
- chain.doFilter(req,res);
-
- SessionManager.endSession();
-
- }
+ public void init(FilterConfig config) throws ServletException {
+ }
- public void destroy() {
- //do nothing
+ public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
+ ServletException {
+
+ // Skip non-http request/response
+ if (!(req instanceof HttpServletRequest && res instanceof HttpServletResponse)) {
+ chain.doFilter(req, res);
+ return;
}
+
+ SessionManager.startSession(req, res);
+
+ // do following part of chain
+ chain.doFilter(req, res);
+
+ SessionManager.endSession();
+
+ }
+
+ public void destroy() {
+ // do nothing
+ }
}
Index: lams_contentrepository/.classpath
===================================================================
RCS file: /usr/local/cvsroot/lams_contentrepository/.classpath,v
diff -u -r1.15 -r1.15.8.1
--- lams_contentrepository/.classpath 30 Oct 2008 03:48:30 -0000 1.15
+++ lams_contentrepository/.classpath 13 May 2009 10:01:37 -0000 1.15.8.1
@@ -5,9 +5,12 @@