1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
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 | |
|
30 | |
|
31 | |
public class MuleSupportModule extends AbstractModule |
32 | |
{ |
33 | |
protected MuleContext muleContext; |
34 | |
|
35 | |
public MuleSupportModule(MuleContext muleContext) |
36 | 0 | { |
37 | 0 | this.muleContext = muleContext; |
38 | 0 | } |
39 | |
|
40 | |
@Override |
41 | |
protected final void configure() |
42 | |
{ |
43 | |
|
44 | 0 | bindListener(Matchers.any(), new TypeListener() |
45 | 0 | { |
46 | |
public <I> void hear(TypeLiteral<I> iTypeLiteral, TypeEncounter<I> iTypeEncounter) |
47 | |
{ |
48 | |
|
49 | 0 | iTypeEncounter.register(new MuleContextAwareInjector<I>()); |
50 | 0 | iTypeEncounter.register(new MuleBindInjector<I>()); |
51 | 0 | } |
52 | |
}); |
53 | 0 | bind(MuleContext.class).toInstance(muleContext); |
54 | 0 | } |
55 | |
|
56 | |
|
57 | 0 | class MuleContextAwareInjector<I> implements MembersInjector<I> |
58 | |
{ |
59 | |
public void injectMembers(I o) |
60 | |
{ |
61 | 0 | if(o instanceof MuleContextAware) |
62 | |
{ |
63 | 0 | ((MuleContextAware)o).setMuleContext(muleContext); |
64 | |
} |
65 | 0 | } |
66 | |
} |
67 | |
|
68 | 0 | class MuleBindInjector<I> implements InjectionListener<I> |
69 | |
{ |
70 | |
public void afterInjection(I i) |
71 | |
{ |
72 | 0 | if(i instanceof ProviderMethod) |
73 | |
{ |
74 | 0 | Class type = ((ProviderMethod)i).getKey().getTypeLiteral().getRawType(); |
75 | 0 | boolean bindRequired = (type.equals(Connector.class) || type.equals(Agent.class)); |
76 | |
|
77 | 0 | Named bindTo = ((ProviderMethod)i).getMethod().getAnnotation(Named.class); |
78 | 0 | if(bindTo!=null) |
79 | |
{ |
80 | |
try |
81 | |
{ |
82 | 0 | Object o = ((ProviderMethod)i).get(); |
83 | 0 | if(o instanceof NamedObject) |
84 | |
{ |
85 | 0 | ((NamedObject)o).setName(bindTo.value()); |
86 | |
} |
87 | 0 | muleContext.getRegistry().registerObject(bindTo.value(), o); |
88 | |
} |
89 | 0 | catch (RegistrationException e) |
90 | |
{ |
91 | 0 | throw new MuleRuntimeException(CoreMessages.createStaticMessage("failed to bind " + bindTo.value())); |
92 | 0 | } |
93 | |
} |
94 | 0 | else if(bindRequired) |
95 | |
{ |
96 | 0 | throw new RuntimeException("Provider object type: " + type + ", must have a @Named annotation so that the object can be bound in Mule"); |
97 | |
} |
98 | |
} |
99 | 0 | } |
100 | |
} |
101 | |
|
102 | |
|
103 | |
} |