HttpSessionTransactionTokenStore implements TransactionTokenStore { @Override public String createAndReserveTokenKey(String tokenName) { String tokenNamePrefix = TOKEN_HOLDER_SESSION_ATTRIBUTE_PREFIX + tokenName; Set<String> sessionAttributeNames = new HashSet<String>(); HttpSession session = getSession(); Object mutex = getMutex(session); String tokenKey = null; synchronized (mutex) { Enumeration<String> tokenNameEnumeration = session .getAttributeNames(); while (tokenNameEnumeration.hasMoreElements()) { String name = tokenNameEnumeration.nextElement(); if (tokenNamePrefix.equals(name.split( TransactionToken.TOKEN_STRING_SEPARATOR)[0])) { sessionAttributeNames.add(name); } } for (int i = 0, max = sessionAttributeNames.size(); i < max; i++) { if (sessionAttributeNames .size() >= transactionTokensPerTokenName) { removeOldTokenName(sessionAttributeNames, session); } else { break; } } for (int i = 0; i < retryCreateTokenName; i++) { String str = generator.generate(session.getId()); String name = tokenNamePrefix + TransactionToken.TOKEN_STRING_SEPARATOR + str; if (!sessionAttributeNames.contains(name)) { tokenKey = str; break; } } } if (tokenKey == null) { throw new IllegalStateException("token key generation failed within retry count " + retryCreateTokenName); } return tokenKey; } HttpSessionTransactionTokenStore();  HttpSessionTransactionTokenStore(
            int transactionTokenSizePerTokenName);  HttpSessionTransactionTokenStore(
            int transactionTokenSizePerTokenName, int retryCreateTokenName);  HttpSessionTransactionTokenStore(
            final TokenStringGenerator generator,
            final int transactionTokenSizePerTokenName,
            final int retryCreateTokenName); @Override String getAndClear(TransactionToken token); @Override void remove(TransactionToken token); @Override String createAndReserveTokenKey(String tokenName); @Override void store(TransactionToken token); static final String TOKEN_HOLDER_SESSION_ATTRIBUTE_PREFIX; static final int NO_OF_TOKENS_PER_TOKEN_NAME; static final int DEFAULT_RETRY_CREATE_TOKEN_NAME; }
@Test(expected = IllegalStateException.class) public void testCreateAndReserveTokenKey_generate_failed() throws Exception { store = new HttpSessionTransactionTokenStore(new TokenStringGenerator() { @Override public String generate(String seed) { return "xxxxx"; } }, 5, 5); MockHttpSession session = new MockHttpSession(); request.setSession(session); session.setAttribute( HttpSessionTransactionTokenStore.TOKEN_HOLDER_SESSION_ATTRIBUTE_PREFIX + "foo" + TransactionToken.TOKEN_STRING_SEPARATOR + "xxxxx", "already in!"); try { store.createAndReserveTokenKey("foo"); } catch (Exception e) { assertThat(e.getMessage(), is( "token key generation failed within retry count 5")); throw e; } }