Skip to content

Commit

Permalink
Remove deprecated methods in HttpServiceProxyFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Nov 7, 2022
1 parent a2ac764 commit aeb3566
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 594 deletions.
Expand Up @@ -30,8 +30,6 @@

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.framework.ReflectiveMethodInvocation;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.core.MethodIntrospector;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.annotation.AnnotatedElementUtils;
Expand All @@ -44,116 +42,36 @@
* Factory for creating a client proxy given an RSocket service interface with
* {@link RSocketExchange @RSocketExchange} methods.
*
* <p>This class is intended to be declared as a bean in Spring configuration.
* <p>To create an instance, use static methods to obtain a
* {@link Builder Builder}.
*
* @author Rossen Stoyanchev
* @since 6.0
*/
public final class RSocketServiceProxyFactory implements InitializingBean, EmbeddedValueResolverAware {
public final class RSocketServiceProxyFactory {

@Nullable
private final BuilderInitializedFactory builderInitializedFactory;
private final RSocketRequester rsocketRequester;

private final List<RSocketServiceArgumentResolver> argumentResolvers;

@Nullable
private final BeanStyleFactory beanStyleFactory;
private final StringValueResolver embeddedValueResolver;

private final ReactiveAdapterRegistry reactiveAdapterRegistry;

private final Duration blockTimeout;

/**
* Create an instance with the underlying RSocketRequester to perform requests with.
* @param rsocketRequester the requester to use
* @deprecated in favor of using the Builder to initialize the
* RSocketServiceProxyFactory instance.
*/
@Deprecated(since = "6.0.0-RC2", forRemoval = true)
public RSocketServiceProxyFactory(RSocketRequester rsocketRequester) {
this.beanStyleFactory = new BeanStyleFactory(rsocketRequester);
this.builderInitializedFactory = null;
}

private RSocketServiceProxyFactory(
RSocketRequester rsocketRequester, List<RSocketServiceArgumentResolver> argumentResolvers,
@Nullable StringValueResolver embeddedValueResolver,
ReactiveAdapterRegistry reactiveAdapterRegistry, Duration blockTimeout) {

this.beanStyleFactory = null;
this.builderInitializedFactory = new BuilderInitializedFactory(
rsocketRequester, argumentResolvers, embeddedValueResolver, reactiveAdapterRegistry, blockTimeout);
}


/**
* Register a custom argument resolver, invoked ahead of default resolvers.
* @param resolver the resolver to add
* @deprecated in favor of using the Builder to initialize the
* RSocketServiceProxyFactory instance.
*/
@Deprecated(since = "6.0.0-RC2", forRemoval = true)
public void addCustomArgumentResolver(RSocketServiceArgumentResolver resolver) {
Assert.state(this.beanStyleFactory != null, "RSocketServiceProxyFactory was created through the builder");
this.beanStyleFactory.addCustomArgumentResolver(resolver);
}

/**
* Set the custom argument resolvers to use, ahead of default resolvers.
* @param resolvers the resolvers to use
* @deprecated in favor of using the Builder to initialize the
* RSocketServiceProxyFactory instance.
*/
@Deprecated(since = "6.0.0-RC2", forRemoval = true)
public void setCustomArgumentResolvers(List<RSocketServiceArgumentResolver> resolvers) {
Assert.state(this.beanStyleFactory != null, "RSocketServiceProxyFactory was created through the builder");
this.beanStyleFactory.setCustomArgumentResolvers(resolvers);
}

/**
* Set the StringValueResolver to use for resolving placeholders and
* expressions in {@link RSocketExchange#value()}.
* @param resolver the resolver to use
* @deprecated in favor of using the Builder to initialize the
* RSocketServiceProxyFactory instance.
*/
@Deprecated(since = "6.0.0-RC2", forRemoval = true)
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
if (this.beanStyleFactory != null) {
this.beanStyleFactory.setEmbeddedValueResolver(resolver);
}
}

/**
* Set the {@link ReactiveAdapterRegistry} to use to support different
* asynchronous types for RSocket service method return values.
* <p>By default this is {@link ReactiveAdapterRegistry#getSharedInstance()}.
* @deprecated in favor of using the Builder to initialize the
* RSocketServiceProxyFactory instance.
*/
@Deprecated(since = "6.0.0-RC2", forRemoval = true)
public void setReactiveAdapterRegistry(ReactiveAdapterRegistry registry) {
Assert.state(this.beanStyleFactory != null, "RSocketServiceProxyFactory was created through the builder");
this.beanStyleFactory.setReactiveAdapterRegistry(registry);
}

/**
* Configure how long to wait for a response for an RSocket service method
* with a synchronous (blocking) method signature.
* <p>By default this is 5 seconds.
* @param blockTimeout the timeout value
* @deprecated in favor of using the Builder to initialize the
* RSocketServiceProxyFactory instance.
*/
@Deprecated(since = "6.0.0-RC2", forRemoval = true)
public void setBlockTimeout(Duration blockTimeout) {
Assert.state(this.beanStyleFactory != null, "RSocketServiceProxyFactory was created through the builder");
this.beanStyleFactory.setBlockTimeout(blockTimeout);
}


@Override
@Deprecated
public void afterPropertiesSet() throws Exception {
if (this.beanStyleFactory != null) {
this.beanStyleFactory.afterPropertiesSet();
}
this.rsocketRequester = rsocketRequester;
this.argumentResolvers = argumentResolvers;
this.embeddedValueResolver = embeddedValueResolver;
this.reactiveAdapterRegistry = reactiveAdapterRegistry;
this.blockTimeout = blockTimeout;
}


Expand All @@ -166,15 +84,26 @@ public void afterPropertiesSet() throws Exception {
* @return the created proxy
*/
public <S> S createClient(Class<S> serviceType) {
if (this.builderInitializedFactory != null) {
return this.builderInitializedFactory.createClient(serviceType);
}
else if (this.beanStyleFactory != null) {
return this.beanStyleFactory.createClient(serviceType);
}
else {
throw new IllegalStateException("Expected Builder initialized or Bean-style delegate");
}

List<RSocketServiceMethod> serviceMethods =
MethodIntrospector.selectMethods(serviceType, this::isExchangeMethod).stream()
.map(method -> createRSocketServiceMethod(serviceType, method))
.toList();

return ProxyFactory.getProxy(serviceType, new ServiceMethodInterceptor(serviceMethods));
}

private boolean isExchangeMethod(Method method) {
return AnnotatedElementUtils.hasAnnotation(method, RSocketExchange.class);
}

private <S> RSocketServiceMethod createRSocketServiceMethod(Class<S> serviceType, Method method) {
Assert.notNull(this.argumentResolvers,
"No argument resolvers: afterPropertiesSet was not called");

return new RSocketServiceMethod(
method, serviceType, this.argumentResolvers, this.rsocketRequester,
this.embeddedValueResolver, this.reactiveAdapterRegistry, this.blockTimeout);
}


Expand Down Expand Up @@ -330,163 +259,4 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
}
}


