@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { T target = this.target; if (target == null) { throw new ServiceUnavailableException("Proxy not bound to a type=" + type.getName() + ". " + "Call AstrixRule.registerProvider to register a provider for the given bean"); } return ReflectionUtil.invokeMethod(method, target, args); } }
public T newProxy() { return ReflectionUtil.newProxy(type, this); }
@SuppressWarnings("unchecked") private <T> RemoteResultReducer<T> newRemoteResultReducer() { return (RemoteResultReducer<T>) ReflectionUtil.newInstance(this.reducerType); }
private RemotingProxy(Class<?> proxiedServiceApi, Class<?> targetServiceApi, AstrixObjectSerializer objectSerializer, RemotingTransport AstrixServiceTransport, RoutingStrategy defaultRoutingStrategy, ReactiveTypeConverter reactiveTypeConverter) { this.reactiveTypeConverter = reactiveTypeConverter; this.serviceApi = targetServiceApi.getName(); this.apiVersion = objectSerializer.version(); RemotingEngine remotingEngine = new RemotingEngine(AstrixServiceTransport, objectSerializer, apiVersion); this.remoteServiceMethodFactory = new RemoteServiceMethodFactory(remotingEngine, defaultRoutingStrategy); /* * For each of the following services the "targetServiceType" resolves to MyService: * - MyService * - MyServiceAsync * - ObservableMyService */ Class<?> targetServiceType = ReflectionUtil.classForName(this.serviceApi); for (Method proxiedMethod : proxiedServiceApi.getMethods()) { Type returnType = getReturnType(proxiedMethod); RemoteServiceMethod remoteServiceMethod = this.remoteServiceMethodFactory.createRemoteServiceMethod(targetServiceType, proxiedMethod, returnType); remoteServiceMethodByMethod.put(proxiedMethod, remoteServiceMethod); } }
public RemoteServiceMethod createRemoteServiceMethod( Class<?> targetServiceType, Method proxiedMethod, Type targetReturnType) { String methodSignature = ReflectionUtil.methodSignatureWithoutReturnType(proxiedMethod); if (proxiedMethod.isAnnotationPresent(AstrixBroadcast.class)) { Method targetServiceMethod = ReflectionUtil.getMethod(targetServiceType, proxiedMethod.getName(),proxiedMethod.getParameterTypes()); return new BroadcastedRemoteServiceMethod(methodSignature, getRemoteResultReducerClass(targetServiceMethod), remotingEngine, targetReturnType); } int partitionedByArgumentIndex = getPartitionedByAnnotation(proxiedMethod); if (partitionedByArgumentIndex >= 0) { Method targetServiceMethod = ReflectionUtil.getMethod(targetServiceType, proxiedMethod.getName(),proxiedMethod.getParameterTypes()); return new PartitionedRemoteServiceMethod(partitionedByArgumentIndex, proxiedMethod, methodSignature, remotingEngine, targetReturnType, targetServiceMethod); } if (proxiedMethod.isAnnotationPresent(AstrixRoutingStrategy.class)) { RoutingStrategy routingStrategy = createRoutingStrategy(proxiedMethod); return new RoutedRemoteServiceMethod(methodSignature, routingStrategy.create(proxiedMethod), remotingEngine, targetReturnType); } return new RoutedRemoteServiceMethod(methodSignature, defaultRoutingStrategy.create(proxiedMethod), remotingEngine, targetReturnType); }
private ContainerType getPartitionedArgumentContainerType(Method proxiedMethod, AstrixPartitionedRouting partitionBy) { Class<?> partitionedArgumentType = proxiedMethod.getParameterTypes()[partitionedArgumentIndex]; if (partitionedArgumentType.isArray()) { return new ArrayContainerType(partitionedArgumentType.getComponentType()); } Class<? extends Collection<?>> collectionFactory = (Class<? extends Collection<?>>) partitionBy.collectionFactory(); if (!proxiedMethod.getParameterTypes()[partitionedArgumentIndex].isAssignableFrom(collectionFactory)) { throw new IllegalArgumentException(String.format("Collection class supplied by @AstrixPartitionedRouting is not " + "compatible with argument type, argumentType=%s classType=%s", proxiedMethod.getParameterTypes()[partitionedArgumentIndex].getName(), collectionFactory)); } Type rawType = proxiedMethod.getGenericParameterTypes()[partitionedArgumentIndex]; if (!(rawType instanceof ParameterizedType)) { throw new IllegalArgumentException("Illegal service method: " + ReflectionUtil.fullMethodName(proxiedMethod) + ".\nWhen defining a routingMethod for @AstrixPartitionedRouting the target Collection type must not be a raw type. \nwas: " + rawType); } ParameterizedType partitionedArgumentTypeParameters = (ParameterizedType) rawType; return new CollectionContainerType(collectionFactory, (Class<?>)partitionedArgumentTypeParameters.getActualTypeArguments()[0]); }
private static void validateRemoteResultReducerReturnType( Method targetServiceMethod, Class<? extends RemoteResultReducer<?>> reducerType) { Method reduceMethod = ReflectionUtil.getMethod(reducerType, "reduce", List.class); Class<?> returnType = targetServiceMethod.getReturnType(); if (returnType.equals(Void.TYPE)) { return; } if (TypeVariable.class.isAssignableFrom(reduceMethod .getGenericReturnType().getClass())) { return; } if (!returnType.isAssignableFrom(reduceMethod.getReturnType())) { throw new IncompatibleRemoteResultReducerException( String.format( "Return type of RemoteResultReducer must be same as (or subtype) of the one returned by the service method. " + "serviceMethod=%s reducerType=%s reducerReturnType=%s serviceMethodReturnType=%s", targetServiceMethod, reducerType, returnType.getName(), reduceMethod.getReturnType().getName())); } }
public PublishedService(T service, AstrixObjectSerializer serializer, Class<?> providedApi) { this.objectSerializer = serializer; this.providedApi = providedApi; // Monitor for service-level metrics (aggregated stats for all methods) this.serviceMonitor = new ServiceInvocationMonitor(metrics.createTimer()); mbeanExporter.registerMBean(this.serviceMonitor, "ExportedServices", providedApi.getName()); for (Method m : providedApi.getMethods()) { ServiceInvocationMonitors serviceInvocationMonitors = serviceInvocationMonitorsByMethodName.computeIfAbsent(m.getName(), this::createServiceInvocationMonitors); methodBySignature.put(ReflectionUtil.methodSignatureWithoutReturnType(m), new PublishedServiceMethod<>(serviceInvocationMonitors, m, objectSerializer, service)); } }
@Override public <T> T create(ServiceDefinition<T> serviceDefinition, ServiceProperties serviceProperties, RemotingTransportSpi remotingTransportSpi, RoutingStrategy routingStrategy) { AstrixObjectSerializer objectSerializer = objectSerializerFactory.create(serviceDefinition.getObjectSerializerDefinition()); RemotingTransport remotingTransport = RemotingTransport.create(remotingTransportSpi); return RemotingProxy.create(serviceDefinition.getServiceType(), ReflectionUtil.classForName(serviceProperties.getProperty(ServiceProperties.API)) , remotingTransport, objectSerializer, routingStrategy, reactiveTypeConverter); }
private PartitionedRouter createRouter(AstrixPartitionedRouting partitionedRouting) { Class<?> elementType = this.partitionedArgumentContainerType.getElementType(); if (!partitionedRouting.routingMethod().isEmpty()) { Method routingMethod; try { routingMethod = elementType.getMethod(partitionedRouting.routingMethod()); return PartitionedRouter.routingMethod(routingMethod); } catch (NoSuchMethodException | SecurityException e) { throw new IllegalArgumentException("Failed to find routing method for partitioned routing:\n" + "service: " + ReflectionUtil.fullMethodName(proxiedMethod) + "\n" + "@AstrixPartitionedRouting.routingMethod: " + partitionedRouting.routingMethod(), e); } } return PartitionedRouter.identity(); }
@Override RoutingKey getRoutingKey(Object element) { try { Object result = ReflectionUtil.invokeMethod(method, element, null); return RoutingKey.create(result); } catch (Throwable e) { throw new RuntimeException("Failed to invoke routing Method on: " + element, e); } } }
private <T> T createProxy(Class<T> proxyApi, final Object targetProvider) { return ReflectionUtil.newProxy(proxyApi, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { Method targetMethod = targetProvider.getClass().getMethod(method.getName(), method.getParameterTypes()); Observable<Object> observableResult = Observable.create((s) -> { try { Object result = ReflectionUtil.invokeMethod(targetMethod, targetProvider, args); s.onNext(result); s.onCompleted(); } catch (Throwable e) { s.onError(e); } }); if (method.getReturnType().equals(Future.class)) { return observableResult.toBlocking().toFuture(); } if (method.getReturnType().equals(Observable.class)) { return observableResult; } return reactiveTypeConverter.toCustomReactiveType(method.getReturnType(), observableResult); } catch (NoSuchMethodException e) { throw new RuntimeException("Target service does not contain method: " + e.getMessage()); } } }); }
private RemoteResultReducer<?> newReducer() { return ReflectionUtil.newInstance(this.reducer); }
@Override public <T> BoundServiceBeanInstance<T> bind(ServiceDefinition<T> serviceDefinition, ServiceProperties serviceProperties) { AstrixObjectSerializer objectSerializer = objectSerializerFactory.create(serviceDefinition.getObjectSerializerDefinition()); GigaSpaceInstance proxyInstance = proxyCache.getProxy(serviceProperties); GsRemotingTransport gsRemotingTransport = new GsRemotingTransport(proxyInstance.getSpaceTaskDispatcher()); RemotingTransport remotingTransport = RemotingTransport.create(gsRemotingTransport); T proxy = RemotingProxy.create(serviceDefinition.getServiceType(), ReflectionUtil.classForName(serviceProperties.getProperty(ServiceProperties.API)) , remotingTransport, objectSerializer, new GsRoutingStrategy(), reactiveTypeConverter); return BoundProxyServiceBeanInstance.create(proxy, proxyInstance); }
@Override public Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable { try { return ReflectionUtil.invokeMethod(method, gigaSpace, args); } catch (SpaceCacheException e) { throw new ServiceUnavailableException("SpaceCacheNotAvailable", e); } }
public static GigaSpace create(GigaSpace gigaSpace) { return ReflectionUtil.newProxy(GigaSpace.class, new GigaSpaceProxy(gigaSpace)); }
@Override public ContainerBuilder newInstance() { return new CollectionContainerBuilder((Collection<? super Object>) ReflectionUtil.newInstance(this.collectionFactory)); }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { Method targetMethod = targetProvider.getClass().getMethod(method.getName(), method.getParameterTypes()); Observable<Object> observableResult = Observable.create((s) -> { try { Object result = ReflectionUtil.invokeMethod(targetMethod, targetProvider, args); s.onNext(result); s.onCompleted(); } catch (Throwable e) { s.onError(e); } }); if (method.getReturnType().equals(Future.class)) { return observableResult.toBlocking().toFuture(); } if (method.getReturnType().equals(Observable.class)) { return observableResult; } return reactiveTypeConverter.toCustomReactiveType(method.getReturnType(), observableResult); } catch (NoSuchMethodException e) { throw new RuntimeException("Target service does not contain method: " + e.getMessage()); } } });
@Override public T create(AstrixBeans beans) { T rawBean = target.create(beans); BeanProxy ftProxy = faultToleranceProxyFactory.createFaultToleranceProxy(beanDefinition.getBeanKey()); BeanInvocationDispatcher beanProxyDispather = new BeanInvocationDispatcher(Arrays.asList(ftProxy), asyncTypeConverter, rawBean); return ReflectionUtil.newProxy(getBeanKey().getBeanType(), beanProxyDispather); }
private RoutingStrategy createRoutingStrategy(Method proxiedMethod) { AstrixRoutingStrategy router = proxiedMethod.getAnnotation(AstrixRoutingStrategy.class); Class<? extends RoutingStrategy> routingStrategyClass = router.value(); try { return ReflectionUtil.newInstance(routingStrategyClass); } catch (Exception e) { throw new IllegalServiceMetadataException("Failed to create RoutingStrategy", e); } }