in src/main/java/org/apache/sling/feature/launcher/impl/launchers/AbstractRunner.java [108:241]
protected void setupFramework(final Framework framework, final Map<Integer, List<URL>> bundlesMap) throws BundleException {
// check for Apache Felix CM persistence manager config
final String pm = framework.getBundleContext().getProperty(CM_CONFIG_PM);
if (PM_FEATURE_LAUNCHER.equals(pm)) {
logger.info("Using feature launcher configuration admin persistence manager");
try {
// we start a tracker for the memory PM
this.configAdminTracker = new ServiceTracker<>(framework.getBundleContext(),
framework.getBundleContext().createFilter(PM_MEMORY_FILTER),
new ServiceTrackerCustomizer<Object, Object>() {
private volatile ServiceRegistration<?> reg;
@Override
public Object addingService(final ServiceReference<Object> reference) {
// get memory pm
final Object memoryPM = framework.getBundleContext().getService(reference);
if (memoryPM != null) {
try {
// we re use the memory PM (it is not used anyway)
// and simply store the configs there using reflection
final Method storeMethod = memoryPM.getClass().getDeclaredMethod("store",
String.class, Dictionary.class);
for (final Object[] obj : configurations) {
@SuppressWarnings("unchecked")
final Dictionary<String, Object> props = (Dictionary<String, Object>) obj[2];
final String pid;
if (obj[1] != null) {
final String factoryPid = (String) obj[1];
pid = factoryPid.concat("~").concat((String) obj[0]);
props.put("service.factoryPid", factoryPid);
} else {
pid = (String) obj[0];
}
props.put(Constants.SERVICE_PID, pid);
storeMethod.invoke(memoryPM, pid, props);
}
// register feature launcher pm
final Dictionary<String, Object> properties = new Hashtable<>();
properties.put("name", PM_FEATURE_LAUNCHER);
reg = reference.getBundle().getBundleContext().registerService(
"org.apache.felix.cm.PersistenceManager", memoryPM, properties);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new RuntimeException(e);
}
}
return memoryPM;
}
@Override
public void modifiedService(ServiceReference<Object> reference, Object service) {
// nothing to do
}
@Override
public void removedService(ServiceReference<Object> reference, Object service) {
if (reg != null) {
reg.unregister();
reg = null;
}
reference.getBundle().getBundleContext().ungetService(reference);
}
});
} catch (final InvalidSyntaxException e) {
// the filter is constant so this should really not happen
throw new RuntimeException(e);
}
this.configAdminTracker.open(true);
} else if (!configurations.isEmpty()) {
this.configAdminTracker = new ServiceTracker<>(framework.getBundleContext(),
"org.osgi.service.cm.ConfigurationAdmin", new ServiceTrackerCustomizer<Object, Object>() {
@Override
public Object addingService(final ServiceReference<Object> reference) {
// get config admin
final Object cm = framework.getBundleContext().getService(reference);
if (cm != null) {
try {
configure(cm);
} finally {
framework.getBundleContext().ungetService(reference);
}
}
return null;
}
@Override
public void modifiedService(ServiceReference<Object> reference, Object service) {
// nothing to do
}
@Override
public void removedService(ServiceReference<Object> reference, Object service) {
// nothing to do
}
});
this.configAdminTracker.open(true);
}
if ( !installables.isEmpty() ) {
this.installerTracker = new ServiceTracker<>(framework.getBundleContext(),
"org.apache.sling.installer.api.OsgiInstaller",
new ServiceTrackerCustomizer<Object, Object>() {
@Override
public Object addingService(final ServiceReference<Object> reference) {
// get installer
final Object installer = framework.getBundleContext().getService(reference);
if ( installer != null ) {
try {
install(installer);
} finally {
framework.getBundleContext().ungetService(reference);
}
}
return null;
}
@Override
public void modifiedService(ServiceReference<Object> reference, Object service) {
// nothing to do
}
@Override
public void removedService(ServiceReference<Object> reference, Object service) {
// nothing to do
}
});
this.installerTracker.open();
}
this.install(framework, bundlesMap);
}