// Path: src/main/java/com/adeptj/runtime/common/ServletContextHolder.java
// public enum ServletContextHolder {
// 
//     INSTANCE;
// 
//     private ServletContext context;
// 
//     public void setServletContext(ServletContext context) { // NOSONAR
//         if (this.context == null) {
//             this.context = context;
//         }
//     }
// 
//     public ServletContext getServletContext() {
//         return this.context;
//     }
// 
//     public <T> T getAttributeOfType(String name, Class<T> type) {
//         final Object value = this.context.getAttribute(name);
//         return type.isInstance(value) ? type.cast(value) : null;
//     }
// 
//     public static ServletContextHolder getInstance() {
//         return INSTANCE;
//     }
// }
// 
// Path: src/main/java/com/adeptj/runtime/exception/RuntimeInitializationException.java
// public class RuntimeInitializationException extends RuntimeException {
// 
//     private static final long serialVersionUID = 6443421220206654015L;
// 
//     public RuntimeInitializationException(Throwable cause) {
//         super(cause);
//     }
// }
// 
// Path: src/main/java/com/adeptj/runtime/osgi/FrameworkShutdownHandler.java
// @WebListener("Stops the OSGi Framework when ServletContext is destroyed")
// public class FrameworkShutdownHandler implements ServletContextListener {
// 
//     @Override
//     public void contextDestroyed(ServletContextEvent event) {
//         long startTime = System.nanoTime();
//         Logger logger = LoggerFactory.getLogger(this.getClass());
//         logger.info("Stopping OSGi Framework as ServletContext is being destroyed!!");
//         ServiceTrackers.getInstance().closeEventDispatcherTracker();
//         // see - https://github.com/AdeptJ/adeptj-runtime/issues/4
//         // Close the DispatcherServletTracker here rather than in BridgeServlet#destroy method.
//         // As with version 3.0.18 of Felix Http base the way with HttpSessionListener(s) handled
//         // is changed which results in a NPE.
//         ServiceTrackers.getInstance().closeDispatcherServletTracker();
//         FrameworkManager.getInstance().stopFramework();
//         ServletContextHolder.getInstance().setServletContext(null);
//         logger.info("OSGi Framework stopped in [{}] ms!!", Times.elapsedMillis(startTime));
//     }
// 
// }


import java.util.Set;
import com.adeptj.runtime.common.ServletContextHolder;
import com.adeptj.runtime.exception.RuntimeInitializationException;
import com.adeptj.runtime.osgi.FrameworkShutdownHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.annotation.HandlesTypes;
import java.util.ServiceLoader;
/*
###############################################################################
#                                                                             # 
#    Copyright 2016, AdeptJ (http://www.adeptj.com)                           #
#                                                                             #
#    Licensed under the Apache License, Version 2.0 (the "License");          #
#    you may not use this file except in compliance with the License.         #
#    You may obtain a copy of the License at                                  #
#                                                                             #
#        http://www.apache.org/licenses/LICENSE-2.0                           #
#                                                                             #
#    Unless required by applicable law or agreed to in writing, software      #
#    distributed under the License is distributed on an "AS IS" BASIS,        #
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
#    See the License for the specific language governing permissions and      #
#    limitations under the License.                                           #
#                                                                             #
###############################################################################
*/

package com.adeptj.runtime.core;



/**
 * An SCI(ServletContainerInitializer) that is called by the Container while startup is in progress.
 * This will further call onStartup method of all of the {@link HandlesTypes} classes registered with this SCI.
 *
 * @author Rakesh.Kumar, AdeptJ
 */
@HandlesTypes(StartupAware.class)
public class RuntimeInitializer implements ServletContainerInitializer {

    private static final String SYS_PROP_SCAN_STARTUP_AWARE_CLASSES = "scan.startup.aware.classes";

