// First stop currently running broker (if any)
deleted(pid);
String config = (String)properties.get("config");
if (config == null) {
throw new ConfigurationException("config", "Property must be set");
}
String name = (String)properties.get("broker-name");
if (name == null) {
throw new ConfigurationException("broker-name", "Property must be set");
}
LOG.info("Starting broker " + name);
try {
Thread.currentThread().setContextClassLoader(BrokerService.class.getClassLoader());
Resource resource = Utils.resourceFromString(config);
ResourceXmlApplicationContext ctx = new ResourceXmlApplicationContext(resource, Collections.EMPTY_LIST, null, Collections.EMPTY_LIST, false) {
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
reader.setValidating(false);
}
};
// Handle properties in configuration
PropertyPlaceholderConfigurer configurator =
new PropertyPlaceholderConfigurer();
//convert dictionary to properties. Is there a better way?
Properties props = new Properties();
Enumeration elements = properties.keys();
while (elements.hasMoreElements()) {
Object key = elements.nextElement();
props.put(key, properties.get(key));
}
configurator.setProperties(props);
configurator.setIgnoreUnresolvablePlaceholders(true);
ctx.addBeanFactoryPostProcessor(configurator);
ctx.refresh();
// Start the broker
BrokerService broker = ctx.getBean(BrokerService.class);
if (broker == null) {
throw new ConfigurationException(null, "Broker not defined");
}
//TODO deal with multiple brokers
SpringBrokerContext brokerContext = new SpringBrokerContext();
brokerContext.setConfigurationUrl(resource.getURL().toExternalForm());
brokerContext.setApplicationContext(ctx);
broker.setBrokerContext(brokerContext);
broker.start();
broker.waitUntilStarted();
brokers.put(pid, broker);
} catch (Exception e) {
throw new ConfigurationException(null, "Cannot start the broker", e);
}
}