Search in sources :

Example 26 with ArtifactClassLoader

use of org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader in project mule by mulesoft.

the class MuleServiceManagerTestCase method stopsServicesAfterStartFail.

@Test
public void stopsServicesAfterStartFail() throws Exception {
    StartableStoppableService firstService = mock(StartableStoppableService.class);
    StartableStoppableService secondService = mock(StartableStoppableService.class);
    Exception firstStartFailure = new RuntimeException();
    doThrow(firstStartFailure).when(firstService).start();
    final ServiceDiscoverer serviceDiscoverer = mock(ServiceDiscoverer.class);
    final List<Pair<ArtifactClassLoader, Service>> services = new ArrayList<>();
    Pair<ArtifactClassLoader, Service> service1Pairs = new Pair(mock(ArtifactClassLoader.class), firstService);
    Pair<ArtifactClassLoader, Service> service2Pairs = new Pair(mock(ArtifactClassLoader.class), secondService);
    services.add(service1Pairs);
    services.add(service2Pairs);
    when(serviceDiscoverer.discoverServices()).thenReturn(services);
    final MuleServiceManager muleServiceManager = new MuleServiceManager(serviceDiscoverer);
    try {
        muleServiceManager.start();
        fail();
    } catch (Exception e) {
        assertThat(e.getCause(), sameInstance(firstStartFailure));
    }
    muleServiceManager.stop();
    verify(secondService, never()).stop();
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ArrayList(java.util.ArrayList) Service(org.mule.runtime.api.service.Service) ServiceDiscoverer(org.mule.runtime.module.service.api.discoverer.ServiceDiscoverer) Pair(org.mule.runtime.api.util.Pair) Test(org.junit.Test)

Example 27 with ArtifactClassLoader

use of org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader in project mule by mulesoft.

the class MuleServiceManagerTestCase method startServices.

@Test
public void startServices() throws Exception {
    final ServiceDiscoverer serviceDiscoverer = mock(ServiceDiscoverer.class);
    final List<Pair<ArtifactClassLoader, Service>> services = new ArrayList<>();
    Pair<ArtifactClassLoader, Service> service1 = new Pair(mock(ArtifactClassLoader.class), mock(StartableService.class));
    Pair<ArtifactClassLoader, Service> service2 = new Pair(mock(ArtifactClassLoader.class), mock(StartableService.class));
    services.add(service1);
    services.add(service2);
    when(serviceDiscoverer.discoverServices()).thenReturn(services);
    final MuleServiceManager muleServiceManager = new MuleServiceManager(serviceDiscoverer);
    muleServiceManager.start();
    InOrder inOrder = inOrder(service1.getSecond(), service2.getSecond());
    inOrder.verify((StartableService) service1.getSecond()).start();
    inOrder.verify((StartableService) service2.getSecond()).start();
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) InOrder(org.mockito.InOrder) ArrayList(java.util.ArrayList) Service(org.mule.runtime.api.service.Service) ServiceDiscoverer(org.mule.runtime.module.service.api.discoverer.ServiceDiscoverer) Pair(org.mule.runtime.api.util.Pair) Test(org.junit.Test)

Example 28 with ArtifactClassLoader

use of org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader in project mule by mulesoft.

the class TestBootstrapServiceDiscovererConfigurationBuilder method getArtifactBootstrapService.

private List<BootstrapService> getArtifactBootstrapService(Object artifactClassLoader) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException {
    // Uses reflection to access the class loader as classes were loaded from different class loaders so they cannot be casted
    ClassLoader classLoader = (ClassLoader) artifactClassLoader.getClass().getMethod("getClassLoader").invoke(artifactClassLoader);
    final Enumeration<URL> resources = (Enumeration<URL>) classLoader.getClass().getMethod("findResources", String.class).invoke(classLoader, BOOTSTRAP_PROPERTIES);
    final List<BootstrapService> bootstrapServices = new ArrayList<>();
    if (resources.hasMoreElements()) {
        while (resources.hasMoreElements()) {
            final URL localResource = resources.nextElement();
            final Properties properties = PropertiesUtils.loadProperties(localResource);
            final PropertiesBootstrapService propertiesBootstrapService = new PropertiesBootstrapService(classLoader, properties);
            bootstrapServices.add(propertiesBootstrapService);
        }
    }
    return bootstrapServices;
}
Also used : PropertiesBootstrapService(org.mule.runtime.core.api.config.bootstrap.PropertiesBootstrapService) Enumeration(java.util.Enumeration) ArrayList(java.util.ArrayList) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) Properties(java.util.Properties) URL(java.net.URL) BootstrapService(org.mule.runtime.core.api.config.bootstrap.BootstrapService) PropertiesBootstrapService(org.mule.runtime.core.api.config.bootstrap.PropertiesBootstrapService)

Example 29 with ArtifactClassLoader

use of org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader in project mule by mulesoft.

the class ArtifactClassLoaderRunner method injectPluginsClassLoaders.

