Merge "Add Mycilla code to odlguice"
[odlguice.git] / inject / inject-guice / src / main / java / org / opendaylight / odlguice / inject / guice / GuiceClassPathBinder.java
1 /*
2  * Copyright (c) 2018 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.odlguice.inject.guice;
9
10 import com.google.inject.Binder;
11 import javax.inject.Singleton;
12 import org.opendaylight.odlguice.inject.ClassPathScanner;
13
14 /**
15  * Binds interfaces to implementations in Guice by scanning the classpath.
16  */
17 public class GuiceClassPathBinder {
18     private final ClassPathScanner scanner;
19
20     public GuiceClassPathBinder(String prefix) {
21         this.scanner = new ClassPathScanner(prefix);
22     }
23
24     /**
25      * Binds all {@link Singleton} annotated classes discovered by scanning the class path to all their interfaces.
26      *
27      * @param prefix the package prefix of Singleton implementations to consider
28      * @param binder The binder to set up.
29      */
30     @SuppressWarnings("unchecked")
31     public void bindAllSingletons(String prefix, Binder binder) {
32         scanner.bindAllSingletons(prefix,
33             (contract, implementation) -> binder.bind(contract).to(implementation),
34             singleton -> binder.bind(singleton));
35     }
36 }