@Override public io.undertow.server.session.Session createSession( HttpServerExchange exchange, SessionConfig config) { if (config == null) { throw UndertowMessages.MESSAGES.couldNotFindSessionCookieConfig(); } String id = config.findSessionId(exchange); if (id == null) { int attempts = 0; do { if (++attempts > MAX_SESSION_ID_GENERATION_ATTEMPTS) { throw UndertowMessages.MESSAGES.couldNotGenerateUniqueSessionId(); } id = this.manager.createIdentifier(); } while (this.manager.containsSession(id)); config.setSessionId(exchange, id); } Batch batch = this.manager.getBatcher().createBatch(); try { Session<LocalSessionContext> session = this.manager.createSession(id); io.undertow.server.session.Session adapter = new DistributableSession(this, session, config, batch); this.sessionListeners.sessionCreated(adapter, exchange); return adapter; } catch (RuntimeException | Error e) { batch.discard(); throw e; } }
@Override public io.undertow.server.session.Session getSession(String sessionId) { Batch batch = this.manager.getBatcher().createBatch(); try { ImmutableSession session = this.manager.viewSession(sessionId); return (session != null) ? new DistributableImmutableSession(this, session) : null; } finally { batch.discard(); } }
@Override public void remove(K id) { try (Batch batch = this.manager.getBatcher().createBatch()) { try { Bean<K, V> bean = this.manager.findBean(id); if (bean != null) { bean.remove(this.listener); } } catch (RuntimeException | Error e) { batch.discard(); throw e; } } }
@Override public void discard(V value) { try (BatchContext context = this.manager.getBatcher().resumeBatch(value.getCacheContext())) { try (Batch batch = value.getCacheContext()) { try { Bean<K, V> bean = this.manager.findBean(value.getId()); if (bean != null) { bean.remove(null); } } catch (RuntimeException | Error e) { batch.discard(); throw e; } } } }
@Override public io.undertow.server.session.Session getSession( HttpServerExchange exchange, SessionConfig config) { String id = config.findSessionId(exchange); if (id == null) return null; Batch batch = this.manager.getBatcher().createBatch(); try { Session<LocalSessionContext> session = this.manager.findSession(id); if (session == null) { batch.discard(); return null; } return new DistributableSession(this, session, config, batch); } catch (RuntimeException | Error e) { batch.discard(); throw e; } }
@Override public V get(K id) { // Batch is not closed here - it will be closed during release(...) or discard(...) @SuppressWarnings("resource") Batch batch = this.manager.getBatcher().createBatch(); try { Bean<K, V> bean = this.manager.findBean(id); if (bean == null) { batch.close(); return null; } V result = bean.acquire(); result.setCacheContext(batch); return result; } catch (RuntimeException | Error e) { batch.discard(); batch.close(); throw e; } }
@SuppressWarnings("unchecked") @Override public V create() { boolean newGroup = CURRENT_GROUP.get() == null; try (Batch batch = this.manager.getBatcher().createBatch()) { try { // This will invoke Cache.create() for nested beans // Nested beans will share the same group identifier V instance = this.factory.createInstance(); K id = instance.getId(); this.manager.createBean(id, (K) CURRENT_GROUP.get(), instance).close(); return instance; } catch (RuntimeException | Error e) { batch.discard(); throw e; } } finally { if (newGroup) { CURRENT_GROUP.remove(); } } }