/**
 *  Copyright 2005-2014 Red Hat, Inc.
 *
 *  Red Hat licenses this file to you 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 io.fabric8.fab.osgi.internal;

import java.io.File;

import io.fabric8.fab.osgi.ServiceConstants;
import org.ops4j.pax.url.commons.handler.HandlerActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.ConfigurationAdmin;

/**
 * Activator for the fab protocol
 */
public class Activator extends HandlerActivator<Configuration> {
    private static Activator instance;

    private BundleContext bundleContext;
    public static OsgiModuleRegistry registry = new OsgiModuleRegistry();

    public static Activator getInstance() {
        return instance;
    }

    public static BundleContext getInstanceBundleContext() {
        Activator activator = getInstance();
        if (activator != null) {
            return activator.getBundleContext();
        }
        return null;
    }

    public Activator() {
        super(ServiceConstants.PROTOCOLS_SUPPORTED, ServiceConstants.PID, new FabConnectionFactory());
        instance = this;
    }

    @Override
    public void start(BundleContext bundleContext) {
        this.bundleContext = bundleContext;

        ServiceReference serviceReference = bundleContext.getServiceReference("org.osgi.service.cm.ConfigurationAdmin");
        ConfigurationAdmin configurationAdmin = (ConfigurationAdmin) bundleContext.getService(serviceReference);

        File data = new File(System.getProperty("karaf.data", "."));
        registry.setDirectory(new File(data, "fab-module-registry"));
        registry.setConfigurationAdmin(configurationAdmin);
        registry.setPid("io.fabric8.fab.osgi.registry");
        registry.load();

        super.start(bundleContext);
    }

    public BundleContext getBundleContext() {
        return bundleContext;
    }
}

@@ -691,16 +691,83 @@
 io.File;
+%0Aimport java.io.IOException;%0Aimport java.io.InterruptedIOException;
 %0A%0Aimport
@@ -905,16 +905,66 @@
 ontext;%0A
+import org.osgi.framework.InvalidSyntaxException;%0A
 import o
@@ -1045,16 +1045,61 @@
 onAdmin;
+%0Aimport org.osgi.util.tracker.ServiceTracker;
 %0A%0A/**%0A *
@@ -1276,24 +1276,61 @@
 dleContext;%0A
+    private ConfigAdmin configAdmin;%0A
     public s
@@ -1965,25 +1965,24 @@
 ontext;%0A
-%0A
         
 ServiceR
@@ -1977,118 +1977,43 @@
     
-ServiceReference serviceReference = bundleContext.getServiceReference(%22org.osgi.service.cm.Configuration
+this.configAdmin = new Config
 Admin
-%22
+(
 );%0A 
@@ -2023,110 +2023,30 @@
     
-ConfigurationAdmin configurationAdmin = (ConfigurationAdmin) bundleContext.getService(serviceReference
+this.configAdmin.open(
 );%0A%0A
@@ -2227,26 +2227,25 @@
 min(
-configuration
+new Config
 Admin
+()
 );%0A 
@@ -2377,79 +2377,2067 @@
     
-public BundleContext getBundleContext() %7B%0A        return bundleContext;
+@Override%0A    public void stop(BundleContext bundleContext) %7B%0A        this.configAdmin.close();%0A        super.stop(bundleContext);%0A    %7D%0A%0A    public BundleContext getBundleContext() %7B%0A        return bundleContext;%0A    %7D%0A%0A    public class ConfigAdmin extends ServiceTracker%3CConfigurationAdmin, ConfigurationAdmin%3E implements ConfigurationAdmin %7B%0A%0A        public ConfigAdmin() %7B%0A            super(bundleContext, ConfigurationAdmin.class, null);%0A        %7D%0A%0A        private ConfigurationAdmin getConfigAdmin() throws IOException %7B%0A            try %7B%0A                ConfigurationAdmin ca = waitForService(5000l);%0A                if (ca != null) %7B%0A                    return ca;%0A                %7D%0A                throw new IllegalStateException(%22ConfigurationAdmin not present%22);%0A            %7D catch (InterruptedException e) %7B%0A                throw (InterruptedIOException) new InterruptedIOException(%22ConfigurationAdmin not present%22).initCause(e);%0A            %7D%0A        %7D%0A%0A        @Override%0A        public org.osgi.service.cm.Configuration createFactoryConfiguration(String factoryPid) throws IOException %7B%0A            return getConfigAdmin().createFactoryConfiguration(factoryPid);%0A        %7D%0A%0A        @Override%0A        public org.osgi.service.cm.Configuration createFactoryConfiguration(String factoryPid, String location) throws IOException %7B%0A            return getConfigAdmin().createFactoryConfiguration(factoryPid, location);%0A        %7D%0A%0A        @Override%0A        public org.osgi.service.cm.Configuration getConfiguration(String pid, String location) throws IOException %7B%0A            return getConfigAdmin().getConfiguration(pid, location);%0A        %7D%0A%0A        @Override%0A        public org.osgi.service.cm.Configuration getConfiguration(String pid) throws IOException %7B%0A            return getConfigAdmin().getConfiguration(pid);%0A        %7D%0A%0A        @Override%0A        public org.osgi.service.cm.Configuration%5B%5D listConfigurations(String filter) throws IOException, InvalidSyntaxException %7B%0A            return getConfigAdmin().listConfigurations(filter);%0A        %7D
 %0A   

Use a service tracker for ConfigurationAdmin in fab-osgi