1 package org.apache.maven.plugin.ear;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.plugin.MojoFailureException;
24 import org.apache.maven.plugin.ear.util.ArtifactRepository;
25 import org.codehaus.plexus.util.xml.XMLWriter;
26
27 import java.util.Set;
28
29
30
31
32
33
34
35 public abstract class AbstractEarModule
36 implements EarModule
37 {
38
39 protected static final String MODULE_ELEMENT = "module";
40
41 protected static final String JAVA_MODULE = "java";
42
43 protected static final String ALT_DD = "alt-dd";
44
45 private Artifact artifact;
46
47
48
49 private String groupId;
50
51 private String artifactId;
52
53 private String classifier;
54
55 protected String bundleDir;
56
57 protected String bundleFileName;
58
59 protected Boolean excluded = Boolean.FALSE;
60
61 private String uri;
62
63 protected Boolean unpack = null;
64
65 protected String altDeploymentDescriptor;
66
67 private String moduleId;
68
69
70
71 protected EarExecutionContext earExecutionContext;
72
73
74
75
76
77 public AbstractEarModule()
78 {
79 }
80
81
82
83
84
85
86 public AbstractEarModule( Artifact a )
87 {
88 this.artifact = a;
89 this.groupId = a.getGroupId();
90 this.artifactId = a.getArtifactId();
91 this.classifier = a.getClassifier();
92 this.bundleDir = null;
93 }
94
95 public void setEarExecutionContext( EarExecutionContext earExecutionContext )
96 {
97 this.earExecutionContext = earExecutionContext;
98 }
99
100 public void resolveArtifact( Set artifacts )
101 throws EarPluginException, MojoFailureException
102 {
103
104 if ( artifact == null )
105 {
106
107 if ( groupId == null || artifactId == null )
108 {
109 throw new MojoFailureException(
110 "Could not resolve artifact[" + groupId + ":" + artifactId + ":" + getType() + "]" );
111 }
112 final ArtifactRepository ar = earExecutionContext.getArtifactRepository();
113 artifact = ar.getUniqueArtifact( groupId, artifactId, getType(), classifier );
114
115 if ( artifact == null )
116 {
117 Set candidates = ar.getArtifacts( groupId, artifactId, getType() );
118 if ( candidates.size() > 1 )
119 {
120 throw new MojoFailureException( "Artifact[" + this + "] has " + candidates.size() +
121 " candidates, please provide a classifier." );
122 }
123 else
124 {
125 throw new MojoFailureException( "Artifact[" + this + "] " + "is not a dependency of the project." );
126 }
127 }
128 }
129 }
130
131 public Artifact getArtifact()
132 {
133 return artifact;
134 }
135
136 public String getModuleId()
137 {
138 return moduleId;
139 }
140
141 public String getUri()
142 {
143 if ( uri == null )
144 {
145 if ( getBundleDir() == null )
146 {
147 uri = getBundleFileName();
148 }
149 else
150 {
151 uri = getBundleDir() + getBundleFileName();
152 }
153 }
154 return uri;
155 }
156
157
158
159
160
161
162 public String getGroupId()
163 {
164 return groupId;
165 }
166
167
168
169
170
171
172 public String getArtifactId()
173 {
174 return artifactId;
175 }
176
177
178
179
180
181
182 public String getClassifier()
183 {
184 return classifier;
185 }
186
187
188
189
190
191
192
193 public String getBundleDir()
194 {
195 if ( bundleDir != null )
196 {
197 bundleDir = cleanBundleDir( bundleDir );
198 }
199 return bundleDir;
200 }
201
202
203
204
205
206
207
208 public String getBundleFileName()
209 {
210 if ( bundleFileName == null )
211 {
212 bundleFileName = earExecutionContext.getFileNameMapping().mapFileName( artifact );
213 }
214 return bundleFileName;
215 }
216
217
218
219
220
221
222
223
224
225
226 public String getAltDeploymentDescriptor()
227 {
228 return altDeploymentDescriptor;
229 }
230
231
232
233
234
235
236 public boolean isExcluded()
237 {
238 return excluded.booleanValue();
239 }
240
241 public Boolean shouldUnpack()
242 {
243 return unpack;
244 }
245
246
247
248
249
250
251
252 protected void writeAltDeploymentDescriptor( XMLWriter writer, String version )
253 {
254 if ( getAltDeploymentDescriptor() != null )
255 {
256 writer.startElement( ALT_DD );
257 writer.writeText( getAltDeploymentDescriptor() );
258 writer.endElement();
259 }
260 }
261
262
263
264
265
266
267
268
269 protected void startModuleElement( XMLWriter writer, Boolean generateId )
270 {
271 writer.startElement( MODULE_ELEMENT );
272
273
274 if ( getModuleId() != null )
275 {
276 writer.addAttribute( "id", getModuleId() );
277 }
278 else if ( generateId.booleanValue() )
279 {
280
281 Artifact artifact = getArtifact();
282 String generatedId =
283 artifact.getType().toUpperCase() + "_" + artifact.getGroupId() + "." + artifact.getArtifactId();
284 if ( null != artifact.getClassifier() && artifact.getClassifier().trim().length() > 0 )
285 {
286 generatedId += "-" + artifact.getClassifier().trim();
287 }
288 writer.addAttribute( "id", generatedId );
289 }
290 }
291
292 public String toString()
293 {
294 StringBuffer sb = new StringBuffer();
295 sb.append( getType() ).append( ":" ).append( groupId ).append( ":" ).append( artifactId );
296 if ( classifier != null )
297 {
298 sb.append( ":" ).append( classifier );
299 }
300 if ( artifact != null )
301 {
302 sb.append( ":" ).append( artifact.getVersion() );
303 }
304 return sb.toString();
305 }
306
307
308
309
310
311
312
313
314 static String cleanBundleDir( String bundleDir )
315 {
316 if ( bundleDir == null )
317 {
318 return bundleDir;
319 }
320
321
322 bundleDir = bundleDir.replace( '\\', '/' );
323
324
325 if ( bundleDir.startsWith( "/" ) )
326 {
327 bundleDir = bundleDir.substring( 1, bundleDir.length() );
328 }
329
330 if ( bundleDir.length() > 0 && !bundleDir.endsWith( "/" ) )
331 {
332
333 bundleDir = bundleDir + "/";
334 }
335
336 return bundleDir;
337 }
338
339
340
341
342
343
344
345
346 static boolean areNullOrEqual( Object first, Object second )
347 {
348 if ( first != null )
349 {
350 return first.equals( second );
351 }
352 else
353 {
354 return second == null;
355 }
356 }
357
358
359
360
361
362
363 void setUri( String uri )
364 {
365 this.uri = uri;
366
367 }
368 }