/**
* Temporary class until bean-style initialization is removed.
*/
private static final class BuilderInitializedFactory {

private final RSocketRequester rsocketRequester;

private final List<RSocketServiceArgumentResolver> argumentResolvers;

@Nullable
private final StringValueResolver embeddedValueResolver;

private final ReactiveAdapterRegistry reactiveAdapterRegistry;

private final Duration blockTimeout;


public BuilderInitializedFactory(
RSocketRequester rsocketRequester, List<RSocketServiceArgumentResolver> argumentResolvers,
@Nullable StringValueResolver embeddedValueResolver,
ReactiveAdapterRegistry reactiveAdapterRegistry, Duration blockTimeout) {

this.rsocketRequester = rsocketRequester;
this.argumentResolvers = argumentResolvers;
this.embeddedValueResolver = embeddedValueResolver;
this.reactiveAdapterRegistry = reactiveAdapterRegistry;
this.blockTimeout = blockTimeout;
}


public <S> S createClient(Class<S> serviceType) {

List<RSocketServiceMethod> serviceMethods =
MethodIntrospector.selectMethods(serviceType, this::isExchangeMethod).stream()
.map(method -> createRSocketServiceMethod(serviceType, method))
.toList();

return ProxyFactory.getProxy(serviceType, new ServiceMethodInterceptor(serviceMethods));
}

private boolean isExchangeMethod(Method method) {
return AnnotatedElementUtils.hasAnnotation(method, RSocketExchange.class);
}

private <S> RSocketServiceMethod createRSocketServiceMethod(Class<S> serviceType, Method method) {
Assert.notNull(this.argumentResolvers,
"No argument resolvers: afterPropertiesSet was not called");

return new RSocketServiceMethod(
method, serviceType, this.argumentResolvers, this.rsocketRequester,
this.embeddedValueResolver, this.reactiveAdapterRegistry, this.blockTimeout);
}
}


