All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bluejeans.tomcat.redissessions.RedisSession Maven / Gradle / Ivy

package com.bluejeans.tomcat.redissessions;

import java.io.IOException;
import java.security.Principal;
import java.util.HashMap;

import org.apache.catalina.Manager;
import org.apache.catalina.session.StandardSession;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;

public class RedisSession extends StandardSession {

    private static final long serialVersionUID = 1576857955772246285L;

    private final Log log = LogFactory.getLog(RedisSession.class);

    protected static Boolean manualDirtyTrackingSupportEnabled = false;

    public static void setManualDirtyTrackingSupportEnabled(final Boolean enabled) {
        manualDirtyTrackingSupportEnabled = enabled;
    }

    protected static String manualDirtyTrackingAttributeKey = "__changed__";

    public static void setManualDirtyTrackingAttributeKey(final String key) {
        manualDirtyTrackingAttributeKey = key;
    }

    protected HashMap changedAttributes;
    protected Boolean dirty;

    public RedisSession(final Manager manager) {
        super(manager);
        resetDirtyTracking();
    }

    public Boolean isDirty() {
        return dirty || !changedAttributes.isEmpty();
    }

    public HashMap getChangedAttributes() {
        return changedAttributes;
    }

    public void resetDirtyTracking() {
        changedAttributes = new HashMap<>();
        dirty = false;
    }

    @Override
    public void setAttribute(final String key, final Object value) {
        if (manualDirtyTrackingSupportEnabled && manualDirtyTrackingAttributeKey.equals(key)) {
            dirty = true;
            return;
        }

        final Object oldValue = getAttribute(key);
        super.setAttribute(key, value);

        if ((value != null || oldValue != null)
                && (value == null && oldValue != null || oldValue == null && value != null
                        || !value.getClass().isInstance(oldValue) || !value.equals(oldValue))) {
            if (this.manager instanceof RedisSessionManager && ((RedisSessionManager) this.manager).getSaveOnChange()) {
                try {
                    ((RedisSessionManager) this.manager).save(this, true);
                } catch (final IOException ex) {
                    log.error("Error saving session on setAttribute (triggered by saveOnChange=true): "
                            + ex.getMessage());
                }
            } else {
                changedAttributes.put(key, value);
            }
        }
    }

    @Override
    public void removeAttribute(final String name) {
        super.removeAttribute(name);
        if (this.manager instanceof RedisSessionManager && ((RedisSessionManager) this.manager).getSaveOnChange()) {
            try {
                ((RedisSessionManager) this.manager).save(this, true);
            } catch (final IOException ex) {
                log.error("Error saving session on setAttribute (triggered by saveOnChange=true): " + ex.getMessage());
            }
        } else {
            dirty = true;
        }
    }

    @Override
    public void setId(final String id) {
        // Specifically do not call super(): it's implementation does unexpected things
        // like calling manager.remove(session.id) and manager.add(session).

        this.id = id;
    }

    @Override
    public void setPrincipal(final Principal principal) {
        dirty = true;
        super.setPrincipal(principal);
    }

    @Override
    public void writeObjectData(final java.io.ObjectOutputStream out) throws IOException {
        super.writeObjectData(out);
        out.writeLong(this.getCreationTime());
    }

    @Override
    public void readObjectData(final java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
        super.readObjectData(in);
        this.setCreationTime(in.readLong());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy