throws MuleArtifactFactoryException
{
String flowName = "flow-" + Integer.toString(element.hashCode());
InputStream xmlConfig = IOUtils.toInputStream(getArtifactMuleConfig(flowName, element, callback, embedInFlow));
MuleContext muleContext = null;
SpringXmlConfigurationBuilder builder = null;
Map<String, String> environmentProperties = callback.getEnvironmentProperties();
Properties systemProperties = System.getProperties();
Map<Object, Object> originalSystemProperties = new HashMap<Object, Object>(systemProperties);
try
{
ConfigResource config = new ConfigResource("embedded-datasense.xml", xmlConfig);
// This configuration overrides the default-mule-config one to replace beans that are not required
ConfigResource defaultConfigOverride = new ConfigResource(getClass().getClassLoader().getResource("default-mule-config-override.xml").toURI().toURL());
if (environmentProperties != null)
{
systemProperties.putAll(environmentProperties);
}
MuleContextFactory factory = new DefaultMuleContextFactory();
builder = new SpringXmlConfigurationBuilder(new ConfigResource[] {config, defaultConfigOverride});
muleContext = factory.createMuleContext(builder);
muleContext.start();
MuleArtifact artifact = null;
if (embedInFlow)
{
Pipeline pipeline = muleContext.getRegistry().lookupObject(flowName);
if (pipeline.getMessageSource() == null)
{
if (pipeline.getMessageProcessors() != null && pipeline.getMessageProcessors().size() > 0)
{
artifact = new DefaultMuleArtifact(pipeline.getMessageProcessors().get(0));
}
else
{
throw new IllegalArgumentException("artifact is null");
}
}
else
{
artifact = new DefaultMuleArtifact(pipeline.getMessageSource());
}
}
else
{
artifact = new DefaultMuleArtifact(muleContext.getRegistry().lookupObject(element.getAttribute("name")));
}
builders.put(artifact, builder);
contexts.put(artifact, muleContext);
return artifact;