use of org.mule.runtime.api.meta.model.ExtensionModel 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;
});
}
use of org.mule.runtime.api.meta.model.ExtensionModel in project mule by mulesoft.
the class ExtensionModelJsonGeneratorTestCase method data.
@Parameterized.Parameters(name = "{1}")
public static Collection<Object[]> data() {
final ClassLoader classLoader = ExtensionModelJsonGeneratorTestCase.class.getClassLoader();
final ServiceRegistry serviceRegistry = mock(ServiceRegistry.class);
when(serviceRegistry.lookupProviders(DeclarationEnricher.class, classLoader)).thenReturn(asList(new JavaXmlDeclarationEnricher()));
List<ExtensionJsonGeneratorTestUnit> extensions;
extensions = asList(newTestUnit(javaLoader, VeganExtension.class, "vegan.json"), newTestUnit(javaLoader, PetStoreConnector.class, "petstore.json"), newTestUnit(javaLoader, MetadataExtension.class, "metadata.json"), newTestUnit(javaLoader, HeisenbergExtension.class, "heisenberg.json"), newTestUnit(javaLoader, SubstitutionGroupExtension.class, "substitutiongroup.json"), newTestUnit(javaLoader, TransactionalExtension.class, "tx-ext.json"), newTestUnit(javaLoader, SubTypesMappingConnector.class, "subtypes.json"), newTestUnit(javaLoader, MarvelExtension.class, "marvel.json"), newTestUnit(soapLoader, RickAndMortyExtension.class, "ram.json"), newTestUnit(javaLoader, TypedValueExtension.class, "typed-value.json"), newTestUnit(javaLoader, TestOAuthExtension.class, "test-oauth.json"), newTestUnit(javaLoader, WeaveFunctionExtension.class, "test-fn.json"), newTestUnit(javaLoader, ValuesExtension.class, "values.json"), newTestUnit(javaLoader, ReconnectionExtension.class, "reconnection-extension.json"));
BiFunction<Class<?>, ExtensionModelLoader, ExtensionModel> createExtensionModel = (extension, loader) -> {
ExtensionModel model = loadExtension(extension, loader);
if (extensionModels.put(model.getName(), model) != null) {
throw new IllegalArgumentException(format("Extension names must be unique. Name [%s] for extension [%s] was already used", model.getName(), extension.getName()));
}
return model;
};
return extensions.stream().map(e -> new Object[] { createExtensionModel.apply(e.getExtensionClass(), e.getLoader()), e.getFileName() }).collect(toList());
}
use of org.mule.runtime.api.meta.model.ExtensionModel in project mule by mulesoft.
the class DefaultConnectionProviderObjectBuilder method applyExtensionClassLoaderProxy.
/**
* Wraps the {@link ConnectionProvider} inside of a dynamic proxy which changes the current {@link ClassLoader} to the the
* extension's {@link ClassLoader} when executing any method of this wrapped {@link ConnectionProvider}
* <p>
* This ensures that every time that the {@link ConnectionProvider} is used, it will work in the correct classloader.
* <p>
* Although if the {@link ConnectionProvider} is created with the correct classloader and then used with an incorrect one this
* may work, due that static class references were loaded correctly, logic loading class in a dynamic way will fail.
*
* @param provider The {@link ConnectionProvider} to wrap
* @return The wrapped {@link ConnectionProvider}
*/
private ConnectionProvider<C> applyExtensionClassLoaderProxy(ConnectionProvider provider) {
Enhancer enhancer = new Enhancer();
ClassLoader classLoader = getClassLoader(extensionModel);
Class[] proxyInterfaces = getProxyInterfaces(provider);
enhancer.setInterfaces(proxyInterfaces);
MethodInterceptor returnProviderInterceptor = (obj, method, args, proxy) -> provider;
MethodInterceptor invokerInterceptor = (obj, method, args, proxy) -> {
Reference<Object> resultReference = new Reference<>();
Reference<Throwable> errorReference = new Reference<>();
withContextClassLoader(classLoader, () -> {
try {
resultReference.set(method.invoke(provider, args));
} catch (InvocationTargetException e) {
errorReference.set(e.getTargetException());
} catch (Throwable t) {
errorReference.set(t);
}
});
if (errorReference.get() != null) {
throw errorReference.get();
} else {
return resultReference.get();
}
};
CallbackHelper callbackHelper = new CallbackHelper(Object.class, proxyInterfaces) {
@Override
protected Object getCallback(Method method) {
if (method.getDeclaringClass().equals(HasDelegate.class) && method.getName().equals("getDelegate")) {
return returnProviderInterceptor;
} else {
return invokerInterceptor;
}
}
};
enhancer.setCallbackTypes(callbackHelper.getCallbackTypes());
enhancer.setCallbackFilter(callbackHelper);
if (Enhancer.class.getClassLoader() != classLoader) {
enhancer.setClassLoader(new CompositeClassLoader(DefaultConnectionProviderObjectBuilder.class.getClassLoader(), classLoader));
enhancer.setUseCache(false);
}
Class<ConnectionProvider<C>> proxyClass = enhancer.createClass();
registerStaticCallbacks(proxyClass, callbackHelper.getCallbacks());
try {
return proxyClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new MuleRuntimeException(e);
}
}
use of org.mule.runtime.api.meta.model.ExtensionModel in project mule by mulesoft.
the class ExtensionConnectionSupplier method getTransactedConnectionHandler.
private <T extends TransactionalConnection> ConnectionHandler<T> getTransactedConnectionHandler(ExecutionContextAdapter<? extends ComponentModel> executionContext, TransactionConfig transactionConfig) throws ConnectionException, TransactionException {
if (!transactionConfig.isTransacted()) {
return getTransactionlessConnectionHandler(executionContext);
}
ExtensionModel extensionModel = executionContext.getExtensionModel();
ComponentModel componentModel = executionContext.getComponentModel();
ConfigurationInstance configuration = executionContext.getConfiguration().orElseThrow(() -> new IllegalStateException(format("%s '%s' of extension '%s' cannot participate in a transaction because it doesn't have a config", getComponentModelTypeName(componentModel), componentModel.getName(), extensionModel.getName())));
final ExtensionTransactionKey txKey = new ExtensionTransactionKey(configuration);
TransactionBindingDelegate transactionBindingDelegate = new TransactionBindingDelegate(extensionModel, componentModel);
return transactionBindingDelegate.getBoundResource(transactionConfig, txKey, () -> getTransactionlessConnectionHandler(executionContext));
}
use of org.mule.runtime.api.meta.model.ExtensionModel in project mule by mulesoft.
the class ApplicationModel method convertArtifactDeclarationToComponentModel.
private void convertArtifactDeclarationToComponentModel(Set<ExtensionModel> extensionModels, ArtifactDeclaration artifactDeclaration) {
if (artifactDeclaration != null && !extensionModels.isEmpty()) {
ExtensionModel muleModel = MuleExtensionModelProvider.getExtensionModel();
if (!extensionModels.contains(muleModel)) {
extensionModels = new HashSet<>(extensionModels);
extensionModels.add(muleModel);
}
DslElementModelFactory elementFactory = DslElementModelFactory.getDefault(DslResolvingContext.getDefault(extensionModels));
ComponentModel rootComponent = new ComponentModel.Builder().setIdentifier(ComponentIdentifier.builder().namespace(CORE_PREFIX).name(CORE_PREFIX).build()).build();
AtomicBoolean atLeastOneComponentAdded = new AtomicBoolean(false);
artifactDeclaration.getGlobalElements().stream().map(e -> elementFactory.create((ElementDeclaration) e)).filter(Optional::isPresent).map(e -> e.get().getConfiguration()).forEach(config -> config.ifPresent(c -> {
atLeastOneComponentAdded.set(true);
ComponentModel componentModel = convertComponentConfiguration(c, true);
componentModel.setParent(rootComponent);
rootComponent.getInnerComponents().add(componentModel);
}));
if (atLeastOneComponentAdded.get()) {
this.muleComponentModels.add(rootComponent);
}
}
}
Aggregations