Created
October 22, 2012 19:16
-
-
Save BowlingX/3933478 to your computer and use it in GitHub Desktop.
Scala Hazelcast Session Store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bowlingx.model | |
import net.liftweb.mongodb.record.{MongoMetaRecord, MongoRecord} | |
import net.liftweb.mongodb.record.field.StringPk | |
import com.hazelcast.core.MapStore | |
import java.util | |
import com.bowlingx.util.Logging | |
import net.liftweb.record.field.{StringField, DateTimeField, BinaryField} | |
import java.io._ | |
import util.Calendar | |
import net.liftweb.mongodb.BsonDSL._ | |
class Session private() extends MongoRecord[Session] with StringPk[Session] { | |
def meta = Session | |
/** | |
* Session Data | |
*/ | |
object data extends BinaryField[Session](this) | |
/** | |
* Last Object Access | |
*/ | |
object lastAccess extends DateTimeField[Session](this) | |
/** | |
* Principal | |
*/ | |
object principal extends StringField[Session](this, 255) | |
} | |
object Session extends Session with MongoMetaRecord[Session] with Logging { | |
ensureIndex(("lastAccess" -> 1)) | |
ensureIndex(("principal" -> 1)) | |
ensureIndex(("principal" -> 1) ~ ("lastAccess" -> 1)) | |
ensureIndex(("_id" -> 1) ~ ("lastAccess" -> 1)) | |
} | |
/** | |
* A MongoDB Backed Map Storage for Apache Shiro Sessions | |
* @param ev value is nullable | |
* @tparam K key | |
* @tparam V value | |
*/ | |
class HazelcastMongoDbSessionStore[K, V] extends MapStore[K, V] with Logging { | |
import scala.collection.JavaConversions._ | |
private def serializeObject(o: V) = { | |
val outputStream = new ByteArrayOutputStream() | |
val out = new ObjectOutputStream(outputStream) | |
out.writeObject(o) | |
out.close() | |
outputStream.toByteArray | |
} | |
private def binaryToObject(o: Array[Byte]) = { | |
val inputStream = new ByteArrayInputStream(o) | |
val in = new ObjectInputStream(inputStream) | |
val toObject = in.readObject().asInstanceOf[V] | |
in.close() | |
toObject | |
} | |
def delete(key: K) { | |
Session.delete(("_id" -> key.toString)) | |
} | |
def deleteAll(keys: util.Collection[K]) { | |
Session.delete(("$in" -> keys.toList.map(_.toString))) | |
} | |
def store(p1: K, p2: V) { | |
val record = Session.createRecord | |
record.id(p1.toString) | |
record.data(serializeObject(p2)) | |
record.lastAccess(Calendar.getInstance()) | |
record.save | |
} | |
/** | |
* Stores a List of Sessions | |
* @param p1 sessions to store | |
*/ | |
def storeAll(p1: util.Map[K, V]) { | |
p1.foreach(t => store(t._1, t._2)) | |
} | |
/** | |
* Loads a Specific Session | |
* @param key Session ID | |
* @return | |
*/ | |
def load(key: K): V = { | |
try { | |
return Session.find(key.toString).map { | |
d => | |
binaryToObject(d.data.get) | |
}.getOrElse(null.asInstanceOf[V]) | |
} catch { | |
case e: InvalidClassException => logger.warn("Session load error: %s " format e.getMessage, e) | |
} | |
null.asInstanceOf[V] | |
} | |
/** | |
* Loads a List of specified Sessions | |
* @param keys a List of Session IDs | |
* @return | |
*/ | |
def loadAll(keys: util.Collection[K]) = { | |
val map = new util.HashMap[K, V]() | |
Session.findAllByList(keys.toList).foreach(f => map.put( | |
f.id.get.asInstanceOf[K], binaryToObject(f.data.get))) | |
map | |
} | |
def loadAllKeys() = { | |
val keys = new util.HashSet[K]() | |
Session.findAll.foreach(s => keys.add(s.id.get.asInstanceOf[K])) | |
keys | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment