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