View Javadoc

1   /*
2    * Copyright 2004-2005 Thomas Cotting
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  /*
17   * $Id: BuildNumberHandler.java,v 1.3 2005/08/06 14:12:36 tcotting Exp $
18   */
19  package ch.oscg.jreleaseinfo;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.FileNotFoundException;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  
27  import java.util.Properties;
28  
29  
30  /***
31   * BuildNumberHandler getting the number from a file.
32   *
33   * @author Thomas Cotting, Tangarena Engineering AG, Luzern
34   * @version $Revision: 1.3 $ ($Date: 2005/08/06 14:12:36 $ / $Author: tcotting $)
35   */
36  public class BuildNumberHandler implements BuildNumberHandlerIF {
37     
38     /*** Initial build number */
39     private static final String INITIAL_BUILDNUM = "1";
40     
41     /*** Property Name in buildnum file. */
42     public static final String PROPERTY_BUILDNUM_LAST = "build.num.last";
43  
44     /***
45      * FileName of the file containing the build number, 'null' means no build
46      * number property.
47      */
48     private String buildNumFileName = null;
49  
50  
51     /***
52      * Main method to keep track of the build number.
53      * @param inc increment
54      * @return update success true/false
55      *
56      * @throws IOException on file errors
57      * @throws IllegalArgumentException on wrong parameters
58      */
59     protected String updateBuildNumber(int buildNumIncrement) throws IllegalArgumentException, IOException {
60        // buildNumFileName == null is legal und means, 
61        // no property must be returned
62        if (buildNumFileName == null) {
63           return null;
64        }
65  
66        // check parameter
67        if (!JReleaseInfoUtil.isValidNameString(buildNumFileName)) {
68           throw new IllegalArgumentException("Invalid name for buildfile (" + buildNumFileName + ")");
69        }
70  
71        File file = new File(buildNumFileName);
72        Properties props = new Properties();
73  
74        // Only store properties when they changed
75        boolean doStoreProps = false;
76        try {
77           FileInputStream fis = new FileInputStream(file);
78           props.load(fis);
79           fis.close();
80  
81           String strBuildNumber = props.getProperty(PROPERTY_BUILDNUM_LAST);
82           int iLast = Integer.parseInt(strBuildNumber);
83           String strNewBuildNumber = "" + (iLast + buildNumIncrement);
84           props.setProperty(PROPERTY_BUILDNUM_LAST, strNewBuildNumber);
85           if (buildNumIncrement != 0) {
86              doStoreProps = true;
87           }
88        } catch (IOException ex) {
89           props.setProperty(PROPERTY_BUILDNUM_LAST, INITIAL_BUILDNUM);
90           doStoreProps = true;
91        }
92  
93        // Store the changed properties 
94        if (doStoreProps) {
95           storeProperties(file, props);
96        }
97  
98        return props.getProperty(PROPERTY_BUILDNUM_LAST);
99     }
100 
101    /***
102     * Store the property file.
103     * @param file File to store the properties
104     * @param props Properties to store
105     * @throws FileNotFoundException
106     * @throws IOException
107     */
108    private void storeProperties(File file, Properties props) throws FileNotFoundException, IOException {
109       File parent = file.getParentFile();
110 
111       if (parent != null) {
112          parent.mkdirs();
113       }
114 
115       FileOutputStream fos = new FileOutputStream(file);
116       props.store(fos, "ANT Task: " + this.getClass().getName());
117       fos.close();
118    }
119 
120 
121    /***
122     * Get the JReleaseInfoProperty with the updated buildNumber.
123     * @param buildNumIncrement  int
124     * @return JReleaseInfoProperty
125     */
126    public JReleaseInfoProperty getUpdatedBuildNumberProperty(int buildNumIncrement) throws IllegalArgumentException, IOException {
127       String updatedBuildNum = updateBuildNumber(buildNumIncrement);
128       if (updatedBuildNum == null) {
129          return null;
130       }
131 
132       JReleaseInfoProperty biProp = new JReleaseInfoProperty();
133       biProp.setName(PROPNAME_BUILDNUM);
134       biProp.setType(JReleaseInfoProperty.TYPE_PRI_INT);
135       biProp.setValue(updatedBuildNum);
136 
137       return biProp;
138    }
139 
140    /***
141     * Set method for the fileName containing the buildnumber.
142     *
143     * @param fileName for build number
144     */
145    public void setBuildNumFile(String fileName) {
146       this.buildNumFileName = fileName;
147    }
148 
149 }