public void notifyElementExpired(Ehcache cache, Element element) {
   JRadiusSession session = (JRadiusSession) element.getValue();
   RadiusLog.debug("Expired session: " + session.getSessionKey());
   if (JRadiusServer.getEventDispatcher() != null) {
     SessionExpiredEvent evt = new SessionExpiredEvent(session);
     evt.setApplicationContext(applicationContext);
     JRadiusServer.getEventDispatcher().post(evt);
   }
 }
 public synchronized JRadiusSession newSession(JRadiusRequest request, Object key)
     throws RadiusException {
   JRadiusSession session =
       (JRadiusSession) getSessionFactory(request.getSender()).newSession(request);
   session.setJRadiusKey((String) key);
   put(session.getJRadiusKey(), session);
   put(session.getSessionKey(), session);
   lock(session);
   return session;
 }
 private synchronized void release(JRadiusSession session) {
   String thisThread = Thread.currentThread().getName();
   String sessionToUnlock = session.getSessionKey();
   String sessionOwner = (String) locks.get(sessionToUnlock);
   if (sessionOwner != null) {
     if (sessionOwner.equals(thisThread)) {
       locks.remove(sessionToUnlock);
       RadiusLog.debug("Release: Thread " + thisThread + " unlocking session " + sessionToUnlock);
     } else {
       RadiusLog.error(
           "Releasing session lock not owned by this thread (owner="
               + sessionOwner
               + ",this="
               + thisThread
               + ")");
     }
   }
   notifyAll();
 }
 private synchronized void lock(JRadiusSession session) {
   String thisThread = Thread.currentThread().getName();
   String sessionToLock = session.getSessionKey();
   String sessionOwner = null;
   while (true) {
     sessionOwner = (String) locks.get(sessionToLock);
     if (sessionOwner == null) {
       locks.put(sessionToLock, thisThread);
       RadiusLog.debug("Lock: Thread " + thisThread + " locked session " + sessionToLock);
       break;
     } else if (sessionOwner.equals(thisThread)) {
       break;
     } else {
       try {
         wait();
       } catch (InterruptedException ex) {
       }
     }
   }
 }
  public synchronized JRadiusSession getSession(JRadiusRequest request, Serializable key)
      throws RadiusException {
    Element element = sessionCache.get(key);
    JRadiusSession session = null;

    if (element != null) session = (JRadiusSession) element.getValue();

    if (session == null && request != null) {
      SessionFactory sf = getSessionFactory(request.getSender());
      session = sf.getSession(request, key);
      if (session != null) {
        put(session.getJRadiusKey(), session);
        put(session.getSessionKey(), session);
      }
    }

    if (session == null) return null;

    lock(session);
    return session;
  }
 public synchronized void removeSession(JRadiusSession session) {
   if (session != null) {
     remove(session.getJRadiusKey());
     remove(session.getSessionKey());
   }
 }
  /**
   * Returns a session object. First, a key is generated by the session manager's key provider,
   * based on the JRadiusRequest. If there is a stored session based on the key, this session is
   * returned, otherwise a new session created by the session factory is returned
   *
   * @param request a JRadiusRequest used to retrieve or generate a session with
   * @return Returns a RadiusSession
   * @throws RadiusException
   */
  public JRadiusSession getSession(JRadiusRequest request) throws RadiusException {
    SessionKeyProvider skp = getSessionKeyProvider(request.getSender());
    Serializable key = skp.getAppSessionKey(request);
    JRadiusSession session = null;
    Serializable nkey = null;

    if (key != null) {
      RadiusLog.debug("** Looking for session: " + key);
      session = getSession(request, key);
      if (session == null) {
        RadiusLog.error("Broken JRadius-Session-Id implementation for session: " + key);
        key = null;
      }
    }

    if (key == null) {
      key = skp.getClassKey(request);

      if (key != null) {
        RadiusLog.debug("** Looking for session: " + key);
        session = getSession(request, key);
        if (session == null) {
          RadiusLog.error("Broken Class implementation for session: " + key);
          key = null;
        } else {
          if (session.getJRadiusKey() != null
              && !session.getJRadiusKey().equals(session.getSessionKey())) {
            rehashSession(session, session.getJRadiusKey(), key);
          }
        }
      }
    }

    if (key == null) {
      Serializable keys = skp.getRequestSessionKey(request);

      if (keys == null) {
        return null;
      }

      if (keys instanceof Serializable[]) {
        key = ((Serializable[]) (keys))[0];
        nkey = ((Serializable[]) (keys))[1];
        RadiusLog.debug("Rehashing session with key " + key + " under new key " + nkey);
      } else {
        key = keys;
      }

      RadiusLog.debug("** Looking for session: " + key);
      session = getSession(request, key);

      if (session != null && nkey != null && !nkey.equals(key)) {
        rehashSession(session, key, nkey);
      }
    }

    if (session == null) {
      session = newSession(request, nkey == null ? key : nkey);
    } else {
      session.setNewSession(false);
    }

    session.setTimeStamp(System.currentTimeMillis());
    session.setLastRadiusRequest(request);

    return session;
  }