View Javadoc

1   package org.apache.onami.guava.eventbus;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static com.google.common.base.Preconditions.checkArgument;
23  import static com.google.inject.matcher.Matchers.any;
24  
25  import static com.google.inject.name.Names.named;
26  
27  import com.google.common.eventbus.EventBus;
28  import com.google.inject.AbstractModule;
29  import com.google.inject.TypeLiteral;
30  import com.google.inject.matcher.Matcher;
31  import com.google.inject.spi.InjectionListener;
32  import com.google.inject.spi.TypeEncounter;
33  import com.google.inject.spi.TypeListener;
34  
35  /**
36   * This class was originally developed by <a href="http://spin.atomicobject.com/author/dewind/">Justin DeWind</a>
37   * on <a href="http://spin.atomicobject.com/2012/01/13/the-guava-eventbus-on-guice/">Atomicobject</a> blog, under the
38   * terms of the MIT License.
39   */
40  public abstract class EventBusModule
41      extends AbstractModule
42  {
43  
44      protected BusMatcher bindBus( final String identifier )
45      {
46          checkArgument( identifier != null, "Event bus identifier must be not null" );
47  
48          return new BusMatcher()
49          {
50  
51              public void toAnyBoundClass()
52              {
53                  to( any() );
54              }
55  
56              public void to( Matcher<? super TypeLiteral<?>> matcher )
57              {
58                  checkArgument( matcher != null, "Event bus matcher must be not null" );
59  
60                  final EventBus eventBus = new EventBus( identifier );
61  
62                  bind( EventBus.class ).annotatedWith( named( identifier ) ).toInstance( eventBus );
63  
64                  bindListener( matcher, new TypeListener()
65                  {
66                      public <I> void hear( TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter )
67                      {
68                          typeEncounter.register( new InjectionListener<I>()
69                          {
70                              public void afterInjection( I injectee )
71                              {
72                                  eventBus.register( injectee );
73                              }
74                          } );
75                      }
76                  } );
77              }
78  
79          };
80      }
81  
82  }