liquibase.osgi.Activator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liquibase-core Show documentation
Show all versions of liquibase-core Show documentation
Liquibase is a tool for managing and executing database changes.
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 = (String) 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;
}
}
}