removed ScopeProvider

This commit is contained in:
Lucio Lelii 2022-05-30 18:30:17 +02:00
parent 9f90b83724
commit e076dc39de
9 changed files with 82 additions and 118 deletions

View File

@ -69,10 +69,14 @@
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>common-security</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -32,16 +32,19 @@ public abstract class ApplicationManagerProvider {
protected static synchronized void init(ApplicationContext appcontext) {
if (!initialized) {
if (appcontext.container().configuration().mode()== Mode.offline)
if (appcontext.container().configuration().mode()== Mode.offline)
instance = new OfflineProvider();
else
instance = new OnlineProvider();
initialized =true;
}
}
public static synchronized ApplicationManager get(final Class<? extends ApplicationManager> applicationManagerClass){
if (!initialized) throw new RuntimeException("ApplicationManagerProvider not yet initialized");
logger.debug("retrieveing application manager of {} ",applicationManagerClass.getCanonicalName());
Object obj = instance.getApplicationManagerObject(applicationManagerClass);
return applicationManagerClass.cast(obj);
}
@ -59,13 +62,14 @@ public abstract class ApplicationManagerProvider {
obj = _class.newInstance();
classProxyObjetMap.put(_class.getCanonicalName(), obj);
}
logger.debug("proxy created for {} ",applicationManagerClass.getCanonicalName());
} catch (Exception e) {
throw new RuntimeException("error creating proxy ", e);
}
((ProxyObject)obj).setHandler(getMethdoHandler(applicationManagerClass));
((ProxyObject)obj).setHandler(this.getMethodHandler(applicationManagerClass));
return obj;
}
@ -108,7 +112,7 @@ public abstract class ApplicationManagerProvider {
}
protected abstract Future<ApplicationManager> retrieveFuture(Class<? extends ApplicationManager> applicationManagerClass);
protected abstract MethodHandler getMethdoHandler(Class<? extends ApplicationManager> applicationManagerClass);
protected abstract MethodHandler getMethodHandler(Class<? extends ApplicationManager> applicationManagerClass);
protected abstract AppManagerObserver getObserver();
}

View File

@ -64,10 +64,9 @@ public class ContextListener implements ServletContextListener {
}
if (managers.size()>0){
observer = ApplicationManagerProvider.instance.getObserver();
observer.setStartingTokens(context.configuration().authorizedContexts());
observer.setAuthorizationProvider(context.container().configuration().authorizationProvider());
observer.setApplicationManagerClasses(managers);
observer.register();
context.events().subscribe(observer);
}
}

View File

@ -1,10 +1,10 @@
package org.gcube.smartgears.application.manager;
import java.util.Collection;
import java.util.Set;
import org.gcube.smartgears.ApplicationManager;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.smartgears.security.AuthorizationProvider;
public interface AppManagerObserver {
@ -16,7 +16,7 @@ public interface AppManagerObserver {
void unregister();
void setStartingTokens(Collection<String> startingTokens);
public void setAuthorizationProvider(AuthorizationProvider authProvider);
void setApplicationManagerClasses(Set<Class<? extends ApplicationManager>> managersClasses);

View File

@ -1,6 +1,5 @@
package org.gcube.smartgears.application.manager;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
@ -13,6 +12,7 @@ import org.gcube.common.events.Observes.Kind;
import org.gcube.smartgears.ApplicationManager;
import org.gcube.smartgears.Constants;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.smartgears.security.AuthorizationProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -109,13 +109,15 @@ public class OfflineObserver implements AppManagerObserver {
}
}
@Override
public void setStartingTokens(Collection<String> startingTokens) {
}
@Override
public void setApplicationManagerClasses(Set<Class<? extends ApplicationManager>> managerClasses) {
this.managerClasses = managerClasses;
}
@Override
public void setAuthorizationProvider(AuthorizationProvider authProvider) {
}
}

View File

