use of org.mule.runtime.core.api.MuleContext in project mule by mulesoft.
the class DefaultApplicationPolicyInstanceTestCase method correctArtifactTypeForPolicies.
@Test
@Issue("MULE-14289")
@Ignore("MULE-14289: The discovered ArtifactConfigurationProcessor is not compatible with the provided mocks.")
public void correctArtifactTypeForPolicies() throws InitialisationException {
MuleContextListener muleContextListener = mock(MuleContextListener.class);
ArgumentCaptor<MuleContext> muleContextCaptor = ArgumentCaptor.forClass(MuleContext.class);
PolicyTemplate policyTemplate = mock(PolicyTemplate.class, RETURNS_DEEP_STUBS);
when(policyTemplate.getArtifactClassLoader().getClassLoader()).thenReturn(this.getClass().getClassLoader());
Application application = mock(Application.class, RETURNS_DEEP_STUBS);
Registry registry = application.getRegistry();
doReturn(of(mockContextWithServices())).when(registry).lookupByType(MuleContext.class);
doReturn(of(mock(ExtensionManager.class))).when(registry).lookupByName(OBJECT_EXTENSION_MANAGER);
PolicyParametrization parameterization = mock(PolicyParametrization.class, RETURNS_DEEP_STUBS);
when(parameterization.getId()).thenReturn("policyId");
DefaultApplicationPolicyInstance applicationPolicyInstance = new DefaultApplicationPolicyInstance(application, policyTemplate, parameterization, mock(ServiceRepository.class), mock(ClassLoaderRepository.class), emptyList(), mock(ExtensionModelLoaderRepository.class), muleContextListener);
applicationPolicyInstance.initialise();
verify(muleContextListener).onCreation(muleContextCaptor.capture());
assertThat(muleContextCaptor.getValue().getArtifactType(), is(POLICY));
}
use of org.mule.runtime.core.api.MuleContext in project mule by mulesoft.
the class ArtifactContextBuilder method build.
/**
* @return the {@code MuleContext} created with the provided configuration
* @throws ConfigurationException when there's a problem creating the {@code MuleContext}
* @throws InitialisationException when a certain configuration component failed during initialisation phase
*/
public ArtifactContext build() throws InitialisationException, ConfigurationException {
checkState(executionClassLoader != null, EXECUTION_CLASSLOADER_WAS_NOT_SET);
checkState(classLoaderRepository != null, CLASS_LOADER_REPOSITORY_WAS_NOT_SET);
checkState(POLICY.equals(artifactType) || APP.equals(artifactType) || parentArtifact == null, ONLY_APPLICATIONS_OR_POLICIES_ARE_ALLOWED_TO_HAVE_A_PARENT_ARTIFACT);
try {
return withContextClassLoader(executionClassLoader, () -> {
List<ConfigurationBuilder> builders = new LinkedList<>();
builders.addAll(additionalBuilders);
builders.add(new ArtifactBootstrapServiceDiscovererConfigurationBuilder(artifactPlugins));
if (extensionManagerFactory == null) {
if (parentArtifact == null) {
extensionManagerFactory = new ArtifactExtensionManagerFactory(artifactPlugins, extensionModelLoaderRepository, new DefaultExtensionManagerFactory());
} else {
extensionManagerFactory = new CompositeArtifactExtensionManagerFactory(parentArtifact, extensionModelLoaderRepository, artifactPlugins, new DefaultExtensionManagerFactory());
}
}
builders.add(new ArtifactExtensionManagerConfigurationBuilder(artifactPlugins, extensionManagerFactory));
builders.add(createConfigurationBuilderFromApplicationProperties());
// TODO MULE-14289 (elrodro83) pass this object to the builder instead of looking it up here
ArtifactConfigurationProcessor artifactConfigurationProcessor = ArtifactConfigurationProcessor.discover();
AtomicReference<ArtifactContext> artifactContext = new AtomicReference<>();
builders.add(new ConfigurationBuilder() {
@Override
public void configure(MuleContext muleContext) throws ConfigurationException {
if (serviceRepository != null) {
serviceConfigurators.add(new ContainerServicesMuleContextConfigurator(serviceRepository));
}
if (classLoaderRepository != null) {
serviceConfigurators.add(customizationService -> customizationService.registerCustomServiceImpl(OBJECT_CLASSLOADER_REPOSITORY, classLoaderRepository));
}
if (policyProvider != null) {
serviceConfigurators.add(customizationService -> customizationService.registerCustomServiceImpl(OBJECT_POLICY_PROVIDER, policyProvider));
}
ArtifactContextConfiguration.ArtifactContextConfigurationBuilder artifactContextConfigurationBuilder = ArtifactContextConfiguration.builder().setMuleContext(muleContext).setConfigResources(configurationFiles).setArtifactDeclaration(artifactDeclaration).setArtifactProperties(merge(artifactProperties, muleContext.getDeploymentProperties())).setArtifactType(artifactType).setEnableLazyInitialization(enableLazyInit).setDisableXmlValidations(disableXmlValidations).setServiceConfigurators(serviceConfigurators);
if (parentArtifact != null) {
artifactContextConfigurationBuilder.setParentContext(parentArtifact.getRegistry().lookupByType(MuleContext.class).get());
}
artifactContext.set(artifactConfigurationProcessor.createArtifactContext(artifactContextConfigurationBuilder.build()));
((DefaultMuleConfiguration) muleContext.getConfiguration()).setDataFolderName(dataFolderName);
}
@Override
public void addServiceConfigurator(ServiceConfigurator serviceConfigurator) {
// Nothing to do
}
});
DefaultMuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
if (muleContextListener != null) {
muleContextFactory.addListener(muleContextListener);
}
if (APP.equals(artifactType)) {
muleContextBuilder = new ApplicationMuleContextBuilder(artifactName, artifactProperties, defaultEncoding);
} else if (POLICY.equals(artifactType)) {
muleContextBuilder = new PolicyMuleContextBuilder(artifactName, artifactProperties, defaultEncoding);
} else {
muleContextBuilder = new DomainMuleContextBuilder(artifactName);
}
muleContextBuilder.setExecutionClassLoader(this.executionClassLoader);
ArtifactObjectSerializer objectSerializer = new ArtifactObjectSerializer(classLoaderRepository);
muleContextBuilder.setObjectSerializer(objectSerializer);
muleContextBuilder.setDeploymentProperties(properties);
if (parentArtifact != null) {
builders.add(new ConnectionManagerConfigurationBuilder(parentArtifact));
muleContextBuilder.setErrorTypeRepository(createCompositeErrorTypeRepository(parentArtifact.getRegistry().lookupByType(MuleContext.class).get().getErrorTypeRepository()));
} else {
builders.add(new ConnectionManagerConfigurationBuilder());
}
try {
muleContextFactory.createMuleContext(builders, muleContextBuilder);
return artifactContext.get();
} catch (InitialisationException e) {
throw new ConfigurationException(e);
}
});
} catch (MuleRuntimeException e) {
// We need this exception to be thrown as they are since the are possible causes of connectivity errors
if (e.getCause() instanceof InitialisationException) {
throw (InitialisationException) e.getCause();
}
if (e.getCause() instanceof ConfigurationException) {
throw (ConfigurationException) e.getCause();
}
throw e;
}
}
use of org.mule.runtime.core.api.MuleContext in project mule by mulesoft.
the class DefaultMuleApplicationStatusTestCase method deploymentFailedOnStart.
@Test
public void deploymentFailedOnStart() throws Exception {
MuleContext mockedMuleContext = mock(MuleContext.class);
when(mockArtifactContext.getMuleContext()).thenReturn(mockedMuleContext);
mockedMuleContext.start();
doThrow(new MuleRuntimeException(createStaticMessage("error")));
try {
application.start();
fail("Was expecting start to fail");
} catch (Exception e) {
assertStatus(ApplicationStatus.DEPLOYMENT_FAILED);
}
}
use of org.mule.runtime.core.api.MuleContext in project mule by mulesoft.
the class CompositeArtifactExtensionManagerFactoryTestCase method createsCompositeExtensionManager.
@Test
public void createsCompositeExtensionManager() throws Exception {
Application application = mock(Application.class, RETURNS_DEEP_STUBS);
ExtensionManager applicationExtensionManager = mock(ExtensionManager.class);
when(application.getRegistry().lookupByName(MuleProperties.OBJECT_EXTENSION_MANAGER)).thenReturn(of(applicationExtensionManager));
ExtensionModelLoaderRepository extensionModelLoaderRepository = mock(ExtensionModelLoaderRepository.class);
ExtensionManagerFactory extensionManagerFactory = mock(ExtensionManagerFactory.class);
CompositeArtifactExtensionManagerFactory factory = new CompositeArtifactExtensionManagerFactory(application, extensionModelLoaderRepository, emptyList(), extensionManagerFactory);
ExtensionManager policyExtensionManager = mock(ExtensionManager.class);
MuleContext muleContext = mock(MuleContext.class);
when(extensionManagerFactory.create(muleContext)).thenReturn(policyExtensionManager);
ExtensionManager extensionManager = factory.create(muleContext);
assertThat(extensionManager, instanceOf(CompositeArtifactExtensionManager.class));
CompositeArtifactExtensionManager compositeArtifactExtensionManager = (CompositeArtifactExtensionManager) extensionManager;
assertThat(compositeArtifactExtensionManager.getParentExtensionManager(), equalTo(applicationExtensionManager));
assertThat(compositeArtifactExtensionManager.getChildExtensionManager(), equalTo(policyExtensionManager));
}
use of org.mule.runtime.core.api.MuleContext in project mule by mulesoft.
the class ConfigurationProviderObjectFactory method createInnerInstance.
private ConfigurationProvider createInnerInstance() throws ConfigurationException {
if (expirationPolicy == null) {
expirationPolicy = muleContext.getConfiguration().getDynamicConfigExpiration().getExpirationPolicy();
}
ResolverSet resolverSet = getParametersResolver().getParametersAsHashedResolverSet(configurationModel, muleContext);
final ConnectionProviderValueResolver connectionProviderResolver = getConnectionProviderResolver();
return withContextClassLoader(getExtensionClassLoader(), () -> {
connectionProviderResolver.getResolverSet().ifPresent((CheckedConsumer) resolver -> initialiseIfNeeded(resolver, true, muleContext));
ConfigurationProvider configurationProvider;
try {
if (resolverSet.isDynamic() || connectionProviderResolver.isDynamic()) {
configurationProvider = configurationProviderFactory.createDynamicConfigurationProvider(name, extensionModel, configurationModel, resolverSet, connectionProviderResolver, expirationPolicy, reflectionCache, muleContext);
} else {
configurationProvider = configurationProviderFactory.createStaticConfigurationProvider(name, extensionModel, configurationModel, resolverSet, connectionProviderResolver, reflectionCache, muleContext);
}
} catch (Exception e) {
throw new MuleRuntimeException(createStaticMessage(format("Could not create an implicit configuration '%s' for the extension '%s'", configurationModel.getName(), extensionModel.getName())), e);
}
return configurationProvider;
});
}
Aggregations