2016-09-30 18:36:48 +02:00
package org.gcube.smartgears ;
2020-11-03 11:11:22 +01:00
import java.net.URL ;
import java.util.Collection ;
2016-09-30 18:36:48 +02:00
import java.util.HashSet ;
import java.util.Set ;
import javax.servlet.ServletContextEvent ;
import javax.servlet.ServletContextListener ;
import javax.servlet.annotation.WebListener ;
import org.gcube.smartgears.annotations.ManagedBy ;
2022-02-07 09:43:30 +01:00
import org.gcube.smartgears.application.manager.AppManagerObserver ;
2016-09-30 18:36:48 +02:00
import org.gcube.smartgears.context.application.ApplicationContext ;
import org.reflections.Reflections ;
2016-10-05 15:15:22 +02:00
import org.reflections.scanners.SubTypesScanner ;
2016-09-30 18:36:48 +02:00
import org.reflections.scanners.TypeAnnotationsScanner ;
import org.reflections.util.ClasspathHelper ;
import org.reflections.util.ConfigurationBuilder ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
@WebListener
public class ContextListener implements ServletContextListener {
private static Logger log = LoggerFactory . getLogger ( ContextListener . class ) ;
2022-02-07 09:43:30 +01:00
AppManagerObserver observer ;
2016-09-30 18:36:48 +02:00
@Override
public void contextInitialized ( ServletContextEvent sce ) {
2022-02-07 09:43:30 +01:00
2016-09-30 18:36:48 +02:00
ApplicationContext context = ( ApplicationContext ) sce . getServletContext ( ) . getAttribute ( Constants . context_attribute ) ;
if ( context = = null ) {
String msg = sce . getServletContext ( ) . getContextPath ( ) + " is a gCube-aware application but is not managed as a gCube resource: missing or invalid context attribute " + Constants . context_attribute ;
throw new RuntimeException ( msg ) ;
}
log . info ( " configuring context provider for {} " , context . name ( ) ) ;
ContextProvider . set ( context ) ;
2022-02-07 09:43:30 +01:00
2016-09-30 18:36:48 +02:00
retrieveAndRegisterManagers ( context ) ;
}
private void retrieveAndRegisterManagers ( ApplicationContext context ) {
2022-02-07 09:43:30 +01:00
ApplicationManagerProvider . init ( context ) ;
2020-11-03 11:11:22 +01:00
Collection < URL > urls = ClasspathHelper . forClassLoader ( ) ;
urls . removeIf ( url - > url . toString ( ) . endsWith ( " .so " ) | | url . toString ( ) . endsWith ( " .zip " ) ) ;
ConfigurationBuilder reflectionConf = new ConfigurationBuilder ( ) . addUrls ( urls ) . setScanners ( new TypeAnnotationsScanner ( ) , new SubTypesScanner ( ) ) ;
2016-10-05 15:15:22 +02:00
Reflections reflection = new Reflections ( reflectionConf ) ;
2016-09-30 18:36:48 +02:00
Set < Class < ? > > toInitialize = reflection . getTypesAnnotatedWith ( ManagedBy . class ) ;
Set < Class < ? extends ApplicationManager > > managers = new HashSet < Class < ? extends ApplicationManager > > ( ) ;
for ( Class < ? > initializer : toInitialize ) {
ManagedBy manageBy = initializer . getAnnotation ( ManagedBy . class ) ;
log . info ( " ApplicationManager added {} @ {} " , manageBy . value ( ) . getSimpleName ( ) , context . name ( ) ) ;
managers . add ( manageBy . value ( ) ) ;
}
if ( managers . size ( ) > 0 ) {
2022-02-07 09:43:30 +01:00
observer = ApplicationManagerProvider . instance . getObserver ( ) ;
2022-05-25 18:58:02 +02:00
observer . setStartingTokens ( context . configuration ( ) . authorizedContexts ( ) ) ;
2022-02-07 09:43:30 +01:00
observer . setApplicationManagerClasses ( managers ) ;
observer . register ( ) ;
2016-09-30 18:36:48 +02:00
context . events ( ) . subscribe ( observer ) ;
}
}
@Override
public void contextDestroyed ( ServletContextEvent sce ) {
if ( observer ! = null ) {
ApplicationContext context = ( ApplicationContext ) sce . getServletContext ( ) . getAttribute ( Constants . context_attribute ) ;
context . events ( ) . unsubscribe ( observer ) ;
observer . onStop ( context ) ;
}
}
}