sessionimpl.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,394 行 · 第 1/3 页

JAVA
1,394
字号
   * Returns true if the session is valid.   */  public boolean isValid()  {    return _isValid;  }  boolean isClosing()  {    return _isClosing;  }  /**   * Callback when the session is removed from the session cache, generally   * because the session cache is full.   */  public void removeEvent()  {    synchronized (this) {      if (_isInvalidating || _useCount <= 0)	_isClosing = true;    }    if (! _isClosing) {      log.warning(L.l("{0} LRU while in use (use-count={1}).  Consider increasing session-count.",		      this,		      _useCount));    }        boolean isValid = _isValid;    if (log.isLoggable(Level.FINE))      log.fine(this + " remove");    long now = Alarm.getCurrentTime();    // server/015k    if (_isInvalidating	|| _accessTime + getMaxInactiveInterval() < now	|| _clusterObject == null)      notifyDestroy();    invalidateLocal();  }  private void notifyDestroy()  {    // server/01ni    if (_clusterObject != null && ! _clusterObject.isPrimary())      return;        ArrayList listeners = _manager.getListeners();    if (listeners != null) {      HttpSessionEvent event = new HttpSessionEvent(this);      for (int i = listeners.size() - 1; i >= 0; i--) {        HttpSessionListener listener;        listener = (HttpSessionListener) listeners.get(i);        listener.sessionDestroyed(event);      }    }  }  /**   * Invalidates the session, called by user code.   *    * This should never be called by Resin code (for logging purposes)   */  public void invalidate()  {    if (log.isLoggable(Level.FINE))      log.fine(this + " invalidate");    _isInvalidating = true;    invalidate(Logout.INVALIDATE);  }    /**   * Invalidates a session based on a logout.   */  public void invalidateLogout()  {    if (log.isLoggable(Level.FINE))      log.fine(this + " logout");       _isInvalidating = true;    invalidate(Logout.INVALIDATE);  }    /**   * Invalidates a session based on a timeout   */  void invalidateTimeout()  {    if (log.isLoggable(Level.FINE))      log.fine(this + " timeout");        invalidate(Logout.TIMEOUT);  }    /**   * Invalidates a session based on a LRU   */  void invalidateLru()  {    if (log.isLoggable(Level.FINE))      log.fine(this + " lru");        invalidateImpl(Logout.LRU);  }  /**   * Invalidates the session, called by user code.   *    * This should never be called by Resin code (for logging purposes)   */  public void invalidateRemote()  {    if (log.isLoggable(Level.FINE))      log.fine(this + " invalidate remote");    _isInvalidating = true;    invalidate(Logout.INVALIDATE);  }  /**   * Invalidates the session.   */  private void invalidate(Logout logout)  {    if (! _isValid)      throw new IllegalStateException(L.l("{0}: Can't call invalidate() when session is no longer valid.",                                          this));    try {      // server/017s      ServletAuthenticator auth = getAuthenticator();      if (! (auth instanceof AbstractAuthenticator)	  || logout == Logout.INVALIDATE	  || (logout == Logout.TIMEOUT	      && ((AbstractAuthenticator) auth).getLogoutOnSessionTimeout())) {	// server/12i1, 12ch	logout(logout == Logout.TIMEOUT ? this : null);      }      _manager.removeSession(this);      invalidateImpl(logout);    } finally {      _isValid = false;    }  }  /**   * Logs out the user   */  public void logout()  {    // server/12bw    logout(null);  }  /**   * Logs out the user   *   * @param session the session in case of timeout and single-signon   */  public void logout(SessionImpl timeoutSession)  {    if (log.isLoggable(Level.FINE))      log.fine(this + " logout " + timeoutSession);        if (_user != null) {      if (_isValid)        removeAttribute(LOGIN);      Principal user = _user;      _user = null;      try {	ServletAuthenticator auth = getAuthenticator();	if (auth != null)	  auth.logout(_manager.getWebApp(), timeoutSession, _id, user);      } catch (Exception e) {        log.log(Level.WARNING, e.toString(), e);      }    }  }  /**   * Invalidate the session, removing it from the manager,   * unbinding the values, and removing it from the objectStore.   */  private void invalidateImpl(Logout logout)  {    boolean invalidateAfterListener = _manager.isInvalidateAfterListener();    if (! invalidateAfterListener)      _isValid = false;    try {      ClusterObject clusterObject = _clusterObject;      // _clusterObject = null;      if (clusterObject != null && _isInvalidating)	clusterObject.objectRemove();    } catch (Exception e) {      log.log(Level.FINE, e.toString(), e);    }    invalidateLocal();  }  /**   * unbinds the session and saves if necessary.   */  private void invalidateLocal()  {    ClusterObject clusterObject = _clusterObject;    if (_isValid && ! _isInvalidating && clusterObject != null) {      if (_manager.isSaveOnlyOnShutdown()) {	try {	  clusterObject.objectStore(this);	} catch (Exception e) {	  log.log(Level.WARNING, this + ": can't serialize session", e);	}        	clusterObject.objectInvalidated();      }    }    unbind(); // we're invalidating, not passivating  }  /**   * Creates a new session.   */  void create(long now, boolean isCreate)  {    if (log.isLoggable(Level.FINE)) {      log.fine(this + " create session");    }    // e.g. server 'C' when 'A' and 'B' have no record of session    if (_isValid)      unbind();    _isValid = true;    _isNew = true;    _accessTime = now;    _creationTime = now;    if (_clusterObject != null && isCreate)      _clusterObject.objectCreate();  }  /**   * Returns true if the session is in use.   */  public boolean inUse()  {    return _useCount > 0;  }  /**   * Set true if the session is in use.   */  boolean addUse()  {    synchronized (this) {      if (_isClosing)	return false;      _useCount++;      return true;    }  }  /**   * Set true if the session is in use.   */  void endUse()  {    synchronized (this) {      _useCount--;    }  }  /**   * Clears the session when reading a bad saved session.   */  void reset(long now)  {    if (log.isLoggable(Level.FINE))      log.fine(this + " reset");    unbind();    _isValid = true;    _isNew = true;    _accessTime = now;    _creationTime = now;  }  /**   * Loads the session.   */  public boolean load()  {    if (! _isValid)      return false;        boolean isValid;    // server/01k0    if (_useCount > 1)      return true;    ClusterObject clusterObject = _clusterObject;    if (clusterObject != null)      isValid = clusterObject.objectLoad(this);    else      isValid = true;    return isValid;  }  /**   * Passivates the session.   */  public void passivate()  {    unbind();  }  /**   * Cleans up the session.   */  public void unbind()  {    if (_values.size() == 0)      return;    ClusterObject clusterObject = _clusterObject;    ArrayList<String> names = new ArrayList<String>();    ArrayList<Object> values = new ArrayList<Object>();        synchronized (_values) {      /*      if (_useCount > 0)	Thread.dumpStack();      */      Iterator<Map.Entry<String,Object>> iter = _values.entrySet().iterator();      while (iter.hasNext()) {	Map.Entry<String,Object> entry = iter.next();	names.add(entry.getKey());	values.add(entry.getValue());      }            _values.clear();    }    if (clusterObject != null)      clusterObject.objectInvalidated();    // server/015a    for (int i = 0; i < names.size(); i++) {      String name = names.get(i);      Object value = values.get(i);      notifyValueUnbound(name, value);    }  }  /**   * Notify any value unbound listeners.   */  private void notifyValueUnbound(String name, Object oldValue)  {    if (oldValue == null)      return;    if (oldValue instanceof HttpSessionBindingListener) {      HttpSessionBindingListener listener;      listener = (HttpSessionBindingListener) oldValue;      listener.valueUnbound(new HttpSessionBindingEvent(this,							name,							oldValue));    }    // Notify the attributes listeners    ArrayList listeners = _manager.getAttributeListeners();    if (listeners != null) {      HttpSessionBindingEvent event;      event = new HttpSessionBindingEvent(this, name, oldValue);      for (int i = 0; i < listeners.size(); i++) {        HttpSessionAttributeListener listener;        listener = (HttpSessionAttributeListener) listeners.get(i);        listener.attributeRemoved(event);      }    }  }  /*   * Set the current objectAccess time to now.   */  void setAccess(long now)  {    // server/01k0    if (_useCount > 1)      return;        _isNew = false;    if (_clusterObject != null)      _clusterObject.objectAccess();    _accessTime = now;  }  /**   * Cleaning up session stuff at the end of a request.   *   * <p>If the session data has changed and we have persistent sessions,   * save the session.  However, if save-on-shutdown is true, only save   * on a server shutdown.   */  public void finish()  {    _accessTime = Alarm.getCurrentTime();    synchronized (this) {      if (_useCount > 1) {	_useCount--;	return;      }    }    try {      saveAfterRequest();    } finally {      synchronized (this) {	_useCount--;      }    }  }  /**   * Save changes before any flush.   */  public final void saveBeforeFlush()  {    if (_manager == null || ! _manager.isSaveBeforeFlush())      return;    save();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?