AWS Elastic Beanstalk を使う その12
StandardSessionクラスがSerializableインタフェースを持っているので、Base64エンコードして直列化した文字列をSimpleDBに書き込むような流れになっています。
オブジェクトの文字列化に使ったのはこれ。
ちゃんとテストしていませんがw。
http://www.source-code.biz/base64coder/java/
道半ば。。
package jp.cm.aws.tomcat.session; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.apache.catalina.Session; import org.apache.catalina.Store; import org.apache.catalina.session.StandardSession; import org.apache.catalina.session.StoreBase; import biz.source_code.base64Coder.Base64Coder; import com.amazonaws.auth.PropertiesCredentials; import com.amazonaws.services.simpledb.AmazonSimpleDB; import com.amazonaws.services.simpledb.AmazonSimpleDBClient; import com.amazonaws.services.simpledb.model.Attribute; import com.amazonaws.services.simpledb.model.DeleteAttributesRequest; import com.amazonaws.services.simpledb.model.GetAttributesRequest; import com.amazonaws.services.simpledb.model.GetAttributesResult; import com.amazonaws.services.simpledb.model.Item; import com.amazonaws.services.simpledb.model.PutAttributesRequest; import com.amazonaws.services.simpledb.model.ReplaceableAttribute; import com.amazonaws.services.simpledb.model.SelectRequest; public class SimpleDBStore extends StoreBase implements Store { private AmazonSimpleDB sdb = null; private String myDomain = "SessionPersistence"; private String mySession = "Session"; public SimpleDBStore() throws IOException { sdb = new AmazonSimpleDBClient(new PropertiesCredentials( SimpleDBStore.class .getResourceAsStream("AwsCredentials.properties"))); } @Override public int getSize() throws IOException { String selectExpression = "select * from `" + myDomain + "`"; SelectRequest selectRequest = new SelectRequest(selectExpression); int count = 0; for (Item item : sdb.select(selectRequest).getItems()) { System.out.println(" Item"); System.out.println(" Name: " + item.getName()); for (Attribute attribute : item.getAttributes()) { System.out.println(" Attribute"); System.out.println(" Name: " + attribute.getName()); System.out.println(" Value: " + attribute.getValue()); } count++; } return count; } @Override public String[] keys() throws IOException { String selectExpression = "select * from `" + myDomain + "`"; SelectRequest selectRequest = new SelectRequest(selectExpression); List<Item> items = sdb.select(selectRequest).getItems(); List<String> keyNames = new ArrayList<String>(); for (Item item : items) { keyNames.add(item.getName()); } return (String[]) keyNames.toArray(); } @Override public Session load(String id) throws ClassNotFoundException, IOException { System.out.println("load : " + id); GetAttributesResult result = sdb .getAttributes(new GetAttributesRequest().withDomainName( myDomain).withItemName(id)); List<Attribute> list = result.getAttributes(); StandardSession session = null; for (int i = 0; i < list.size(); i++) { Attribute attribute = list.get(i); if (attribute.getName().equals(mySession)) { String value = attribute.getValue(); session = (StandardSession) fromString(value); } } System.out.println(session.toString()); return session; } @Override public void remove(String id) throws IOException { System.out.println("remove : " + id); sdb.deleteAttributes(new DeleteAttributesRequest().withDomainName( myDomain).withItemName(id)); } @Override public void clear() throws IOException { System.out.println("clear"); } @Override public void save(Session session) throws IOException { System.out.println(session.toString()); System.out.println("save"); String id = session.getId(); sdb.putAttributes(new PutAttributesRequest() .withDomainName(myDomain) .withItemName(id) .withAttributes( new ReplaceableAttribute(mySession, toString((StandardSession) session), true))); System.out.println(toString((StandardSession) session)); } private Object fromString(String s) throws IOException, ClassNotFoundException { byte[] data = Base64Coder.decode(s); ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream( data)); Object o = ois.readObject(); ois.close(); return o; } private String toString(Serializable o) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(o); oos.close(); return new String(Base64Coder.encode(baos.toByteArray())); } }