2016-09-30 18:36:48 +02:00
package org.gcube.smartgears ;
import java.util.HashSet ;
import java.util.Set ;
2024-02-21 12:05:21 +01:00
import java.util.stream.Collectors ;
2024-03-12 14:25:51 +01:00
import jakarta.servlet.ServletContextEvent ;
import jakarta.servlet.ServletContextListener ;
import jakarta.servlet.annotation.WebListener ;
2016-09-30 18:36:48 +02:00
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.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2024-02-21 12:05:21 +01:00
import io.github.classgraph.ClassGraph ;
import io.github.classgraph.ClassInfo ;
import io.github.classgraph.ClassInfoList ;
import io.github.classgraph.ScanResult ;
2016-09-30 18:36:48 +02:00
@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 ) ;
2024-02-21 12:05:21 +01:00
}
2016-09-30 18:36:48 +02:00
log . info ( " configuring context provider for {} " , context . name ( ) ) ;
ContextProvider . set ( context ) ;
retrieveAndRegisterManagers ( context ) ;
2024-02-21 12:05:21 +01:00
2016-09-30 18:36:48 +02:00
}
private void retrieveAndRegisterManagers ( ApplicationContext context ) {
2022-02-07 09:43:30 +01:00
ApplicationManagerProvider . init ( context ) ;
2024-02-21 12:05:21 +01:00
Set < Class < ? > > annotatedManaged ;
try ( ScanResult result = new ClassGraph ( ) . enableClassInfo ( ) . enableAnnotationInfo ( ) . scan ( ) ) {
ClassInfoList classInfos = result . getClassesWithAnnotation ( ManagedBy . class . getName ( ) ) ;
annotatedManaged = classInfos . stream ( ) . map ( ClassInfo : : loadClass )
. collect ( Collectors . toSet ( ) ) ;
}
/ *
Collection < URL > urls = ClasspathHelper . forJavaClassPath ( ) ;
2020-11-03 11:11:22 +01:00
urls . removeIf ( url - > url . toString ( ) . endsWith ( " .so " ) | | url . toString ( ) . endsWith ( " .zip " ) ) ;
2024-02-21 12:05:21 +01:00
ConfigurationBuilder reflectionConf = new ConfigurationBuilder ( ) . addUrls ( urls ) . addClassLoaders ( Thread . currentThread ( ) . getContextClassLoader ( ) ) . 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 ) ;
2024-02-21 12:05:21 +01:00
* /
2024-02-22 15:41:40 +01:00
log . debug ( " annotated managed are {} for {} " , annotatedManaged . size ( ) , context . name ( ) ) ;
2016-09-30 18:36:48 +02:00
Set < Class < ? extends ApplicationManager > > managers = new HashSet < Class < ? extends ApplicationManager > > ( ) ;
2024-02-21 12:05:21 +01:00
for ( Class < ? > initializer : annotatedManaged ) {
2016-09-30 18:36:48 +02:00
ManagedBy manageBy = initializer . getAnnotation ( ManagedBy . class ) ;
2022-06-22 18:26:37 +02:00
log . info ( " ApplicationManager added {} to {} @ {} " , manageBy . value ( ) . getSimpleName ( ) , initializer . getSimpleName ( ) , context . name ( ) ) ;
2016-09-30 18:36:48 +02:00
managers . add ( manageBy . value ( ) ) ;
}
if ( managers . size ( ) > 0 ) {
2022-02-07 09:43:30 +01:00
observer = ApplicationManagerProvider . instance . getObserver ( ) ;
2023-01-19 16:16:48 +01:00
observer . setAuthorizationProvider ( context . container ( ) . authorizationProvider ( ) ) ;
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 ) ;
}
2024-02-22 15:41:40 +01:00
else {
2024-03-12 14:25:51 +01:00
log . info ( " no application managers found for {} " , context . name ( ) ) ;
2024-02-22 15:41:40 +01:00
}
2016-09-30 18:36:48 +02:00
}
@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 ) ;
}
}
}