KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > BuildNumber


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Properties JavaDoc;
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Task;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.util.FileUtils;
29
30 /**
31  * Read, increment, and write a build number in a file
32  * It will first
33  * attempt to read a build number from a file, then set the property
34  * "build.number" to the value that was read in (or 0 if no such value). Then
35  * it will increment the build number by one and write it back out into the
36  * file.
37  *
38  * @since Ant 1.5
39  * @ant.task name="buildnumber"
40  */

41 public class BuildNumber
42      extends Task {
43     /**
44      * The name of the property in which the build number is stored.
45      */

46     private static final String JavaDoc DEFAULT_PROPERTY_NAME = "build.number";
47
48     /** The default filename to use if no file specified. */
49     private static final String JavaDoc DEFAULT_FILENAME = DEFAULT_PROPERTY_NAME;
50
51     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
52
53     /** The File in which the build number is stored. */
54     private File JavaDoc myFile;
55
56
57     /**
58      * The file in which the build number is stored. Defaults to
59      * "build.number" if not specified.
60      *
61      * @param file the file in which build number is stored.
62      */

63     public void setFile(final File JavaDoc file) {
64         myFile = file;
65     }
66
67
68     /**
69      * Run task.
70      *
71      * @exception BuildException if an error occurs
72      */

73     public void execute()
74          throws BuildException {
75         File JavaDoc savedFile = myFile; // may be altered in validate
76

77         validate();
78
79         final Properties JavaDoc properties = loadProperties();
80         final int buildNumber = getBuildNumber(properties);
81
82         properties.put(DEFAULT_PROPERTY_NAME,
83             String.valueOf(buildNumber + 1));
84
85         // Write the properties file back out
86
FileOutputStream JavaDoc output = null;
87
88         try {
89             output = new FileOutputStream JavaDoc(myFile);
90
91             final String JavaDoc header = "Build Number for ANT. Do not edit!";
92
93             properties.store(output, header);
94         } catch (final IOException JavaDoc ioe) {
95             final String JavaDoc message = "Error while writing " + myFile;
96
97             throw new BuildException(message, ioe);
98         } finally {
99             if (null != output) {
100                 try {
101                     output.close();
102                 } catch (final IOException JavaDoc ioe) {
103                     log("error closing output stream " + ioe, Project.MSG_ERR);
104                 }
105             }
106             myFile = savedFile;
107         }
108
109         //Finally set the property
110
getProject().setNewProperty(DEFAULT_PROPERTY_NAME,
111             String.valueOf(buildNumber));
112     }
113
114
115     /**
116      * Utility method to retrieve build number from properties object.
117      *
118      * @param properties the properties to retrieve build number from
119      * @return the build number or if no number in properties object
120      * @throws BuildException if build.number property is not an integer
121      */

122     private int getBuildNumber(final Properties JavaDoc properties)
123          throws BuildException {
124         final String JavaDoc buildNumber =
125             properties.getProperty(DEFAULT_PROPERTY_NAME, "0").trim();
126
127         // Try parsing the line into an integer.
128
try {
129             return Integer.parseInt(buildNumber);
130         } catch (final NumberFormatException JavaDoc nfe) {
131             final String JavaDoc message =
132                 myFile + " contains a non integer build number: " + buildNumber;
133             throw new BuildException(message, nfe);
134         }
135     }
136
137
138     /**
139      * Utility method to load properties from file.
140      *
141      * @return the loaded properties
142      * @throws BuildException
143      */

144     private Properties JavaDoc loadProperties()
145          throws BuildException {
146         FileInputStream JavaDoc input = null;
147
148         try {
149             final Properties JavaDoc properties = new Properties JavaDoc();
150
151             input = new FileInputStream JavaDoc(myFile);
152             properties.load(input);
153             return properties;
154         } catch (final IOException JavaDoc ioe) {
155             throw new BuildException(ioe);
156         } finally {
157             if (null != input) {
158                 try {
159                     input.close();
160                 } catch (final IOException JavaDoc ioe) {
161                     log("error closing input stream " + ioe, Project.MSG_ERR);
162                 }
163             }
164         }
165     }
166
167
168     /**
169      * Validate that the task parameters are valid.
170      *
171      * @throws BuildException if parameters are invalid
172      */

173     private void validate()
174          throws BuildException {
175         if (null == myFile) {
176             myFile = FILE_UTILS.resolveFile(getProject().getBaseDir(), DEFAULT_FILENAME);
177         }
178
179         if (!myFile.exists()) {
180             try {
181                 FILE_UTILS.createNewFile(myFile);
182             } catch (final IOException JavaDoc ioe) {
183                 final String JavaDoc message =
184                     myFile + " doesn't exist and new file can't be created.";
185                 throw new BuildException(message, ioe);
186             }
187         }
188
189         if (!myFile.canRead()) {
190             final String JavaDoc message = "Unable to read from " + myFile + ".";
191             throw new BuildException(message);
192         }
193
194         if (!myFile.canWrite()) {
195             final String JavaDoc message = "Unable to write to " + myFile + ".";
196             throw new BuildException(message);
197         }
198     }
199 }
200
201
Popular Tags