use of org.mule.runtime.dsl.api.component.ComponentBuildingDefinition in project mule by mulesoft.
the class ExtensionDefinitionParser method parse.
/**
* Creates a list of {@link ComponentBuildingDefinition} built on copies of {@link #baseDefinitionBuilder}. It also sets the
* parsed parsed parameters on the backing {@link AbstractExtensionObjectFactory}
*
* @return a list with the generated {@link ComponentBuildingDefinition}
* @throws ConfigurationException if a parsing error occurs
*/
public final List<ComponentBuildingDefinition> parse() throws ConfigurationException {
Builder builder = baseDefinitionBuilder;
builder = doParse(builder);
AttributeDefinition parametersDefinition = fromFixedValue(new HashMap<>()).build();
if (!parameters.isEmpty()) {
KeyAttributeDefinitionPair[] attributeDefinitions = parameters.entrySet().stream().map(entry -> newBuilder().withAttributeDefinition(entry.getValue().build()).withKey(entry.getKey()).build()).toArray(KeyAttributeDefinitionPair[]::new);
parametersDefinition = fromMultipleDefinitions(attributeDefinitions).build();
}
builder = builder.withSetterParameterDefinition("parameters", parametersDefinition);
addDefinition(builder.build());
return parsedDefinitions;
}
use of org.mule.runtime.dsl.api.component.ComponentBuildingDefinition in project mule by mulesoft.
the class BeanDefinitionFactory method resolveComponent.
private BeanDefinition resolveComponent(ComponentModel parentComponentModel, SpringComponentModel componentModel, BeanDefinitionRegistry registry, BiConsumer<ComponentModel, BeanDefinitionRegistry> componentDefinitionModelProcessor, SpringConfigurationComponentLocator componentLocator) {
if (isComponentIgnored(componentModel.getIdentifier())) {
return null;
}
if (!componentModel.isEnabled()) {
// Just register the location, for support of lazyInit scenarios
componentLocator.addComponentLocation(componentModel.getComponentLocation());
return null;
}
resolveComponentBeanDefinition(parentComponentModel, componentModel);
componentDefinitionModelProcessor.accept(componentModel, registry);
// TODO MULE-9638: Once we migrate all core definitions we need to define a mechanism for customizing
// how core constructs are processed.
processMuleConfiguration(componentModel, registry);
processMuleSecurityManager(componentModel, registry);
processRaiseError(componentModel);
componentBuildingDefinitionRegistry.getBuildingDefinition(componentModel.getIdentifier()).ifPresent(componentBuildingDefinition -> {
if ((componentModel.getType() != null) && Component.class.isAssignableFrom(componentModel.getType())) {
addAnnotation(ANNOTATION_NAME, componentModel.getIdentifier(), componentModel);
// We need to use a mutable map since spring will resolve the properties placeholder present in the value if needed
// and it will be done by mutating the same map.
addAnnotation(ANNOTATION_PARAMETERS, new HashMap<>(componentModel.getParameters()), componentModel);
// add any error mappings if present
List<ComponentModel> errorMappingComponents = componentModel.getInnerComponents().stream().filter(innerComponent -> ERROR_MAPPING_IDENTIFIER.equals(innerComponent.getIdentifier())).collect(toList());
if (!errorMappingComponents.isEmpty()) {
addAnnotation(ANNOTATION_ERROR_MAPPINGS, errorMappingComponents.stream().map(innerComponent -> {
Map<String, String> parameters = innerComponent.getParameters();
ComponentIdentifier source = buildFromStringRepresentation(parameters.get(SOURCE_TYPE));
ErrorType errorType = errorTypeRepository.lookupErrorType(source).orElseThrow(() -> new MuleRuntimeException(createStaticMessage("Could not find error '%s'.", source)));
ErrorTypeMatcher errorTypeMatcher = new SingleErrorTypeMatcher(errorType);
ErrorType targetValue = resolveErrorType(parameters.get(TARGET_TYPE));
return new ErrorMapping(errorTypeMatcher, targetValue);
}).collect(toList()), componentModel);
}
componentLocator.addComponentLocation(componentModel.getComponentLocation());
}
});
addAnnotation(LOCATION_KEY, componentModel.getComponentLocation(), componentModel);
BeanDefinition beanDefinition = componentModel.getBeanDefinition();
return beanDefinition;
}
use of org.mule.runtime.dsl.api.component.ComponentBuildingDefinition in project mule by mulesoft.
the class CommonBeanDefinitionCreator method handleRequest.
@Override
public boolean handleRequest(CreateBeanDefinitionRequest request) {
SpringComponentModel componentModel = request.getComponentModel();
ComponentBuildingDefinition buildingDefinition = request.getComponentBuildingDefinition();
componentModel.setType(retrieveComponentType(componentModel, buildingDefinition));
BeanDefinitionBuilder beanDefinitionBuilder = createBeanDefinitionBuilder(componentModel, buildingDefinition);
processAnnotations(componentModel, beanDefinitionBuilder);
processComponentDefinitionModel(request.getParentComponentModel(), componentModel, buildingDefinition, beanDefinitionBuilder);
return true;
}
use of org.mule.runtime.dsl.api.component.ComponentBuildingDefinition in project mule by mulesoft.
the class ObjectFactoryClassRepository method getObjectFactoryDynamicClass.
private Class<ObjectFactory> getObjectFactoryDynamicClass(final ComponentBuildingDefinition componentBuildingDefinition, final Class objectFactoryType, final Class createdObjectType, final Supplier<Boolean> isLazyInitFunction, final Optional<Consumer<Object>> instancePostCreationFunction) {
/*
* We need this to allow spring create the object using a FactoryBean but using the object factory setters and getters so we
* create as FactoryBean a dynamic class that will have the same attributes and methods as the ObjectFactory that the user
* defined. This way our API does not expose spring specific classes.
*/
Enhancer enhancer = new Enhancer();
// Use SmartFactoryBean since it's the only way to force spring to pre-instantiate FactoryBean for singletons
enhancer.setInterfaces(new Class[] { SmartFactoryBean.class });
enhancer.setSuperclass(objectFactoryType);
enhancer.setCallbackType(MethodInterceptor.class);
if (SmartFactoryBean.class.getClassLoader() != objectFactoryType.getClassLoader()) {
// CGLIB needs access to both the spring interface and the extended factory class.
// If the factory class is defined in a plugin, its classloader has to be passed.
enhancer.setClassLoader(new CompositeClassLoader(ObjectFactoryClassRepository.class.getClassLoader(), objectFactoryType.getClassLoader()));
}
// The use of the CGLIB cache is turned off when a post creation function is passed as argument in order to
// enrich the created proxy with properties. This is only to enable injecting properties in components
// from the compatibility module.
// Setting this to false will generate an excessive amount of different proxy classes loaded by the container CL
// that will end up in Metaspace OOM.
enhancer.setUseCache(!instancePostCreationFunction.isPresent());
Class<ObjectFactory> factoryBeanClass = enhancer.createClass();
registerStaticCallbacks(factoryBeanClass, new Callback[] { (MethodInterceptor) (obj, method, args, proxy) -> {
final boolean eager = !isLazyInitFunction.get();
if (method.getName().equals("isSingleton")) {
return !componentBuildingDefinition.isPrototype();
}
if (method.getName().equals("getObjectType") && !ObjectTypeProvider.class.isAssignableFrom(obj.getClass())) {
return createdObjectType;
}
if (method.getName().equals("getObject")) {
Object createdInstance = proxy.invokeSuper(obj, args);
instancePostCreationFunction.ifPresent(consumer -> consumer.accept(createdInstance));
return createdInstance;
}
if (method.getName().equals("isPrototype")) {
return componentBuildingDefinition.isPrototype();
}
if (method.getName().equals("isEagerInit")) {
return eager;
}
return proxy.invokeSuper(obj, args);
} });
return factoryBeanClass;
}
use of org.mule.runtime.dsl.api.component.ComponentBuildingDefinition in project mule by mulesoft.
the class TestParsersComponentBuildingDefinitionProvider method getComponentBuildingDefinitions.
@Override
public List<ComponentBuildingDefinition> getComponentBuildingDefinitions() {
List<ComponentBuildingDefinition> definitions = new ArrayList<>();
ComponentBuildingDefinition.Builder baseBuilder = new ComponentBuildingDefinition.Builder().withNamespace(TestParsersNamespaceInfoProvider.PARSERS_TEST_NAMESPACE);
ComponentBuildingDefinition.Builder baseParameterCollectionParserBuilder = baseBuilder.withTypeDefinition(fromType(ParsersTestObject.class)).asNamed().withSetterParameterDefinition("simpleTypeWithConverter", fromChildConfiguration(String.class).build()).withSetterParameterDefinition("simpleTypeList", fromChildConfiguration(List.class).withWrapperIdentifier("simple-type-child-list").build()).withSetterParameterDefinition("simpleTypeListWithConverter", fromChildConfiguration(List.class).withWrapperIdentifier("simple-type-child-list-with-converter").build()).withSetterParameterDefinition("simpleTypeSet", fromChildConfiguration(Set.class).withWrapperIdentifier("simple-type-child-set").build()).withSetterParameterDefinition("simpleTypeMap", fromChildMapConfiguration(String.class, Integer.class).withWrapperIdentifier("simple-type-map").build()).withSetterParameterDefinition("simpleListTypeMap", fromChildMapConfiguration(String.class, String.class).withWrapperIdentifier("simple-list-type-map").build()).withSetterParameterDefinition("complexTypeMap", fromChildMapConfiguration(Long.class, ParsersTestObject.class).withWrapperIdentifier("complex-type-map").build()).withSetterParameterDefinition("simpleParameters", fromMultipleDefinitions(newBuilder().withAttributeDefinition(fromSimpleParameter("firstname").build()).withKey("firstname").build(), newBuilder().withAttributeDefinition(fromSimpleParameter("lastname").build()).withKey("lastname").build(), newBuilder().withAttributeDefinition(fromSimpleParameter("age").build()).withKey("age").build(), newBuilder().withAttributeDefinition(fromChildConfiguration(ParsersTestObject.class).withWrapperIdentifier("first-child").build()).withKey("first-child").build(), newBuilder().withAttributeDefinition(fromChildConfiguration(ParsersTestObject.class).withWrapperIdentifier("second-child").build()).withKey("second-child").build(), newBuilder().withAttributeDefinition(fromChildConfiguration(List.class).withWrapperIdentifier("other-children").build()).withKey("other-children").build(), newBuilder().withAttributeDefinition(fromChildConfiguration(List.class).withWrapperIdentifier("other-children-custom-collection-type").build()).withKey("other-children-custom-collection-type").build(), newBuilder().withAttributeDefinition(fromChildConfiguration(List.class).withWrapperIdentifier("other-simple-type-child-list").build()).withKey("other-simple-type-child-list-custom-key").build()).build());
definitions.add(baseParameterCollectionParserBuilder.withIdentifier("parameter-collection-parser").build());
definitions.add(baseParameterCollectionParserBuilder.withIdentifier("elementTypeA").build());
definitions.add(baseParameterCollectionParserBuilder.withIdentifier("anotherElementTypeA").build());
definitions.add(baseBuilder.withIdentifier("simple-type-child-list").withTypeDefinition(fromType(List.class)).build());
definitions.add(baseBuilder.withIdentifier("simple-type-child-list-with-converter").withTypeDefinition(fromType(List.class)).build());
definitions.add(baseBuilder.withIdentifier("other-children-custom-collection-type").withTypeDefinition(fromType(LinkedList.class)).build());
definitions.add(baseBuilder.withIdentifier("other-children").withTypeDefinition(fromType(List.class)).build());
definitions.add(baseBuilder.withIdentifier("simple-type-child-set").withTypeDefinition(fromType(TreeSet.class)).build());
definitions.add(baseBuilder.withIdentifier("other-simple-type-child-list").withTypeDefinition(fromType(List.class)).build());
definitions.add(baseBuilder.withIdentifier("simple-type-child").withTypeDefinition(fromType(String.class)).build());
definitions.add(baseBuilder.withIdentifier("simple-type-child-with-converter").withTypeDefinition(fromType(String.class)).withTypeConverter(input -> input + "-with-converter").build());
definitions.add(baseBuilder.withIdentifier("simple-type-map").withTypeDefinition(fromType(TreeMap.class)).build());
definitions.add(baseBuilder.withIdentifier("simple-type-entry").withTypeDefinition(fromMapEntryType(String.class, Integer.class)).withKeyTypeConverter(input -> input + "-with-converter").withTypeConverter(input -> valueOf((String) input) + 1).build());
definitions.add(baseBuilder.withIdentifier("simple-list-type-map").withTypeDefinition(fromType(Map.class)).build());
definitions.add(baseBuilder.withIdentifier("simple-list-entry").withTypeDefinition(fromMapEntryType(String.class, List.class)).build());
definitions.add(baseBuilder.withIdentifier("complex-type-map").withTypeDefinition(fromType(Map.class)).build());
definitions.add(baseBuilder.withIdentifier("complex-type-entry").withTypeDefinition(fromMapEntryType(Long.class, ParsersTestObject.class)).build());
definitions.add(baseBuilder.withIdentifier("global-element-with-object-factory").withTypeDefinition(fromType(LifecycleSensingMessageProcessor.class)).withObjectFactoryType(LifecycleSensingObjectFactory.class).build());
definitions.add(baseBuilder.withIdentifier("inner-element-with-object-factory").withTypeDefinition(fromType(LifecycleSensingMessageProcessor.class)).withObjectFactoryType(LifecycleSensingObjectFactory.class).build());
definitions.add(baseBuilder.withIdentifier("element-with-attribute-and-child").withTypeDefinition(fromType(ParameterAndChildElement.class)).withSetterParameterDefinition("simplePojo", fromSimpleParameter("myPojo", input -> new SimplePojo((String) input)).withDefaultValue("jose").build()).withSetterParameterDefinition("simplePojo", fromChildConfiguration(SimplePojo.class).build()).build());
definitions.add(baseBuilder.withIdentifier("my-pojo").withTypeDefinition(fromType(SimplePojo.class)).withSetterParameterDefinition("someParameter", fromSimpleParameter("someParameter").build()).build());
definitions.add(baseBuilder.withIdentifier("text-pojo").withTypeDefinition(fromType(SimplePojo.class)).withSetterParameterDefinition("someParameter", fromChildConfiguration(String.class).withIdentifier("text").build()).build());
definitions.add(baseBuilder.withIdentifier("text").withTypeDefinition(fromType(String.class)).build());
definitions.add(baseBuilder.withIdentifier("same-child-type-container").withTypeDefinition(fromType(PojoWithSameTypeChildren.class)).withSetterParameterDefinition("elementTypeA", fromChildConfiguration(ParsersTestObject.class).withIdentifier("elementTypeA").build()).withSetterParameterDefinition("anotherElementTypeA", fromChildConfiguration(ParsersTestObject.class).withIdentifier("anotherElementTypeA").build()).build());
definitions.add(baseBuilder.withIdentifier("simple-type").withTypeConverter(o -> new SimplePojo((String) o)).withTypeDefinition(fromType(String.class)).build());
definitions.add(baseBuilder.withIdentifier("component-created-with-object-factory").withObjectFactoryType(TestObjectFactory.class).withTypeDefinition(fromType(TestObject.class)).build());
definitions.add(baseBuilder.withIdentifier("composite-processor-chain-router").withTypeDefinition(fromType(CompositeProcessorChainRouter.class)).withSetterParameterDefinition("processorChains", fromChildCollectionConfiguration(Object.class).build()).build());
definitions.add(baseBuilder.withIdentifier("chain").withTypeDefinition(fromType(Component.class)).withObjectFactoryType(MessageProcessorChainObjectFactory.class).withSetterParameterDefinition("messageProcessors", fromChildCollectionConfiguration(Object.class).build()).build());
definitions.add(baseBuilder.withIdentifier("processor-chain-router").withTypeDefinition(fromType(ProcessorChainRouter.class)).withSetterParameterDefinition("processors", fromChildCollectionConfiguration(Object.class).build()).build());
return definitions;
}
Aggregations