applicationContext.setClassLoader(classLoader);
// Note: before loading any beans in the spring container:
// * the spring build context needs access to the module, for possible injection & module-protocol resolving during bean initialization
// * the module also needs to have the reference to the applicationcontext, as there might be beans trying to get while initializing
ModuleImpl module = new ModuleImpl(classLoader, applicationContext, cfg.getDefinition(), cfg.getModuleSource());
SpringBuildContext springBuildContext = new SpringBuildContext(runtime, module, classLoader);
SPRING_BUILD_CONTEXT.set(springBuildContext);
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
xmlReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
xmlReader.setBeanClassLoader(classLoader);
for (ModuleSource.SpringConfigEntry entry : cfg.getModuleSource().getSpringConfigs(runtime.getMode())) {
InputStream is = entry.getStream();
try {
xmlReader.loadBeanDefinitions(new InputStreamResource(is, entry.getLocation() + " in " + cfg.getDefinition().getFile().getAbsolutePath()));
} finally {
IOUtils.closeQuietly(is, entry.getLocation());
}
}
applicationContext.refresh();
// Handle the service exports
for (SpringBuildContext.JavaServiceExport entry : springBuildContext.getExportedJavaServices()) {
Class serviceType = entry.serviceType;
if (!serviceType.isInterface()) {
throw new LilyRTException("Exported service is not an interface: " + serviceType.getName());
}
String beanName = entry.beanName;
Object component;
try {
component = applicationContext.getBean(beanName);
} catch (NoSuchBeanDefinitionException e) {
throw new LilyRTException("Bean not found for service to export, service type " + serviceType.getName() + ", bean name " + beanName, e);
}
if (!serviceType.isAssignableFrom(component.getClass())) {
throw new LilyRTException("Exported service does not implemented specified type interface. Bean = " + beanName + ", interface = " + serviceType.getName());
}
infolog.debug(" exporting bean " + beanName + " for service " + serviceType.getName());
Object service = shieldJavaService(serviceType, component, module, classLoader);
runtime.getJavaServiceManager().addService(serviceType, cfg.getId(), entry.name, service);
}
module.start();
return module;
} catch (Throwable e) {
// TODO module source and classloader handle might need disposing!
// especially important if the lily runtime is launched as part of a longer-living VM
throw new LilyRTException("Error constructing module defined at " + cfg.getDefinition().getFile().getAbsolutePath(), e);