Search in sources :

Example 1 with DefaultMuleContextFactory

use of org.mule.runtime.core.api.context.DefaultMuleContextFactory in project mule by mulesoft.

the class QueueManagerLifecycleOrderTestCase method before.

@Before
public void before() throws InitialisationException, ConfigurationException {
    Map<String, Object> objects = new HashMap<>();
    objects.put(OBJECT_QUEUE_MANAGER, rtqm);
    objects.put(OBJECT_SECURITY_MANAGER, new DefaultMuleSecurityManager());
    objects.put(PROCESSOR_INTERCEPTOR_MANAGER_REGISTRY_KEY, mock(ProcessorInterceptorManager.class));
    muleContext = new DefaultMuleContextFactory().createMuleContext(testServicesConfigurationBuilder, new SimpleConfigurationBuilder(objects));
    testServicesConfigurationBuilder.configure(muleContext);
}
Also used : DefaultMuleContextFactory(org.mule.runtime.core.api.context.DefaultMuleContextFactory) DefaultMuleSecurityManager(org.mule.runtime.core.internal.security.DefaultMuleSecurityManager) HashMap(java.util.HashMap) ProcessorInterceptorManager(org.mule.runtime.core.internal.interception.ProcessorInterceptorManager) SimpleConfigurationBuilder(org.mule.runtime.core.api.config.builders.SimpleConfigurationBuilder) Before(org.junit.Before)

Example 2 with DefaultMuleContextFactory

use of org.mule.runtime.core.api.context.DefaultMuleContextFactory in project mule by mulesoft.

the class DomainContextBuilder method build.

public MuleContext build() throws Exception {
    List<ConfigurationBuilder> builders = new ArrayList<>(3);
    ConfigurationBuilder cfgBuilder = getDomainBuilder(domainConfig);
    builders.add(cfgBuilder);
    addBuilders(builders);
    DefaultMuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
    MuleContext domainContext = muleContextFactory.createMuleContext(builders, muleContextBuilder);
    domainContext.start();
    return domainContext;
}
Also used : SpringXmlConfigurationBuilderFactory.createConfigurationBuilder(org.mule.runtime.config.api.SpringXmlConfigurationBuilderFactory.createConfigurationBuilder) ConfigurationBuilder(org.mule.runtime.core.api.config.ConfigurationBuilder) TestServicesConfigurationBuilder(org.mule.tck.config.TestServicesConfigurationBuilder) DefaultMuleContextFactory(org.mule.runtime.core.api.context.DefaultMuleContextFactory) MuleContext(org.mule.runtime.core.api.MuleContext) ArrayList(java.util.ArrayList)

Example 3 with DefaultMuleContextFactory

use of org.mule.runtime.core.api.context.DefaultMuleContextFactory in project mule by mulesoft.

the class AbstractBenchmark method createMuleContextWithServices.

protected MuleContext createMuleContextWithServices() throws MuleException {
    MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
    List<ConfigurationBuilder> builderList = new ArrayList<>();
    builderList.add(new BasicRuntimeServicesConfigurationBuilder());
    builderList.add(new DefaultsConfigurationBuilder());
    return muleContextFactory.createMuleContext(builderList.toArray(new ConfigurationBuilder[] {}));
}
Also used : DefaultMuleContextFactory(org.mule.runtime.core.api.context.DefaultMuleContextFactory) DefaultsConfigurationBuilder(org.mule.runtime.core.internal.config.builders.DefaultsConfigurationBuilder) ConfigurationBuilder(org.mule.runtime.core.api.config.ConfigurationBuilder) MuleContextFactory(org.mule.runtime.core.api.context.MuleContextFactory) DefaultMuleContextFactory(org.mule.runtime.core.api.context.DefaultMuleContextFactory) ArrayList(java.util.ArrayList) DefaultsConfigurationBuilder(org.mule.runtime.core.internal.config.builders.DefaultsConfigurationBuilder)

Example 4 with DefaultMuleContextFactory

use of org.mule.runtime.core.api.context.DefaultMuleContextFactory in project mule by mulesoft.

the class AbstractMuleContextTestCase method createMuleContext.

