/** * Overridden to change the attribute (which sets the "dirty" state) only if the new attribute value * is not equal to the old attribute value. */ @Override public Object doPutOrRemove(String name, Object value) { Object oldValue = doGet(name); return (valueEquals(oldValue, value)) ? value : super.doPutOrRemove(name, value); }
public NoSqlSessionData(String id, String cpath, String vhost, long created, long accessed, long lastAccessed, long maxInactiveMs) { super(id, cpath, vhost, created, accessed, lastAccessed, maxInactiveMs); setVersion (new Long(0)); }
private void handleSessionBindings(NoSqlSession session, HazelcastSessionData o) { Map<String, Object> attrs = o.getAttributeMap(); if (attrs != null) { for (String name : attrs.keySet()) { String attr = name; Object value = attrs.get(name); if (attrs.keySet().contains(name)) { session.doPutOrRemove(attr, value); session.bindValue(attr, value); } else { session.doPutOrRemove(attr, value); } } // cleanup, remove values from session, that don't exist in data anymore: for (String name : session.getNames()) { if (!attrs.keySet().contains(name)) { session.doPutOrRemove(name, null); session.unbindValue(name, session.getAttribute(name)); } } } }
@Override protected Object save(NoSqlSession session, Object version, boolean activateAfterSave) { LOG.info("HazelcastSessionManager:save: " + session); session.willPassivate(); HazelcastSessionData sessionData = sessions.get(session.getClusterId()); if (sessionData == null) { sessionData = new HazelcastSessionData(); } // handle valid or invalid if (session.isValid()) { // handle new or existing version = handleSessionAddition(session, version, sessionData); } else { sessionData.setValid(false); } sessions.set(session.getClusterId(), sessionData); if (activateAfterSave) { session.didActivate(); } return version; }
@Override protected synchronized NoSqlSession loadSession(String clusterId) { LOG.info("HazelcastSessionManager:loadSession: " + clusterId); HazelcastSessionData o = sessions.get(clusterId); if (o == null) { return null; } Boolean valid = o.isValid(); if (valid == null || !valid) { return null; } Object version = o.getVersion(); Long created = o.getCreationTime(); Long accessed = o.getAccessed(); NoSqlSession session = new NoSqlSession(this, created, accessed, clusterId, version); // get the attributes for the context Map<String, Object> attrs = o.getAttributeMap(); if (attrs != null) { for (String name : attrs.keySet()) { String attr = name; Object value = attrs.get(name); session.doPutOrRemove(attr, value); session.bindValue(attr, value); } } session.didActivate(); return session; }
@Override protected void update(final NoSqlSession session, final String newClusterId, final String newNodeId) throws Exception { ISerializableSession data = getKey(session.getClusterId()); if (data == null) { log.warn("Couldn't get session data for old key {}", session.getClusterId()); return; } deleteKey(session.getClusterId()); setKey(newClusterId, data); }
private void setData(NoSqlSession session, Map<String, Object> data) { for (Map.Entry<String, Object> col : data.entrySet()) { String name = col.getKey(); Object value = col.getValue(); session.doPutOrRemove(name, value); session.bindValue(name, value); } }
/** * Overridden to update the session state prior to saving if any attribute value has a different * hash code than it used to. This allows us to detect changes when a mutable object is used as * an attribute value. */ @Override protected void complete() { for (Map.Entry<String, Object> a: getAttributeMap().entrySet()) { Integer oldHash = attributeHashes.get(a.getKey()); if (oldHash == null || oldHash.intValue() != safeHash(a.getValue())) { // super.doPutOrRemove always sets the dirty state super.doPutOrRemove(a.getKey(), a.getValue()); } } super.complete(); }
@Override protected NoSqlSession loadSession(String clusterId) { Optional<JettySession> row = cache.getUnchecked(clusterId); if (!row.isPresent()) { return null; } JettySession jettySession = row.get(); NoSqlSession session = new NoSqlSession( this, jettySession.getCreated(), jettySession.getAccessed(), jettySession.getClusterId(), jettySession.getVersion()); setData(session, jettySession.getData()); session.didActivate(); return session; }
@Override protected void update(NoSqlSession session, String newClusterId, String newNodeId) throws Exception { HazelcastSessionData sessionData = sessions.get(session.getClusterId()); sessions.set(newClusterId, sessionData); sessions.remove(session.getClusterId()); }
@Override protected void save(boolean activate) { for (Map.Entry<String, Object> a: getAttributeMap().entrySet()) { attributeHashes.put(a.getKey(), safeHash(a.getValue())); } super.save(activate); }
@Override public String toString() { return String.format("%s[collection=%s]", super.toString(),getDBCollection()); }
@Override public void doStart() throws Exception { LOG.info("HazelcastSessionManager.doStart()"); super.doStart(); String[] hosts = getContextHandler().getVirtualHosts(); if (hosts == null || hosts.length == 0) { // IPv6 equiv of 0.0.0.0 hosts = new String[]{"::"}; } String contextPath = getContext().getContextPath(); if (contextPath == null || "".equals(contextPath)) { contextPath = "*"; } LOG.debug("HazelcastSessionManager:doStart():contextpath: " + contextPath); contextId = createContextId(hosts, contextPath); LOG.debug("HazelcastSessionManager:doStart():contextId: " + contextId); sessions = ((HazelcastSessionIdManager) getSessionIdManager()).getSessions(); }
@Override public void setSessionIdManager(final SessionIdManager idManager) { try { KeyValueStoreSessionIdManager kvsIdManager = (KeyValueStoreSessionIdManager) idManager; super.setSessionIdManager(kvsIdManager); } catch (ClassCastException error) { log.warn("unable to cast " + idManager.getClass() + " to " + KeyValueStoreSessionIdManager.class + "."); throw (error); } }
@Override public SessionData newSessionData(String id, long created, long accessed, long lastAccessed, long maxInactiveMs) { return new NoSqlSessionData(id, _context.getCanonicalContextPath(), _context.getVhost(), created, accessed, lastAccessed, maxInactiveMs); }
@Override public void doStop() throws Exception { // override doStop() and invalidatedSessions() to skip invoking NoSqlSessionManager#invalidatedSeessions() // we do not want to invalidate all sessions on servlets restart. log.info("stopping..."); super.doStop(); log.info("stopped."); }
@Override protected void expire(String idInCluster) { super.expire(idInCluster); /* * pull back the 'valid' value, we can check if its false, if is we don't need to reset it to false */ HazelcastSessionData o = sessions.get(idInCluster); if (o != null && o.isValid()) { o.setValid(false); sessions.set(idInCluster, o); } }
@Override protected boolean remove(NoSqlSession session) { /* * Check if the session exists and if it does remove the context associated with this session */ HazelcastSessionData o = sessions.get(session.getClusterId()); if (o != null) { LOG.debug("HazelcastSessionManager:remove: " + session.getClusterId()); // ??sessions.remove(session.getClusterId()); return true; } else { return false; } }
@Override protected boolean remove(NoSqlSession session) { String id = session.getClusterId(); Optional<JettySession> optRow = cache.getUnchecked(id); simpleOrmSession.delete(JettySession.class, id, getSimpleOrmContext()); cache.invalidate(id); return optRow.isPresent(); }