private static ThriftCallService findThriftService(Service<?, ?> delegate) { return delegate.as(ThriftCallService.class).orElseThrow( () -> new IllegalStateException("service being decorated is not a ThriftCallService: " + delegate)); }
private static boolean isSupported( ServiceConfig serviceCfg, Set<Class<? extends Service<?, ?>>> supportedServiceTypes) { return supportedServiceTypes.stream().anyMatch(type -> serviceCfg.service().as(type).isPresent()); }
/** * Returns a newly-created decorator that decorates a {@link Service} with a new {@link CorsService} * based on the properties of this builder. */ public Function<Service<HttpRequest, HttpResponse>, ? extends Service<HttpRequest, HttpResponse>> newDecorator() { return s -> { if (s.as(CorsService.class).isPresent()) { return s; } return build(s); }; }
/** * Retrieves thrift service names of {@code service} using reflection. */ static Set<String> serviceNames(Service<HttpRequest, HttpResponse> service) { if (thriftServiceClass == null || entriesMethod == null || interfacesMethod == null) { return ImmutableSet.of(); } return service.as(thriftServiceClass) .map(s -> { @SuppressWarnings("unchecked") final Map<String, ?> entries = (Map<String, ?>) invokeMethod(entriesMethod, s); assert entries != null; return toServiceName(entries.values()); }) .orElse(ImmutableSet.of()); }
/** * Retrieves thrift service names of {@code service} using reflection. */ static Set<String> serviceNames(Service<HttpRequest, HttpResponse> service) { if (thriftServiceClass == null || entriesMethod == null || interfacesMethod == null) { return ImmutableSet.of(); } return service.as(thriftServiceClass) .map(s -> { @SuppressWarnings("unchecked") final Map<String, ?> entries = (Map<String, ?>) invokeMethod(entriesMethod, s); assert entries != null; return toServiceName(entries.values()); }) .orElse(ImmutableSet.of()); }
@Override public Map<String, String> loadDocStrings(Set<ServiceConfig> serviceConfigs) { return serviceConfigs.stream() .flatMap(c -> c.service().as(GrpcService.class).get().services().stream()) .flatMap(s -> docstringExtractor.getAllDocStrings(s.getClass().getClassLoader()) .entrySet().stream()) .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a)); }
@Override public Map<String, String> loadDocStrings(Set<ServiceConfig> serviceConfigs) { return serviceConfigs.stream() .flatMap(c -> c.service().as(THttpService.class).get().entries().values().stream()) .flatMap(entry -> entry.interfaces().stream().map(Class::getClassLoader)) .flatMap(loader -> docstringExtractor.getAllDocStrings(loader) .entrySet().stream()) .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a)); }
/** * Creates a new instance that decorates the specified {@link Service}. */ UnframedGrpcService(Service<HttpRequest, HttpResponse> delegate) { super(delegate); delegateGrpcService = delegate.as(GrpcService.class) .orElseThrow( () -> new IllegalArgumentException("Decorated service must be a GrpcService.")); methodsByName = delegateGrpcService.services() .stream() .flatMap(service -> service.getMethods().stream()) .map(ServerMethodDefinition::getMethodDescriptor) .collect(ImmutableMap.toImmutableMap(MethodDescriptor::getFullMethodName, Function.identity())); }
/** * Finds the {@link List} of {@link VirtualHost}s that contains the specified {@link Service}. If there's * no match, an empty {@link List} is returned. Note that this is potentially an expensive operation and * thus should not be used in a performance-sensitive path. */ public List<VirtualHost> findVirtualHosts(Service<?, ?> service) { requireNonNull(service, "service"); @SuppressWarnings("rawtypes") final Class<? extends Service> serviceType = service.getClass(); final List<VirtualHost> res = new ArrayList<>(); for (VirtualHost h : virtualHosts) { for (ServiceConfig c : h.serviceConfigs()) { // Consider the case where the specified service is decorated before being added. final Service<?, ?> s = c.service(); @SuppressWarnings("rawtypes") final Optional<? extends Service> sOpt = s.as(serviceType); if (!sOpt.isPresent()) { continue; } if (sOpt.get() == service) { res.add(c.virtualHost()); break; } } } return res; }
@Override public ServiceSpecification generateSpecification(Set<ServiceConfig> serviceConfigs) { requireNonNull(serviceConfigs, "serviceConfigs"); final Map<Class<?>, Set<MethodInfo>> methodInfos = new HashMap<>(); final Map<Class<?>, String> serviceDescription = new HashMap<>(); serviceConfigs.forEach(sc -> { final Optional<AnnotatedHttpService> service = sc.service().as(AnnotatedHttpService.class); service.ifPresent( httpService -> { addMethodInfo(methodInfos, sc.virtualHost().hostnamePattern(), httpService); addServiceDescription(serviceDescription, httpService); }); }); return generate(serviceDescription, methodInfos); }
private static ServiceConfig findServiceConfig(Server server, String path, Service<?, ?> service) { for (ServiceConfig cfg : server.config().defaultVirtualHost().serviceConfigs()) { final Optional<String> exactPath = cfg.pathMapping().exactPath(); if (!exactPath.isPresent()) { continue; } if (!path.equals(exactPath.get())) { continue; } if (cfg.service().as(service.getClass()).isPresent()) { return cfg; } } throw new Error(); // Never reaches here. } }
@Override public ServiceSpecification generateSpecification(Set<ServiceConfig> serviceConfigs) { final Map<Class<?>, EntryBuilder> map = new LinkedHashMap<>(); for (ServiceConfig c : serviceConfigs) { final THttpService service = c.service().as(THttpService.class).get(); service.entries().forEach((serviceName, entry) -> { for (Class<?> iface : entry.interfaces()) { final Class<?> serviceClass = iface.getEnclosingClass(); final EntryBuilder builder = map.computeIfAbsent(serviceClass, cls -> new EntryBuilder(serviceClass)); // Add all available endpoints. Accept only the services with exact and prefix path // mappings, whose endpoint path can be determined. final PathMapping pathMapping = c.pathMapping(); final String path = pathMapping.exactPath().orElse(pathMapping.prefix().orElse(null)); if (path != null) { builder.endpoint(new EndpointInfoBuilder(c.virtualHost().hostnamePattern(), path) .fragment(serviceName) .defaultFormat(service.defaultSerializationFormat()) .availableFormats(service.allowedSerializationFormats()) .build()); } } }); } final List<Entry> entries = map.values().stream() .map(EntryBuilder::build) .collect(Collectors.toList()); return generate(entries); }
final Map<String, ServiceEntryBuilder> map = new LinkedHashMap<>(); for (ServiceConfig serviceConfig : serviceConfigs) { final GrpcService grpcService = serviceConfig.service().as(GrpcService.class).get(); final ImmutableSet.Builder<MediaType> supportedMediaTypesBuilder = ImmutableSet.builder(); supportedMediaTypesBuilder.addAll(grpcService.supportedSerializationFormats() .stream() .map(SerializationFormat::mediaType)::iterator); if (serviceConfig.service().as(UnframedGrpcService.class).isPresent()) { if (grpcService.supportedSerializationFormats().contains(GrpcSerializationFormats.PROTO)) {
@Test public void makesSureDecoratedServiceIsAdded() { final Function<Service<HttpRequest, HttpResponse>, ? extends Service<HttpRequest, HttpResponse>> decorator = spy(new DecoratingFunction()); final AnnotatedServiceRegistrationBean bean = new AnnotatedServiceRegistrationBean() .setServiceName("test") .setService(new SimpleService()) .setDecorators(decorator); final ServerBuilder sb = new ServerBuilder(); configureAnnotatedHttpServices(sb, ImmutableList.of(bean), null); verify(decorator).apply(any()); assertThat(service(sb).as(SimpleDecorator.class)).isPresent(); }
@Test public void makesSureDecoratorsAreConfigured() { final Function<Service<HttpRequest, HttpResponse>, ? extends Service<HttpRequest, HttpResponse>> decorator = spy(new IdentityFunction()); final AnnotatedServiceRegistrationBean bean = new AnnotatedServiceRegistrationBean() .setServiceName("test") .setService(new SimpleService()) .setDecorators(decorator); final ServerBuilder sb1 = new ServerBuilder(); configureAnnotatedHttpServices(sb1, ImmutableList.of(bean), MeterIdPrefixFunctionFactory.DEFAULT); verify(decorator).apply(any()); assertThat(service(sb1).as(MetricCollectingService.class)).isPresent(); reset(decorator); final ServerBuilder sb2 = new ServerBuilder(); configureAnnotatedHttpServices(sb2, ImmutableList.of(bean), null); verify(decorator).apply(any()); assertThat(service(sb2)).isInstanceOf(AnnotatedHttpService.class); }
final boolean isTransient = service.as(TransientService.class).isPresent(); if (!isTransient) { gracefulShutdownSupport.inc();
private static ThriftCallService findThriftService(Service<?, ?> delegate) { return delegate.as(ThriftCallService.class).orElseThrow( () -> new IllegalStateException("service being decorated is not a ThriftCallService: " + delegate)); }
@Override public Map<String, String> loadDocStrings(Set<ServiceConfig> serviceConfigs) { return serviceConfigs.stream() .flatMap(c -> c.service().as(THttpService.class).get().entries().values().stream()) .flatMap(entry -> entry.interfaces().stream().map(Class::getClassLoader)) .flatMap(loader -> docstringExtractor.getAllDocStrings(loader) .entrySet().stream()) .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a)); }
@Override public Map<String, String> loadDocStrings(Set<ServiceConfig> serviceConfigs) { return serviceConfigs.stream() .flatMap(c -> c.service().as(THttpService.class).get().entries().values().stream()) .flatMap(entry -> entry.interfaces().stream().map(Class::getClassLoader)) .flatMap(loader -> docstringExtractor.getAllDocStrings(loader) .entrySet().stream()) .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a)); }