    /**
     * {@inheritDoc}
     */
    @Override
// Path: src/main/java/com/adeptj/runtime/common/ServletContextHolder.java
// public enum ServletContextHolder {
// 
//     INSTANCE;
// 
//     private ServletContext context;
// 
//     public void setServletContext(ServletContext context) { // NOSONAR
//         if (this.context == null) {
//             this.context = context;
//         }
//     }
// 
//     public ServletContext getServletContext() {
//         return this.context;
//     }
// 
//     public <T> T getAttributeOfType(String name, Class<T> type) {
//         final Object value = this.context.getAttribute(name);
//         return type.isInstance(value) ? type.cast(value) : null;
//     }
// 
//     public static ServletContextHolder getInstance() {
//         return INSTANCE;
//     }
// }
// 
// Path: src/main/java/com/adeptj/runtime/exception/RuntimeInitializationException.java
// public class RuntimeInitializationException extends RuntimeException {
// 
//     private static final long serialVersionUID = 6443421220206654015L;
// 
//     public RuntimeInitializationException(Throwable cause) {
//         super(cause);
//     }
// }
// 
// Path: src/main/java/com/adeptj/runtime/osgi/FrameworkShutdownHandler.java
// @WebListener("Stops the OSGi Framework when ServletContext is destroyed")
// public class FrameworkShutdownHandler implements ServletContextListener {
// 
//     @Override
//     public void contextDestroyed(ServletContextEvent event) {
//         long startTime = System.nanoTime();
//         Logger logger = LoggerFactory.getLogger(this.getClass());
//         logger.info("Stopping OSGi Framework as ServletContext is being destroyed!!");
//         ServiceTrackers.getInstance().closeEventDispatcherTracker();
//         // see - https://github.com/AdeptJ/adeptj-runtime/issues/4
//         // Close the DispatcherServletTracker here rather than in BridgeServlet#destroy method.
//         // As with version 3.0.18 of Felix Http base the way with HttpSessionListener(s) handled
//         // is changed which results in a NPE.
//         ServiceTrackers.getInstance().closeDispatcherServletTracker();
//         FrameworkManager.getInstance().stopFramework();
//         ServletContextHolder.getInstance().setServletContext(null);
//         logger.info("OSGi Framework stopped in [{}] ms!!", Times.elapsedMillis(startTime));
//     }
// 
// }


// Path: src/main/java/com/adeptj/runtime/core/RuntimeInitializer.java
import java.util.Set;
import com.adeptj.runtime.common.ServletContextHolder;
import com.adeptj.runtime.exception.RuntimeInitializationException;
import com.adeptj.runtime.osgi.FrameworkShutdownHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.annotation.HandlesTypes;
import java.util.ServiceLoader;

/*
###############################################################################
#                                                                             # 
#    Copyright 2016, AdeptJ (http://www.adeptj.com)                           #
#                                                                             #
#    Licensed under the Apache License, Version 2.0 (the "License");          #
#    you may not use this file except in compliance with the License.         #
#    You may obtain a copy of the License at                                  #
#                                                                             #
#        http://www.apache.org/licenses/LICENSE-2.0                           #
#                                                                             #
#    Unless required by applicable law or agreed to in writing, software      #
#    distributed under the License is distributed on an "AS IS" BASIS,        #
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
#    See the License for the specific language governing permissions and      #
#    limitations under the License.                                           #
#                                                                             #
###############################################################################
*/

package com.adeptj.runtime.core;



/**
 * An SCI(ServletContainerInitializer) that is called by the Container while startup is in progress.
 * This will further call onStartup method of all of the {@link HandlesTypes} classes registered with this SCI.
 *
 * @author Rakesh.Kumar, AdeptJ
 */
@HandlesTypes(StartupAware.class)
public class RuntimeInitializer implements ServletContainerInitializer {

    private static final String SYS_PROP_SCAN_STARTUP_AWARE_CLASSES = "scan.startup.aware.classes";

    /**
     * {@inheritDoc}
     */
    @Override
    public void onStartup(Set<Class<?>> startupAwareClasses, ServletContext context) {