View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Creates a single module within the archive of modules.
40   */
41  public final class ModuleBuilder
42  {
43    // ********************************* Fields *********************************
44  
45    // --- constants ------------------------------------------------------------
46  
47    // --- members --------------------------------------------------------------
48  
49    /**
50     * The configuration to control the building of the modules archive.
51     */
52    private final ExecutionContext context;
53  
54    /**
55     * The descriptor of the module to build.
56     */
57    private final ModuleDescriptor module;
58  
59    /**
60     * The artifacts that are part of this module.
61     */
62    private final List<Dependency> dependencies;
63  
64    // ****************************** Initializer *******************************
65  
66    // ****************************** Constructors ******************************
67  
68    /**
69     * Default constructor.
70     *
71     * @param context the configuration to control the building of the modules
72     *          archive.
73     * @param module the descriptor of the module to build.
74     * @param dependencies the dependencies that are part of this module.
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    // ****************************** Inner Classes *****************************
85  
86    // ********************************* Methods ********************************
87  
88    // --- init -----------------------------------------------------------------
89  
90    // --- get&set --------------------------------------------------------------
91  
92    // --- business -------------------------------------------------------------
93  
94    /**
95     * Creates the module.
96     *
97     * @throws IOException on any problem creating the module in the given base
98     *           directory.
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 //  private static String calcSlot(final SlotStrategy strategy,
140 //      final String defaultSlot, final String moduleSlot, final Artifact artifact)
141 //  {
142 //    final String slot;
143 //    if (StringUtils.isBlank(moduleSlot) || strategy != SlotStrategy.MAIN)
144 //    {
145 //      if (artifact != null)
146 //      {
147 //        slot = strategy.calcSlot(artifact, defaultSlot);
148 //      }
149 //      else
150 //      {
151 //        slot = defaultSlot;
152 //      }
153 //    }
154 //    else
155 //    {
156 //      slot = moduleSlot;
157 //    }
158 //    return slot;
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   // --- object basics --------------------------------------------------------
203 
204 }