Skip to content
This repository was archived by the owner on Feb 7, 2018. It is now read-only.

Commit d899849

Browse files
committed
More fixes to support Tomcat 8. This release drops support for Tomcat 7.
1 parent 2cf7400 commit d899849

File tree

6 files changed

+89
-109
lines changed

6 files changed

+89
-109
lines changed

pom.xml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<artifactId>aws-dynamodb-session-tomcat</artifactId>
77
<packaging>jar</packaging>
88
<name>Amazon DynamoDB Session Manager for Tomcat</name>
9-
<version>2.0.3</version>
9+
<version>2.0.4</version>
1010
<description>The Amazon DynamoDB Session Manager for Tomcat provides a custom session manager for Tomcat 7 that stores session data in Amazon DynamoDB, Amazon's fully managed NoSQL database service.</description>
1111
<url>https://aws.amazon.com/java</url>
1212

@@ -43,7 +43,7 @@
4343
<dependency>
4444
<groupId>org.apache.tomcat</groupId>
4545
<artifactId>tomcat-catalina</artifactId>
46-
<version>7.0.64</version>
46+
<version>8.0.1</version>
4747
<scope>provided</scope>
4848
</dependency>
4949
<dependency>
@@ -73,13 +73,13 @@
7373
<dependency>
7474
<groupId>org.apache.tomcat.embed</groupId>
7575
<artifactId>tomcat-embed-core</artifactId>
76-
<version>7.0.64</version>
76+
<version>8.0.1</version>
7777
<scope>test</scope>
7878
</dependency>
7979
<dependency>
8080
<groupId>org.apache.tomcat.embed</groupId>
8181
<artifactId>tomcat-embed-jasper</artifactId>
82-
<version>7.0.64</version>
82+
<version>8.0.1</version>
8383
<scope>test</scope>
8484
</dependency>
8585
</dependencies>
@@ -102,8 +102,8 @@
102102
<artifactId>maven-compiler-plugin</artifactId>
103103
<version>2.3</version>
104104
<configuration>
105-
<source>1.6</source>
106-
<target>1.6</target>
105+
<source>1.7</source>
106+
<target>1.7</target>
107107
<encoding>UTF-8</encoding>
108108
</configuration>
109109
</plugin>
@@ -220,7 +220,6 @@
220220
<configuration>
221221
<serverId>sonatype-nexus-staging</serverId>
222222
<nexusUrl>https://oss.sonatype.org</nexusUrl>
223-
<releaseAfterClose>true</releaseAfterClose>
224223
</configuration>
225224
</plugin>
226225
</plugins>

src/main/java/com/amazonaws/services/dynamodb/sessionmanager/DynamoDBSessionManager.java

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@
1414
*/
1515
package com.amazonaws.services.dynamodb.sessionmanager;
1616

17-
import java.io.File;
18-
19-
import org.apache.catalina.Context;
20-
import org.apache.catalina.LifecycleException;
21-
import org.apache.catalina.session.PersistentManagerBase;
22-
import org.apache.juli.logging.Log;
23-
import org.apache.juli.logging.LogFactory;
24-
2517
import com.amazonaws.AmazonClientException;
2618
import com.amazonaws.ClientConfiguration;
2719
import com.amazonaws.auth.AWSCredentialsProvider;
@@ -37,6 +29,13 @@
3729
import com.amazonaws.services.dynamodbv2.util.Tables;
3830
import com.amazonaws.util.StringUtils;
3931

32+
import org.apache.catalina.LifecycleException;
33+
import org.apache.catalina.session.PersistentManagerBase;
34+
import org.apache.juli.logging.Log;
35+
import org.apache.juli.logging.LogFactory;
36+
37+
import java.io.File;
38+
4039
/**
4140
* Tomcat persistent session manager implementation that uses Amazon DynamoDB to store HTTP session
4241
* data.
@@ -71,7 +70,6 @@ public DynamoDBSessionManager() {
7170
setMaxIdleBackup(30); // 30 seconds
7271
}
7372

74-
@Override
7573
public String getInfo() {
7674
return info;
7775
}
@@ -131,8 +129,6 @@ public void setDeleteCorruptSessions(boolean deleteCorruptSessions) {
131129

132130
@Override
133131
protected void initInternal() throws LifecycleException {
134-
this.setDistributable(true);
135-
136132
AmazonDynamoDBClient dynamoClient = createDynamoClient();
137133
initDynamoTable(dynamoClient);
138134
DynamoSessionStorage sessionStorage = createSessionStorage(dynamoClient);
@@ -246,26 +242,8 @@ private DynamoSessionStorage createSessionStorage(AmazonDynamoDBClient dynamoCli
246242
}
247243

248244
private SessionConverter getSessionConverter() {
249-
ClassLoader classLoader = getAssociatedContext().getLoader().getClassLoader();
245+
ClassLoader classLoader = getContext().getLoader().getClassLoader();
250246
return SessionConverter.createDefaultSessionConverter(this, classLoader);
251247
}
252248

253-
/**
254-
* To be compatible with Tomcat7 we have to call the getContainer method rather than getContext.
255-
* The cast is safe as it only makes sense to use a session manager within the context of a
256-
* webapp, the Tomcat 8 version of getContainer just delegates to getContext. When Tomcat7 is no
257-
* longer supported this can be changed to getContext
258-
*
259-
* @return The context this manager is associated with
260-
*/
261-
// TODO Inline this method with getManager().getContext() when Tomcat7 is no longer supported
262-
private Context getAssociatedContext() {
263-
try {
264-
return (Context) getContainer();
265-
} catch (ClassCastException e) {
266-
logger.fatal("Unable to cast " + getClass().getName() + " to a Context."
267-
+ " DynamoDB SessionManager can only be used with a Context");
268-
throw new IllegalStateException(e);
269-
}
270-
}
271249
}

