Skip to content

Instantly share code, notes, and snippets.

@klcodanr
Created June 17, 2020 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klcodanr/81fb04cb7285742a966796cca155212c to your computer and use it in GitHub Desktop.
Save klcodanr/81fb04cb7285742a966796cca155212c to your computer and use it in GitHub Desktop.
Component to Register org.apache.sling.jcr.repoinit.RepositoryInitializer Configurations using the Bundle URLs
package com.company;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import org.osgi.framework.BundleContext;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Register the repoint scripts in the bundle's /repoinits context path.
*/
@Component(immediate = true)
public class RegisterRepoInits {
public static final String PID = "org.apache.sling.jcr.repoinit.RepositoryInitializer";
private static final Logger log = LoggerFactory.getLogger(RegisterRepoinits.class);
@Reference
private ConfigurationAdmin configAdmin;
private Configuration config;
/**
* Install the Sling RepoInit configuration based on the RepoInit scripts
* embedded in this bundle
*
* @param ctx the OSGi ComponentContext
* @throws IOException an exception occurs resolving the RepoInit scripts
*/
@Activate
@Modified
protected void activate(ComponentContext ctx) throws IOException {
log.info("activate");
BundleContext bundleContext = ctx.getBundleContext();
// Get or create the configuration if it does not already exist
config = configAdmin.getConfiguration(PID + "-company");
List<String> urls = new ArrayList<>();
// Find all of the RepoInit text files in ./src/main/resources/repoinits by
// enumerating the Bundle's entries
Enumeration<?> repoinits = bundleContext.getBundle().getEntryPaths("/repoinits");
while (repoinits.hasMoreElements()) {
Object repoinit = repoinits.nextElement();
log.info("Adding RepoInit: {}", repoinit);
// Resolve the Bundle URL for the Bundle Resource
URL repointUrl = bundleContext.getBundle().getResource(repoinit.toString());
urls.add(repointUrl.toString());
}
// Create and register an OSGi configuration with an Array of Bundle Resource
// URLs
@SuppressWarnings("java:S1149")
Dictionary<String,Object> properties = new Hashtable<>();
properties.put(ConfigurationAdmin.SERVICE_FACTORYPID, PID);
properties.put("references", urls.toArray(new String[urls.size()]));
properties.put("scripts", new String[] {});
config.update(properties);
log.info("RepoInit Registered");
}
/*
* Remove the config when the component is deactivated
*/
@Deactivate
protected void deactivate() throws IOException {
log.info("deactivate");
if (config != null) {
config.delete();
log.info("Repoinit De-registered");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment