1 /*
2  * Copyright (C) 2016 The Dagger Authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dagger.grpc.functional.server;
18 
19 import dagger.Component;
20 import dagger.Module;
21 import dagger.Provides;
22 import dagger.Subcomponent;
23 import dagger.grpc.functional.server.CoffeeServerWithCallScopeService.CallScopeServiceModule;
24 import dagger.grpc.functional.server.CountingInterceptor.CountingInterceptorModule;
25 import dagger.grpc.server.CallScoped;
26 import dagger.grpc.server.GrpcCallMetadataModule;
27 import dagger.grpc.server.InProcessServerModule;
28 import javax.inject.Singleton;
29 
30 @Singleton
31 @Component(modules = {InProcessServerModule.class, CallScopeServiceModule.class})
32 abstract class CoffeeServerWithCallScopeService
33     extends CoffeeServer<CoffeeServerWithCallScopeService> {
34 
35   @Component.Builder
36   interface Builder extends CoffeeServer.Builder<CoffeeServerWithCallScopeService> {}
37 
baristaCallScope(GrpcCallMetadataModule callMetadataModule)38   abstract BaristaCallScope baristaCallScope(GrpcCallMetadataModule callMetadataModule);
39 
40   @CallScoped
41   @Subcomponent(
42     modules = {
43       GrpcCallMetadataModule.class,
44       FriendlyBaristaGrpcServiceModule.class,
45       CountingInterceptorModule.class
46     }
47   )
48   interface BaristaCallScope extends FriendlyBaristaServiceDefinition {}
49 
50   @Module(includes = FriendlyBaristaGrpcProxyModule.class)
51   static class CallScopeServiceModule {
52     @Provides
friendlyBaristaServiceDefinitionFactory( final CoffeeServerWithCallScopeService testServer)53     static FriendlyBaristaServiceDefinition.Factory friendlyBaristaServiceDefinitionFactory(
54         final CoffeeServerWithCallScopeService testServer) {
55       return new FriendlyBaristaServiceDefinition.Factory() {
56         @Override
57         public FriendlyBaristaServiceDefinition grpcService(
58             GrpcCallMetadataModule grpcCallMetadataModule) {
59           return testServer.baristaCallScope(grpcCallMetadataModule);
60         }
61       };
62     }
63   }
64 }
65