in services/minho-osgi/src/main/java/org/apache/karaf/minho/osgi/OsgiModuleManagerService.java [71:154]
public void onRegister(final ServiceRegistry serviceRegistry) throws Exception {
log.info("Starting OSGi module manager service");
log.info("Creating OSGi framework runtime");
Map<String, Object> frameworkConfig = new HashMap<>();
// looking for service properties
final Map<String, Object> properties = serviceRegistry.get(ConfigService.class).getProperties().entrySet()
.stream().filter(entry -> entry.getKey().startsWith(PREFIX))
.collect(toMap(entry -> entry.getKey().substring(PREFIX.length()), Map.Entry::getValue));
// cache
String cache;
if (properties.get(STORAGE_PROPERTY) != null) {
cache = properties.get(STORAGE_PROPERTY).toString();
} else {
cache = "./osgi";
}
log.info("OSGi framework storage: " + cache);
frameworkConfig.put(Constants.FRAMEWORK_STORAGE, cache);
// clear cache
if (properties.get(CLEAR_CACHE_PROPERTY) != null) {
frameworkConfig.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
}
// start level
if (properties.get(START_LEVEL_PROPERTY) != null) {
frameworkConfig.put(Constants.FRAMEWORK_BEGINNING_STARTLEVEL, properties.get(START_LEVEL_PROPERTY));
} else {
frameworkConfig.put(Constants.FRAMEWORK_BEGINNING_STARTLEVEL, "100");
}
// bundle start level
int bundleStartLevel = 80;
if (properties.get(BUNDLE_START_LEVEL_PROPERTY) != null) {
bundleStartLevel = (int) properties.get(BUNDLE_START_LEVEL_PROPERTY);
}
// log level
if (properties.get(LOG_LEVEL_PROPERTY) != null) {
frameworkConfig.put(FelixConstants.LOG_LEVEL_PROP, properties.get(LOG_LEVEL_PROPERTY));
} else {
frameworkConfig.put(FelixConstants.LOG_LEVEL_PROP, "3");
}
// cache
String cacheRootDir;
if (properties.get(CACHE_PROPERTY) != null) {
cacheRootDir = properties.get(CACHE_PROPERTY).toString();
} else {
cacheRootDir = "./osgi/bundles";
}
log.info("OSGi bundles cache: " + cacheRootDir);
frameworkConfig.put(BundleCache.CACHE_ROOTDIR_PROP, cacheRootDir);
FrameworkFactory frameworkFactory = new FrameworkFactory();
framework = frameworkFactory.newFramework(frameworkConfig);
framework.init();
framework.start();
FrameworkStartLevel frameworkStartLevel = framework.adapt(FrameworkStartLevel.class);
frameworkStartLevel.setInitialBundleStartLevel(bundleStartLevel);
log.info("Registering service into lifecycle service");
LifeCycleService lifeCycleService = serviceRegistry.get(LifeCycleService.class);
lifeCycleService.onStart(() -> {
serviceRegistry.get(Config.class).getApplications().forEach(application -> {
try {
if (application.getType() == null && canHandle(application.getUrl())) {
store.put(start(application.getUrl()), application.getUrl());
} else if (application.getType().equals(name())) {
store.put(start(application.getUrl()), application.getUrl());
}
} catch (Exception e) {
throw new RuntimeException("Can't start OSGi module " + application.getUrl(), e);
}
});
});
lifeCycleService.onShutdown(() -> {
store.keySet().forEach(id -> {
try {
stop(id);
} catch (Exception e) {
throw new RuntimeException("Can't stop OSGi module " + id, e);
}
});
});
}