// Path: src/main/java/com/adeptj/runtime/common/BundleContextHolder.java
// public enum BundleContextHolder {
// 
//     INSTANCE;
// 
//     private BundleContext bundleContext;
// 
//     public BundleContext getBundleContext() {
//         return this.bundleContext;
//     }
// 
//     public void setBundleContext(BundleContext bundleContext) { // NOSONAR
//         this.bundleContext = bundleContext;
//     }
// 
//     public static BundleContextHolder getInstance() {
//         return INSTANCE;
//     }
// }
// 
// 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/Constants.java
// public static final String ATTRIBUTE_BUNDLE_CONTEXT = "org.osgi.framework.BundleContext";


import static org.osgi.framework.FrameworkEvent.ERROR;
import static org.osgi.framework.FrameworkEvent.STARTED;
import com.adeptj.runtime.common.BundleContextHolder;
import com.adeptj.runtime.common.ServletContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkEvent;
import org.osgi.framework.FrameworkListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.adeptj.runtime.common.Constants.ATTRIBUTE_BUNDLE_CONTEXT;
/*
###############################################################################
#                                                                             # 
#    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;



/**
 * OSGi FrameworkListener which takes care of OSGi framework restart from Felix WebConsole.
 *
 * @author Rakesh.Kumar, AdeptJ
 */
public class FrameworkLifecycleListener implements FrameworkListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(FrameworkLifecycleListener.class);

    private static final String SYS_PROP_LOG_FRAMEWORK_ERROR = "adeptj.rt.log.framework.error";

    /**
     * Handles OSGi Framework restart, does following on System Bundle STARTED event.
     * <p>
     * 1. Removes the BundleContext from ServletContext attributes. <br>
     * 2. Gets the new BundleContext from System Bundle and sets that to ServletContext. <br>
     * 3. Closes the DispatcherServletTracker and open again with new BundleContext. <br>
     * 4. As the BundleContext is removed and added from ServletContext the corresponding BridgeServletContextAttributeListener
     * is fired with events which in turn closes the EventDispatcherTracker and opens again with new BundleContext.
     */
    @Override
    public void frameworkEvent(FrameworkEvent event) {
        switch (event.getType()) {
            case STARTED:
                if (ServiceTrackers.getInstance().isDispatcherServletTrackerOpened()) {
                    LOGGER.info("Handling OSGi Framework Restart!!");
                    BundleContext bundleContext = event.getBundle().getBundleContext();
                    BundleContextHolder.getInstance().setBundleContext(bundleContext);
                    // Set the new BundleContext as a ServletContext attribute, remove the stale BundleContext.
// Path: src/main/java/com/adeptj/runtime/common/BundleContextHolder.java
// public enum BundleContextHolder {
// 
//     INSTANCE;
// 
//     private BundleContext bundleContext;
// 
//     public BundleContext getBundleContext() {
//         return this.bundleContext;
//     }
// 
//     public void setBundleContext(BundleContext bundleContext) { // NOSONAR
//         this.bundleContext = bundleContext;
//     }
// 
//     public static BundleContextHolder getInstance() {
//         return INSTANCE;
//     }
// }
// 
// 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/Constants.java
// public static final String ATTRIBUTE_BUNDLE_CONTEXT = "org.osgi.framework.BundleContext";


// Path: src/main/java/com/adeptj/runtime/osgi/FrameworkLifecycleListener.java
import static org.osgi.framework.FrameworkEvent.ERROR;
import static org.osgi.framework.FrameworkEvent.STARTED;
import com.adeptj.runtime.common.BundleContextHolder;
import com.adeptj.runtime.common.ServletContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkEvent;
import org.osgi.framework.FrameworkListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.adeptj.runtime.common.Constants.ATTRIBUTE_BUNDLE_CONTEXT;

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



/**
 * OSGi FrameworkListener which takes care of OSGi framework restart from Felix WebConsole.
 *
 * @author Rakesh.Kumar, AdeptJ
 */
public class FrameworkLifecycleListener implements FrameworkListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(FrameworkLifecycleListener.class);

    private static final String SYS_PROP_LOG_FRAMEWORK_ERROR = "adeptj.rt.log.framework.error";

    /**
     * Handles OSGi Framework restart, does following on System Bundle STARTED event.
     * <p>
     * 1. Removes the BundleContext from ServletContext attributes. <br>
     * 2. Gets the new BundleContext from System Bundle and sets that to ServletContext. <br>
     * 3. Closes the DispatcherServletTracker and open again with new BundleContext. <br>
     * 4. As the BundleContext is removed and added from ServletContext the corresponding BridgeServletContextAttributeListener
     * is fired with events which in turn closes the EventDispatcherTracker and opens again with new BundleContext.
     */
    @Override
    public void frameworkEvent(FrameworkEvent event) {
        switch (event.getType()) {
            case STARTED:
                if (ServiceTrackers.getInstance().isDispatcherServletTrackerOpened()) {
                    LOGGER.info("Handling OSGi Framework Restart!!");
                    BundleContext bundleContext = event.getBundle().getBundleContext();
                    BundleContextHolder.getInstance().setBundleContext(bundleContext);
                    // Set the new BundleContext as a ServletContext attribute, remove the stale BundleContext.
                    ServletContextHolder.getInstance()