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

org.glassfish.web.ha.session.management.BaseHASession Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

/*
 * BaseHASession.java
 *
 * Created on October 23, 2003, 11:20 AM
 */

package org.glassfish.web.ha.session.management;

import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSessionBindingEvent;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Principal;
import java.util.Enumeration;

import org.apache.catalina.Globals;
import org.apache.catalina.Manager;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.session.StandardSession;

/**
 *
 * @author  lwhite
 * @author  Rajiv Mordani
 */
public abstract class BaseHASession extends StandardSession
        implements HASession {

    protected String userName="";
    protected boolean persistentFlag = false;

    /** Creates a new instance of BaseHASession */
    public BaseHASession(Manager manager) {
        super(manager);
    }

    /**
     * Set the session identifier for this session.
     *
     * @param id The new session identifier
     */
    public void setId(String id) {
        super.setId(id);

        // Set the jreplica value for the first request here when the session is created - after that it is done in the valve
        ReplicationManagerBase manager = (ReplicationManagerBase)(getManager());
        String jReplicaValue = manager.getReplicaFromPredictor(id, null);
        if (jReplicaValue != null) {
            setNote(Globals.JREPLICA_SESSION_NOTE, jReplicaValue);
        }

    }


    /**
     * always return true for isDirty()
     * this type of session is always dirty
     */
    public abstract boolean isDirty();

    /**
     * this is deliberately a no-op
     * store framework calls this method
     * so it must be there but must not have
     * any effect
     * @param value
     */
    public abstract void setDirty(boolean value);

    /**
     * is the session persistent
     */
    public boolean isPersistent() {
        return persistentFlag;
    }

    /**
     * this sets the persistent flag
     */
    public void setPersistent(boolean value) {
        persistentFlag = value;
    }

    /**
     * this returns the user name
     */
    public String getUserName() {
        return userName;
    }

    /**
     * this sets the user name
     */
    public void setUserName(String value) {
        userName = value;
    }

    /**
     * Overriding the setPrincipal of StandardSession
     *
     * @param principal The new Principal, or null if none
     */
    public void setPrincipal(Principal principal) {
        super.setPrincipal(principal);
        this.setDirty(true);
    }

    public boolean isPersistentFlag() {
        return persistentFlag;
    }


    public void recycle() {
        super.recycle();
        userName = "";
        ssoId = "";
        persistentFlag = false;
    }

    public void save() {
        ReplicationManagerBase manager = (ReplicationManagerBase) getManager();
        if (manager != null) {
            manager.doValveSave(this);
        }
    }

    public void sync() {

        HttpSessionBindingEvent event = null;
        event = new HttpSessionBindingEvent
                ((HttpSession) this, null, null);

        // Notify special event listeners on sync()
        Manager manager = this.getManager();
        StandardContext stdContext = (StandardContext) manager.getContainer();
        // fire container event
        stdContext.fireContainerEvent("sessionSync", event);
    }

    /**
     * Read a serialized version of this session object from the specified
     * object input stream.
     * 

* IMPLEMENTATION NOTE: The reference to the owning Manager * is not restored by this method, and must be set explicitly. * * @param stream The input stream to read from * * @exception ClassNotFoundException if an unknown class is specified * @exception IOException if an input/output error occurs */ private void readObject(ObjectInputStream stream) throws ClassNotFoundException, IOException { // Deserialize the scalar instance variables (except Manager) userName = (String) stream.readObject(); } /** * Write a serialized version of this session object to the specified * object output stream. *

* IMPLEMENTATION NOTE: The owning Manager will not be stored * in the serialized representation of this Session. After calling * readObject(), you must set the associated Manager * explicitly. *

* IMPLEMENTATION NOTE: Any attribute that is not Serializable * will be unbound from the session, with appropriate actions if it * implements HttpSessionBindingListener. If you do not want any such * attributes, be sure the distributable property of the * associated Manager is set to true. * * @param stream The output stream to write to * * @exception IOException if an input/output error occurs */ private void writeObject(ObjectOutputStream stream) throws IOException { // Write the scalar instance variables stream.writeObject(userName); } /** * Return a string representation of this object. */ public String superToString() { StringBuffer sb = new StringBuffer(1000); sb.append("BaseHASession["); sb.append(id); sb.append("]"); sb.append("\n"); sb.append("isValid:" + this.isValid); if (this.isValid) { Enumeration attrNamesEnum = getAttributeNames(); while(attrNamesEnum.hasMoreElements()) { String nextAttrName = attrNamesEnum.nextElement(); Object nextAttrValue = getAttributeInternal(nextAttrName); sb.append("\n"); sb.append("attrName = " + nextAttrName); sb.append(" : attrValue = " + nextAttrValue); } } return sb.toString(); // END S1AS } public String toString() { StringBuffer sb = new StringBuffer(200); //sb.append(super.toString()); sb.append(this.superToString()); sb.append(" ssoid: " + this.getSsoId()); sb.append(" userName: " + this.getUserName()); sb.append(" version: " + this.getVersion()); sb.append(" persistent: " + this.isPersistent()); return sb.toString(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy