1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.module.guice; |
11 | |
|
12 | |
import org.mule.api.MuleContext; |
13 | |
import org.mule.api.config.ConfigurationException; |
14 | |
import org.mule.config.builders.AbstractConfigurationBuilder; |
15 | |
import org.mule.config.i18n.CoreMessages; |
16 | |
import org.mule.util.ClassUtils; |
17 | |
import org.mule.util.scan.ClasspathScanner; |
18 | |
|
19 | |
import com.google.inject.Guice; |
20 | |
import com.google.inject.Injector; |
21 | |
import com.google.inject.Module; |
22 | |
import com.google.inject.Stage; |
23 | |
|
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Arrays; |
26 | |
import java.util.List; |
27 | |
import java.util.Set; |
28 | |
|
29 | |
import org.guiceyfruit.mule.MuleModule; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class GuiceConfigurationBuilder extends AbstractConfigurationBuilder |
41 | |
{ |
42 | |
public static final String DEFAULT_PACKAGE = ""; |
43 | |
|
44 | 0 | protected String basepath = DEFAULT_PACKAGE; |
45 | |
|
46 | 0 | protected Module[] modules = null; |
47 | |
|
48 | |
protected Stage stage; |
49 | |
|
50 | |
protected ClassLoader classLoader; |
51 | |
|
52 | |
public GuiceConfigurationBuilder() |
53 | |
{ |
54 | 0 | super(); |
55 | 0 | classLoader = Thread.currentThread().getContextClassLoader(); |
56 | 0 | } |
57 | |
|
58 | |
public GuiceConfigurationBuilder(ClassLoader classLoader) |
59 | 0 | { |
60 | 0 | this.classLoader = classLoader; |
61 | 0 | } |
62 | |
|
63 | |
public GuiceConfigurationBuilder(String basepath) |
64 | |
{ |
65 | 0 | this(); |
66 | 0 | this.basepath = basepath; |
67 | 0 | } |
68 | |
|
69 | |
public GuiceConfigurationBuilder(String basepath, ClassLoader classLoader) |
70 | 0 | { |
71 | 0 | this.basepath = basepath; |
72 | 0 | this.classLoader = classLoader; |
73 | 0 | } |
74 | |
|
75 | |
public GuiceConfigurationBuilder(Module... modules) |
76 | 0 | { |
77 | 0 | this.modules = modules; |
78 | 0 | } |
79 | |
|
80 | |
public GuiceConfigurationBuilder(Stage stage, Module... modules) |
81 | 0 | { |
82 | 0 | this.stage = stage; |
83 | 0 | this.modules = modules; |
84 | 0 | } |
85 | |
|
86 | |
protected void doConfigure(MuleContext muleContext) throws Exception |
87 | |
{ |
88 | |
|
89 | 0 | List<Module> allModules = getSystemModules(muleContext); |
90 | |
|
91 | |
Injector injector; |
92 | 0 | if (basepath != null && basepath.startsWith("/")) |
93 | |
{ |
94 | 0 | basepath = basepath.substring(1); |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | 0 | if (modules == null) |
100 | |
{ |
101 | 0 | ClasspathScanner scanner = new ClasspathScanner(classLoader, basepath); |
102 | 0 | Set<Class> classes = scanner.scanFor(Module.class); |
103 | 0 | Set<Class> factories = scanner.scanFor(GuiceModuleFactory.class); |
104 | |
|
105 | 0 | if (classes.size() == 0 && factories.size() == 0) |
106 | |
{ |
107 | |
try |
108 | |
{ |
109 | 0 | basepath = getClass().getClassLoader().getResources(basepath).toString(); |
110 | |
} |
111 | 0 | catch (Exception e) |
112 | |
{ |
113 | 0 | basepath = (basepath.equals("") ? "/" : basepath); |
114 | 0 | } |
115 | |
|
116 | 0 | logger.warn(new ConfigurationException(CoreMessages.createStaticMessage("There are no Guice modules or module factories on the classpath under: " + basepath))); |
117 | 0 | return; |
118 | |
} |
119 | |
|
120 | 0 | for (Class moduleClass : classes) |
121 | |
{ |
122 | 0 | allModules.add((Module) ClassUtils.instanciateClass(moduleClass, ClassUtils.NO_ARGS)); |
123 | |
} |
124 | 0 | for (Class factoryClass : factories) |
125 | |
{ |
126 | 0 | GuiceModuleFactory factory = (GuiceModuleFactory) ClassUtils.instanciateClass(factoryClass, ClassUtils.NO_ARGS); |
127 | 0 | allModules.add(factory.createModule()); |
128 | 0 | } |
129 | 0 | } |
130 | |
else |
131 | |
{ |
132 | 0 | allModules.addAll(Arrays.asList(modules)); |
133 | |
} |
134 | |
|
135 | 0 | for (Module module : allModules) |
136 | |
{ |
137 | 0 | if (module instanceof AbstractMuleGuiceModule) |
138 | |
{ |
139 | 0 | ((AbstractMuleGuiceModule) module).setMuleContext(muleContext); |
140 | |
} |
141 | |
} |
142 | |
|
143 | 0 | if (stage != null) |
144 | |
{ |
145 | 0 | injector = Guice.createInjector(stage, allModules); |
146 | |
} |
147 | |
else |
148 | |
{ |
149 | 0 | injector = Guice.createInjector(allModules); |
150 | |
} |
151 | |
|
152 | 0 | GuiceRegistry registry = new GuiceRegistry(injector, muleContext); |
153 | 0 | registry.initialise(); |
154 | 0 | muleContext.addRegistry(registry); |
155 | 0 | } |
156 | |
|
157 | |
protected List<Module> getSystemModules(MuleContext muleContext) |
158 | |
{ |
159 | 0 | List<Module> modules = new ArrayList<Module>(); |
160 | |
|
161 | 0 | modules.add(new MuleModule()); |
162 | 0 | modules.add(new MuleSupportModule(muleContext)); |
163 | 0 | return modules; |
164 | |
} |
165 | |
} |