@SuppressWarnings("unchecked") private <T> RemoteResultReducer<T> newRemoteResultReducer() { return (RemoteResultReducer<T>) ReflectionUtil.newInstance(this.reducerType); }
public T newProxy() { return ReflectionUtil.newProxy(type, this); }
/** * Creates a daemon thread with the specified name prefix. Thread names will be namePrefix-<threadId>. Thread ID is * incremented each time a thread is created using this factory. * * @param namePrefix * not null. */ public NamedThreadFactory(String namePrefix) { this.namePrefix = Objects.requireNonNull(namePrefix); group = getThreadGroup(); daemon = true; }
@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 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); }
@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 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)); } }
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 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); } }
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 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); } } }
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)); }
private Object proxyInvocation(final Method method, final Object[] args) throws Throwable { CheckedCommand<Object> serviceInvocation = () -> ReflectionUtil.invokeMethod(method, targetBean, args); for (BeanProxy proxy : proxys) { if (proxy.isEnabled()) { serviceInvocation = proxy.proxyInvocation(serviceInvocation); } } return serviceInvocation.call(); }
@Override public T getProvider(ObjectSerializerFactory objectSerializerFactory, ObjectSerializerDefinition serializerDefinition) { if (serverSerializerDefinition.isVersioned() || serializerDefinition.isVersioned()) { VersionedServiceProviderProxy serializationHandlingProxy = new VersionedServiceProviderProxy(provider, serializerDefinition.version(), objectSerializerFactory.create(serializerDefinition), objectSerializerFactory.create(serverSerializerDefinition)); return ReflectionUtil.newProxy(type, serializationHandlingProxy); } return provider; } }
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); } }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object[] marshalledAndUnmarshalledArgs = new Object[args.length]; for (int i = 0; i < args.length; i++) { // simulate client serialization before sending request over network Object serialized = clientSerializer.serialize(args[i], clientVersion); // simulate server deserialization after receiving request from network Object deserialized = serverSerializer.deserialize(serialized, method.getParameterTypes()[i], clientVersion); if (args[i] != null && !args[i].getClass().equals(deserialized.getClass())) { throw new IllegalArgumentException("Deserialization of service arguments failed. clientSerializer=" + clientSerializer.getClass().getName() + " serverSerializer=" + serverSerializer.getClass().getName()); } marshalledAndUnmarshalledArgs[i] = deserialized; } Object result = ReflectionUtil.invokeMethod(method, provider, marshalledAndUnmarshalledArgs); // simulate server serialization before sending response over network Object serialized = serverSerializer.serialize(result, clientVersion); // simulate client deserialization after receiving response from server. Object deserialized = clientSerializer.deserialize(serialized, method.getReturnType(), clientVersion); return deserialized; }
@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); }