Search in sources :

Example 1 with FrameworkShutdownHandler

use of com.adeptj.runtime.osgi.FrameworkShutdownHandler in project adeptj-runtime by AdeptJ.

the class ContainerInitializer method onStartup.

/**
 * {@inheritDoc}
 */
@Override
public void onStartup(Set<Class<?>> startupAwareClasses, ServletContext context) {
    Logger logger = LoggerFactory.getLogger(ContainerInitializer.class);
    if (startupAwareClasses == null || startupAwareClasses.isEmpty()) {
        // We can't go ahead if StartupAware implementations are not passed by container.
        logger.error("No @HandlesTypes(StartupAware) on classpath!!");
        throw new IllegalStateException("No @HandlesTypes(StartupAware) on classpath!!");
    } else {
        ServletContextHolder.INSTANCE.setServletContext(context);
        startupAwareClasses.stream().sorted(new StartupAwareComparator()).peek(startupAwareClass -> logger.info("@HandlesTypes: [{}]", startupAwareClass)).forEach(startupAwareClass -> {
            try {
                startupAwareClass.asSubclass(StartupAware.class).getDeclaredConstructor().newInstance().onStartup(context);
            } catch (Exception ex) {
                // NOSONAR
                logger.error("Exception while executing StartupAware#onStartup!!", ex);
                throw new InitializationException("Exception while executing StartupAware#onStartup!!", ex);
            }
        });
        // If we are here means startup went well above, register FrameworkShutdownHandler now.
        context.addListener(FrameworkShutdownHandler.class);
    }
}
Also used : ServletContainerInitializer(javax.servlet.ServletContainerInitializer) Logger(org.slf4j.Logger) ServletContextHolder(com.adeptj.runtime.common.ServletContextHolder) LoggerFactory(org.slf4j.LoggerFactory) HandlesTypes(javax.servlet.annotation.HandlesTypes) FrameworkShutdownHandler(com.adeptj.runtime.osgi.FrameworkShutdownHandler) Set(java.util.Set) InitializationException(com.adeptj.runtime.exception.InitializationException) ServletContext(javax.servlet.ServletContext) Logger(org.slf4j.Logger) InitializationException(com.adeptj.runtime.exception.InitializationException) InitializationException(com.adeptj.runtime.exception.InitializationException)

Aggregations

ServletContextHolder (com.adeptj.runtime.common.ServletContextHolder)1 InitializationException (com.adeptj.runtime.exception.InitializationException)1 FrameworkShutdownHandler (com.adeptj.runtime.osgi.FrameworkShutdownHandler)1 Set (java.util.Set)1 ServletContainerInitializer (javax.servlet.ServletContainerInitializer)1 ServletContext (javax.servlet.ServletContext)1 HandlesTypes (javax.servlet.annotation.HandlesTypes)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1