protected MuleContext createMuleContext() throws Exception {
    // Should we set up the manager for every method?
    MuleContext context;
    if (isDisposeContextPerClass() && muleContext != null) {
        context = muleContext;
    } else {
        final ClassLoader executionClassLoader = getExecutionClassLoader();
        final ClassLoader originalContextClassLoader = currentThread().getContextClassLoader();
        try {
            currentThread().setContextClassLoader(executionClassLoader);
            MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
            List<ConfigurationBuilder> builders = new ArrayList<>();
            builders.add(new SimpleConfigurationBuilder(getStartUpRegistryObjects()));
            addBuilders(builders);
            builders.add(getBuilder());
            MuleContextBuilder contextBuilder = MuleContextBuilder.builder(APP);
            DefaultMuleConfiguration muleConfiguration = new DefaultMuleConfiguration();
            String workingDirectory = this.workingDirectory.getRoot().getAbsolutePath();
            LOGGER.info("Using working directory for test: " + workingDirectory);
            muleConfiguration.setWorkingDirectory(workingDirectory);
            muleConfiguration.setId(this.getClass().getSimpleName() + "#" + name.getMethodName());
            contextBuilder.setMuleConfiguration(muleConfiguration);
            contextBuilder.setExecutionClassLoader(executionClassLoader);
            contextBuilder.setObjectSerializer(getObjectSerializer());
            contextBuilder.setDeploymentProperties(getDeploymentProperties());
            configureMuleContext(contextBuilder);
            context = muleContextFactory.createMuleContext(builders, contextBuilder);
            recordSchedulersOnInit(context);
            if (!isGracefulShutdown()) {
                // Even though graceful shutdown is disabled allow small amount of time to avoid rejection errors when stream emits
                // complete signal
                ((DefaultMuleConfiguration) context.getConfiguration()).setShutdownTimeout(NON_GRACEFUL_SHUTDOWN_TIMEOUT);
            }
        } finally {
            currentThread().setContextClassLoader(originalContextClassLoader);
        }
    }
    return context;
}
Also used : MuleContext(org.mule.runtime.core.api.MuleContext) DefaultMuleContextFactory(org.mule.runtime.core.api.context.DefaultMuleContextFactory) SimpleConfigurationBuilder(org.mule.runtime.core.api.config.builders.SimpleConfigurationBuilder) DefaultsConfigurationBuilder(org.mule.runtime.core.internal.config.builders.DefaultsConfigurationBuilder) ConfigurationBuilder(org.mule.runtime.core.api.config.ConfigurationBuilder) TestServicesConfigurationBuilder(org.mule.tck.config.TestServicesConfigurationBuilder) MuleContextFactory(org.mule.runtime.core.api.context.MuleContextFactory) DefaultMuleContextFactory(org.mule.runtime.core.api.context.DefaultMuleContextFactory) DefaultMuleConfiguration(org.mule.runtime.core.api.config.DefaultMuleConfiguration) ArrayList(java.util.ArrayList) SimpleConfigurationBuilder(org.mule.runtime.core.api.config.builders.SimpleConfigurationBuilder) MuleContextBuilder(org.mule.runtime.core.api.context.MuleContextBuilder)

Example 5 with DefaultMuleContextFactory

