1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.plugin.jboss.modules.domain;
17
18 import java.io.BufferedOutputStream;
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
26
27 import org.apache.commons.io.FileUtils;
28 import org.apache.commons.io.IOUtils;
29 import org.jdom2.Document;
30 import org.jdom2.output.Format;
31 import org.jdom2.output.XMLOutputter;
32 import org.sonatype.aether.artifact.Artifact;
33 import org.sonatype.aether.graph.Dependency;
34
35 import de.smartics.maven.plugin.jboss.modules.descriptor.ModuleDescriptor;
36 import de.smartics.maven.plugin.jboss.modules.xml.ModuleXmlBuilder;
37
38
39
40
41 public final class ModuleBuilder
42 {
43
44
45
46
47
48
49
50
51
52 private final ExecutionContext context;
53
54
55
56
57 private final ModuleDescriptor module;
58
59
60
61
62 private final List<Dependency> dependencies;
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public ModuleBuilder(final ExecutionContext context,
77 final ModuleDescriptor module, final Collection<Dependency> dependencies)
78 {
79 this.context = context;
80 this.module = module;
81 this.dependencies = new ArrayList<Dependency>(dependencies);
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public void create() throws IOException
101 {
102 if (!module.getDirectives().getSkip())
103 {
104 final File moduleFolder = createModuleFolder();
105 createModuleXml(moduleFolder);
106 copyResources(moduleFolder);
107 }
108 }
109
110 private File createModuleFolder() throws IOException
111 {
112 final String path = module.getName().replace('.', '/');
113
114 final File folder = new File(context.getTargetFolder(), path);
115 final File slotFolder = new File(folder, calcSlot());
116 final boolean created = slotFolder.mkdirs();
117 if (!created)
118 {
119 throw new IOException(String.format(
120 "Cannot created folder '%s' for module '%s'.",
121 slotFolder.getAbsolutePath(), module.getName()));
122 }
123 return slotFolder;
124 }
125
126 private String calcSlot()
127 {
128 final SlotStrategy strategy = context.getSlotStrategy();
129
130 final Artifact artifact =
131 (dependencies.isEmpty() ? null : dependencies.get(0).getArtifact());
132
133 final String moduleSlot = module.getSlot();
134 final String defaultSlot = context.getDefaultSlot();
135 final String slot = strategy.calcSlot(defaultSlot, moduleSlot, artifact);
136 return slot;
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 private void createModuleXml(final File moduleFolder) throws IOException
162 {
163 final ModuleXmlBuilder xml =
164 new ModuleXmlBuilder(context, module, dependencies);
165 final XMLOutputter outputter = new XMLOutputter();
166 outputter.setFormat(Format.getPrettyFormat());
167 final File file = new File(moduleFolder, "module.xml");
168 OutputStream out = null;
169 try
170 {
171 out = new BufferedOutputStream(new FileOutputStream(file));
172 final Document document = xml.build();
173 outputter.output(document, out);
174 }
175 finally
176 {
177 IOUtils.closeQuietly(out);
178 }
179 }
180
181 private void copyResources(final File moduleFolder) throws IOException
182 {
183 for (final Dependency dependency : dependencies)
184 {
185 final Artifact artifact = dependency.getArtifact();
186 final File remoteFile = artifact.getFile();
187 if (remoteFile != null)
188 {
189 final File localFile = new File(moduleFolder, remoteFile.getName());
190 FileUtils.copyFile(remoteFile, localFile);
191 }
192 else
193 {
194 context.getLog().warn(
195 String.format(
196 "Cannot copy non-existing remote file for dependency '%s'.",
197 dependency.getArtifact()));
198 }
199 }
200 }
201
202
203
204 }