All Downloads are FREE. Search and download functionalities are using the official Maven repository.

liquibase.osgi.Activator Maven / Gradle / Ivy

There is a newer version: 4.29.1
Show newest version
package liquibase.osgi;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import liquibase.osgi.Activator.LiquibaseBundle;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.util.tracker.BundleTracker;
import org.osgi.util.tracker.BundleTrackerCustomizer;

public class Activator implements BundleActivator, BundleTrackerCustomizer {

    private static final String LIQUIBASE_CUSTOM_CHANGE_WRAPPER_PACKAGES = "Liquibase-Custom-Change-Packages";
    private BundleTracker bundleTracker;
    private static final List liquibaseBundles = new CopyOnWriteArrayList<>();

    @Override
    public void start(final BundleContext bc) throws Exception {
        OSGIContainerChecker.osgiPlatform();
        bundleTracker = new BundleTracker<>(bc, Bundle.ACTIVE, this);
        bundleTracker.open();
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        bundleTracker.close();
        liquibaseBundles.clear();
    }

    public static List getLiquibaseBundles() {
        return Collections.unmodifiableList(liquibaseBundles);
    }

    @Override
    public LiquibaseBundle addingBundle(Bundle bundle, BundleEvent event) {
        if (bundle.getBundleId() == 0) {
            return null;
        }
        String customWrapperPackages = bundle.getHeaders().get(LIQUIBASE_CUSTOM_CHANGE_WRAPPER_PACKAGES);
        if (customWrapperPackages != null) {
            LiquibaseBundle lb = new LiquibaseBundle(bundle, customWrapperPackages);
            liquibaseBundles.add(lb);
            return lb;
        }
        return null;
    }

    @Override
    public void modifiedBundle(Bundle bundle, BundleEvent event, LiquibaseBundle liquibaseBundle) {
        // nothing to do
    }

    @Override
    public void removedBundle(Bundle bundle, BundleEvent event, LiquibaseBundle liquibaseBundle) {
        if (liquibaseBundle != null) {
            liquibaseBundles.remove(liquibaseBundle);
        }
    }

    public static class LiquibaseBundle {

        private final Bundle bundle;
        private final List allowedPackages;

        public LiquibaseBundle(Bundle bundle, String allowedPackages) {
            if (bundle == null) {
                throw new IllegalArgumentException("bundle cannot be empty");
            }
            if (allowedPackages == null || allowedPackages.isEmpty()) {
                throw new IllegalArgumentException("packages cannot be empty");
            }
            this.bundle = bundle;
            this.allowedPackages = Collections.unmodifiableList(Arrays.asList(allowedPackages.split(",")));
        }

        public Bundle getBundle() {
            return bundle;
        }

        public boolean allowedAllPackages() {
            return allowedPackages.size() == 1
                    && "*".equals(allowedPackages.get(0));
        }

        public List getAllowedPackages() {
            return allowedPackages;
        }

    }

    public static class OSGIContainerChecker {

        private static volatile boolean osgiPlatform = false;

        public static boolean isOsgiPlatform() {
            return osgiPlatform;
        }

        static void osgiPlatform() {
            osgiPlatform = true;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy