@Override public Object call(Object[] parameters, BindingContext context) { return withContextClassLoader(extensionClassLoader, () -> invokeMethod(method, componentInstance, parametersResolver.apply(parameters))); }
public Object execute(ExecutionContext<M> executionContext) { return withContextClassLoader(extensionClassLoader, () -> invokeMethod(method, componentInstance, stream(getParameterValues(executionContext, method.getParameterTypes())) .map(Supplier::get).toArray(Object[]::new))); }
private void doResourceInitialization() { withContextClassLoader(getSystemClassLoader(), () -> { try { new DefaultResourceInitializer().initialize(); } catch (Exception e) { logger.error("Cannot create resource initializer instance", e); } }); }
@Override protected T instantiateObject() { try { return withContextClassLoader(prototypeClass.getClassLoader(), () -> createInstance(prototypeClass)); } catch (Exception e) { throw new MuleRuntimeException(createStaticMessage("Could not create instance of " + prototypeClass), e); } }
@Override public void enrich(ExtensionLoadingContext extensionLoadingContext) { withContextClassLoader(extensionLoadingContext.getExtensionClassLoader(), () -> new EnricherDelegate().apply(extensionLoadingContext)); }
@Override public Object instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner, Object factoryBean, Method factoryMethod, Object... args) throws BeansException { return withContextClassLoader(getClassLoader(bd), () -> { try { return delegate.instantiate(bd, beanName, owner, factoryBean, factoryMethod, args); } catch (BeansException e) { return failIfNotOptional(e, beanName); } }); }
/** * Returns a new instance on each invocation {@inheritDoc} */ @Override public Object newInstance() { try { return withContextClassLoader(extensionClassLoader, configurationType.get()::newInstance); } catch (Exception e) { throw new MuleRuntimeException(createStaticMessage("Could not instantiate configuration of type " + configurationType.get().getName()), e); } }
@Override public void dispose() { withContextClassLoader(null, () -> { log(miniSplash(format("Disposing domain '%s'", getArtifactName()))); }); if (this.artifactContext != null) { withContextClassLoader(deploymentClassLoader.getClassLoader(), () -> this.artifactContext.getMuleContext().dispose()); } this.deploymentClassLoader.dispose(); }
@Override public Object instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner) throws BeansException { // TODO MULE-10002 - review approach to load the internal classes of the extensions when using them in the parsers. return withContextClassLoader(getClassLoader(bd), () -> { try { return delegate.instantiate(bd, beanName, owner); } catch (BeansException e) { return failIfNotOptional(e, beanName); } }); }
@Override public Object instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner, Constructor<?> ctor, Object... args) throws BeansException { return withContextClassLoader(getClassLoader(bd), () -> { try { return delegate.instantiate(bd, beanName, owner, ctor, args); } catch (BeansException e) { return failIfNotOptional(e, beanName); } }); }
/** * Creates an empty flow with the provided name. */ public static Flow createFlow(MuleContext context, String flowName) throws MuleException { final Flow flow = builder(flowName, context).processingStrategyFactory((muleContext, schedulersNamePrefix) -> { return withContextClassLoader(MuleTestUtils.class.getClassLoader(), () -> spy(new TestDirectProcessingStrategy())); }).build(); flow.setAnnotations(singletonMap(LOCATION_KEY, fromSingleComponent(flowName))); return flow; }
@Override public void enrich(ExtensionLoadingContext extensionLoadingContext) { withContextClassLoader(extensionLoadingContext.getExtensionClassLoader(), () -> new EnricherDelegate(extensionLoadingContext.getDslResolvingContext()) .apply(extensionLoadingContext)); }
@Test public void runWithClassLoader() { final ClassLoader originalClassLoader = currentThread().getContextClassLoader(); final ClassLoader mockClassLoader = mock(ClassLoader.class); withContextClassLoader(mockClassLoader, () -> assertContextClassLoader(mockClassLoader)); assertContextClassLoader(originalClassLoader); }
@Override public MetadataResult<TypeMetadataDescriptor> getEntityMetadata(MetadataKey key) throws MetadataResolvingException { try { return runWithMetadataContext( context -> withContextClassLoader(classLoader, () -> entityMetadataMediator .getEntityMetadata(context, key))); } catch (ConnectionException e) { return failure(newFailure(e).onKeys()); } }
@Test public void returnWithClassLoader() { final String value = "Hello World!"; final ClassLoader originalClassLoader = currentThread().getContextClassLoader(); final ClassLoader mockClassLoader = mock(ClassLoader.class); String response = withContextClassLoader(mockClassLoader, () -> { assertContextClassLoader(mockClassLoader); return value; }); assertContextClassLoader(originalClassLoader); assertThat(response, is(value)); }
@Test public void regionClassLoader() { RegionClassLoader regionClassLoader = mock(RegionClassLoader.class); withContextClassLoader(regionClassLoader, () -> { logger.info(MESSAGE); verify(contextSelector).getContext(LOGGER_NAME, regionClassLoader, true); }); } }
@Test public void anotherClassLoader() { withContextClassLoader(additionalClassLoader, () -> { logger.info(MESSAGE); verify(originalLogger).info(MESSAGE); }); }
public TopLevelParameterObjectFactory(ObjectType type, ClassLoader classLoader, MuleContext muleContext) { super(muleContext); this.classLoader = classLoader; this.objectType = type; objectClass = new LazyValue<>(() -> withContextClassLoader(classLoader, () -> { // We must add the annotations support with a proxy to avoid the SDK user to clutter the POJO definitions in an extension // with the annotations stuff. return addAnnotationsToClass(getType(type)); })); builder = new LazyValue<>(() -> new DefaultObjectBuilder(objectClass.get(), reflectionCache)); }
@Test public void useContextClassLoader() throws ClassNotFoundException { withContextClassLoader(new MyClassClassLoader(), () -> { try { assertFalse((Boolean) mvel.execute("1 is org.MyClass", null)); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }); }
protected void executeApplicationFlow(String flowName) throws Exception { ClassLoader appClassLoader = deploymentService.getApplications().get(0).getArtifactClassLoader().getClassLoader(); withContextClassLoader(appClassLoader, () -> { final FlowRunner flowRunner = new FlowRunner(deploymentService.getApplications().get(0).getRegistry(), flowName); CoreEvent result = flowRunner.withPayload(TEST_MESSAGE).run(); flowRunner.dispose(); assertThat(currentThread().getContextClassLoader(), sameInstance(appClassLoader)); return result; }); }