// 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/common/Times.java
// public final class Times {
// 
//     // No instances, just utility methods.
//     private Times() {
//     }
// 
//     /**
//      * Returns elapsed time in milliseconds from the provided time in nanoseconds.
//      *
//      * @param startTime time in nanoseconds
//      * @return elapsed time in milliseconds
//      */
//     public static long elapsedMillis(final long startTime) {
//         return NANOSECONDS.toMillis(System.nanoTime() - startTime);
//     }
// 
//     /**
//      * Returns elapsed time in seconds from the provided time in milliseconds.
//      *
//      * @param startTime time in milliseconds
//      * @return elapsed time in seconds
//      */
//     public static long elapsedSeconds(final long startTime) {
//         return MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime);
//     }
// }


import com.adeptj.runtime.common.ServletContextHolder;
import com.adeptj.runtime.common.Times;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/*
###############################################################################
#                                                                             # 
#    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.osgi;



/**
 * ContextListener that handles the OSGi Framework shutdown.
 *
 * @author Rakesh.Kumar, AdeptJ
 */
@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);
// 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/common/Times.java
// public final class Times {
// 
//     // No instances, just utility methods.
//     private Times() {
//     }
// 
//     /**
//      * Returns elapsed time in milliseconds from the provided time in nanoseconds.
//      *
//      * @param startTime time in nanoseconds
//      * @return elapsed time in milliseconds
//      */
//     public static long elapsedMillis(final long startTime) {
//         return NANOSECONDS.toMillis(System.nanoTime() - startTime);
//     }
// 
//     /**
//      * Returns elapsed time in seconds from the provided time in milliseconds.
//      *
//      * @param startTime time in milliseconds
//      * @return elapsed time in seconds
//      */
//     public static long elapsedSeconds(final long startTime) {
//         return MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime);
//     }
// }


// Path: src/main/java/com/adeptj/runtime/osgi/FrameworkShutdownHandler.java
import com.adeptj.runtime.common.ServletContextHolder;
import com.adeptj.runtime.common.Times;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/*
###############################################################################
#                                                                             # 
#    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.osgi;



/**
 * ContextListener that handles the OSGi Framework shutdown.
 *
 * @author Rakesh.Kumar, AdeptJ
 */
@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));