@Override public Session getSession(final HttpServerExchange serverExchange, final SessionConfig config) { if (serverExchange != null) { SessionImpl newSession = serverExchange.getAttachment(NEW_SESSION); if (newSession != null) { return newSession; } } String sessionId = config.findSessionId(serverExchange); return getSession(sessionId); }
@Override public String changeSessionId(final HttpServerExchange exchange, final SessionConfig config) { final String oldId = sessionId; String newId = sessionManager.sessionIdGenerator.createSessionId(); this.sessionId = newId; if (!invalid) { sessionManager.sessions.put(newId, this); config.setSessionId(exchange, this.getId()); } sessionManager.sessions.remove(oldId); sessionManager.sessionListeners.sessionIdChanged(this, oldId); return newId; }
void invalidate( final HttpServerExchange exchange, SessionListener.SessionDestroyedReason reason) { synchronized (SessionImpl.this) { if (timerCancelKey != null) { timerCancelKey.remove(); } SessionImpl sess = sessionManager.sessions.remove(sessionId); if (sess == null) { if (reason == SessionListener.SessionDestroyedReason.INVALIDATED) { throw UndertowMessages.MESSAGES.sessionAlreadyInvalidated(); } return; } invalidationStarted = true; } sessionManager.sessionListeners.sessionDestroyed(this, exchange, reason); invalid = true; if (sessionManager.statisticsEnabled) { long avg, newAvg; do { avg = sessionManager.averageSessionLifetime.get(); BigDecimal bd = new BigDecimal(avg); bd.multiply(new BigDecimal(sessionManager.expiredSessionCount.get())).add(bd); newAvg = bd.divide( new BigDecimal(sessionManager.expiredSessionCount.get() + 1), MathContext.DECIMAL64) .longValue(); } while (!sessionManager.averageSessionLifetime.compareAndSet(avg, newAvg)); sessionManager.expiredSessionCount.incrementAndGet(); long life = System.currentTimeMillis() - creationTime; long existing = sessionManager.longestSessionLifetime.get(); while (life > existing) { if (sessionManager.longestSessionLifetime.compareAndSet(existing, life)) { break; } existing = sessionManager.longestSessionLifetime.get(); } } if (exchange != null) { sessionCookieConfig.clearSession(exchange, this.getId()); } }
public void init() throws Exception { LOG.trace("Initializing RFA."); Context.initialize(null); /* Populate Config Database. */ LOG.trace("Populating RFA config database."); ConfigDb staging = new ConfigDb(); for (SessionConfig session_config : this.config.getSessions()) { final String session_name = session_config.getSessionName(), connection_name = session_config.getConnectionName(); String name, value; /* Session list */ name = "/Sessions/" + session_name + "/connectionList"; value = connection_name; staging.addVariable(fixRfaStringPath(name), value); /* Logging per connection */ name = "/Connections/" + connection_name + "/logFileName"; value = "none"; staging.addVariable(fixRfaStringPath(name), value); /* List of servers */ name = "/Connections/" + connection_name + "/serverList"; value = Joiner.on(",").join(session_config.getServers()); staging.addVariable(fixRfaStringPath(name), value); /* Default port */ if (session_config.hasDefaultPort()) { name = "/Connections/" + connection_name + "/portNumber"; value = session_config.getDefaultPort(); staging.addVariable(fixRfaStringPath(name), value); } /* Protocol ping interval */ if (session_config.hasPingInterval()) { name = "/Connections/" + connection_name + "/pingInterval"; value = session_config.getPingInterval(); staging.addVariable(fixRfaStringPath(name), value); } /* Communications protocol */ name = "/Connections/" + connection_name + "/connectionType"; if (session_config.getProtocol().equalsIgnoreCase(RSSL_PROTOCOL)) { value = "RSSL"; staging.addVariable(fixRfaStringPath(name), value); if (RSSL_TRACE) { name = "/Connections/" + connection_name + "/ipcTraceFlags"; value = "31"; staging.addVariable(fixRfaStringPath(name), value); } if (RSSL_MOUNT_TRACE) { name = "/Connections/" + connection_name + "/mountTrace"; value = "True"; staging.addVariable(fixRfaStringPath(name), value); } } else if (session_config.getProtocol().equalsIgnoreCase(SSLED_PROTOCOL)) { value = "SSL"; staging.addVariable(fixRfaStringPath(name), value); name = "/Connections/" + connection_name + "/userName"; StringBuilder username = new StringBuilder(); if (session_config.hasUserName()) { username.append(session_config.getUserName()); } else { username.append(System.getProperty("user.name")); } if (session_config.hasApplicationId()) { username.append('+'); username.append(session_config.getApplicationId()); if (session_config.hasPosition()) { if (!session_config.getPosition().isEmpty()) { username.append('+'); username.append(session_config.getPosition()); } } else { username.append('+'); username.append(InetAddress.getLocalHost().getHostAddress()); username.append('/'); username.append(InetAddress.getLocalHost().getHostName()); } } value = username.toString(); staging.addVariable(fixRfaStringPath(name), value); /* 9.10.2. Merged Thread Model */ name = "/Sessions/" + session_name + "/shareConnections"; value = "False"; staging.addVariable(fixRfaStringPath(name), value); /* Enable dictionary download */ name = "/Connections/" + connection_name + "/downloadDataDict"; value = !(session_config.hasFieldDictionary() && session_config.hasEnumDictionary()) ? "True" : "False"; staging.addVariable(fixRfaStringPath(name), value); /* appendix_a */ if (session_config.hasFieldDictionary()) { name = "/Connections/" + connection_name + "/masterFidFile"; value = session_config.getFieldDictionary(); staging.addVariable(fixRfaStringPath(name), value); } /* enumtype.def */ if (session_config.hasEnumDictionary()) { name = "/Connections/" + connection_name + "/enumTypeFile"; value = session_config.getEnumDictionary(); staging.addVariable(fixRfaStringPath(name), value); } /* Disable client side DACS usage */ name = "/Connections/" + connection_name + "/dacs_CbeEnabled"; value = "False"; staging.addVariable(fixRfaStringPath(name), value); name = "/Connections/" + connection_name + "/dacs_SbeSubEnabled"; staging.addVariable(fixRfaStringPath(name), value); if (SSL_TRACE) { name = "/Connections/" + connection_name + "/ipcTraceFlags"; value = "15"; staging.addVariable(fixRfaStringPath(name), value); } } else { throw new Exception( "Unsupported transport protocol \"" + session_config.getProtocol() + "\"."); } } LOG.trace("Merging RFA config database with staging database."); this.rfa_config = staging; Context.initialize(this.rfa_config); /* TODO: Java properties override */ /* Dump effective Java properties configuration */ LOG.debug("Dumping configuration database:{}{}", LINE_SEPARATOR, this.rfa_config); LOG.trace("RFA initialization complete."); }
@Override public Session createSession( final HttpServerExchange serverExchange, final SessionConfig config) { if (evictionQueue != null) { if (expireOldestUnusedSessionOnMax) { while (sessions.size() >= maxSize && !evictionQueue.isEmpty()) { String key = evictionQueue.poll(); UndertowLogger.REQUEST_LOGGER.debugf("Removing session %s as max size has been hit", key); SessionImpl toRemove = sessions.get(key); if (toRemove != null) { toRemove.invalidate( null, SessionListener.SessionDestroyedReason.TIMEOUT); // todo: better reason } } } else if (sessions.size() >= maxSize) { if (statisticsEnabled) { rejectedSessionCount.incrementAndGet(); } throw UndertowMessages.MESSAGES.tooManySessions(maxSize); } } if (config == null) { throw UndertowMessages.MESSAGES.couldNotFindSessionCookieConfig(); } String sessionID = config.findSessionId(serverExchange); int count = 0; while (sessionID == null) { sessionID = sessionIdGenerator.createSessionId(); if (sessions.containsKey(sessionID)) { sessionID = null; } if (count++ == 100) { // this should never happen // but we guard against pathalogical session id generators to prevent an infinite loop throw UndertowMessages.MESSAGES.couldNotGenerateUniqueSessionId(); } } Object evictionToken; if (evictionQueue != null) { evictionToken = evictionQueue.offerLastAndReturnToken(sessionID); } else { evictionToken = null; } if (statisticsEnabled) { createdSessionCount.incrementAndGet(); } final SessionImpl session = new SessionImpl( this, sessionID, config, serverExchange.getIoThread(), serverExchange.getConnection().getWorker(), evictionToken, defaultSessionTimeout); sessions.put(sessionID, session); config.setSessionId(serverExchange, session.getId()); session.lastAccessed = System.currentTimeMillis(); session.bumpTimeout(); sessionListeners.sessionCreated(session, serverExchange); serverExchange.putAttachment(NEW_SESSION, session); return session; }