• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.server;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 
21 import dagger.Module;
22 import dagger.Provides;
23 import io.grpc.Server;
24 import io.grpc.ServerBuilder;
25 import io.grpc.inprocess.InProcessServerBuilder;
26 import javax.inject.Singleton;
27 
28 /**
29  * Installing this module into a {@link Singleton @Singleton} component means the component can
30  * provide a {@link Server} that serves {@linkplain InProcessServerBuilder in-process} requests.
31  */
32 @Module(includes = ServerModule.class)
33 public final class InProcessServerModule {
34 
35   private final String name;
36 
InProcessServerModule(String name)37   private InProcessServerModule(String name) {
38     this.name = checkNotNull(name);
39   }
40 
41   /**
42    * Creates a module that provides a server that binds to a given name
43    *
44    * @param name the identity of the server for clients to connect to
45    */
serverNamed(String name)46   public static InProcessServerModule serverNamed(String name) {
47     return new InProcessServerModule(name);
48   }
49 
50   @Provides
serverBuilder()51   ServerBuilder<?> serverBuilder() {
52     return InProcessServerBuilder.forName(name);
53   }
54 }
55