/**
 * Invokes the method to inject the plugin class loaders as the test is annotated with {@link PluginClassLoadersAware}.
 *
 * @param artifactClassLoaderHolder the result {@link ArtifactClassLoader}s defined for container, plugins and application
 * @param isolatedTestClass the test {@link Class} loaded with the isolated {@link ClassLoader}
 * @throws IllegalStateException if the test doesn't have an annotated method to inject plugin class loaders or if it has more
 *         than one method annotated.
 * @throws Throwable if an error ocurrs while setting the list of {@link ArtifactClassLoader}s for plugins.
 */
private static void injectPluginsClassLoaders(ArtifactClassLoaderHolder artifactClassLoaderHolder, Class<?> isolatedTestClass) throws Throwable {
    final Class<PluginClassLoadersAware> pluginClassLoadersAwareClass = PluginClassLoadersAware.class;
    final String expectedParamType = "List<" + ArtifactClassLoader.class + ">";
    final FrameworkMethod method = getAnnotatedMethod(artifactClassLoaderHolder, isolatedTestClass, pluginClassLoadersAwareClass, expectedParamType);
    final Object valueToInject = artifactClassLoaderHolder.getPluginsClassLoaders();
    doFieldInjection(pluginClassLoadersAwareClass, method, valueToInject, expectedParamType);
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) FrameworkMethod(org.junit.runners.model.FrameworkMethod)

Example 30 with ArtifactClassLoader

use of org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader in project mule by mulesoft.

the class IsolatedClassLoaderFactory method createTestRunnerPlugin.