src/main/java/com/amazonaws/services/dynamodb/sessionmanager/DynamoDBSessionStore.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public DynamoDBSessionStore(DynamoSessionStorage sessionStorage, boolean deleteC
4646
this.deleteCorruptSessions = deleteCorruptSessions;
4747
}
4848

49-
@Override
5049
public String getInfo() {
5150
return info;
5251
}

src/test/java/com/amazonaws/services/dynamodb/sessionmanager/DynamoDBSessionManagerIntegrationTest.java

Lines changed: 14 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,11 @@
1414
*/
1515
package com.amazonaws.services.dynamodb.sessionmanager;
1616

17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertFalse;
19-
import static org.junit.Assert.assertNotNull;
20-
import static org.junit.Assert.assertNull;
21-
import static org.junit.Assert.assertTrue;
22-
23-
import java.io.File;
24-
import java.nio.ByteBuffer;
25-
import java.nio.file.Files;
26-
import java.nio.file.Paths;
27-
import java.util.Map;
28-
import java.util.concurrent.TimeUnit;
17+
import com.amazonaws.AmazonServiceException;
18+
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
19+
import com.amazonaws.services.dynamodbv2.model.DescribeTableRequest;
20+
import com.amazonaws.services.dynamodbv2.model.TableDescription;
21+
import com.amazonaws.test.AWSTestBase;
2922

3023
import org.apache.catalina.Context;
3124
import org.apache.catalina.Session;
@@ -36,15 +29,14 @@
3629
import org.junit.BeforeClass;
3730
import org.junit.Test;
3831

39-
import com.amazonaws.AmazonServiceException;
40-
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
41-
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
42-
import com.amazonaws.services.dynamodbv2.model.DescribeTableRequest;
43-
import com.amazonaws.services.dynamodbv2.model.GetItemResult;
44-
import com.amazonaws.services.dynamodbv2.model.PutItemRequest;
45-
import com.amazonaws.services.dynamodbv2.model.TableDescription;
46-
import com.amazonaws.test.AWSTestBase;
47-
import com.amazonaws.util.ImmutableMapParameter;
32+
import java.io.File;
33+
import java.nio.file.Files;
34+
import java.nio.file.Paths;
35+
import java.util.concurrent.TimeUnit;
36+
37+
import static org.junit.Assert.assertEquals;
38+
import static org.junit.Assert.assertFalse;
39+
import static org.junit.Assert.assertTrue;
4840

4941
public class DynamoDBSessionManagerIntegrationTest extends AWSTestBase {
5042

@@ -148,61 +140,13 @@ public void sessionSwappedOutToDynamo_IsUnchangedWhenSwappedBackIn() throws Exce
148140
assertEquals(originalCreationTime, sessionManager.getCreationTimestamp(SESSION_ID));
149141
}
150142

