aslcore/src/org/gcube/application/framework/core/session/ASLSession.java

223 lines
5.3 KiB
Java

package org.gcube.application.framework.core.session;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import org.gcube.application.framework.core.cache.CachesManager;
import org.gcube.application.framework.core.security.PortalSecurityManager;
import org.gcube.application.framework.core.util.SessionConstants;
import org.gcube.application.framework.core.util.UserCredential;
import org.gcube.common.core.scope.GCUBEScope;
import org.gridforum.jgss.ExtendedGSSCredential;
/**
* @author Valia Tsagkalidou (NKUA)
*
*/
public class ASLSession{
/**
*
*/
private static final long serialVersionUID = 1L;
private HashMap<String, Object> innerSession;
private long lastUsedTime;
private String externalSessionID;
private String username;
private ExtendedGSSCredential credential;
private GCUBEScope scope;
private HashMap<String, Notifier> notifiers;
/**
* A constructor based on the user and an external ID
* @param externalSessionId the external id
* @param user the username
*/
ASLSession(String externalSessionId, String user)
{
innerSession = new HashMap<String, Object>();
notifiers = new HashMap<String, Notifier>();
lastUsedTime = System.currentTimeMillis();
username = user;
externalSessionID = externalSessionId;
}
private void initializeAttributes() {
}
/**
* @return whether the session is still valid or not
*/
public boolean isValid()
{
if((System.currentTimeMillis() - lastUsedTime) > 1800000) // 30 minutes
return false;
return true;
}
/**
* @return whether the session is empty or not
*/
public boolean isEmpty()
{
lastUsedTime = System.currentTimeMillis();
return innerSession.isEmpty();
}
/**
* @param name the name of the attribute
* @return whether the name attribute exists in the session
*/
public boolean hasAttribute(String name)
{
lastUsedTime = System.currentTimeMillis();
return innerSession.containsKey(name);
}
/**
* @return a set of all the attributes in the session
*/
public Set<String> getAttributeNames()
{
lastUsedTime = System.currentTimeMillis();
return innerSession.keySet();
}
/**
* @param name the name of the attribute
* @return the value of the named attribute
*/
public Object getAttribute(String name)
{
lastUsedTime = System.currentTimeMillis();
return innerSession.get(name);
}
/**
* @param name the name of the attribute
* @param value the value of the attribute
*/
public void setAttribute(String name, Object value)
{
lastUsedTime = System.currentTimeMillis();
innerSession.put(name, value);
}
/**
* Removes the named attribute from the session
* @param name the name of the attribute
* @return the removed object
*/
public Object removeAttribute(String name)
{
lastUsedTime = System.currentTimeMillis();
return innerSession.remove(name);
}
/**
* Removes all the attributes from the session
*/
public void removeAll()
{
lastUsedTime = System.currentTimeMillis();
innerSession.clear();
}
/**
* invalidates the session
*/
public void invalidate()
{
lastUsedTime = System.currentTimeMillis() - 2000000; //more than 30 minutes
}
/**
* @return the credential
*/
public ExtendedGSSCredential getCredential() {
return credential;
}
/**
* @return the external session id (passed to the constructor)
*/
public String getExternalSessionID() {
return externalSessionID;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @return the scope
*/
public GCUBEScope getScope() {
return scope;
}
/**
* @return the name of the scope (VRE)
*/
public String getScopeName()
{
return scope.toString();
}
/**
* @param scope the scope name (VRE)
*/
public void setScope(String scope) {
lastUsedTime = System.currentTimeMillis();
String[] split = scope.trim().substring(1).split("/",2);
String vo = "/" + split[0].toLowerCase();
if(split.length > 1)
vo += "/" + split[1];
System.out.println("*** VRE to be set:" + vo + " ***");
this.scope = GCUBEScope.getScope(vo);
if(new PortalSecurityManager(this.scope).isSecurityEnabled())
this.credential = UserCredential.getCredential(username, scope);
innerSession.clear();
initializeAttributes();
}
/**
* @param notification the name of the notification to wait for
* @throws InterruptedException when the thread is interrupted
*/
public void waitNotification(String notification) throws InterruptedException
{
Notifier notifier = notifiers.get(notification);
if(notifier == null)
{
notifier = new Notifier();
notifiers.put(notification, notifier);
}
lastUsedTime = System.currentTimeMillis();
notifier.waitNotification();
}
/**
* @param notification the name of the notification to send notification
* @throws InterruptedException when the thread is interrupted
*/
public void notifyAllWaiting(String notification) throws InterruptedException
{
Notifier notifier = notifiers.get(notification);
if(notifier == null)
{
notifier = new Notifier();
notifiers.put(notification, notifier);
}
lastUsedTime = System.currentTimeMillis();
notifier.notifyAllWaiting();
}
}