Skip to content

Commit

Permalink
add objectIOFactoryClassName to context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
exabrial committed Jun 7, 2021
1 parent 3bb1d13 commit a9f98d6
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package de.javakaffee.web.msm;

import de.javakaffee.web.msm.MemcachedSessionService.SessionManager;

public class DefaultObjectIOFactory implements ObjectIOFactory {
@Override
public ObjectIOStrategy createObjectIOStrategy() {
public ObjectIOStrategy createObjectIOStrategy(final SessionManager _manager) {
return new DefaultObjectIOStrategy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
import java.io.OutputStream;

public class DefaultObjectIOStrategy implements ObjectIOStrategy {

@Override
public ObjectInput createObjectInput(InputStream is) throws IOException {
public ObjectInput createObjectInput(final InputStream is) throws IOException {
return new ObjectInputStream(is);
}

@Override
public ObjectOutput createObjectOutput(OutputStream os) throws IOException {
public ObjectOutput createObjectOutput(final OutputStream os) throws IOException {
return new ObjectOutputStream(os);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,23 @@ static enum LockStatus {
* {@link JavaSerializationTranscoderFactory}.
*/
private String _transcoderFactoryClassName = JavaSerializationTranscoderFactory.class.getName();

/**
* The class name of the factory for
* {@link net.spy.memcached.transcoders.Transcoder}s. Default class name is
* {@link JavaSerializationTranscoderFactory}.
* {@link de.javakaffee.web.msm.ObjectIOFactory}s. Default class name is
* {@link de.javakaffee.web.msm.DefaultObjectIOFactory}.
*/
private String _objectIOFactoryClassName = DefaultObjectIOFactory.class.getName();

/**
public String getObjectIOFactoryClassName() {
return _objectIOFactoryClassName;
}

public void setObjectIOFactoryClassName(final String objectIOFactoryClassName) {
this._objectIOFactoryClassName = objectIOFactoryClassName;
}

/**
* Specifies, if iterating over collection elements shall be done on a copy
* of the collection or on the collection itself.
* <p>
Expand Down Expand Up @@ -305,8 +313,10 @@ public static interface SessionManager extends Manager {
String getString(final String key, final Object... args);

boolean isMaxInactiveIntervalSet();
int getMaxInactiveInterval();
void setMaxInactiveInterval(int interval);
@Override
int getMaxInactiveInterval();
@Override
void setMaxInactiveInterval(int interval);

int getMaxActiveSessions();
void incrementSessionCounter();
Expand Down Expand Up @@ -427,11 +437,12 @@ public void shutdown() {
* @param storage the storage client to use, for normal operations this should be <code>null</code>.
*/
void startInternal( final StorageClient storage ) throws LifecycleException {
if (storage == null)
_storage = null;
else
_storage = storage;

if (storage == null) {
_storage = null;
} else {
_storage = storage;
}

startInternal();
}

Expand Down Expand Up @@ -506,7 +517,7 @@ protected MemcachedNodesManager createMemcachedNodesManager(final String memcach
}

private TranscoderService createTranscoderService( final Statistics statistics ) {
return new TranscoderService( getTranscoderFactory().createTranscoder( _manager ), getObjectIOFactory().createObjectIOStrategy() );
return new TranscoderService( getTranscoderFactory().createTranscoder( _manager ), getObjectIOFactory().createObjectIOStrategy( _manager ) );
}

protected TranscoderFactory getTranscoderFactory() {
Expand All @@ -519,7 +530,7 @@ protected TranscoderFactory getTranscoderFactory() {
}
return _transcoderFactory;
}

protected ObjectIOFactory getObjectIOFactory() {
if ( _objectIOFactory == null ) {
try {
Expand Down Expand Up @@ -553,7 +564,7 @@ private TranscoderFactory createTranscoderFactory() throws InstantiationExceptio
}
return transcoderFactory;
}

private ObjectIOFactory createObjectIOFactory() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
_log.info( "Creating objectIO factory " + _objectIOFactoryClassName );
final Class<? extends ObjectIOFactory> objectIOFactoryClass = loadFactoryClass(_objectIOFactoryClassName, _manager.getContainerClassLoader(), ObjectIOFactory.class);
Expand All @@ -562,7 +573,7 @@ private ObjectIOFactory createObjectIOFactory() throws InstantiationException, I
}


private <T> Class<? extends T> loadFactoryClass(String className, ClassLoader classLoader, Class<T> factoryInterface) throws ClassNotFoundException {
private <T> Class<? extends T> loadFactoryClass(final String className, final ClassLoader classLoader, final Class<T> factoryInterface) throws ClassNotFoundException {
Class<? extends T> factoryClass;
try {
_log.debug( "Loading transcoder factory class " + className + " using classloader " + classLoader );
Expand Down Expand Up @@ -751,7 +762,7 @@ public MemcachedBackupSession createSession( String sessionId ) {
if ( _log.isDebugEnabled() ) {
_log.debug( "Remove session id " + session.getId() + " from _invalidSessionsCache, marking new session valid" );
}
_invalidSessionsCache.remove(session.getId());
_invalidSessionsCache.remove(session.getId());
}
return session;

Expand Down Expand Up @@ -998,15 +1009,17 @@ private MemcachedBackupSession loadBackupSession(final String requestedSessionId
try {
final SessionValidityInfo validityInfo = _lockingStrategy.loadBackupSessionValidityInfo( requestedSessionId );
if ( validityInfo == null || !validityInfo.isValid() ) {
if(_log.isDebugEnabled())
_log.debug( "No validity info (or no valid one) found for sessionId " + requestedSessionId );
if(_log.isDebugEnabled()) {
_log.debug( "No validity info (or no valid one) found for sessionId " + requestedSessionId );
}
return null;
}

final byte[] obj = _storage.get( getSessionIdFormat().createBackupKey( requestedSessionId ) );
if ( obj == null ) {
if(_log.isDebugEnabled())
_log.debug( "No backup found for sessionId " + requestedSessionId );
if(_log.isDebugEnabled()) {
_log.debug( "No backup found for sessionId " + requestedSessionId );
}
return null;
}

Expand Down Expand Up @@ -1036,14 +1049,16 @@ public void requestFinished(final String sessionId, final String requestId) {
if(!_sticky) {
final MemcachedBackupSession msmSession = _manager.getSessionInternal( sessionId );
if ( msmSession == null ) {
if(_log.isDebugEnabled())
_log.debug( "No session found in session map for " + sessionId );
if(_log.isDebugEnabled()) {
_log.debug( "No session found in session map for " + sessionId );
}
return;
}

if ( !msmSession.isValidInternal() ) {
if(_log.isDebugEnabled())
_log.debug( "Non valid session found in session map for " + sessionId );
if(_log.isDebugEnabled()) {
_log.debug( "Non valid session found in session map for " + sessionId );
}
return;
}

Expand All @@ -1052,8 +1067,9 @@ public void requestFinished(final String sessionId, final String requestId) {
// we must not remove it as this would case session data loss
// for the other request
if ( msmSession.releaseReference() > 0 ) {
if(_log.isDebugEnabled())
_log.debug( "Session " + sessionId + " is still used by another request, skipping backup and (optional) lock handling/release." );
if(_log.isDebugEnabled()) {
_log.debug( "Session " + sessionId + " is still used by another request, skipping backup and (optional) lock handling/release." );
}
return;
}
msmSession.passivate();
Expand Down Expand Up @@ -1090,8 +1106,9 @@ public Future<BackupResult> backupSession( final String sessionId, final boolean

final MemcachedBackupSession msmSession = _manager.getSessionInternal( sessionId );
if ( msmSession == null ) {
if(_log.isDebugEnabled())
_log.debug( "No session found in session map for " + sessionId );
if(_log.isDebugEnabled()) {
_log.debug( "No session found in session map for " + sessionId );
}
if ( !_sticky ) {
// Issue 116/137: Only notify the lockingStrategy if the session was loaded and has not been removed/invalidated
if(!_invalidSessionsCache.containsKey(sessionId)) {
Expand All @@ -1102,8 +1119,9 @@ public Future<BackupResult> backupSession( final String sessionId, final boolean
}

if ( !msmSession.isValidInternal() ) {
if(_log.isDebugEnabled())
_log.debug( "Non valid session found in session map for " + sessionId );
if(_log.isDebugEnabled()) {
_log.debug( "Non valid session found in session map for " + sessionId );
}
return new SimpleFuture<BackupResult>( BackupResult.SKIPPED );
}

Expand All @@ -1113,16 +1131,17 @@ public Future<BackupResult> backupSession( final String sessionId, final boolean
// we must not remove it as this would case session data loss
// for the other request
if ( msmSession.releaseReference() > 0 ) {
if(_log.isDebugEnabled())
_log.debug( "Session " + sessionId + " is still used by another request, skipping backup and (optional) lock handling/release." );
if(_log.isDebugEnabled()) {
_log.debug( "Session " + sessionId + " is still used by another request, skipping backup and (optional) lock handling/release." );
}
return new SimpleFuture<BackupResult>( BackupResult.SKIPPED );
}
msmSession.passivate();
_manager.removeInternal( msmSession, false );
}
}

final boolean force = sessionIdChanged || msmSession.isSessionIdChanged() || !_sticky && (msmSession.getSecondsSinceLastBackup() >= msmSession.getMaxInactiveInterval());
final boolean force = sessionIdChanged || msmSession.isSessionIdChanged() || !_sticky && msmSession.getSecondsSinceLastBackup() >= msmSession.getMaxInactiveInterval();
final Future<BackupResult> result = _backupSessionService.backupSession( msmSession, force );

if ( !_sticky ) {
Expand Down Expand Up @@ -1256,7 +1275,7 @@ public void setMemcachedNodes( final String memcachedNodes ) {
public String getMemcachedNodes() {
return _memcachedNodes;
}

private MemcachedNodesManager reloadMemcachedConfig( final String memcachedNodes, final String failoverNodes ) {

/* first create all dependent services
Expand Down Expand Up @@ -1666,7 +1685,7 @@ protected void updateExpirationInMemcached() {
public void setSessionBackupAsync( final boolean sessionBackupAsync ) {
final boolean oldSessionBackupAsync = _sessionBackupAsync;
_sessionBackupAsync = sessionBackupAsync;
if ( ( oldSessionBackupAsync != sessionBackupAsync ) && _manager.isInitialized() ) {
if ( oldSessionBackupAsync != sessionBackupAsync && _manager.isInitialized() ) {
_log.info( "SessionBackupAsync was changed to " + sessionBackupAsync + ", creating new BackupSessionService with new configuration." );
_backupSessionService = new BackupSessionService( _transcoderService, _sessionBackupAsync, _sessionBackupTimeout,
_backupThreadCount, _storage, _memcachedNodesManager, _statistics );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.javakaffee.web.msm;

import de.javakaffee.web.msm.MemcachedSessionService.SessionManager;

public interface ObjectIOFactory {
ObjectIOStrategy createObjectIOStrategy();
ObjectIOStrategy createObjectIOStrategy(SessionManager sessionManager);
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ public TranscoderService( final SessionAttributesTranscoder attributesTranscoder
_attributesTranscoder = attributesTranscoder;
_objectIOStrategy = new DefaultObjectIOStrategy();
}

/**
* Creates a new {@link TranscoderService}.
*
* @param attributesTranscoder the {@link SessionAttributesTranscoder} strategy to use.
*/
public TranscoderService( final SessionAttributesTranscoder attributesTranscoder, ObjectIOStrategy objectIOStrategy) {
public TranscoderService( final SessionAttributesTranscoder attributesTranscoder, final ObjectIOStrategy objectIOStrategy) {
_attributesTranscoder = attributesTranscoder;
_objectIOStrategy = objectIOStrategy;
}
Expand Down Expand Up @@ -512,7 +512,7 @@ public static int encodeNum( final long num, final byte[] data, final int beginI
for ( int i = 0; i < maxBytes; i++ ) {
final int pos = maxBytes - i - 1; // the position of the byte in the number
final int idx = beginIndex + pos; // the index in the data array
data[idx] = (byte) ( ( num >> ( 8 * i ) ) & 0xff );
data[idx] = (byte) ( num >> 8 * i & 0xff );
}
return beginIndex + maxBytes;
}
Expand All @@ -521,7 +521,7 @@ public static long decodeNum( final byte[] data, final int beginIndex, final int
long result = 0;
for ( int i = 0; i < numBytes; i++ ) {
final byte b = data[beginIndex + i];
result = ( result << 8 ) | ( b < 0
result = result << 8 | ( b < 0
? 256 + b
: b );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1120,4 +1120,11 @@ public String[] getSetCookieHeaders(final Response response) {
return response.getHeaderValues("Set-Cookie");
}

public String getObjectIOFactoryClassName() {
return _msm.getObjectIOFactoryClassName();
}

public void setObjectIOFactoryClassName(final String objectIOFactoryClassName) {
_msm.setObjectIOFactoryClassName(objectIOFactoryClassName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -942,4 +942,11 @@ public String[] getSetCookieHeaders(final Response response) {
return result.toArray(new String[result.size()]);
}

public String getObjectIOFactoryClassName() {
return _msm.getObjectIOFactoryClassName();
}

public void setObjectIOFactoryClassName(final String objectIOFactoryClassName) {
_msm.setObjectIOFactoryClassName(objectIOFactoryClassName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -917,4 +917,11 @@ public String[] getSetCookieHeaders(final Response response) {
return result.toArray(new String[result.size()]);
}

public String getObjectIOFactoryClassName() {
return _msm.getObjectIOFactoryClassName();
}

public void setObjectIOFactoryClassName(final String objectIOFactoryClassName) {
_msm.setObjectIOFactoryClassName(objectIOFactoryClassName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -917,4 +917,11 @@ public String[] getSetCookieHeaders(final Response response) {
return result.toArray(new String[result.size()]);
}

public String getObjectIOFactoryClassName() {
return _msm.getObjectIOFactoryClassName();
}

public void setObjectIOFactoryClassName(final String objectIOFactoryClassName) {
_msm.setObjectIOFactoryClassName(objectIOFactoryClassName);
}
}

0 comments on commit a9f98d6

Please sign in to comment.