151-
@Test
152-
public void deleteCorruptSessionsEnabled_DeletesNonSerializableSessions() throws InterruptedException {
153-
GetItemResult result = saveAndTamperWithSession(true);
154-
assertNull(result.getItem());
155-
}
156-
157-
@Test
158-
public void deleteCorruptSessionsDisabled_DoesNotDeleteNonSerializableSessions() throws InterruptedException {
159-
GetItemResult result = saveAndTamperWithSession(false);
160-
assertNotNull(result.getItem());
161-
}
162-
163-
/**
164-
* Creates a new Session, makes sure it's backed up in Dynamo, tampers with that session to
165-
* corrupt it, forces the session manager to load it back in and then finally returns the
166-
* session if it still exists in Dynamo
167-
*
168-
* @param deleteCorruptSessions
169-
* Whether to configure the session manager to delete corrupt sessions or not
170-
* @return DynamoDB record after sessions have been loaded back in, if it exists. If
171-
* deleteCorruptSessions is true this 'should' return null, if deleteCorruptSessions is
172-
* false this 'should' return a non null item.
173-
* @throws InterruptedException
174-
*/
175-
private GetItemResult saveAndTamperWithSession(boolean deleteCorruptSessions) throws InterruptedException {
176-
TestDynamoDBSessionManager sessionManager = new TestDynamoDBSessionManager();
177-
sessionManager.setDeleteCorruptSessions(deleteCorruptSessions);
178-
sessionManager.setMaxIdleBackup(MAX_IDLE_BACKUP_SECONDS);
179-
configureWithExplicitCredentials(sessionManager);
180-
sessionManager.createSession(SESSION_ID);
181-
182-
// Make sure it's persisted to Dynamo first before corrupting
183-
Thread.sleep(TimeUnit.MILLISECONDS.convert(MAX_IDLE_BACKUP_SECONDS + 1, TimeUnit.SECONDS));
184-
sessionManager.reallyProcessExpires();
185-
186-
// Corrupt the session persisted in Dynamo
187-
Map<String, AttributeValue> attributes = ImmutableMapParameter.of(DynamoSessionItem.SESSION_ID_ATTRIBUTE_NAME,
188-
new AttributeValue(SESSION_ID), DynamoSessionItem.SESSION_DATA_ATTRIBUTE_NAME,
189-
new AttributeValue().withB(ByteBuffer.wrap(new byte[] { 1, 3, 45, 2, 24, 92 })));
190-
dynamo.putItem(new PutItemRequest(sessionTableName, attributes));
191-
192-
// Force a load of sessions so that corrupt sessions are evaluated by the session store
193-
sessionManager.reallyProcessExpires();
194-
195-
return dynamo.getItem(sessionTableName,
196-
ImmutableMapParameter.of(DynamoSessionItem.SESSION_ID_ATTRIBUTE_NAME, new AttributeValue(SESSION_ID)));
197-
}
198-
199143
/**
200144
* Bug in the deserialization of sessions was causing persisted sessions loaded via the
201145
* processExpires method to replace the active session in memory by incorrectly registering it
202146
* with the manager. This tests makes sure that any sessions loaded by process expires do not
203147
* affect the attributes of active sessions.
204148
*
205-
* @see https://github.ksda9001.workers.dev.aws/aws-dynamodb-session-tomcat/pull/19
149+
* @see <a href="https://github.ksda9001.workers.dev.aws/aws-dynamodb-session-tomcat/pull/19">PR #19</a>
206150
*/
207151
@Test
208152
public void swappedOutSessionsDoNotReplaceActiveSessionDuringProcessExpires() throws InterruptedException {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2011-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package com.amazonaws.services.dynamodb.sessionmanager;
16+
17+
import com.amazonaws.services.dynamodb.sessionmanager.converters.SessionConversionException;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.mockito.Mock;
22+
import org.mockito.runners.MockitoJUnitRunner;
23+
24+
import static org.junit.Assert.assertNull;
25+
import static org.mockito.Mockito.never;
26+
import static org.mockito.Mockito.times;
27+
import static org.mockito.Mockito.verify;
28+
import static org.mockito.Mockito.when;
29+
30+
@RunWith(MockitoJUnitRunner.class)
31+
public class DynamoDBSessionStoreIntegrationTest {
32+
33+
private static final String SESSION_ID = "1234";
34+
35+
@Mock
36+
private DynamoSessionStorage storage;
37+
38+
@Test
39+
public void loadCorruptSession_DeletesSessionWhenDeleteCorruptSessionsEnabled() throws
40+
Exception {
41+
stubLoadCorruptSession();
42+
final DynamoDBSessionStore sessionStore = new DynamoDBSessionStore(storage, true);
43+
assertNull(sessionStore.load(SESSION_ID));
44+
verify(storage, times(1)).deleteSession(SESSION_ID);
45+
}
46+
47+
@Test
48+
public void loadCorruptSession_DoesNotDeletesSessionWhenDeleteCorruptSessionsDisabled() throws
49+
Exception {
50+
stubLoadCorruptSession();
51+
final DynamoDBSessionStore sessionStore = new DynamoDBSessionStore(storage, false);
52+
assertNull(sessionStore.load(SESSION_ID));
53+
verify(storage, never()).deleteSession(SESSION_ID);
54+
}
55+
56+
private void stubLoadCorruptSession() {
57+
when(storage.loadSession(SESSION_ID))
58+
.thenThrow(new SessionConversionException("Unable to convert session"));
59+
}
60+
}

src/test/java/com/amazonaws/services/dynamodb/sessionmanager/converters/TestSessionFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private static Map<String, Object> getDefaultSessionAttributes() {
127127

128128
private static Manager getDefaultManager() {
129129
Manager mockManager = mock(Manager.class, RETURNS_DEEP_STUBS);
130-
when(mockManager.getContainer().getLogger().isDebugEnabled()).thenReturn(false);
130+
when(mockManager.getContext().getLogger().isDebugEnabled()).thenReturn(false);
131131
return mockManager;
132132
}
133133

0 commit comments

Comments
 (0)