1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.maven.plugins.spring;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Properties;
22
23 import org.apache.maven.plugin.AbstractMojo;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.project.MavenProject;
26 import org.kuali.common.util.PropertyUtils;
27 import org.kuali.common.util.service.SpringService;
28 import org.springframework.util.Assert;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class LoadMojo extends AbstractMojo {
56
57
58
59
60
61
62
63
64 private MavenProject project;
65
66
67
68
69
70
71
72
73 private String location;
74
75
76
77
78
79
80 private List<String> locations;
81
82
83
84
85
86
87 private Properties properties;
88
89
90
91
92
93
94 private boolean injectProperties;
95
96
97
98
99
100
101 private boolean injectProject;
102
103
104
105
106
107
108
109 private String propertiesBeanName;
110
111
112
113
114
115
116 private String projectBeanName;
117
118
119
120
121
122
123
124 private String serviceClassname;
125
126 @Override
127 public void execute() throws MojoExecutionException {
128
129
130
131
132
133 this.properties = PropertyUtils.combine(project.getProperties(), properties, MavenUtils.getInternalProperties(project));
134
135
136 this.locations = combine(location, locations);
137
138
139 logConfiguration();
140
141
142 SpringService service = getService(serviceClassname);
143 service.load(locations, getBeanNames(), getBeans());
144 }
145
146 protected List<Object> getBeans() {
147 List<Object> beans = new ArrayList<Object>();
148 if (injectProperties) {
149 beans.add(properties);
150 }
151 if (injectProject) {
152 beans.add(project);
153 }
154 return beans;
155 }
156
157 protected List<String> getBeanNames() {
158 List<String> beanNames = new ArrayList<String>();
159 if (injectProperties) {
160 beanNames.add(propertiesBeanName);
161 }
162 if (injectProject) {
163 beanNames.add(projectBeanName);
164 }
165 return beanNames;
166 }
167
168 protected void logConfiguration() {
169 if (injectProperties) {
170 getLog().info("Injecting " + properties.size() + " Maven properties as a [" + properties.getClass().getName() + "] bean under the id [" + propertiesBeanName + "]");
171 getLog().debug("Displaying " + properties.size() + " properties\n\n" + PropertyUtils.toString(properties));
172 }
173 if (injectProject) {
174 getLog().info("Injecting the Maven project as a [" + project.getClass().getName() + "] bean under the id [" + projectBeanName + "]");
175 }
176 if (locations.size() > 1) {
177 getLog().info("Loading " + locations.size() + " Spring context files");
178 }
179 }
180
181 protected SpringService getService(String serviceClassname) {
182 try {
183 Class<?> serviceClass = Class.forName(serviceClassname);
184 return (SpringService) serviceClass.newInstance();
185 } catch (ClassNotFoundException e) {
186 throw new IllegalStateException("Unexpected error", e);
187 } catch (IllegalAccessException e) {
188 throw new IllegalStateException("Unexpected error", e);
189 } catch (InstantiationException e) {
190 throw new IllegalStateException("Unexpected error", e);
191 }
192 }
193
194
195
196
197 protected List<String> combine(String required, List<String> optional) {
198 Assert.notNull(required);
199 if (optional == null) {
200 return Collections.singletonList(required);
201 } else {
202 List<String> combined = new ArrayList<String>(optional);
203
204 combined.add(required);
205
206 for (String element : optional) {
207 boolean doesNotContain = !combined.contains(element);
208 Assert.isTrue(doesNotContain);
209 combined.add(element);
210 }
211 return combined;
212 }
213 }
214
215 public String getLocation() {
216 return location;
217 }
218
219 public void setLocation(String location) {
220 this.location = location;
221 }
222
223 public List<String> getLocations() {
224 return locations;
225 }
226
227 public void setLocations(List<String> locations) {
228 this.locations = locations;
229 }
230
231 public Properties getProperties() {
232 return properties;
233 }
234
235 public void setProperties(Properties properties) {
236 this.properties = properties;
237 }
238
239 public String getPropertiesBeanName() {
240 return propertiesBeanName;
241 }
242
243 public void setPropertiesBeanName(String propertiesBeanName) {
244 this.propertiesBeanName = propertiesBeanName;
245 }
246
247 public String getServiceClassname() {
248 return serviceClassname;
249 }
250
251 public void setServiceClassname(String serviceClassname) {
252 this.serviceClassname = serviceClassname;
253 }
254
255 public MavenProject getProject() {
256 return project;
257 }
258
259 public boolean isInjectProperties() {
260 return injectProperties;
261 }
262
263 public void setInjectProperties(boolean injectProperties) {
264 this.injectProperties = injectProperties;
265 }
266
267 public boolean isInjectProject() {
268 return injectProject;
269 }
270
271 public void setInjectProject(boolean injectProject) {
272 this.injectProject = injectProject;
273 }
274
275 public String getProjectBeanName() {
276 return projectBeanName;
277 }
278
279 public void setProjectBeanName(String projectBeanName) {
280 this.projectBeanName = projectBeanName;
281 }
282
283 }