/**
* Temporary class to support bean-style initialization during deprecation period.
*/
private static final class BeanStyleFactory implements InitializingBean, EmbeddedValueResolverAware {

private final RSocketRequester rsocketRequester;

@Nullable
private List<RSocketServiceArgumentResolver> customArgumentResolvers;

@Nullable
private List<RSocketServiceArgumentResolver> argumentResolvers;

@Nullable
private StringValueResolver embeddedValueResolver;

private ReactiveAdapterRegistry reactiveAdapterRegistry = ReactiveAdapterRegistry.getSharedInstance();

private Duration blockTimeout = Duration.ofSeconds(5);


public BeanStyleFactory(RSocketRequester rsocketRequester) {
Assert.notNull(rsocketRequester, "RSocketRequester is required");
this.rsocketRequester = rsocketRequester;
}


public void addCustomArgumentResolver(RSocketServiceArgumentResolver resolver) {
if (this.customArgumentResolvers == null) {
this.customArgumentResolvers = new ArrayList<>();
}
this.customArgumentResolvers.add(resolver);
}

public void setCustomArgumentResolvers(List<RSocketServiceArgumentResolver> resolvers) {
this.customArgumentResolvers = new ArrayList<>(resolvers);
}

@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.embeddedValueResolver = resolver;
}

public void setReactiveAdapterRegistry(ReactiveAdapterRegistry registry) {
this.reactiveAdapterRegistry = registry;
}

public void setBlockTimeout(Duration blockTimeout) {
this.blockTimeout = blockTimeout;
}


@Override
public void afterPropertiesSet() {
this.argumentResolvers = initArgumentResolvers();
}

private List<RSocketServiceArgumentResolver> initArgumentResolvers() {
List<RSocketServiceArgumentResolver> resolvers = new ArrayList<>();

// Custom
if (this.customArgumentResolvers != null) {
resolvers.addAll(this.customArgumentResolvers);
}

// Annotation-based
resolvers.add(new PayloadArgumentResolver(this.reactiveAdapterRegistry, false));
resolvers.add(new DestinationVariableArgumentResolver());

// Type-based
resolvers.add(new MetadataArgumentResolver());

// Fallback
resolvers.add(new PayloadArgumentResolver(this.reactiveAdapterRegistry, true));

return resolvers;
}


public <S> S createClient(Class<S> serviceType) {

List<RSocketServiceMethod> serviceMethods =
MethodIntrospector.selectMethods(serviceType, this::isExchangeMethod).stream()
.map(method -> createRSocketServiceMethod(serviceType, method))
.toList();

return ProxyFactory.getProxy(serviceType, new ServiceMethodInterceptor(serviceMethods));
}

private boolean isExchangeMethod(Method method) {
return AnnotatedElementUtils.hasAnnotation(method, RSocketExchange.class);
}

private <S> RSocketServiceMethod createRSocketServiceMethod(Class<S> serviceType, Method method) {
Assert.notNull(this.argumentResolvers,
"No argument resolvers: afterPropertiesSet was not called");

return new RSocketServiceMethod(
method, serviceType, this.argumentResolvers, this.rsocketRequester,
this.embeddedValueResolver, this.reactiveAdapterRegistry, this.blockTimeout);
}
}

}

0 comments on commit aeb3566

Please sign in to comment.