Migrated to latest windows azure java version #1

Open
wants to merge 1 commit into
from
Binary file not shown.
Binary file not shown.
@@ -14,14 +14,15 @@
package uk.co.atomus.session;
-import org.soyatec.windows.azure.table.TableStorageEntity;
+import org.soyatec.windowsazure.table.AbstractTableServiceEntity;
+
/**
* Represents the tomcat session entity to be persisted to Azure Table Storage
*
* @author Simon Dingle and Chris Derham
*/
-public class TomcatSessionStorageEntity extends TableStorageEntity {
+public class TomcatSessionStorageEntity extends AbstractTableServiceEntity {
private byte[] data;
private String sessionId;
private boolean validSession;
@@ -23,8 +23,7 @@
import org.apache.catalina.session.StandardSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.soyatec.windows.azure.error.StorageException;
-import org.soyatec.windows.azure.error.StorageServerException;
+import org.soyatec.windowsazure.error.StorageServerException;
import uk.co.atomus.session.AtomusSession;
import uk.co.atomus.session.SessionStorageFacade;
@@ -191,9 +190,7 @@ private void deleteExpiredSessions() {
sessionService.deleteExpiredSessions();
} catch (StorageServerException e) {
log.warn("Error deleting expired sessions", e);
- } catch (StorageException e) {
- log.error("Error deleting expired sessions", e);
- }
+ }
}
@Override
@@ -20,8 +20,9 @@
import org.apache.catalina.Session;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.soyatec.windows.azure.error.StorageException;
-import org.soyatec.windows.azure.table.TableStorageEntity;
+import org.soyatec.windowsazure.error.StorageException;
+import org.soyatec.windowsazure.table.AbstractTableServiceEntity;
+import org.soyatec.windowsazure.table.ITableServiceEntity;
import uk.co.atomus.session.TomcatSessionStorageEntity;
import uk.co.atomus.session.service.dao.SessionDao;
@@ -84,7 +85,7 @@ protected String getPartitionKey() {
private TomcatSessionStorageEntity findInStorage(String id) {
try {
- TableStorageEntity storageEntity = sessionDao.retrieveEntity(partitionKey, id);
+ ITableServiceEntity storageEntity = sessionDao.retrieveEntity(partitionKey, id);
return storageEntity == null ? null : (TomcatSessionStorageEntity) storageEntity;
} catch (StorageException e) {
log.error("Error loading from storage", e);
@@ -171,7 +172,6 @@ public ObjectInputStream getSessionAsStream(long thisAccessedTime, String id, Cl
+ sessionStorageEntity.getLastAccessedTime());
return null;
}
- log.debug("found more recent session in storage timeStamp " + sessionStorageEntity.getTimestamp().getTime());
return sessionMapper.toObjectInputStream(sessionStorageEntity, classLoader);
}
@@ -16,8 +16,9 @@
import java.util.List;
-import org.soyatec.windows.azure.error.StorageException;
-import org.soyatec.windows.azure.table.TableStorageEntity;
+import org.soyatec.windowsazure.error.StorageException;
+import org.soyatec.windowsazure.table.AbstractTableServiceEntity;
+import org.soyatec.windowsazure.table.ITableServiceEntity;
import uk.co.atomus.session.TomcatSessionStorageEntity;
@@ -27,11 +28,11 @@
int countEntities(String partitionKey, String rowKey);
- TableStorageEntity retrieveEntity(String partitionKey, String rowKey);
+ ITableServiceEntity retrieveEntity(String partitionKey, String rowKey);
- List<TableStorageEntity> retrieveEntitiesByKey(String partitionKey, String rowKey);
+ List<ITableServiceEntity> retrieveEntitiesByKey(String partitionKey, String rowKey);
- List<TableStorageEntity> queryEntitiesByKeys(String partitionKey, String rowKey);
+ List<ITableServiceEntity> queryEntitiesByKeys(String partitionKey, String rowKey);
void insertStorageEntity(TomcatSessionStorageEntity storageEntity);
@@ -20,13 +20,15 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.soyatec.windows.azure.blob.RetryPolicies;
-import org.soyatec.windows.azure.blob.RetryPolicy;
-import org.soyatec.windows.azure.error.StorageException;
-import org.soyatec.windows.azure.table.AzureTable;
-import org.soyatec.windows.azure.table.TableStorage;
-import org.soyatec.windows.azure.table.TableStorageEntity;
-import org.soyatec.windows.azure.util.TimeSpan;
+import org.soyatec.windowsazure.blob.IRetryPolicy;
+import org.soyatec.windowsazure.blob.internal.RetryPolicies;
+import org.soyatec.windowsazure.error.StorageException;
+import org.soyatec.windowsazure.internal.util.TimeSpan;
+import org.soyatec.windowsazure.table.AbstractTableServiceEntity;
+import org.soyatec.windowsazure.table.ITableServiceEntity;
+import org.soyatec.windowsazure.table.TableServiceContext;
+import org.soyatec.windowsazure.table.TableStorageClient;
+import org.soyatec.windowsazure.table.internal.CloudTable;
import uk.co.atomus.session.TomcatSessionStorageEntity;
@@ -41,8 +43,8 @@
private static final int DEFAULT_RETRY_POLICY_INTERVAL_SECONDS = 1;
private String accountName;
private String accountKey;
- private AzureTable azureTable;
- private TableStorage tableStorage;
+ private CloudTable azureTable;
+ private TableStorageClient tableStorage;
private String tableName;
private int retryPolicyRetries = DEFAULT_RETRY_POLICY_RETRIES;
private int retryPolicyIntervalSeconds = DEFAULT_RETRY_POLICY_INTERVAL_SECONDS;
@@ -87,19 +89,19 @@ public void setTableName(String tableName) {
this.tableName = tableName;
}
- private AzureTable getAzureTable() {
+ private CloudTable getAzureTable() {
if (azureTable == null) {
- azureTable = getTableStorage().getAzureTable(tableName);
+ azureTable = (CloudTable) getTableStorage().getTableReference(tableName);
if (null == azureTable) {
throw new NullPointerException(String.format("TableStorage returned null AzureTable '%s'.", tableName));
}
azureTable.setRetryPolicy(getRetryPolicy());
- if (!azureTable.doesTableExist()) {
+ if (!azureTable.isTableExist()) {
azureTable.createTable();
- if (!azureTable.doesTableExist()) {
+ if (!azureTable.isTableExist()) {
throw new RuntimeException(String.format("Table '%s' was not created.", tableName));
}
}
@@ -108,108 +110,109 @@ private AzureTable getAzureTable() {
return azureTable;
}
- private TableStorage getTableStorage() {
+ private TableStorageClient getTableStorage() {
if (tableStorage == null) {
- tableStorage = TableStorage.create(URI.create(TABLE_NAMESPACE), false, accountName, accountKey);
+ tableStorage = TableStorageClient.create(URI.create(TABLE_NAMESPACE), false, accountName, accountKey);
}
return tableStorage;
}
- private RetryPolicy getRetryPolicy() {
+ private IRetryPolicy getRetryPolicy() {
return RetryPolicies.retryN(retryPolicyRetries, TimeSpan.fromSeconds(retryPolicyIntervalSeconds));
}
@Override
public void removeAll(String partitionKey) {
- List<TableStorageEntity> entities = queryEntitiesByKeys(partitionKey, null);
+ List<ITableServiceEntity> entities = queryEntitiesByKeys(partitionKey, null);
deleteBatch(entities);
}
@Override
public int countEntities(String partitionKey, String rowKey) {
- List<TableStorageEntity> entities = queryEntitiesByKeys(partitionKey, rowKey);
+ List<ITableServiceEntity> entities = queryEntitiesByKeys(partitionKey, rowKey);
return entities.size();
}
@Override
public void updateStorageEntity(TomcatSessionStorageEntity storageEntity) {
- AzureTable table = getAzureTable();
+ CloudTable table = getAzureTable();
table.updateEntity(storageEntity);
}
@Override
public void insertStorageEntity(TomcatSessionStorageEntity storageEntity) {
- AzureTable table = getAzureTable();
+ CloudTable table = getAzureTable();
table.insertEntity(storageEntity);
}
@Override
- public List<TableStorageEntity> queryEntitiesByKeys(String partitionKey, String rowKey) {
- List<TableStorageEntity> storageEntities = null;
- AzureTable table = getAzureTable();
+ public List<ITableServiceEntity> queryEntitiesByKeys(String partitionKey, String rowKey) {
+ List<ITableServiceEntity> storageEntities = null;
+ CloudTable table = getAzureTable();
if (null == partitionKey || partitionKey.isEmpty()) {
/* Return list of all Entities in Table */
- storageEntities = table.retrieveEntities();
+ storageEntities = table.retrieveEntities(TomcatSessionStorageEntity.class);
} else if (null != partitionKey && !partitionKey.isEmpty() && (null == rowKey || rowKey.isEmpty())) {
- storageEntities = table.retrieveEntitiesByKey(partitionKey, null);
+ storageEntities = table.retrieveEntitiesByKey(partitionKey, null, TomcatSessionStorageEntity.class);
} else if (null != partitionKey && !partitionKey.isEmpty() && null != rowKey && !rowKey.isEmpty()) {
- storageEntities = table.retrieveEntitiesByKey(partitionKey, rowKey);
+ storageEntities = table.retrieveEntitiesByKey(partitionKey, rowKey, TomcatSessionStorageEntity.class);
} else {
throw new StorageException(String.format("Unexpected condition: partitionKey '%s', rowKey '%s'",
partitionKey, rowKey));
}
if (null == storageEntities) {
- return new ArrayList<TableStorageEntity>();
+ return new ArrayList<ITableServiceEntity>();
}
return storageEntities;
}
@Override
- public TableStorageEntity retrieveEntity(String partitionKey, String rowKey) {
- List<TableStorageEntity> entities = retrieveEntitiesByKey(partitionKey, rowKey);
+ public ITableServiceEntity retrieveEntity(String partitionKey, String rowKey) {
+ List<ITableServiceEntity> entities = retrieveEntitiesByKey(partitionKey, rowKey);
return entities.isEmpty() ? null : entities.get(0);
}
@Override
- public List<TableStorageEntity> retrieveEntitiesByKey(String partitionKey, String rowKey) {
- AzureTable table = getAzureTable();
- return table.retrieveEntitiesByKey(partitionKey, rowKey);
+ public List<ITableServiceEntity> retrieveEntitiesByKey(String partitionKey, String rowKey) {
+ CloudTable table = getAzureTable();
+ return table.retrieveEntitiesByKey(partitionKey, rowKey, TomcatSessionStorageEntity.class);
}
@Override
public void remove(String partitionKey, String rowKey) throws StorageException {
- List<TableStorageEntity> entities = queryEntitiesByKeys(partitionKey, rowKey);
+ List<ITableServiceEntity> entities = queryEntitiesByKeys(partitionKey, rowKey);
if (entities.isEmpty()) {
log.debug("record not deleted as not found with rowKey " + rowKey);
return;
}
deleteBatch(entities);
}
- private void deleteBatch(List<TableStorageEntity> entities) {
+ private void deleteBatch(List<ITableServiceEntity> entities) {
if (entities.isEmpty()) {
return;
}
- AzureTable table = getAzureTable();
- table.startBatch();
- for (TableStorageEntity entity : entities) {
+ CloudTable table = getAzureTable();
+ TableServiceContext tableServiceContext = new TableServiceContext(table);
+ tableServiceContext.startBatch();
+ for (ITableServiceEntity entity : entities) {
deleteTableEntity(entity);
}
- table.executeBatch();
+ tableServiceContext.executeBatch();
}
- private void deleteTableEntity(TableStorageEntity entity) {
+ private void deleteTableEntity(ITableServiceEntity entity) {
log.debug("deleting record with rowKey" + entity.getRowKey());
getAzureTable().deleteEntity(entity);
}
@Override
public void removeExpired(String partitionKey) throws StorageException {
- List<TableStorageEntity> entities = queryEntitiesByKeys(partitionKey, null);
+ List<ITableServiceEntity> entities = queryEntitiesByKeys(partitionKey, null);
log.debug("found " + entities.size() + " records");
- for (TableStorageEntity entity : entities) {
+ for (ITableServiceEntity entity : entities) {
TomcatSessionStorageEntity sessionStorageEntity = (TomcatSessionStorageEntity) entity;
if (sessionStorageEntity.hasExpired()) {
log.debug("found expired record with rowKey" + entity.getRowKey());
@@ -20,7 +20,7 @@
import org.apache.catalina.core.StandardWrapper;
import org.apache.catalina.session.StandardSession;
-import org.soyatec.windows.azure.table.TableStorageEntity;
+import org.soyatec.windowsazure.table.ITableServiceEntity;
import uk.co.atomus.session.AtomusSession;
import uk.co.atomus.session.TomcatSessionStorageEntity;
@@ -54,15 +54,15 @@ public static void main(String[] args) throws Exception {
sessionDao.setAccountKey(accountKey);
sessionDao.setTableName(tableName);
- List<TableStorageEntity> entities = sessionDao.queryEntitiesByKeys(partitionKey, rowKey);
+ List<ITableServiceEntity> entities = sessionDao.queryEntitiesByKeys(partitionKey, rowKey);
System.out.println("Found " + entities.size() + " entities");
SessionMapper sessionMapper = new SessionMapperImpl();
SessionDeserializer sessionDeserializer = new SessionDeserializerImpl();
AtomusManager manager = new AtomusManager();
manager.setContainer(new StandardWrapper());
- for (TableStorageEntity tableStorageEntity : entities) {
+ for (ITableServiceEntity tableStorageEntity : entities) {
dumpTableStorageEntity(tableStorageEntity, sessionMapper, manager, sessionDeserializer, urlClassLoader);
}
}
@@ -90,7 +90,7 @@ public boolean accept(File dir, String name) {
return urlClassLoader;
}
- private static void dumpTableStorageEntity(TableStorageEntity tableStorageEntity, SessionMapper sessionMapper,
+ private static void dumpTableStorageEntity(ITableServiceEntity tableStorageEntity, SessionMapper sessionMapper,
AtomusManager manager, SessionDeserializer sessionDeserializer, ClassLoader urlClassLoader)
throws Exception {
try {
@@ -25,7 +25,7 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
-import org.soyatec.windows.azure.table.TableStorageEntity;
+import org.soyatec.windowsazure.table.ITableServiceEntity;
import uk.co.atomus.session.service.dao.SessionDaoImpl;
@@ -81,7 +81,7 @@ public void testUpdate() throws Exception {
@Test
public void testRetrieveEntitiesByKey() throws Exception {
- List<TableStorageEntity> entities = sessionDao.retrieveEntitiesByKey(partitionKey,
+ List<ITableServiceEntity> entities = sessionDao.retrieveEntitiesByKey(partitionKey,
sessionStorageEntity.getRowKey());
Assert.assertNotNull(entities);
assertEquals(entities.size(), 1);