@ -27,7 +27,7 @@ public class OfflineProvider extends ApplicationManagerProvider {
}
@Override
protected MethodHandler getMethdoHandler(Class<? extends ApplicationManager> applicationManagerClass) {
protected MethodHandler getMethodHandler(Class<? extends ApplicationManager> applicationManagerClass) {
MethodHandler handler = new MethodHandler() {
@Override
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {

View File

@ -1,9 +1,6 @@
package org.gcube.smartgears.application.manager;
import static org.gcube.common.authorization.client.Constants.authorizationService;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -13,14 +10,14 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.events.Observes;
import org.gcube.common.events.Observes.Kind;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.security.secrets.Secret;
import org.gcube.smartgears.ApplicationManager;
import org.gcube.smartgears.Constants;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.smartgears.security.AuthorizationProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -33,32 +30,36 @@ public class OnlineObserver implements AppManagerObserver{
private Map<String, List<Future<ApplicationManager>>> instanciatedManagerPerScope = new HashMap<String, List<Future<ApplicationManager>>>();
private OnlineProvider provider;
private Collection<String> startingTokens;
private AuthorizationProvider authProvider;
private Set<Class<? extends ApplicationManager>> managersClasses;
public OnlineObserver(OnlineProvider provider) {
this.provider = provider;
}
public void register() {
for (String startingToken : startingTokens )
this.onRegistration(startingToken);
for (String context : authProvider.getContexts()) {
this.onRegistration(context);
}
}
@Observes(value=Constants.token_registered, kind=Kind.safe)
public synchronized void onRegistration(final String securityToken){
log.info("token registered called with token {}", securityToken);
public synchronized void onRegistration(String context){
log.info("registration called in context {}", context);
List<Future<ApplicationManager>> futureList = new ArrayList<Future<ApplicationManager>>();
try {
final String context = authorizationService().get(securityToken).getContext();
Secret secret = authProvider.getSecretForContext(context);
for (Class<? extends ApplicationManager> appManager: managersClasses){
Future<ApplicationManager> appManagerFuture = service.submit(new InitAppManager(securityToken, context, appManager));
log.info("intializing app in context {} with token {} ",context, securityToken);
Future<ApplicationManager> appManagerFuture = service.submit(new InitAppManager(secret, appManager));
log.info("intializing app using manager {} in context {}",appManager.getClass().getCanonicalName(),context);
futureList.add(appManagerFuture);
if (provider.getAppmanagerMap().containsKey(appManager.getCanonicalName()))
@ -71,31 +72,24 @@ public class OnlineObserver implements AppManagerObserver{
}
if (!futureList.isEmpty())
instanciatedManagerPerScope.put(context, futureList);
} catch (ObjectNotFound e1) {
log.error("it should never happen (token has just been created)",e1);
throw new RuntimeException("it should never happen (token has just been created",e1);
} catch (Exception e1) {
log.error("something failed getting token",e1);
throw new RuntimeException("something failed getting token",e1);
log.error("something failed getting authorization",e1);
throw new RuntimeException("something failed getting authorization",e1);
}
}
@Observes(value=Constants.token_removed, kind=Kind.critical)
public synchronized void onRemove(final String securityToken){
public synchronized void onRemove(String context){
try {
final String context = authorizationService().get(securityToken).getContext();
Secret secret = authProvider.getSecretForContext(context);
for (Future<ApplicationManager> appManager: instanciatedManagerPerScope.get(context)){
service.execute(new ShutDownAppManager(securityToken, context, appManager));
service.execute(new ShutDownAppManager(secret, appManager));
provider.getAppmanagerMap().get(appManager).remove(context);
}
instanciatedManagerPerScope.remove(context);
SecurityTokenProvider.instance.reset();
} catch (ObjectNotFound e1) {
log.error("it should never happen (token has just been created)",e1);
throw new RuntimeException("it should never happen (token has just been created",e1);
} catch (Exception e1) {
log.error("something failed getting token",e1);
throw new RuntimeException("something failed getting token",e1);
@ -104,35 +98,9 @@ public class OnlineObserver implements AppManagerObserver{
public synchronized void onStop(ApplicationContext appContext){
for (String token :appContext.configuration().authorizedContexts()){
try {
String context = authorizationService().get(token).getContext();
for (Future<ApplicationManager> appManagerEntry: instanciatedManagerPerScope.get(context)){
try{
log.info("stoppping {} in context {} ",appContext.name(), context);
for (String context :appContext.configuration().authorizedContexts())
this.onRemove(context);
SecurityTokenProvider.instance.set(token);
ScopeProvider.instance.set(context);
try {
appManagerEntry.get().onShutdown();
log.info("manager {} correctly suhtdown on context {}",appContext.name(), context);
} catch (Exception e){
log.warn("problem calling onShutdown for context {}", context, e);
}
}catch(Exception e){
log.error("error retrieving token on shutdown on context {}", context,e);
throw new RuntimeException("error retrieving token on shutdown",e);
}finally{
ScopeProvider.instance.reset();
SecurityTokenProvider.instance.reset();
}
}
} catch (ObjectNotFound e1) {
log.error("token not found : {}",token,e1);
} catch (Exception e1) {
log.error("something failed getting token {}",token,e1);
}
}
unregister();
}
@ -143,28 +111,27 @@ public class OnlineObserver implements AppManagerObserver{
public class InitAppManager implements Callable<ApplicationManager>{
private Class<? extends ApplicationManager> managerClass;
private String securityToken;
private String context;
private Secret secret;
public InitAppManager(String securityToken, String context, Class<? extends ApplicationManager> managerClass){
public InitAppManager(Secret secret, Class<? extends ApplicationManager> managerClass){
this.managerClass = managerClass;
this.securityToken = securityToken;
this.context = context;
this.secret = secret;
}
@Override
public ApplicationManager call() throws Exception {
SecurityTokenProvider.instance.set(securityToken);
ScopeProvider.instance.set(context);
ApplicationManager manager = managerClass.newInstance();
log.info("on init called");
SecretManagerProvider.instance.set(secret);
ApplicationManager manager = null;
try {
log.info("calling on onInit of {} on token {}",manager.getClass().getCanonicalName(), securityToken);
manager = managerClass.newInstance();
log.info("calling on onInit on manager {} with secret {}",manager.getClass().getCanonicalName(),secret);
manager.onInit();
} catch (Exception e) {
log.warn("error on onInit of {} on token {}",manager.getClass().getCanonicalName(), securityToken, e);
log.warn("error on onInit of {} on context {}",manager.getClass().getCanonicalName(), secret.getContext(), e);
} finally{
ScopeProvider.instance.reset();
SecurityTokenProvider.instance.reset();
SecretManagerProvider.instance.reset();
}
return manager;
}
@ -173,35 +140,31 @@ public class OnlineObserver implements AppManagerObserver{
public class ShutDownAppManager implements Runnable{
private Future<ApplicationManager> appManager;
private String securityToken;
private String context;
private Secret secret;
public ShutDownAppManager(String securityToken, String context, Future<ApplicationManager> appManager){
public ShutDownAppManager(Secret secret, Future<ApplicationManager> appManager){
this.appManager = appManager;
this.securityToken = securityToken;
this.context = context;
}
@Override
public void run() {
SecurityTokenProvider.instance.set(securityToken);
ScopeProvider.instance.set(context);
SecretManagerProvider.instance.set(secret);
try {
log.info("calling on ShutDown of {} on token {}",appManager.getClass().getCanonicalName(), securityToken);
log.info("calling on ShutDown of {} on context {}",appManager.getClass().getCanonicalName(), secret.getContext());
appManager.get().onShutdown();
} catch (Exception e) {
log.warn("error on onShutdown of {} on token {}",appManager.getClass().getCanonicalName(), securityToken, e);
log.warn("error on onShutdown of {} on context {}",appManager.getClass().getCanonicalName(), secret.getContext(), e);
} finally{
ScopeProvider.instance.reset();
SecurityTokenProvider.instance.reset();
SecretManagerProvider.instance.reset();
}
}
}
@Override
public void setStartingTokens(Collection<String> startingTokens) {
this.startingTokens = startingTokens;
public void setAuthorizationProvider(AuthorizationProvider authProvider) {
this.authProvider = authProvider;
}
@Override
@ -209,5 +172,5 @@ public class OnlineObserver implements AppManagerObserver{
this.managersClasses = managersClasses;
}
}

View File

@ -5,7 +5,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.security.secrets.Secret;
import org.gcube.smartgears.ApplicationManager;
import org.gcube.smartgears.ApplicationManagerProvider;
import org.slf4j.Logger;
@ -25,15 +26,18 @@ public class OnlineProvider extends ApplicationManagerProvider {
@Override
protected Future<ApplicationManager> retrieveFuture(Class<? extends ApplicationManager> applicationManagerClass) {
return appManagerMap.get(applicationManagerClass.getCanonicalName()).get(ScopeProvider.instance.get());
Secret secret = SecretManagerProvider.instance.get();
String context = secret.getContext();
logger.debug("retrieving appManager {} in context {}",applicationManagerClass.getCanonicalName(), context);
return appManagerMap.get(applicationManagerClass.getCanonicalName()).get(context);
}
@Override
protected MethodHandler getMethdoHandler(Class<? extends ApplicationManager> applicationManagerClass) {
protected MethodHandler getMethodHandler(Class<? extends ApplicationManager> applicationManagerClass) {
MethodHandler handler = new MethodHandler() {
@Override
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {
if (ScopeProvider.instance.get()==null) throw new RuntimeException("error invoking application manager method, scope is not set in this thread");
if (SecretManagerProvider.instance.get()==null) throw new Exception("error invoking application manager method, secret is not set in this thread");
logger.debug("applicationManagerClass is {}",applicationManagerClass.getCanonicalName());
Future<ApplicationManager> appManagerFuture = retrieveFuture(applicationManagerClass);
logger.debug("appmanager future is null? {}", appManagerFuture==null);

View File

@ -1,12 +0,0 @@
package org.gcube.smartgears.stateful;
import org.junit.Test;
public class ResourceBinder {
@Test
public void bind() throws Exception{
}
}