private void createTestRunnerPlugin(ArtifactsUrlClassification artifactsUrlClassification, Map<String, LookupStrategy> appExportedLookupStrategies, ClassLoaderLookupPolicy childClassLoaderLookupPolicy, RegionClassLoader regionClassLoader, List<ArtifactClassLoader> filteredPluginsArtifactClassLoaders, List<ArtifactClassLoader> pluginsArtifactClassLoaders, List<ArtifactClassLoaderFilter> pluginArtifactClassLoaderFilters, DefaultModuleRepository moduleRepository, TestContainerClassLoaderFactory testContainerClassLoaderFactory, Set<String> parentExportedPackages) {
    JarInfo testRunnerJarInfo = getTestRunnerJarInfo(artifactsUrlClassification);
    String testRunnerArtifactId = getArtifactPluginId(regionClassLoader.getArtifactId(), "test-runner");
    List<String> pluginDependencies = artifactsUrlClassification.getPluginUrlClassifications().stream().map(p -> p.getName()).collect(toList());
    PluginUrlClassification testRunnerPluginClassification = new PluginUrlClassification(TEST_RUNNER_ARTIFACT_ID + ":", artifactsUrlClassification.getTestRunnerLibUrls(), emptyList(), pluginDependencies, testRunnerJarInfo.getPackages(), testRunnerJarInfo.getResources(), emptySet(), emptySet());
    ClassLoaderLookupPolicy pluginLookupPolicy = extendLookupPolicyForPrivilegedAccess(childClassLoaderLookupPolicy, moduleRepository, testContainerClassLoaderFactory, testRunnerPluginClassification);
    pluginLookupPolicy = pluginLookupPolicy.extend(appExportedLookupStrategies);
    MuleArtifactClassLoader pluginCL = new MuleArtifactClassLoader(testRunnerArtifactId, new ArtifactDescriptor(testRunnerPluginClassification.getName()), testRunnerPluginClassification.getUrls().toArray(new URL[0]), regionClassLoader, pluginLookupPolicyGenerator.createLookupPolicy(testRunnerPluginClassification, artifactsUrlClassification.getPluginUrlClassifications(), pluginLookupPolicy, pluginsArtifactClassLoaders));
    pluginsArtifactClassLoaders.add(pluginCL);
    ArtifactClassLoaderFilter filter = createArtifactClassLoaderFilter(testRunnerPluginClassification, parentExportedPackages, childClassLoaderLookupPolicy);
    pluginArtifactClassLoaderFilters.add(filter);
    filteredPluginsArtifactClassLoaders.add(new FilteringArtifactClassLoader(pluginCL, filter, emptyList()));
    logClassLoaderUrls("PLUGIN (" + testRunnerPluginClassification.getName() + ")", testRunnerPluginClassification.getUrls());
}
Also used : TEST_RUNNER_ARTIFACT_ID(org.mule.test.runner.RunnerConfiguration.TEST_RUNNER_ARTIFACT_ID) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) PARENT_FIRST(org.mule.runtime.module.artifact.api.classloader.ParentFirstLookupStrategy.PARENT_FIRST) MuleModule(org.mule.runtime.container.api.MuleModule) DefaultArtifactClassLoaderFilter(org.mule.runtime.module.artifact.api.classloader.DefaultArtifactClassLoaderFilter) FileUtils.toFile(org.apache.commons.io.FileUtils.toFile) ContainerClassLoaderFilterFactory(org.mule.runtime.container.internal.ContainerClassLoaderFilterFactory) FilteringArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.FilteringArtifactClassLoader) ClassLoaderFilterFactory(org.mule.runtime.module.artifact.api.classloader.ClassLoaderFilterFactory) Map(java.util.Map) System.getProperty(java.lang.System.getProperty) JarExplorer(org.mule.runtime.module.artifact.internal.util.JarExplorer) URI(java.net.URI) MULE_LOG_VERBOSE_CLASSLOADING(org.mule.runtime.core.api.config.MuleProperties.MULE_LOG_VERBOSE_CLASSLOADING) Collectors.toSet(java.util.stream.Collectors.toSet) ArtifactClassLoaderHolder(org.mule.test.runner.api.ArtifactClassLoaderHolder) ClassLoaderFilter(org.mule.runtime.module.artifact.api.classloader.ClassLoaderFilter) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) Collections.emptyList(java.util.Collections.emptyList) RegionClassLoader(org.mule.runtime.module.artifact.api.classloader.RegionClassLoader) Collection(java.util.Collection) Set(java.util.Set) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) LookupStrategy(org.mule.runtime.module.artifact.api.classloader.LookupStrategy) DefaultModuleRepository(org.mule.runtime.container.internal.DefaultModuleRepository) Collectors.joining(java.util.stream.Collectors.joining) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) JarInfo(org.mule.runtime.module.artifact.internal.util.JarInfo) ChildFirstLookupStrategy(org.mule.runtime.module.artifact.api.classloader.ChildFirstLookupStrategy) ModuleRepository(org.mule.runtime.container.api.ModuleRepository) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) FileJarExplorer(org.mule.runtime.module.artifact.internal.util.FileJarExplorer) DefaultNativeLibraryFinderFactory(org.mule.runtime.deployment.model.internal.nativelib.DefaultNativeLibraryFinderFactory) ArtifactUrlClassification(org.mule.test.runner.api.ArtifactUrlClassification) ContainerClassLoaderFactory(org.mule.runtime.container.internal.ContainerClassLoaderFactory) HashMap(java.util.HashMap) MuleClassLoaderLookupPolicy(org.mule.runtime.container.internal.MuleClassLoaderLookupPolicy) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PluginUrlClassification(org.mule.test.runner.api.PluginUrlClassification) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) ContainerOnlyLookupStrategy(org.mule.runtime.container.internal.ContainerOnlyLookupStrategy) ArtifactDescriptor(org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor) Collections.emptyMap(java.util.Collections.emptyMap) SYSTEM_PACKAGES(org.mule.runtime.container.internal.ContainerClassLoaderFactory.SYSTEM_PACKAGES) ArtifactClassLoaderFilterFactory(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoaderFilterFactory) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) MalformedURLException(java.net.MalformedURLException) Boolean.valueOf(java.lang.Boolean.valueOf) File(java.io.File) DefaultRegionPluginClassLoadersFactory.getArtifactPluginId(org.mule.runtime.deployment.model.internal.DefaultRegionPluginClassLoadersFactory.getArtifactPluginId) Collectors.toList(java.util.stream.Collectors.toList) MuleApplicationClassLoader(org.mule.runtime.deployment.model.internal.application.MuleApplicationClassLoader) ArtifactsUrlClassification(org.mule.test.runner.api.ArtifactsUrlClassification) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ArtifactClassLoaderFilter(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoaderFilter) JarInfo(org.mule.runtime.module.artifact.internal.util.JarInfo) ArtifactDescriptor(org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) MuleClassLoaderLookupPolicy(org.mule.runtime.container.internal.MuleClassLoaderLookupPolicy) FilteringArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.FilteringArtifactClassLoader) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) DefaultArtifactClassLoaderFilter(org.mule.runtime.module.artifact.api.classloader.DefaultArtifactClassLoaderFilter) ArtifactClassLoaderFilter(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoaderFilter) PluginUrlClassification(org.mule.test.runner.api.PluginUrlClassification) URL(java.net.URL)

Aggregations

ArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader)58 Test (org.junit.Test)30 ArrayList (java.util.ArrayList)17 URL (java.net.URL)12 ClassLoaderLookupPolicy (org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy)12 Pair (org.mule.runtime.api.util.Pair)10 ArtifactPluginDescriptor (org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor)10 MuleArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader)10 Service (org.mule.runtime.api.service.Service)8 SmallTest (org.mule.tck.size.SmallTest)8 LookupStrategy (org.mule.runtime.module.artifact.api.classloader.LookupStrategy)7 Map (java.util.Map)6 RegionClassLoader (org.mule.runtime.module.artifact.api.classloader.RegionClassLoader)6 File (java.io.File)5 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)5 List (java.util.List)5 MuleModule (org.mule.runtime.container.api.MuleModule)5 ContainerOnlyLookupStrategy (org.mule.runtime.container.internal.ContainerOnlyLookupStrategy)5 HashSet (java.util.HashSet)4