public synchronized void initialize() throws MuleException {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
DefaultMuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
if (muleContext != null && muleContext.isStarted()) {
muleContext.stop();
muleContext.dispose();
}
while (muleContext != null) {
if (muleContext.isDisposed()) {
logger.warning("Mule already disposed!");
muleContext = null;
break;
}
logger.info("Waiting 1s for mule to stop and dispose itself");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
}
DefaultMuleConfiguration muleConfiguration = new
PropertiesMuleConfigurationFactory(PropertiesMuleConfigurationFactory.getMuleAppConfiguration(MuleServer.DEFAULT_CONFIGURATION))
.createConfiguration();
muleConfiguration.setId(""+Math.random()*System.currentTimeMillis());
muleConfiguration.setContainerMode(true);
muleConfiguration.setShutdownTimeout(3);
muleConfiguration.setWorkingDirectory(ProcessToolContext.Util.getHomePath() + File.separator + ".mule");
MuleContextBuilder muleContextBuilder = new DefaultMuleContextBuilder();
muleContextBuilder.setMuleConfiguration(muleConfiguration);
List<PluginConfiguration> configurationBuilders = new ArrayList<PluginConfiguration>(configMap.values());
List<ConfigurationBuilder> builders = new ArrayList<ConfigurationBuilder>();
List<ConfigResource> resources = new ArrayList<ConfigResource>();
for (PluginConfiguration c : configurationBuilders) {
InputStream is = c.getIs();
try {
is.reset();
resources.add(new ConfigResource(c.getName(), new ByteArrayInputStream(IOUtils.toByteArray(is))));
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
throw new RuntimeException(e);
}
}
try {
SpringXmlConfigurationBuilder builder = (SpringXmlConfigurationBuilder)
ClassUtils.instanciateClass(SpringXmlConfigurationBuilder.class.getName(),
new Object[]{resources.toArray(new ConfigResource[resources.size()])},
getClass().getClassLoader());
builders.add(builder);
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
throw new RuntimeException(e);
}
muleContext = muleContextFactory.createMuleContext(builders, muleContextBuilder);
muleContext.start();
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
}