80 lines
2.2 KiB
Java
80 lines
2.2 KiB
Java
package org.gcube.common.authorization.utils.manager;
|
|
|
|
import java.util.SortedSet;
|
|
|
|
import org.gcube.common.authorization.utils.secret.Secret;
|
|
import org.gcube.common.authorization.utils.user.User;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
*/
|
|
public class SecretManager {
|
|
|
|
public static final InheritableThreadLocal<SecretManager> instance = new InheritableThreadLocal<SecretManager>() {
|
|
|
|
@Override
|
|
protected SecretManager initialValue() {
|
|
return new SecretManager();
|
|
}
|
|
|
|
};
|
|
|
|
private SecretHolder initialSecretHolder;
|
|
private SecretHolder currentSecretHolder;
|
|
|
|
private SecretManager(){
|
|
initialSecretHolder = new SecretHolder();
|
|
currentSecretHolder = initialSecretHolder;
|
|
}
|
|
|
|
public synchronized void addSecret(Secret secret) throws Exception {
|
|
if(currentSecretHolder!=initialSecretHolder) {
|
|
throw new Exception("You can't add a Secret in a session. You must terminate the session first.");
|
|
}
|
|
initialSecretHolder.addSecret(secret);
|
|
}
|
|
|
|
public synchronized void startSession(Secret secret) throws Exception {
|
|
if(currentSecretHolder!=initialSecretHolder) {
|
|
throw new Exception("You are already in a session. You must terminate the session first.");
|
|
}
|
|
initialSecretHolder.reset();
|
|
currentSecretHolder = new SecretHolder(secret);
|
|
currentSecretHolder.set();
|
|
}
|
|
|
|
public synchronized void startSession(SortedSet<Secret> secrets) throws Exception {
|
|
if(currentSecretHolder!=initialSecretHolder) {
|
|
throw new Exception("You are already in a session. You must terminate the session first.");
|
|
}
|
|
initialSecretHolder.reset();
|
|
currentSecretHolder = new SecretHolder(secrets);
|
|
currentSecretHolder.set();
|
|
}
|
|
|
|
public synchronized void endSession() throws Exception {
|
|
if(currentSecretHolder!=initialSecretHolder) {
|
|
currentSecretHolder.reset();
|
|
initialSecretHolder.set();
|
|
currentSecretHolder = initialSecretHolder;
|
|
}
|
|
}
|
|
|
|
public synchronized void reset() {
|
|
initialSecretHolder.reset();
|
|
if(initialSecretHolder!=currentSecretHolder) {
|
|
currentSecretHolder.reset();
|
|
}
|
|
instance.remove();
|
|
}
|
|
|
|
public synchronized String getContext() {
|
|
return currentSecretHolder.getContext();
|
|
}
|
|
|
|
public synchronized User getUser() {
|
|
return currentSecretHolder.getUser();
|
|
}
|
|
|
|
}
|