2022-02-07 09:43:30 +01:00
|
|
|
package org.gcube.smartgears.application.manager;
|
2016-09-30 18:36:48 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.Future;
|
|
|
|
|
|
|
|
import org.gcube.common.events.Observes;
|
|
|
|
import org.gcube.common.events.Observes.Kind;
|
2022-05-30 18:30:17 +02:00
|
|
|
import org.gcube.common.security.providers.SecretManagerProvider;
|
|
|
|
import org.gcube.common.security.secrets.Secret;
|
2022-02-07 09:43:30 +01:00
|
|
|
import org.gcube.smartgears.ApplicationManager;
|
|
|
|
import org.gcube.smartgears.Constants;
|
2016-09-30 18:36:48 +02:00
|
|
|
import org.gcube.smartgears.context.application.ApplicationContext;
|
2022-05-30 18:30:17 +02:00
|
|
|
import org.gcube.smartgears.security.AuthorizationProvider;
|
2016-09-30 18:36:48 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2022-02-07 09:43:30 +01:00
|
|
|
public class OnlineObserver implements AppManagerObserver{
|
2016-09-30 18:36:48 +02:00
|
|
|
|
2022-02-07 09:43:30 +01:00
|
|
|
private static Logger log = LoggerFactory.getLogger(OnlineObserver.class);
|
2016-09-30 18:36:48 +02:00
|
|
|
|
|
|
|
private static ExecutorService service = Executors.newCachedThreadPool();
|
|
|
|
|
|
|
|
private Map<String, List<Future<ApplicationManager>>> instanciatedManagerPerScope = new HashMap<String, List<Future<ApplicationManager>>>();
|
|
|
|
|
2022-02-07 09:43:30 +01:00
|
|
|
private OnlineProvider provider;
|
2022-05-30 18:30:17 +02:00
|
|
|
|
|
|
|
private AuthorizationProvider authProvider;
|
|
|
|
|
2022-02-07 09:43:30 +01:00
|
|
|
private Set<Class<? extends ApplicationManager>> managersClasses;
|
2022-05-30 18:30:17 +02:00
|
|
|
|
2022-02-07 09:43:30 +01:00
|
|
|
public OnlineObserver(OnlineProvider provider) {
|
|
|
|
this.provider = provider;
|
2022-05-30 18:30:17 +02:00
|
|
|
|
2016-09-30 18:36:48 +02:00
|
|
|
}
|
|
|
|
|
2022-02-07 09:43:30 +01:00
|
|
|
public void register() {
|
2022-05-30 18:30:17 +02:00
|
|
|
for (String context : authProvider.getContexts()) {
|
|
|
|
this.onRegistration(context);
|
|
|
|
}
|
2022-02-07 09:43:30 +01:00
|
|
|
}
|
2022-05-30 18:30:17 +02:00
|
|
|
|
2016-09-30 18:36:48 +02:00
|
|
|
@Observes(value=Constants.token_registered, kind=Kind.safe)
|
2022-05-30 18:30:17 +02:00
|
|
|
public synchronized void onRegistration(String context){
|
|
|
|
log.info("registration called in context {}", context);
|
2016-09-30 18:36:48 +02:00
|
|
|
List<Future<ApplicationManager>> futureList = new ArrayList<Future<ApplicationManager>>();
|
|
|
|
|
|
|
|
try {
|
2022-05-30 18:30:17 +02:00
|
|
|
|
|
|
|
Secret secret = authProvider.getSecretForContext(context);
|
2016-09-30 18:36:48 +02:00
|
|
|
|
2022-02-07 09:43:30 +01:00
|
|
|
for (Class<? extends ApplicationManager> appManager: managersClasses){
|
2022-05-30 18:30:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
Future<ApplicationManager> appManagerFuture = service.submit(new InitAppManager(secret, appManager));
|
|
|
|
log.info("intializing app using manager {} in context {}",appManager.getClass().getCanonicalName(),context);
|
2016-09-30 18:36:48 +02:00
|
|
|
|
|
|
|
futureList.add(appManagerFuture);
|
2022-02-07 09:43:30 +01:00
|
|
|
if (provider.getAppmanagerMap().containsKey(appManager.getCanonicalName()))
|
|
|
|
provider.getAppmanagerMap().get(appManager.getCanonicalName()).put(context, appManagerFuture);
|
2016-09-30 18:36:48 +02:00
|
|
|
else {
|
|
|
|
Map<String, Future<ApplicationManager>> tokenFutureMap = new HashMap<String, Future<ApplicationManager>>();
|
|
|
|
tokenFutureMap.put(context, appManagerFuture);
|
2022-02-07 09:43:30 +01:00
|
|
|
provider.getAppmanagerMap().put(appManager.getCanonicalName(), tokenFutureMap);
|
2016-09-30 18:36:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!futureList.isEmpty())
|
|
|
|
instanciatedManagerPerScope.put(context, futureList);
|
|
|
|
} catch (Exception e1) {
|
2022-05-30 18:30:17 +02:00
|
|
|
log.error("something failed getting authorization",e1);
|
|
|
|
throw new RuntimeException("something failed getting authorization",e1);
|
2016-09-30 18:36:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Observes(value=Constants.token_removed, kind=Kind.critical)
|
2022-05-30 18:30:17 +02:00
|
|
|
public synchronized void onRemove(String context){
|
2016-09-30 18:36:48 +02:00
|
|
|
|
|
|
|
try {
|
2022-05-30 18:30:17 +02:00
|
|
|
|
|
|
|
Secret secret = authProvider.getSecretForContext(context);
|
2016-09-30 18:36:48 +02:00
|
|
|
|
|
|
|
for (Future<ApplicationManager> appManager: instanciatedManagerPerScope.get(context)){
|
2022-05-30 18:30:17 +02:00
|
|
|
service.execute(new ShutDownAppManager(secret, appManager));
|
2022-09-12 15:32:57 +02:00
|
|
|
provider.getAppmanagerMap().get(appManager.getClass().getCanonicalName()).remove(context);
|
2016-09-30 18:36:48 +02:00
|
|
|
}
|
|
|
|
instanciatedManagerPerScope.remove(context);
|
|
|
|
} catch (Exception e1) {
|
|
|
|
log.error("something failed getting token",e1);
|
|
|
|
throw new RuntimeException("something failed getting token",e1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void onStop(ApplicationContext appContext){
|
|
|
|
|
2022-06-10 17:11:39 +02:00
|
|
|
for (String context :appContext.container().configuration().authorizationProvider().getContexts())
|
2022-05-30 18:30:17 +02:00
|
|
|
this.onRemove(context);
|
|
|
|
|
2016-09-30 18:36:48 +02:00
|
|
|
unregister();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void unregister(){
|
|
|
|
service.shutdownNow();
|
|
|
|
}
|
|
|
|
|
|
|
|
public class InitAppManager implements Callable<ApplicationManager>{
|
|
|
|
|
|
|
|
private Class<? extends ApplicationManager> managerClass;
|
2022-05-30 18:30:17 +02:00
|
|
|
private Secret secret;
|
2016-09-30 18:36:48 +02:00
|
|
|
|
2022-05-30 18:30:17 +02:00
|
|
|
public InitAppManager(Secret secret, Class<? extends ApplicationManager> managerClass){
|
2016-09-30 18:36:48 +02:00
|
|
|
this.managerClass = managerClass;
|
2022-05-30 18:30:17 +02:00
|
|
|
this.secret = secret;
|
|
|
|
|
2016-09-30 18:36:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ApplicationManager call() throws Exception {
|
2022-05-30 18:30:17 +02:00
|
|
|
log.info("on init called");
|
|
|
|
SecretManagerProvider.instance.set(secret);
|
|
|
|
ApplicationManager manager = null;
|
2016-09-30 18:36:48 +02:00
|
|
|
try {
|
2022-05-30 18:30:17 +02:00
|
|
|
manager = managerClass.newInstance();
|
|
|
|
log.info("calling on onInit on manager {} with secret {}",manager.getClass().getCanonicalName(),secret);
|
2016-09-30 18:36:48 +02:00
|
|
|
manager.onInit();
|
|
|
|
} catch (Exception e) {
|
2022-05-30 18:30:17 +02:00
|
|
|
log.warn("error on onInit of {} on context {}",manager.getClass().getCanonicalName(), secret.getContext(), e);
|
2016-09-30 18:36:48 +02:00
|
|
|
} finally{
|
2022-05-30 18:30:17 +02:00
|
|
|
SecretManagerProvider.instance.reset();
|
2016-09-30 18:36:48 +02:00
|
|
|
}
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ShutDownAppManager implements Runnable{
|
|
|
|
|
|
|
|
private Future<ApplicationManager> appManager;
|
2022-05-30 18:30:17 +02:00
|
|
|
private Secret secret;
|
2016-09-30 18:36:48 +02:00
|
|
|
|
2022-05-30 18:30:17 +02:00
|
|
|
public ShutDownAppManager(Secret secret, Future<ApplicationManager> appManager){
|
2016-09-30 18:36:48 +02:00
|
|
|
this.appManager = appManager;
|
2022-05-30 18:30:17 +02:00
|
|
|
|
2016-09-30 18:36:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2022-05-30 18:30:17 +02:00
|
|
|
SecretManagerProvider.instance.set(secret);
|
2016-09-30 18:36:48 +02:00
|
|
|
try {
|
2022-05-30 18:30:17 +02:00
|
|
|
log.info("calling on ShutDown of {} on context {}",appManager.getClass().getCanonicalName(), secret.getContext());
|
2016-09-30 18:36:48 +02:00
|
|
|
appManager.get().onShutdown();
|
|
|
|
} catch (Exception e) {
|
2022-05-30 18:30:17 +02:00
|
|
|
log.warn("error on onShutdown of {} on context {}",appManager.getClass().getCanonicalName(), secret.getContext(), e);
|
2016-09-30 18:36:48 +02:00
|
|
|
} finally{
|
2022-05-30 18:30:17 +02:00
|
|
|
SecretManagerProvider.instance.reset();
|
2016-09-30 18:36:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-07 09:43:30 +01:00
|
|
|
|
|
|
|
@Override
|
2022-05-30 18:30:17 +02:00
|
|
|
public void setAuthorizationProvider(AuthorizationProvider authProvider) {
|
|
|
|
this.authProvider = authProvider;
|
|
|
|
|
2022-02-07 09:43:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setApplicationManagerClasses(Set<Class<? extends ApplicationManager>> managersClasses) {
|
|
|
|
this.managersClasses = managersClasses;
|
|
|
|
}
|
|
|
|
|
2022-05-30 18:30:17 +02:00
|
|
|
|
2016-09-30 18:36:48 +02:00
|
|
|
}
|