use of org.mule.runtime.api.util.Reference in project mule by mulesoft.
the class DeclarationBasedElementModelFactory method createWrappedObject.
private void createWrappedObject(ParameterObjectValue objectValue, ParameterModel parameterModel, DslElementSyntax paramDsl, InternalComponentConfiguration.Builder parentConfig, DslElementModel.Builder parentElement) {
DslElementModel.Builder<ParameterModel> wrapperElement = DslElementModel.<ParameterModel>builder().withModel(parameterModel).withDsl(paramDsl);
InternalComponentConfiguration.Builder wrapperConfig = InternalComponentConfiguration.builder().withIdentifier(asIdentifier(paramDsl));
Reference<DslSyntaxResolver> customDsl = new Reference<>(dsl);
ObjectType nestedElementType;
if (objectValue.getTypeId() == null || objectValue.getTypeId().trim().isEmpty() || getId(parameterModel.getType()).map(id -> id.equals(objectValue.getTypeId())).orElse(false)) {
nestedElementType = (ObjectType) parameterModel.getType();
} else {
nestedElementType = lookupType(objectValue);
context.getTypeCatalog().getDeclaringExtension(objectValue.getTypeId()).ifPresent(owner -> context.getExtension(owner).ifPresent(extensionModel -> customDsl.set(resolvers.get(extensionModel))));
}
customDsl.get().resolve(nestedElementType).ifPresent(typeDsl -> createObject(objectValue, typeDsl, nestedElementType, nestedElementType, wrapperConfig, wrapperElement));
ComponentConfiguration result = wrapperConfig.build();
parentConfig.withNestedComponent(result);
parentElement.containing(wrapperElement.withConfig(result).build());
}
use of org.mule.runtime.api.util.Reference in project mule by mulesoft.
the class PoolingByteBufferManagerTestCase method assertMemoryLimit.
private void assertMemoryLimit(int bufferCapacity, long waitTimeoutMillis) throws InterruptedException {
ByteBuffer buffer1 = bufferManager.allocate(bufferCapacity);
ByteBuffer buffer2 = bufferManager.allocate(bufferCapacity);
assertThat(buffer1.capacity(), is(bufferCapacity));
assertThat(buffer2.capacity(), is(bufferCapacity));
Latch latch = new Latch();
Reference<Boolean> maxMemoryExhausted = new Reference<>(false);
new Thread(() -> {
try {
bufferManager.allocate(bufferCapacity);
latch.release();
} catch (Exception e) {
maxMemoryExhausted.set(e.getCause() instanceof MaxStreamingMemoryExceededException);
}
}).start();
assertThat(latch.await(waitTimeoutMillis * 2, MILLISECONDS), is(false));
assertThat(maxMemoryExhausted.get(), is(true));
bufferManager.deallocate(buffer1);
Latch secondLatch = new Latch();
new Thread(() -> {
try {
bufferManager.allocate(bufferCapacity);
maxMemoryExhausted.set(false);
} finally {
secondLatch.release();
}
}).start();
assertThat(secondLatch.await(waitTimeoutMillis, MILLISECONDS), is(true));
assertThat(maxMemoryExhausted.get(), is(false));
}
use of org.mule.runtime.api.util.Reference in project mule by mulesoft.
the class ExtensionDefinitionParser method getValueResolverFromMetadataType.
private ValueResolver getValueResolverFromMetadataType(final String parameterName, MetadataType expectedType, final Object value, final Object defaultValue, final boolean acceptsReferences, final Class<?> expectedClass) {
final Reference<ValueResolver> resolverValueHolder = new Reference<>();
expectedType.accept(new BasicTypeMetadataVisitor() {
@Override
protected void visitBasicType(MetadataType metadataType) {
if (conversionService.canConvert(value.getClass(), expectedClass)) {
resolverValueHolder.set(new StaticValueResolver<>(convertSimpleValue(value, expectedClass, parameterName)));
} else {
defaultVisit(metadataType);
}
}
@Override
public void visitDateTime(DateTimeType dateTimeType) {
resolverValueHolder.set(parseDate(value, dateTimeType, defaultValue));
}
@Override
public void visitDate(DateType dateType) {
resolverValueHolder.set(parseDate(value, dateType, defaultValue));
}
@Override
public void visitObject(ObjectType objectType) {
if (isMap(objectType)) {
defaultVisit(objectType);
return;
}
ValueResolver valueResolver;
Optional<? extends ParsingDelegate> delegate = locateParsingDelegate(valueResolverParsingDelegates, objectType);
Optional<DslElementSyntax> typeDsl = dslResolver.resolve(objectType);
if (delegate.isPresent() && typeDsl.isPresent()) {
valueResolver = (ValueResolver) delegate.get().parse(value.toString(), objectType, typeDsl.get());
} else {
valueResolver = acceptsReferences ? defaultValueResolverParsingDelegate.parse(value.toString(), objectType, null) : new StaticValueResolver<>(value);
}
resolverValueHolder.set(valueResolver);
}
@Override
protected void defaultVisit(MetadataType metadataType) {
ValueResolver delegateResolver = locateParsingDelegate(valueResolverParsingDelegates, metadataType).map(delegate -> delegate.parse(value.toString(), metadataType, null)).orElseGet(() -> acceptsReferences ? defaultValueResolverParsingDelegate.parse(value.toString(), metadataType, null) : new TypeSafeValueResolverWrapper(new StaticValueResolver<>(value), expectedClass));
resolverValueHolder.set(delegateResolver);
}
});
return resolverValueHolder.get();
}
use of org.mule.runtime.api.util.Reference in project mule by mulesoft.
the class SchemaTypeConversion method convertType.
public static QName convertType(final MetadataType type, ExpressionSupport expressionSupport) {
final boolean dynamic = acceptsExpressions(expressionSupport);
final Reference<QName> qName = new Reference<>(null);
type.accept(new MetadataTypeVisitor() {
@Override
public void visitBoolean(BooleanType booleanType) {
qName.set(dynamic ? EXPRESSION_BOOLEAN : SUBSTITUTABLE_BOOLEAN);
}
@Override
public void visitNumber(NumberType numberType) {
if (getId(numberType).isPresent()) {
Class<Number> type = JavaTypeUtils.getType(numberType);
if (anyOf(type, Integer.class, int.class)) {
qName.set(dynamic ? EXPRESSION_INTEGER : SUBSTITUTABLE_INT);
} else if (anyOf(type, Double.class, double.class)) {
qName.set(dynamic ? EXPRESSION_DOUBLE : SUBSTITUTABLE_DECIMAL);
} else if (anyOf(type, Long.class, long.class)) {
qName.set(dynamic ? EXPRESSION_LONG : SUBSTITUTABLE_LONG);
} else {
qName.set(dynamic ? EXPRESSION_DECIMAL : SUBSTITUTABLE_DECIMAL);
}
} else {
if (numberType.getAnnotation(IntAnnotation.class).isPresent()) {
qName.set(dynamic ? EXPRESSION_INTEGER : SUBSTITUTABLE_INT);
} else {
qName.set(dynamic ? EXPRESSION_DECIMAL : SUBSTITUTABLE_DECIMAL);
}
}
}
@Override
public void visitString(StringType stringType) {
qName.set(dynamic ? EXPRESSION_STRING : STRING);
}
@Override
public void visitDateTime(DateTimeType dateTimeType) {
onDate();
}
@Override
public void visitDate(DateType dateType) {
onDate();
}
@Override
public void visitArrayType(ArrayType arrayType) {
qName.set(dynamic ? EXPRESSION_LIST : SUBSTITUTABLE_NAME);
}
@Override
public void visitObject(ObjectType objectType) {
if (isMap(objectType)) {
qName.set(dynamic ? EXPRESSION_MAP : SUBSTITUTABLE_NAME);
} else {
defaultVisit(objectType);
}
}
@Override
protected void defaultVisit(MetadataType metadataType) {
qName.set(STRING);
}
private void onDate() {
qName.set(dynamic ? EXPRESSION_DATE_TIME : SUBSTITUTABLE_DATE_TIME);
}
private boolean anyOf(Class<Number> type, Class<?>... targets) {
for (Class<?> target : targets) {
if (type.equals(target)) {
return true;
}
}
return false;
}
});
return qName.get();
}
use of org.mule.runtime.api.util.Reference 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);
}
}
Aggregations