// 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/Constants.java
// public static final String ATTRIBUTE_BUNDLE_CONTEXT = "org.osgi.framework.BundleContext";


import com.adeptj.runtime.common.BundleContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.osgi.framework.BundleContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
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;




/**
 * A {@link ServletContextAttributeListener} which initializes the {@link EventDispatcherTracker}
 * when {@link BundleContext} is being set as a {@link javax.servlet.ServletContext} attribute and again closes
 * and opens when {@link BundleContext} is replaced as a {@link javax.servlet.ServletContext} attribute.
 *
 * @author Rakesh.Kumar, AdeptJ
 */
public class BridgeServletContextAttributeListener implements ServletContextAttributeListener {

    @Override
    public void attributeAdded(ServletContextAttributeEvent event) {
        if (StringUtils.equals(event.getName(), ATTRIBUTE_BUNDLE_CONTEXT)) {
            ServiceTrackers.getInstance().openEventDispatcherTracker((BundleContext) event.getValue());
        }
    }

    @Override
// 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/Constants.java
// public static final String ATTRIBUTE_BUNDLE_CONTEXT = "org.osgi.framework.BundleContext";


// Path: src/main/java/com/adeptj/runtime/osgi/BridgeServletContextAttributeListener.java
import com.adeptj.runtime.common.BundleContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.osgi.framework.BundleContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
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;




/**
 * A {@link ServletContextAttributeListener} which initializes the {@link EventDispatcherTracker}
 * when {@link BundleContext} is being set as a {@link javax.servlet.ServletContext} attribute and again closes
 * and opens when {@link BundleContext} is replaced as a {@link javax.servlet.ServletContext} attribute.
 *
 * @author Rakesh.Kumar, AdeptJ
 */
public class BridgeServletContextAttributeListener implements ServletContextAttributeListener {

    @Override
    public void attributeAdded(ServletContextAttributeEvent event) {
        if (StringUtils.equals(event.getName(), ATTRIBUTE_BUNDLE_CONTEXT)) {
            ServiceTrackers.getInstance().openEventDispatcherTracker((BundleContext) event.getValue());
        }
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent event) {