public synchronized void start(StartContext context) throws StartException {
log.infof("Starting OSGi Framework");
try {
// Start the OSGi {@link Framework}
final ServiceContainer serviceContainer = context.getController().getServiceContainer();
BundleManager bundleManager = injectedBundleManager.getValue();
framework = bundleManager.getFrameworkState();
framework.start();
// Register the {@link MBeanServer} as OSGi service
BundleContext sysContext = framework.getBundleContext();
MBeanServer mbeanServer = injectedMBeanServer.getValue();
sysContext.registerService(MBeanServer.class.getName(), mbeanServer, null);
// Register a {@link SynchronousBundleListener} that removes the {@link DeploymentService}
BundleListener uninstallListener = new SynchronousBundleListener() {
@Override
public void bundleChanged(BundleEvent event) {
if (event.getType() == BundleEvent.UNINSTALLED) {
AbstractUserBundle userBundle;
try {
userBundle = AbstractUserBundle.assertBundleState(event.getBundle());
} catch (RuntimeException ex) {
// ignore
return;
}
Deployment deployment = userBundle.getDeployment();
ServiceName serviceName = deployment.getAttachment(ServiceName.class);
if (serviceName != null) {
ServiceController<?> controller = serviceContainer.getService(serviceName);
if (controller != null) {
controller.setMode(ServiceController.Mode.REMOVE);
}
}
}
}
};
sysContext.addBundleListener(uninstallListener);
// Create the list of {@link Deployment}s for the configured modules
List<Deployment> deployments = new ArrayList<Deployment>();
BundleDeploymentPlugin depPlugin = bundleManager.getPlugin(BundleDeploymentPlugin.class);
for (OSGiModule module : injectedConfig.getValue().getModules()) {
ModuleIdentifier identifier = module.getIdentifier();
Deployment dep = depPlugin.createDeployment(identifier);
dep.setAutoStart(module.isStart());
deployments.add(dep);