View Javadoc

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