Skip to content

Commit a9f98d6

Browse files
committed
add objectIOFactoryClassName to context manager
1 parent 3bb1d13 commit a9f98d6

File tree

9 files changed

+94
-44
lines changed

9 files changed

+94
-44
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package de.javakaffee.web.msm;
22

3+
import de.javakaffee.web.msm.MemcachedSessionService.SessionManager;
4+
35
public class DefaultObjectIOFactory implements ObjectIOFactory {
46
@Override
5-
public ObjectIOStrategy createObjectIOStrategy() {
7+
public ObjectIOStrategy createObjectIOStrategy(final SessionManager _manager) {
68
return new DefaultObjectIOStrategy();
79
}
810
}

core/src/main/java/de/javakaffee/web/msm/DefaultObjectIOStrategy.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
import java.io.OutputStream;
1010

1111
public class DefaultObjectIOStrategy implements ObjectIOStrategy {
12-
1312
@Override
14-
public ObjectInput createObjectInput(InputStream is) throws IOException {
13+
public ObjectInput createObjectInput(final InputStream is) throws IOException {
1514
return new ObjectInputStream(is);
1615
}
1716

1817
@Override
19-
public ObjectOutput createObjectOutput(OutputStream os) throws IOException {
18+
public ObjectOutput createObjectOutput(final OutputStream os) throws IOException {
2019
return new ObjectOutputStream(os);
2120
}
2221
}

core/src/main/java/de/javakaffee/web/msm/MemcachedSessionService.java

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,23 @@ static enum LockStatus {
162162
* {@link JavaSerializationTranscoderFactory}.
163163
*/
164164
private String _transcoderFactoryClassName = JavaSerializationTranscoderFactory.class.getName();
165-
165+
166166
/**
167167
* The class name of the factory for
168-
* {@link net.spy.memcached.transcoders.Transcoder}s. Default class name is
169-
* {@link JavaSerializationTranscoderFactory}.
168+
* {@link de.javakaffee.web.msm.ObjectIOFactory}s. Default class name is
169+
* {@link de.javakaffee.web.msm.DefaultObjectIOFactory}.
170170
*/
171171
private String _objectIOFactoryClassName = DefaultObjectIOFactory.class.getName();
172172

173-
/**
173+
public String getObjectIOFactoryClassName() {
174+
return _objectIOFactoryClassName;
175+
}
176+
177+
public void setObjectIOFactoryClassName(final String objectIOFactoryClassName) {
178+
this._objectIOFactoryClassName = objectIOFactoryClassName;
179+
}
180+
181+
/**
174182
* Specifies, if iterating over collection elements shall be done on a copy
175183
* of the collection or on the collection itself.
176184
* <p>
@@ -305,8 +313,10 @@ public static interface SessionManager extends Manager {
305313
String getString(final String key, final Object... args);
306314

307315
boolean isMaxInactiveIntervalSet();
308-
int getMaxInactiveInterval();
309-
void setMaxInactiveInterval(int interval);
316+
@Override
317+
int getMaxInactiveInterval();
318+
@Override
319+
void setMaxInactiveInterval(int interval);
310320

311321
int getMaxActiveSessions();
312322
void incrementSessionCounter();
@@ -427,11 +437,12 @@ public void shutdown() {
427437
* @param storage the storage client to use, for normal operations this should be <code>null</code>.
428438
*/
429439
void startInternal( final StorageClient storage ) throws LifecycleException {
430-
if (storage == null)
431-
_storage = null;
432-
else
433-
_storage = storage;
434-
440+
if (storage == null) {
441+
_storage = null;
442+
} else {
443+
_storage = storage;
444+
}
445+
435446
startInternal();
436447
}
437448

@@ -506,7 +517,7 @@ protected MemcachedNodesManager createMemcachedNodesManager(final String memcach
506517
}
507518

508519
private TranscoderService createTranscoderService( final Statistics statistics ) {
509-
return new TranscoderService( getTranscoderFactory().createTranscoder( _manager ), getObjectIOFactory().createObjectIOStrategy() );
520+
return new TranscoderService( getTranscoderFactory().createTranscoder( _manager ), getObjectIOFactory().createObjectIOStrategy( _manager ) );
510521
}
511522

512523
protected TranscoderFactory getTranscoderFactory() {
@@ -519,7 +530,7 @@ protected TranscoderFactory getTranscoderFactory() {
519530
}
520531
return _transcoderFactory;
521532
}
522-
533+
523534
protected ObjectIOFactory getObjectIOFactory() {
524535
if ( _objectIOFactory == null ) {
525536
try {
@@ -553,7 +564,7 @@ private TranscoderFactory createTranscoderFactory() throws InstantiationExceptio
553564
}
554565
return transcoderFactory;
555566
}
556-
567+
557568
private ObjectIOFactory createObjectIOFactory() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
558569
_log.info( "Creating objectIO factory " + _objectIOFactoryClassName );
559570
final Class<? extends ObjectIOFactory> objectIOFactoryClass = loadFactoryClass(_objectIOFactoryClassName, _manager.getContainerClassLoader(), ObjectIOFactory.class);
@@ -562,7 +573,7 @@ private ObjectIOFactory createObjectIOFactory() throws InstantiationException, I
562573
}
563574

564575

565-
private <T> Class<? extends T> loadFactoryClass(String className, ClassLoader classLoader, Class<T> factoryInterface) throws ClassNotFoundException {
576+
private <T> Class<? extends T> loadFactoryClass(final String className, final ClassLoader classLoader, final Class<T> factoryInterface) throws ClassNotFoundException {
566577
Class<? extends T> factoryClass;
567578
try {
568579
_log.debug( "Loading transcoder factory class " + className + " using classloader " + classLoader );
@@ -751,7 +762,7 @@ public MemcachedBackupSession createSession( String sessionId ) {
751762
if ( _log.isDebugEnabled() ) {
752763
_log.debug( "Remove session id " + session.getId() + " from _invalidSessionsCache, marking new session valid" );
753764
}
754-
_invalidSessionsCache.remove(session.getId());
765+
_invalidSessionsCache.remove(session.getId());
755766
}
756767
return session;
757768

@@ -998,15 +1009,17 @@ private MemcachedBackupSession loadBackupSession(final String requestedSessionId
9981009
try {
9991010
final SessionValidityInfo validityInfo = _lockingStrategy.loadBackupSessionValidityInfo( requestedSessionId );
10001011
if ( validityInfo == null || !validityInfo.isValid() ) {
1001-
if(_log.isDebugEnabled())
1002-
_log.debug( "No validity info (or no valid one) found for sessionId " + requestedSessionId );
1012+
if(_log.isDebugEnabled()) {
1013+
_log.debug( "No validity info (or no valid one) found for sessionId " + requestedSessionId );
1014+
}
10031015
return null;
10041016
}
10051017

10061018
final byte[] obj = _storage.get( getSessionIdFormat().createBackupKey( requestedSessionId ) );
10071019
if ( obj == null ) {
1008-
if(_log.isDebugEnabled())
1009-
_log.debug( "No backup found for sessionId " + requestedSessionId );
1020+
if(_log.isDebugEnabled()) {
1021+
_log.debug( "No backup found for sessionId " + requestedSessionId );
1022+
}
10101023
return null;
10111024
}
10121025

@@ -1036,14 +1049,16 @@ public void requestFinished(final String sessionId, final String requestId) {
10361049
if(!_sticky) {
10371050
final MemcachedBackupSession msmSession = _manager.getSessionInternal( sessionId );
10381051
if ( msmSession == null ) {
1039-
if(_log.isDebugEnabled())
1040-
_log.debug( "No session found in session map for " + sessionId );
1052+
if(_log.isDebugEnabled()) {
1053+
_log.debug( "No session found in session map for " + sessionId );
1054+
}
10411055
return;
10421056
}
10431057

10441058
if ( !msmSession.isValidInternal() ) {
1045-
if(_log.isDebugEnabled())
1046-
_log.debug( "Non valid session found in session map for " + sessionId );
1059+
if(_log.isDebugEnabled()) {
1060+
_log.debug( "Non valid session found in session map for " + sessionId );
1061+
}
10471062
return;
10481063
}
10491064

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

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

11041121
if ( !msmSession.isValidInternal() ) {
1105-
if(_log.isDebugEnabled())
1106-
_log.debug( "Non valid session found in session map for " + sessionId );
1122+
if(_log.isDebugEnabled()) {
1123+
_log.debug( "Non valid session found in session map for " + sessionId );
1124+
}
11071125
return new SimpleFuture<BackupResult>( BackupResult.SKIPPED );
11081126
}
11091127

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

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

11281147
if ( !_sticky ) {
@@ -1256,7 +1275,7 @@ public void setMemcachedNodes( final String memcachedNodes ) {
12561275
public String getMemcachedNodes() {
12571276
return _memcachedNodes;
12581277
}
1259-
1278+
12601279
private MemcachedNodesManager reloadMemcachedConfig( final String memcachedNodes, final String failoverNodes ) {
12611280

12621281
/* first create all dependent services
@@ -1666,7 +1685,7 @@ protected void updateExpirationInMemcached() {
16661685
public void setSessionBackupAsync( final boolean sessionBackupAsync ) {
16671686
final boolean oldSessionBackupAsync = _sessionBackupAsync;
16681687
_sessionBackupAsync = sessionBackupAsync;
1669-
if ( ( oldSessionBackupAsync != sessionBackupAsync ) && _manager.isInitialized() ) {
1688+
if ( oldSessionBackupAsync != sessionBackupAsync && _manager.isInitialized() ) {
16701689
_log.info( "SessionBackupAsync was changed to " + sessionBackupAsync + ", creating new BackupSessionService with new configuration." );
16711690
_backupSessionService = new BackupSessionService( _transcoderService, _sessionBackupAsync, _sessionBackupTimeout,
16721691
_backupThreadCount, _storage, _memcachedNodesManager, _statistics );
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.javakaffee.web.msm;
22

3+
import de.javakaffee.web.msm.MemcachedSessionService.SessionManager;
4+
35
public interface ObjectIOFactory {
4-
ObjectIOStrategy createObjectIOStrategy();
6+
ObjectIOStrategy createObjectIOStrategy(SessionManager sessionManager);
57
}

core/src/main/java/de/javakaffee/web/msm/TranscoderService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ public TranscoderService( final SessionAttributesTranscoder attributesTranscoder
7979
_attributesTranscoder = attributesTranscoder;
8080
_objectIOStrategy = new DefaultObjectIOStrategy();
8181
}
82-
82+
8383
/**
8484
* Creates a new {@link TranscoderService}.
8585
*
8686
* @param attributesTranscoder the {@link SessionAttributesTranscoder} strategy to use.
8787
*/
88-
public TranscoderService( final SessionAttributesTranscoder attributesTranscoder, ObjectIOStrategy objectIOStrategy) {
88+
public TranscoderService( final SessionAttributesTranscoder attributesTranscoder, final ObjectIOStrategy objectIOStrategy) {
8989
_attributesTranscoder = attributesTranscoder;
9090
_objectIOStrategy = objectIOStrategy;
9191
}
@@ -512,7 +512,7 @@ public static int encodeNum( final long num, final byte[] data, final int beginI
512512
for ( int i = 0; i < maxBytes; i++ ) {
513513
final int pos = maxBytes - i - 1; // the position of the byte in the number
514514
final int idx = beginIndex + pos; // the index in the data array
515-
data[idx] = (byte) ( ( num >> ( 8 * i ) ) & 0xff );
515+
data[idx] = (byte) ( num >> 8 * i & 0xff );
516516
}
517517
return beginIndex + maxBytes;
518518
}
@@ -521,7 +521,7 @@ public static long decodeNum( final byte[] data, final int beginIndex, final int
521521
long result = 0;
522522
for ( int i = 0; i < numBytes; i++ ) {
523523
final byte b = data[beginIndex + i];
524-
result = ( result << 8 ) | ( b < 0
524+
result = result << 8 | ( b < 0
525525
? 256 + b
526526
: b );
527527
}

tomcat6/src/main/java/de/javakaffee/web/msm/MemcachedBackupSessionManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,4 +1120,11 @@ public String[] getSetCookieHeaders(final Response response) {
11201120
return response.getHeaderValues("Set-Cookie");
11211121
}
11221122

1123+
public String getObjectIOFactoryClassName() {
1124+
return _msm.getObjectIOFactoryClassName();
1125+
}
1126+
1127+
public void setObjectIOFactoryClassName(final String objectIOFactoryClassName) {
1128+
_msm.setObjectIOFactoryClassName(objectIOFactoryClassName);
1129+
}
11231130
}

tomcat7/src/main/java/de/javakaffee/web/msm/MemcachedBackupSessionManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,4 +942,11 @@ public String[] getSetCookieHeaders(final Response response) {
942942
return result.toArray(new String[result.size()]);
943943
}
944944

945+
public String getObjectIOFactoryClassName() {
946+
return _msm.getObjectIOFactoryClassName();
947+
}
948+
949+
public void setObjectIOFactoryClassName(final String objectIOFactoryClassName) {
950+
_msm.setObjectIOFactoryClassName(objectIOFactoryClassName);
951+
}
945952
}

tomcat8/src/main/java/de/javakaffee/web/msm/MemcachedBackupSessionManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,4 +917,11 @@ public String[] getSetCookieHeaders(final Response response) {
917917
return result.toArray(new String[result.size()]);
918918
}
919919

920+
public String getObjectIOFactoryClassName() {
921+
return _msm.getObjectIOFactoryClassName();
922+
}
923+
924+
public void setObjectIOFactoryClassName(final String objectIOFactoryClassName) {
925+
_msm.setObjectIOFactoryClassName(objectIOFactoryClassName);
926+
}
920927
}

tomcat9/src/main/java/de/javakaffee/web/msm/MemcachedBackupSessionManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,4 +917,11 @@ public String[] getSetCookieHeaders(final Response response) {
917917
return result.toArray(new String[result.size()]);
918918
}
919919

920+
public String getObjectIOFactoryClassName() {
921+
return _msm.getObjectIOFactoryClassName();
922+
}
923+
924+
public void setObjectIOFactoryClassName(final String objectIOFactoryClassName) {
925+
_msm.setObjectIOFactoryClassName(objectIOFactoryClassName);
926+
}
920927
}

0 commit comments

Comments
 (0)