View Javadoc

1   /*
2    * Copyright 2006-2012 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.buildmetadata.io;
17  
18  import java.io.BufferedInputStream;
19  import java.io.BufferedOutputStream;
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileNotFoundException;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  import java.util.Properties;
28  
29  import org.apache.commons.io.IOUtils;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugin.logging.Log;
32  import org.apache.maven.project.MavenProject;
33  
34  import de.smartics.maven.plugin.buildmetadata.common.MojoUtils;
35  import de.smartics.maven.plugin.buildmetadata.common.SortedProperties;
36  
37  /**
38   * Helper to handle the build meta data properties file.
39   *
40   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
41   * @version $Revision:591 $
42   */
43  public final class BuildPropertiesFileHelper
44  {
45    // ********************************* Fields *********************************
46  
47    // --- constants ------------------------------------------------------------
48  
49    // --- members --------------------------------------------------------------
50  
51    /**
52     * The logger to use.
53     */
54    private final Log log;
55  
56    /**
57     * The file to write to.
58     */
59    private final File propertiesOutputFile;
60  
61    // ****************************** Initializer *******************************
62  
63    // ****************************** Constructors ******************************
64  
65    /**
66     * Default constructor.
67     *
68     * @param log the logger to use.
69     * @param propertiesOutputFile the file to write to.
70     */
71    public BuildPropertiesFileHelper(final Log log,
72        final File propertiesOutputFile)
73    {
74      this.log = log;
75      this.propertiesOutputFile = propertiesOutputFile;
76    }
77  
78    // ****************************** Inner Classes *****************************
79  
80    // ********************************* Methods ********************************
81  
82    // --- init -----------------------------------------------------------------
83  
84    // --- get&set --------------------------------------------------------------
85  
86    // --- business -------------------------------------------------------------
87  
88    /**
89     * Writes the build meta data properties to the target file.
90     *
91     * @param buildMetaDataProperties the properties to write.
92     * @return the reference to the written file.
93     * @throws MojoExecutionException on any problem encountered while writing the
94     *           properties.
95     */
96    public File writePropertiesFile(final Properties buildMetaDataProperties)
97      throws MojoExecutionException
98    {
99      final File buildMetaDataFile =
100         createBuildMetaDataFile(propertiesOutputFile);
101     if (log.isInfoEnabled())
102     {
103       log.info("Writing properties '" + buildMetaDataFile.getAbsolutePath()
104                + "'...");
105     }
106 
107     OutputStream out = null;
108     try
109     {
110       out = new BufferedOutputStream(new FileOutputStream(buildMetaDataFile));
111       final String comments = "Created by maven-buildmetadata-plugin.";
112       final Properties sortedBuildMetaDataProperties =
113           SortedProperties.createSorted(buildMetaDataProperties);
114       sortedBuildMetaDataProperties.store(out, comments);
115     }
116     catch (final FileNotFoundException e)
117     {
118       final String message =
119           "Cannot find file '" + buildMetaDataFile
120               + "' to write properties to.";
121       throw MojoUtils.createException(log, e, message);
122     }
123     catch (final IOException e)
124     {
125       final String message =
126           "Cannot write properties to file '" + buildMetaDataFile + "'.";
127       throw MojoUtils.createException(log, e, message);
128     }
129     finally
130     {
131       IOUtils.closeQuietly(out);
132     }
133 
134     return buildMetaDataFile;
135   }
136 
137   /**
138    * Creates the properties file for the build meta data. If the directory to
139    * place it in is not present, it will be created.
140    *
141    * @return the file to write the build properties to.
142    * @throws MojoExecutionException if the output directory is not present and
143    *           cannot be created.
144    */
145   private File createBuildMetaDataFile(final File propertiesOutputFile)
146     throws MojoExecutionException
147   {
148     final File outputDirectory = propertiesOutputFile.getParentFile();
149     if (!outputDirectory.exists())
150     {
151       final boolean created = outputDirectory.mkdirs();
152       if (!created)
153       {
154         throw new MojoExecutionException("Cannot create output directory '"
155                                          + outputDirectory + "'.");
156       }
157     }
158     return propertiesOutputFile;
159   }
160 
161   /**
162    * Reads the build properties file from stream. The properties file is passed
163    * to this instance via the {@link #BuildPropertiesFileHelper(Log, File)
164    * constructor} {@code propertiesOutputFile}.
165    *
166    * @param buildMetaDataProperties the properties instance to append the read
167    *          properties to.
168    * @throws MojoExecutionException if the properties cannot be read.
169    */
170   public void readBuildPropertiesFile(final Properties buildMetaDataProperties)
171     throws MojoExecutionException
172   {
173     InputStream inStream = null;
174     try
175     {
176       inStream =
177           new BufferedInputStream(new FileInputStream(propertiesOutputFile));
178       buildMetaDataProperties.load(inStream);
179     }
180     catch (final IOException e)
181     {
182       throw new MojoExecutionException(
183           "Cannot read provided properties file: "
184               + propertiesOutputFile.getAbsolutePath(), e);
185     }
186     finally
187     {
188       IOUtils.closeQuietly(inStream);
189     }
190   }
191 
192   /**
193    * Fetches the project properties and if <code>null</code> returns a new empty
194    * properties instance that is associated with the project.
195    *
196    * @param project the project whose properties are requested.
197    * @return the properties of the project.
198    */
199   public Properties getProjectProperties(final MavenProject project)
200   {
201     Properties projectProperties = project.getProperties();
202     if (projectProperties == null)
203     {
204       projectProperties = new Properties();
205       project.getModel().setProperties(projectProperties);
206     }
207 
208     return projectProperties;
209   }
210 
211   // --- object basics --------------------------------------------------------
212 
213 }