ConfigurationUnbinder.java 3.49 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.launchpad.api.StartupHandler
 *  org.osgi.framework.Bundle
 *  org.osgi.framework.BundleContext
 *  org.osgi.service.cm.Configuration
 *  org.osgi.service.cm.ConfigurationAdmin
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.compat.impl.config;

import java.io.IOException;
import java.util.Dictionary;
import java.util.HashSet;
import java.util.LinkedList;
import org.apache.sling.launchpad.api.StartupHandler;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConfigurationUnbinder {
    private static String PROP_UPDATE = "compat.upgrade.configs.update";
    private final Logger log;

    public ConfigurationUnbinder(final BundleContext bundleContext, final ConfigurationAdmin ca, final StartupHandler startupHandler) {
        this.log = LoggerFactory.getLogger(this.getClass());
        final boolean doUpdate = "true".equalsIgnoreCase(bundleContext.getProperty(PROP_UPDATE));
        startupHandler.waitWithStartup(true);
        Thread t = new Thread(){

            /*
             * WARNING - Removed try catching itself - possible behaviour change.
             */
            @Override
            public void run() {
                try {
                    ConfigurationUnbinder.this.unbindAndUpdateAllConfigurations(bundleContext, ca, doUpdate);
                }
                finally {
                    startupHandler.waitWithStartup(false);
                }
            }
        };
        t.setName("Compat Configuration Unbinder");
        t.setDaemon(false);
        t.start();
    }

    void unbindAndUpdateAllConfigurations(BundleContext bundleContext, ConfigurationAdmin ca, boolean doUpdate) {
        Bundle[] bundles = bundleContext.getBundles();
        HashSet<String> ignoreLocations = new HashSet<String>();
        for (Bundle b : bundles) {
            if (b.getState() != 32 && b.getState() != 8) continue;
            ignoreLocations.add(b.getLocation());
        }
        Configuration[] allConfigs = null;
        try {
            allConfigs = ca.listConfigurations(null);
        }
        catch (Exception e) {
            this.log.error("Unable to list all configurations.", (Throwable)e);
        }
        if (allConfigs != null) {
            LinkedList<Configuration> toUpdate = new LinkedList<Configuration>();
            for (Configuration cfg : allConfigs) {
                String location = cfg.getBundleLocation();
                if (location == null || location.length() <= 0 || ignoreLocations.contains(location)) continue;
                this.log.info("Unbinding Configuration {} from {}", (Object)cfg.getPid(), (Object)location);
                cfg.setBundleLocation(null);
                toUpdate.add(cfg);
            }
            if (doUpdate) {
                for (Configuration cfg2 : toUpdate) {
                    this.log.info("Updating Configuration {} to re-activate it", (Object)cfg2.getPid());
                    Dictionary props = cfg2.getProperties();
                    try {
                        cfg2.update(props);
                    }
                    catch (IOException e) {
                        this.log.warn("Error during configuration update of " + cfg2.getPid(), (Throwable)e);
                    }
                }
            }
        }
    }

}