This is the modified "DistributableSessionRegistryImpl" class based on JGroup ReplicatedHashtable.
download the source code from here
public class DistributableSessionRegistryImpl
implements SessionRegistry,
ApplicationListener {
private Map sessionIds = new HashMap();
String channelName = "Acegi Cluster";
private String clusterOptions = null;
private static final int TIME_OUT = 0;
private boolean distributable=false;
public void setDistributable(boolean distributable) {
this.distributable = distributable;
}
public void setClusterOptions(String clusterOptions) {
this.clusterOptions = clusterOptions;
}
public void setChannelName(String channelName) {
this.channelName = channelName;
}
public Object[] getAllPrincipals() {
Collection collection = sessionIds.values();
Set principals = new HashSet();
for (Iterator iterator = collection.iterator();
iterator.hasNext();) {
DistributableSessionInformation sessionInformation = (DistributableSessionInformation) iterator.next();
principals.add(sessionInformation.getPrincipal());
}
return principals.toArray(new String[principals.size()]);
}
public SessionInformation[] getAllSessions(Object principal,
boolean includeExpiredSessions) {
Set sessionsUsedByPrincipal = getSessionIds(principal);
List list = new ArrayList();
Iterator iter = sessionsUsedByPrincipal.iterator();
while (iter.hasNext()) {
synchronized (sessionsUsedByPrincipal) {
String sessionId = (String) iter.next();
SessionInformation sessionInformation = getSessionInformation(sessionId);
if (includeExpiredSessions || !sessionInformation.isExpired()) {
list.add(sessionInformation);
}
}
}
return (SessionInformation[]) list.toArray(new SessionInformation[]{});
}
public SessionInformation getSessionInformation(String sessionId) {
Assert.hasText(sessionId, "SessionId required as per interface contract");
DistributableSessionInformation sessionInformation = ((DistributableSessionInformation) sessionIds.get(sessionId));
if(sessionInformation==null)return null;
return sessionInformation.sessionInformation(this);
}
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof HttpSessionDestroyedEvent) {
String sessionId = ((HttpSession) event.getSource()).getId();
removeSessionInformation(sessionId);
}
}
public void refreshLastRequest(String sessionId) {
Assert.hasText(sessionId, "SessionId required as per interface contract");
SessionInformation info = getSessionInformation(sessionId);
if (info != null) {
sessionIds.put(sessionId, new DistributableSessionInformation(info,new Date()) );
}
}
public void expireSession(String sessionId) {
Assert.hasText(sessionId, "SessionId required as per interface contract");
SessionInformation info = getSessionInformation(sessionId);
if (info != null) {
sessionIds.put(sessionId, new DistributableSessionInformation(info,new Date(),true) );
}
}
public synchronized void registerNewSession(String sessionId, Object principal) {
Assert.hasText(sessionId, "SessionId required as per interface contract");
Assert.notNull(principal, "Principal required as per interface contract");
if (getSessionInformation(sessionId) != null) {
removeSessionInformation(sessionId);
}
sessionIds.put(sessionId,new DistributableSessionInformation(principal, sessionId, new Date()));
}
private Set getSessionIds(Object principal) {
Collection collection = sessionIds.values();
Set principals = new HashSet();
for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
DistributableSessionInformation sessionInformation = (DistributableSessionInformation) iterator.next();
if (sessionInformation.getPrincipal().equals(principal))
principals.add(sessionInformation.getSessionId());
}
return principals;
}
public void removeSessionInformation(String sessionId) {
Assert.hasText(sessionId, "SessionId required as per interface contract");
SessionInformation info = getSessionInformation(sessionId);
if (info != null) {
sessionIds.remove(sessionId);
}
}
public void init() throws ChannelException {
if(!distributable)return;
JChannel jChannel;
ReplicatedHashtable hashtable;
if (clusterOptions == null) {
jChannel = new JChannel();
} else {
jChannel = new JChannel(Thread.currentThread().getContextClassLoader().getResource(clusterOptions));
}
jChannel.connect(channelName);
hashtable=new ReplicatedHashtable(jChannel,TIME_OUT);
System.out.println(" Members are "+ jChannel.getView().getMembers());
sessionIds = hashtable;
}
public void destroy() throws ChannelException {
if (sessionIds instanceof ReplicatedHashtable) {
ReplicatedHashtable o = (ReplicatedHashtable) sessionIds;
o.getChannel().close();
}
}
}

