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

upgrade on aws sdk version #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,22 @@
</developer>
</developers>

<properties>
<tomcatVersion>8.5.16</tomcatVersion>
<awsVersion>1.11.259</awsVersion>
</properties>

<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.10.63</version>
<version>${awsVersion}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>8.0.1</version>
<version>${tomcatVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -61,7 +66,7 @@
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-test-utils</artifactId>
<version>1.10.63</version>
<version>${awsVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -73,13 +78,13 @@
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.0.1</version>
<version>${tomcatVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.0.1</version>
<version>${tomcatVersion}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@

import com.amazonaws.AmazonClientException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.internal.StaticCredentialsProvider;
import com.amazonaws.regions.RegionUtils;
import com.amazonaws.auth.*;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.dynamodb.sessionmanager.converters.SessionConverter;
import com.amazonaws.services.dynamodb.sessionmanager.util.DynamoUtils;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.util.Tables;
import com.amazonaws.util.StringUtils;

import org.apache.catalina.LifecycleException;
Expand All @@ -42,26 +39,30 @@
*/
public class DynamoDBSessionManager extends PersistentManagerBase {

private static final Log LOGGER = LogFactory.getLog(DynamoDBSessionManager.class);

public static final String DEFAULT_TABLE_NAME = "Tomcat_SessionState";

private static final String USER_AGENT = "DynamoSessionManager/2.0.1";
private static final String name = "AmazonDynamoDBSessionManager";
private static final String info = name + "/2.0.1";
private static final String AWS_REGION = "us-east-1";
private static final String USER_AGENT = "DynamoSessionManager/2.0.5";
private static final String NAME = "AmazonDynamoDBSessionManager";
private static final String INFO = NAME + "/2.0.5";

private String regionId = "us-east-1";
// aws dynamo behaviors
private String tableName = DEFAULT_TABLE_NAME;
private String regionId = AWS_REGION;
private String endpoint;
private long readCapacityUnits = 10;
private long writeCapacityUnits = 5;
private boolean deleteCorruptSessions = false;
// aws credentials
private File credentialsFile;
private String accessKey;
private String secretKey;
private long readCapacityUnits = 10;
private long writeCapacityUnits = 5;
private boolean createIfNotExist = true;
private String tableName = DEFAULT_TABLE_NAME;
// local network config
private String proxyHost;
private Integer proxyPort;
private boolean deleteCorruptSessions = false;

private static final Log logger = LogFactory.getLog(DynamoDBSessionManager.class);

public DynamoDBSessionManager() {
setSaveOnRestart(true);
Expand All @@ -71,12 +72,12 @@ public DynamoDBSessionManager() {
}

public String getInfo() {
return info;
return INFO;
}

@Override
public String getName() {
return name;
return NAME;
}

public void setRegionId(String regionId) {
Expand Down Expand Up @@ -111,10 +112,6 @@ public void setWriteCapacityUnits(int writeCapacityUnits) {
this.writeCapacityUnits = writeCapacityUnits;
}

public void setCreateIfNotExist(boolean createIfNotExist) {
this.createIfNotExist = createIfNotExist;
}

public void setProxyHost(String proxyHost) {
this.proxyHost = proxyHost;
}
Expand All @@ -129,24 +126,24 @@ public void setDeleteCorruptSessions(boolean deleteCorruptSessions) {

@Override
protected void initInternal() throws LifecycleException {
AmazonDynamoDBClient dynamoClient = createDynamoClient();
AmazonDynamoDB dynamoClient = createDynamoClient();
initDynamoTable(dynamoClient);
DynamoSessionStorage sessionStorage = createSessionStorage(dynamoClient);
setStore(new DynamoDBSessionStore(sessionStorage, deleteCorruptSessions));
new ExpiredSessionReaperExecutor(new ExpiredSessionReaper(sessionStorage));
}

private AmazonDynamoDBClient createDynamoClient() {
AWSCredentialsProvider credentialsProvider = initCredentials();
ClientConfiguration clientConfiguration = initClientConfiguration();
AmazonDynamoDBClient dynamoClient = new AmazonDynamoDBClient(credentialsProvider, clientConfiguration);
if (this.regionId != null) {
dynamoClient.setRegion(RegionUtils.getRegion(this.regionId));
}
private AmazonDynamoDB createDynamoClient() {
final String region = regionId != null ? regionId : AWS_REGION;
final AmazonDynamoDBClientBuilder dynamoClient = AmazonDynamoDBClient
.builder()
.withCredentials(initCredentials())
.withClientConfiguration(initClientConfiguration())
.withRegion(region);
if (this.endpoint != null) {
dynamoClient.setEndpoint(this.endpoint);
dynamoClient.setEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region));
}
return dynamoClient;
return dynamoClient.build();
}

private AWSCredentialsProvider initCredentials() {
Expand All @@ -156,17 +153,17 @@ private AWSCredentialsProvider initCredentials() {
if (credentialsInContextConfigAreValid()) {
throw new AmazonClientException("Incomplete AWS security credentials specified in context.xml.");
}
logger.debug("Using AWS access key ID and secret key from context.xml");
return new StaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey));
LOGGER.debug("Using AWS access key ID and secret key from context.xml");
return new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey));
}

// Use any explicitly specified credentials properties file next
if (credentialsFile != null) {
try {
logger.debug("Reading security credentials from properties file: " + credentialsFile);
LOGGER.debug("Reading security credentials from properties file: " + credentialsFile);
PropertiesCredentials credentials = new PropertiesCredentials(credentialsFile);
logger.debug("Using AWS credentials from file: " + credentialsFile);
return new StaticCredentialsProvider(credentials);
LOGGER.debug("Using AWS credentials from file: " + credentialsFile);
return new AWSStaticCredentialsProvider(credentials);
} catch (Exception e) {
throw new AmazonClientException(
"Unable to read AWS security credentials from file specified in context.xml: "
Expand All @@ -178,12 +175,12 @@ private AWSCredentialsProvider initCredentials() {
// Fall back to the default credentials chain provider if credentials weren't explicitly set
AWSCredentialsProvider defaultChainProvider = new DefaultAWSCredentialsProviderChain();
if (defaultChainProvider.getCredentials() == null) {
logger.debug("Loading security credentials from default credentials provider chain.");
LOGGER.debug("Loading security credentials from default credentials provider chain.");
throw new AmazonClientException("Unable to find AWS security credentials. "
+ "Searched JVM system properties, OS env vars, and EC2 instance roles. "
+ "Specify credentials in Tomcat's context.xml file or put them in one of the places mentioned above.");
}
logger.debug("Using default AWS credentials provider chain to load credentials");
LOGGER.debug("Using default AWS credentials provider chain to load credentials");
return defaultChainProvider;
}

Expand All @@ -205,38 +202,27 @@ private boolean credentialsInContextConfigAreValid() {

private ClientConfiguration initClientConfiguration() {
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setUserAgent(USER_AGENT);
clientConfiguration.setUserAgentPrefix(USER_AGENT);

// Attempt to use an explicit proxy configuration
if (proxyHost != null || proxyPort != null) {
logger.debug("Reading proxy settings from context.xml");
LOGGER.debug("Reading proxy settings from context.xml");
if (proxyHost == null || proxyPort == null) {
throw new AmazonClientException("Incomplete proxy settings specified in context.xml."
+ " Both proxy hot and proxy port needs to be specified");
}
logger.debug("Using proxy host and port from context.xml");
LOGGER.debug("Using proxy host and port from context.xml");
clientConfiguration.withProxyHost(proxyHost).withProxyPort(proxyPort);
}

return clientConfiguration;
}

private void initDynamoTable(AmazonDynamoDBClient dynamo) {
boolean tableExists = Tables.doesTableExist(dynamo, this.tableName);

if (!tableExists && !createIfNotExist) {
throw new AmazonClientException("Session table '" + tableName + "' does not exist, "
+ "and automatic table creation has been disabled in context.xml");
}

if (!tableExists) {
DynamoUtils.createSessionTable(dynamo, this.tableName, this.readCapacityUnits, this.writeCapacityUnits);
}

Tables.waitForTableToBecomeActive(dynamo, this.tableName);
private void initDynamoTable(AmazonDynamoDB dynamo) {
DynamoUtils.createSessionTable(dynamo, this.tableName, this.readCapacityUnits, this.writeCapacityUnits);
}

private DynamoSessionStorage createSessionStorage(AmazonDynamoDBClient dynamoClient) {
private DynamoSessionStorage createSessionStorage(AmazonDynamoDB dynamoClient) {
DynamoDBMapper dynamoMapper = DynamoUtils.createDynamoMapper(dynamoClient, tableName);
return new DynamoSessionStorage(dynamoMapper, getSessionConverter());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class DynamoDBSessionStore extends StoreBase {

private static final Log logger = LogFactory.getLog(DynamoDBSessionStore.class);
private static final String name = "AmazonDynamoDBSessionStore";
private static final String info = name + "/1.0";
private static final String info = name + "/2.0";

private final Set<String> sessionIds = Collections.synchronizedSet(new HashSet<String>());
private final DynamoSessionStorage sessionStorage;
Expand All @@ -58,8 +58,8 @@ public String getStoreName() {
@Override
public void clear() throws IOException {
synchronized (sessionIds) {
final Set<String> sessionsToDelete = new HashSet<String>(sessionIds);
new Thread("dynamodb-session-manager-clear") {
final Set<String> sessionsToDelete = new HashSet<>(sessionIds);
new Thread("dynamo-db-session-manager-clear") {
@Override
public void run() {
for (String sessionId : sessionsToDelete) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,25 @@ private <T> Iterator<T> getIteratorSafe(Iterable<T> iterable) {
*/
private class SessionConverterIterator implements Iterator<Session> {

private final Iterator<DynamoSessionItem> sessionItemterator;
private final Iterator<DynamoSessionItem> sessionItemIterator;

private SessionConverterIterator(Iterator<DynamoSessionItem> sessionItemIterator) {
this.sessionItemterator = sessionItemIterator;
this.sessionItemIterator = sessionItemIterator;
}

@Override
public boolean hasNext() {
return sessionItemterator.hasNext();
return sessionItemIterator.hasNext();
}

@Override
public Session next() {
return sessionConverter.toSession(sessionItemterator.next());
return sessionConverter.toSession(sessionItemIterator.next());
}

@Override
public void remove() {
sessionItemterator.remove();
sessionItemIterator.remove();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ExpiredSessionReaperExecutor {

private static final int REAP_FREQUENCY_HOURS = 12;
private static final int MAX_JITTER_HOURS = 5;
private static final String THREAD_NAME = "dynamo-session-manager-expired-sesion-reaper";
private static final String THREAD_NAME = "dynamo-db-session-manager-expired-sesion-reaper";

private final ScheduledThreadPoolExecutor executor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
*/
package com.amazonaws.services.dynamodb.sessionmanager.util;

import com.amazonaws.AmazonClientException;
import com.amazonaws.services.dynamodb.sessionmanager.DynamoSessionItem;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig.TableNameOverride;
Expand All @@ -25,10 +26,11 @@
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
import com.amazonaws.services.dynamodbv2.util.TableUtils;

public class DynamoUtils {

public static void createSessionTable(AmazonDynamoDBClient dynamo,
public static void createSessionTable(AmazonDynamoDB dynamo,
String tableName,
long readCapacityUnits,
long writeCapacityUnits) {
Expand All @@ -44,14 +46,25 @@ public static void createSessionTable(AmazonDynamoDBClient dynamo,
request.setProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(readCapacityUnits)
.withWriteCapacityUnits(writeCapacityUnits));

dynamo.createTable(request);
boolean created = TableUtils.createTableIfNotExists(dynamo, request);

if(created) {
try {
TableUtils.waitUntilActive(dynamo, tableName);
} catch (InterruptedException e) {
throw new AmazonClientException(e.getMessage(), e);
}
}
}

/**
* Create a new DynamoDBMapper with table name override
*/
public static DynamoDBMapper createDynamoMapper(AmazonDynamoDBClient dynamoDbClient, String tableName) {
return new DynamoDBMapper(dynamoDbClient, new DynamoDBMapperConfig(new TableNameOverride(tableName)));
public static DynamoDBMapper createDynamoMapper(AmazonDynamoDB dynamoDbClient, String tableName) {
final DynamoDBMapperConfig.Builder builder = DynamoDBMapperConfig
.builder()
.withTableNameOverride(new TableNameOverride(tableName));
return new DynamoDBMapper(dynamoDbClient, builder.build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void assertSessionDataEquals(HttpSession expectedSession, HttpSess
}

private static <T> SortedSet<T> toSortedSet(Enumeration<T> enumeration) {
SortedSet<T> list = new TreeSet<T>();
SortedSet<T> list = new TreeSet<>();
while (enumeration.hasMoreElements()) {
list.add(enumeration.nextElement());
}
Expand Down
Loading