View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.guice;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleRuntimeException;
11  import org.mule.api.NamedObject;
12  import org.mule.api.agent.Agent;
13  import org.mule.api.context.MuleContextAware;
14  import org.mule.api.registry.RegistrationException;
15  import org.mule.api.transport.Connector;
16  import org.mule.config.i18n.CoreMessages;
17  
18  import com.google.inject.AbstractModule;
19  import com.google.inject.MembersInjector;
20  import com.google.inject.TypeLiteral;
21  import com.google.inject.internal.ProviderMethod;
22  import com.google.inject.matcher.Matchers;
23  import com.google.inject.name.Named;
24  import com.google.inject.spi.InjectionListener;
25  import com.google.inject.spi.TypeEncounter;
26  import com.google.inject.spi.TypeListener;
27  
28  /**
29   * TODO
30   */
31  public class MuleSupportModule extends AbstractModule
32  {
33      protected MuleContext muleContext;
34  
35      public MuleSupportModule(MuleContext muleContext)
36      {
37          this.muleContext = muleContext;
38      }
39  
40      @Override
41      protected final void configure()
42      {
43  
44          bindListener(Matchers.any(), new TypeListener()
45          {
46              public <I> void hear(TypeLiteral<I> iTypeLiteral, TypeEncounter<I> iTypeEncounter)
47              {
48                  //iTypeEncounter.register(new MuleRegistryInjectionLister());
49                  iTypeEncounter.register(new MuleContextAwareInjector<I>());
50                  iTypeEncounter.register(new MuleBindInjector<I>());
51              }
52          });
53          bind(MuleContext.class).toInstance(muleContext);
54      }
55  
56  
57      class MuleContextAwareInjector<I> implements MembersInjector<I>
58      {
59          public void injectMembers(I o)
60          {
61              if(o instanceof MuleContextAware)
62              {
63                  ((MuleContextAware)o).setMuleContext(muleContext);
64              }
65          }
66      }
67  
68      class MuleBindInjector<I> implements InjectionListener<I>
69      {
70          public void afterInjection(I i)
71          {
72              if(i instanceof ProviderMethod)
73              {
74                  Class type = ((ProviderMethod)i).getKey().getTypeLiteral().getRawType();
75                  boolean bindRequired = (type.equals(Connector.class) || type.equals(Agent.class));
76  
77                  Named bindTo = ((ProviderMethod)i).getMethod().getAnnotation(Named.class);
78                  if(bindTo!=null)
79                  {
80                      try
81                      {
82                          Object o = ((ProviderMethod)i).get();
83                          if(o instanceof NamedObject)
84                          {
85                              ((NamedObject)o).setName(bindTo.value());
86                          }
87                          muleContext.getRegistry().registerObject(bindTo.value(), o);
88                      }
89                      catch (RegistrationException e)
90                      {
91                          throw new MuleRuntimeException(CoreMessages.createStaticMessage("failed to bind " + bindTo.value()));
92                      }
93                  }
94                  else if(bindRequired)
95                  {
96                      throw new RuntimeException("Provider object type: " + type + ", must have a @Named annotation so that the object can be bound in Mule");
97                  }
98              }
99          }
100     }
101 
102     
103 }