/** * Filter map of attributes using our name pattern. * * @return the filtered attribute map that only includes attributes that shall be stored in memcached. */ public ConcurrentMap<String, Object> getAttributesFiltered() { if ( this.manager == null ) { throw new IllegalStateException( "There's no manager set." ); } final Pattern pattern = ((SessionManager)manager).getMemcachedSessionService().getSessionAttributePattern(); final ConcurrentMap<String, Object> attributes = getAttributesInternal(); if ( pattern == null ) { return attributes; } final ConcurrentMap<String, Object> result = new ConcurrentHashMap<String, Object>( attributes.size() ); for ( final Map.Entry<String, Object> entry: attributes.entrySet() ) { if ( pattern.matcher(entry.getKey()).matches() ) { result.put( entry.getKey(), entry.getValue() ); } } return result; }
/** * Serialize the given session to a byte array. This is a shortcut for * <code><pre> * final byte[] attributesData = serializeAttributes( session, session.getAttributes() ); * serialize( session, attributesData ); * </pre></code> * The returned byte array can be deserialized using {@link #deserialize(byte[], SessionManager)}. * * @see #serializeAttributes(MemcachedBackupSession, ConcurrentMap) * @see #serialize(MemcachedBackupSession, byte[]) * @see #deserialize(byte[], SessionManager) * @param session the session to serialize. * @return the serialized session data. */ public byte[] serialize( final MemcachedBackupSession session ) { final byte[] attributesData = serializeAttributes( session, session.getAttributesInternal() ); return serialize( session, attributesData ); }
private void checkSession(final MemcachedBackupSession actual, final MemcachedBackupSession expected) { assertNotNull(actual); assertEquals(actual.getId(), expected.getId()); assertEquals(actual.getAttributesInternal(), expected.getAttributesInternal()); } }
private void checkSession(final MemcachedBackupSession actual, final MemcachedBackupSession expected) { assertNotNull(actual); assertEquals(actual.getId(), expected.getId()); assertEquals(actual.getAttributesInternal(), expected.getAttributesInternal()); }
/** * Test that session attribute serialization and hash calculation is only * performed if the session and its attributes were accessed since the last backup/backup check. * Otherwise this computing time shall be saved for a better world :-) * @throws ExecutionException * @throws InterruptedException */ @Test public void testOnlyHashAttributesOfAccessedSessionsAndAttributes() throws InterruptedException, ExecutionException { final TranscoderService transcoderServiceMock = mock( TranscoderService.class ); @SuppressWarnings( "unchecked" ) final ConcurrentMap<String, Object> anyMap = any( ConcurrentMap.class ); when( transcoderServiceMock.serializeAttributes( any( MemcachedBackupSession.class ), anyMap ) ).thenReturn( new byte[0] ); _service.setTranscoderService( transcoderServiceMock ); final MemcachedBackupSession session = createSession( _service ); session.setAttribute( "foo", "bar" ); _service.backupSession( session.getIdInternal(), false, null ).get(); verify( transcoderServiceMock, times( 1 ) ).serializeAttributes( eq( session ), eq( session.getAttributesInternal() ) ); // we need some millis between last backup and next access (due to check in BackupSessionService) Thread.sleep(5L); session.access(); session.getAttribute( "foo" ); _service.backupSession( session.getIdInternal(), false, null ).get(); verify( transcoderServiceMock, times( 2 ) ).serializeAttributes( eq( session ), eq( session.getAttributesInternal() ) ); // we need some millis between last backup and next access (due to check in BackupSessionService) Thread.sleep(5L); _service.backupSession( session.getIdInternal(), false, null ).get(); verify( transcoderServiceMock, times( 2 ) ).serializeAttributes( eq( session ), eq( session.getAttributesInternal() ) ); }
/** * Test that session attribute serialization and hash calculation is only * performed if session attributes were accessed since the last backup. * Otherwise this computing time shall be saved for a better world :-) * @throws ExecutionException * @throws InterruptedException */ @Test public void testOnlyHashAttributesOfAccessedAttributes() throws InterruptedException, ExecutionException { final TranscoderService transcoderServiceMock = mock( TranscoderService.class ); @SuppressWarnings( "unchecked" ) final ConcurrentMap<String, Object> anyMap = any( ConcurrentMap.class ); when( transcoderServiceMock.serializeAttributes( any( MemcachedBackupSession.class ), anyMap ) ).thenReturn( new byte[0] ); _service.setTranscoderService( transcoderServiceMock ); final MemcachedBackupSession session = createSession( _service ); session.access(); session.endAccess(); session.setAttribute( "foo", "bar" ); _service.backupSession( session.getIdInternal(), false, null ).get(); verify( transcoderServiceMock, times( 1 ) ).serializeAttributes( eq( session ), eq( session.getAttributesInternal() ) ); session.access(); session.endAccess(); _service.backupSession( session.getIdInternal(), false, null ).get(); verify( transcoderServiceMock, times( 1 ) ).serializeAttributes( eq( session ), eq( session.getAttributesInternal() ) ); }
/** * Filter map of attributes using our name pattern. * * @return the filtered attribute map that only includes attributes that shall be stored in memcached. */ public ConcurrentMap<String, Object> getAttributesFiltered() { if ( this.manager == null ) { throw new IllegalStateException( "There's no manager set." ); } final Pattern pattern = ((SessionManager)manager).getMemcachedSessionService().getSessionAttributePattern(); final ConcurrentMap<String, Object> attributes = getAttributesInternal(); if ( pattern == null ) { return attributes; } final ConcurrentMap<String, Object> result = new ConcurrentHashMap<String, Object>( attributes.size() ); for ( final Map.Entry<String, Object> entry: attributes.entrySet() ) { if ( pattern.matcher(entry.getKey()).matches() ) { result.put( entry.getKey(), entry.getValue() ); } } return result; }
/** * Serialize the given session to a byte array. This is a shortcut for * <code><pre> * final byte[] attributesData = serializeAttributes( session, session.getAttributes() ); * serialize( session, attributesData ); * </pre></code> * The returned byte array can be deserialized using {@link #deserialize(byte[], SessionManager)}. * * @see #serializeAttributes(MemcachedBackupSession, ConcurrentMap) * @see #serialize(MemcachedBackupSession, byte[]) * @see #deserialize(byte[], SessionManager) * @param session the session to serialize. * @return the serialized session data. */ public byte[] serialize( final MemcachedBackupSession session ) { final byte[] attributesData = serializeAttributes( session, session.getAttributesInternal() ); return serialize( session, attributesData ); }