use of org.mule.runtime.core.api.context.DefaultMuleContextFactory 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;
    }
}
Also used : ServiceConfigurator(org.mule.runtime.api.config.custom.ServiceConfigurator) APP_HOME_DIRECTORY_PROPERTY(org.mule.runtime.core.api.config.MuleProperties.APP_HOME_DIRECTORY_PROPERTY) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) DomainMuleContextBuilder(org.mule.runtime.module.deployment.impl.internal.domain.DomainMuleContextBuilder) ArtifactContextConfiguration(org.mule.runtime.deployment.model.api.artifact.ArtifactContextConfiguration) OBJECT_CLASSLOADER_REPOSITORY(org.mule.runtime.core.api.config.MuleProperties.OBJECT_CLASSLOADER_REPOSITORY) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) DefaultExtensionManagerFactory(org.mule.runtime.module.extension.api.manager.DefaultExtensionManagerFactory) Preconditions.checkState(org.mule.runtime.api.util.Preconditions.checkState) ErrorTypeRepositoryFactory.createCompositeErrorTypeRepository(org.mule.runtime.core.internal.exception.ErrorTypeRepositoryFactory.createCompositeErrorTypeRepository) ApplicationMuleContextBuilder(org.mule.runtime.module.deployment.impl.internal.application.ApplicationMuleContextBuilder) POLICY(org.mule.runtime.core.api.config.bootstrap.ArtifactType.POLICY) Collections.emptyList(java.util.Collections.emptyList) ArtifactPlugin(org.mule.runtime.deployment.model.api.plugin.ArtifactPlugin) PolicyMuleContextBuilder(org.mule.runtime.module.deployment.impl.internal.application.PolicyMuleContextBuilder) DefaultMuleConfiguration(org.mule.runtime.core.api.config.DefaultMuleConfiguration) DefaultMuleContextFactory(org.mule.runtime.core.api.context.DefaultMuleContextFactory) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) List(java.util.List) Domain(org.mule.runtime.deployment.model.api.domain.Domain) ArtifactConfigurationProcessor(org.mule.runtime.deployment.model.api.artifact.ArtifactConfigurationProcessor) APP(org.mule.runtime.core.api.config.bootstrap.ArtifactType.APP) Optional(java.util.Optional) OBJECT_POLICY_PROVIDER(org.mule.runtime.core.api.config.MuleProperties.OBJECT_POLICY_PROVIDER) ArtifactExtensionManagerFactory(org.mule.runtime.module.deployment.impl.internal.policy.ArtifactExtensionManagerFactory) ServiceRepository(org.mule.runtime.api.service.ServiceRepository) SimpleConfigurationBuilder(org.mule.runtime.core.api.config.builders.SimpleConfigurationBuilder) Optional.empty(java.util.Optional.empty) ClassLoaderRepository(org.mule.runtime.module.artifact.api.classloader.ClassLoaderRepository) ExtensionModelLoaderRepository(org.mule.runtime.module.extension.internal.loader.ExtensionModelLoaderRepository) ArtifactContext(org.mule.runtime.deployment.model.api.artifact.ArtifactContext) MuleContextListener(org.mule.runtime.core.api.context.notification.MuleContextListener) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) HashMap(java.util.HashMap) APP_NAME_PROPERTY(org.mule.runtime.core.api.config.MuleProperties.APP_NAME_PROPERTY) Location(org.mule.runtime.api.component.location.Location) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) UUID.getUUID(org.mule.runtime.core.api.util.UUID.getUUID) ArtifactObjectSerializer(org.mule.runtime.module.artifact.api.serializer.ArtifactObjectSerializer) MuleContext(org.mule.runtime.core.api.MuleContext) ArtifactDeclaration(org.mule.runtime.app.declaration.api.ArtifactDeclaration) ConfigurationBuilder(org.mule.runtime.core.api.config.ConfigurationBuilder) ClassUtils.withContextClassLoader(org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader) LinkedList(java.util.LinkedList) Properties(java.util.Properties) ConfigurationException(org.mule.runtime.core.api.config.ConfigurationException) File(java.io.File) MuleContextBuilder(org.mule.runtime.core.api.context.MuleContextBuilder) ConnectivityTestingService(org.mule.runtime.api.connectivity.ConnectivityTestingService) PolicyProvider(org.mule.runtime.core.api.policy.PolicyProvider) DeployableArtifact(org.mule.runtime.deployment.model.api.DeployableArtifact) ArtifactType(org.mule.runtime.core.api.config.bootstrap.ArtifactType) ExtensionManagerFactory(org.mule.runtime.module.extension.api.manager.ExtensionManagerFactory) Collections(java.util.Collections) SimpleConfigurationBuilder(org.mule.runtime.core.api.config.builders.SimpleConfigurationBuilder) ConfigurationBuilder(org.mule.runtime.core.api.config.ConfigurationBuilder) ArtifactExtensionManagerFactory(org.mule.runtime.module.deployment.impl.internal.policy.ArtifactExtensionManagerFactory) ArtifactContext(org.mule.runtime.deployment.model.api.artifact.ArtifactContext) ArtifactObjectSerializer(org.mule.runtime.module.artifact.api.serializer.ArtifactObjectSerializer) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MuleContext(org.mule.runtime.core.api.MuleContext) ConfigurationException(org.mule.runtime.core.api.config.ConfigurationException) ArtifactConfigurationProcessor(org.mule.runtime.deployment.model.api.artifact.ArtifactConfigurationProcessor) DomainMuleContextBuilder(org.mule.runtime.module.deployment.impl.internal.domain.DomainMuleContextBuilder) DefaultExtensionManagerFactory(org.mule.runtime.module.extension.api.manager.DefaultExtensionManagerFactory) PolicyMuleContextBuilder(org.mule.runtime.module.deployment.impl.internal.application.PolicyMuleContextBuilder) ServiceConfigurator(org.mule.runtime.api.config.custom.ServiceConfigurator) AtomicReference(java.util.concurrent.atomic.AtomicReference) ApplicationMuleContextBuilder(org.mule.runtime.module.deployment.impl.internal.application.ApplicationMuleContextBuilder) LinkedList(java.util.LinkedList) DefaultMuleContextFactory(org.mule.runtime.core.api.context.DefaultMuleContextFactory) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Aggregations

DefaultMuleContextFactory (org.mule.runtime.core.api.context.DefaultMuleContextFactory)8 ArrayList (java.util.ArrayList)5 ConfigurationBuilder (org.mule.runtime.core.api.config.ConfigurationBuilder)5 MuleContext (org.mule.runtime.core.api.MuleContext)4 HashMap (java.util.HashMap)2 Before (org.junit.Before)2 SpringXmlConfigurationBuilderFactory.createConfigurationBuilder (org.mule.runtime.config.api.SpringXmlConfigurationBuilderFactory.createConfigurationBuilder)2 DefaultMuleConfiguration (org.mule.runtime.core.api.config.DefaultMuleConfiguration)2 SimpleConfigurationBuilder (org.mule.runtime.core.api.config.builders.SimpleConfigurationBuilder)2 MuleContextFactory (org.mule.runtime.core.api.context.MuleContextFactory)2 DefaultsConfigurationBuilder (org.mule.runtime.core.internal.config.builders.DefaultsConfigurationBuilder)2 TestServicesConfigurationBuilder (org.mule.tck.config.TestServicesConfigurationBuilder)2 File (java.io.File)1 Arrays.asList (java.util.Arrays.asList)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1