Skip to main content
summaryrefslogtreecommitdiffstats
blob: 654ec465bac7a35c9a57b275229d4667fab0edff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*******************************************************************************
 * Copyright (c) 2013 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.orcs.rest.client.internal;

import java.util.Map;
import org.eclipse.osee.orcs.rest.client.OseeClient;
import com.google.inject.AbstractModule;
import com.google.inject.TypeLiteral;
import com.google.inject.matcher.AbstractMatcher;
import com.google.inject.matcher.Matcher;
import com.google.inject.spi.InjectionListener;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.spi.TypeListener;

/**
 * @author Roberto E. Escobar
 */
public class OrcsClientModule extends AbstractModule {

   private final Map<String, Object> config;

   public OrcsClientModule(Map<String, Object> config) {
      super();
      this.config = config;
   }

   @Override
   protected void configure() {
      bind(OseeClient.class).to(OseeClientImpl.class);
      TypeListener listener = new TypeListener() {

         @Override
         public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
            encounter.register(new InjectionListener<I>() {

               @Override
               public void afterInjection(I injectee) {
                  OseeClientImpl client = (OseeClientImpl) injectee;
                  client.start(config);
               }
            });
         }
      };
      bindListener(subtypeOf(OseeClientImpl.class), listener);
   }

   private static Matcher<? super TypeLiteral<?>> subtypeOf(Class<?> superclass) {
      return new SubTypeOfMatcher(TypeLiteral.get(OseeClientImpl.class));
   }

   private static final class SubTypeOfMatcher extends AbstractMatcher<TypeLiteral<?>> {
      private final TypeLiteral<OseeClientImpl> superType;

      public SubTypeOfMatcher(TypeLiteral<OseeClientImpl> superType) {
         super();
         this.superType = superType;
      }

      @Override
      public boolean matches(TypeLiteral<?> subType) {
         return subType.equals(superType) || superType.getRawType().isAssignableFrom(subType.getRawType());
      }
   }

}

Back to the top