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: JReleaseInfoAntTask.java,v 1.10 2005/08/06 14:13:17 tcotting Exp $
18   */
19  package ch.oscg.jreleaseinfo.anttask;
20  
21  import java.io.File;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.tools.ant.BuildException;
26  import org.apache.tools.ant.Project;
27  import org.apache.tools.ant.Task;
28  import org.apache.tools.ant.types.Parameter;
29  
30  import ch.oscg.jreleaseinfo.BuildNumberHandler;
31  import ch.oscg.jreleaseinfo.BuildNumberHandlerIF;
32  import ch.oscg.jreleaseinfo.JReleaseInfoBean;
33  import ch.oscg.jreleaseinfo.JReleaseInfoProperty;
34  import ch.oscg.jreleaseinfo.SourceGenerator;
35  import ch.oscg.jreleaseinfo.SourceGeneratorApp;
36  
37  
38  /***
39   * This Task creates a new java source file with embedded informationen about
40   * the current build.
41   *
42   * <p>
43   * There will be some standard arguments like Version, BuildDate, as well as
44   * user defined properties.
45   * </p>
46   *
47   * <p>
48   * The java class will provide final get methods to access these informations.
49   * </p>
50   *
51   * @author Thomas Cotting, Tangarena Engineering AG, Luzern
52   * @version $Revision: 1.10 $ ($Date: 2005/08/06 14:13:17 $ / $Author: tcotting $)
53   */
54  public class JReleaseInfoAntTask extends Task {
55     /*** Membervariables for created class. */
56     protected Map props = new HashMap();
57  
58     /*** Generator for build info source file. */
59     protected JReleaseInfoBean creator = new JReleaseInfoBean();
60  
61     /*** Build number handler */
62     protected BuildNumberHandlerIF buildNumberHandler = new BuildNumberHandler();
63  
64     /*** Flag for additional viewer code */
65     protected boolean isWithViewer = false;
66  
67     /*** Build number property name */
68     protected String buildNumPropertyName = null;
69  
70     /*** Increment for build number. Default is 1, 0 could be used to prevent upcounting */
71     private int buildNumIncrement = 1;
72  
73     /***
74      * This is the standard execute() method from Task which must be
75      * overwritten.
76      * @throws BuildException on wrong argument or IO error
77      */
78     public void execute() throws BuildException {
79        try {
80           JReleaseInfoProperty prop = buildNumberHandler.getUpdatedBuildNumberProperty(buildNumIncrement);
81  
82           if (prop != null) {
83              props.put(prop.getName(), prop);
84              if (buildNumPropertyName != null) {
85                 getProject().setNewProperty(buildNumPropertyName, prop.getValue());
86              }
87           }
88  
89           // now create version file
90           if (isWithViewer) {
91              creator.execute(props, new SourceGeneratorApp());
92           }
93           else {
94              creator.execute(props, new SourceGenerator());
95           }
96        } catch (Exception ex) {
97           throw new BuildException(ex);
98        }
99     }
100 
101    /***
102     * Set method for any property using the ant type Parameter.
103     *
104     * <p>
105     * Note: if you define the type, then only use one of these (if you do not
106     * define the type, String will be used):
107     * </p>
108     *
109     * <ul>
110     * <li>
111     * boolean
112     * </li>
113     * <li>
114     * int
115     * </li>
116     * <li>
117     * String
118     * </li>
119     * <li>
120     * Boolean
121     * </li>
122     * <li>
123     * Integer
124     * </li>
125     * </ul>
126     *
127     *
128     * @param prop Ant type Parameter
129     */
130    public void addConfiguredParameter(Parameter prop) {
131       JReleaseInfoProperty p = new JReleaseInfoProperty();
132       p.setName(prop.getName());
133 
134       String type = prop.getType();
135 
136       if (type == null) {
137          p.setType(JReleaseInfoProperty.TYPE_OBJ_STRING);
138       } else {
139          p.setType(type);
140       }
141 
142       p.setValue(prop.getValue());
143       props.put(p.getName(), p);
144    }
145 
146 	/***
147 	 * Set the build number property name.
148 	 *
149 	 * @param buildNumPropertyName Name of property.
150 	 */
151     public void setBuildNumProperty(String buildNumPropertyName) {
152        this.buildNumPropertyName = buildNumPropertyName;
153     }
154 
155    /***
156     * Set method for the fileName containing the buildnumber.
157     * Note: The fileName should be set with an absolute path.
158     *
159     * @param fileName Name of buildnumber file
160     */
161     public void setBuildNumFile(String fileName) {
162        if (buildNumberHandler instanceof BuildNumberHandler) {
163           ((BuildNumberHandler)buildNumberHandler).setBuildNumFile(fileName);
164           if (isRelativePath(fileName)) {
165              this.log("BuildNum File should have an absolute path!", Project.MSG_WARN);
166           }
167        }
168     }
169 
170    /***
171     * Set method for the buildNumber increment.
172     *
173     * @param increment int
174     */
175    public void setBuildNumIncrement(int inc) {
176       this.buildNumIncrement = inc;
177    }
178 
179    /***
180     * Set method for the classname of the JReleaseInfo file to be created.
181     *
182     * @param className of the JReleaseInfo
183     */
184    public void setClassName(String className) {
185       creator.setClassName(className);
186    }
187 
188    /***
189     * Set method for the version-number.
190     *
191     * @param version info
192     */
193    public void setVersion(String version) {
194       String name = "Version";
195       this.props.put(name,
196          new JReleaseInfoProperty(name, JReleaseInfoProperty.TYPE_OBJ_STRING, version));
197    }
198 
199    /***
200     * Set method for the name of the project
201     *
202     * @param version info
203     */
204    public void setProject(String project) {
205       String name = "Project";
206       this.props.put(name,
207          new JReleaseInfoProperty(name, JReleaseInfoProperty.TYPE_OBJ_STRING, project));
208    }
209 
210    /***
211     * Set method for the package of the JReleaseInfo class to be created.
212     *
213     * @param packageName of JReleaseInfo class
214     */
215    publicng> void setPackageName(String packageName) {
216       creator.setPackageName(packageName);
217    }
218 
219    /***
220     * Set method for target directory where the JReleaseInfo file should be created.
221     * Note: The targetDir should be set as an absolute path.
222     * @param targetDir of JReleaseInfo file
223     */
224    public void setTargetDir(String targetDir) {
225       if (isRelativePath(targetDir)) {
226          this.log("Target directory should be defined absolute!", Project.MSG_WARN);
227       }
228       creator.setTargetDir(targetDir);
229    }
230 
231 
232    /***
233     * Utility method to delete the JReleaseInfo file (for test purposes).
234     */
235    public void deleteJReleaseInfoFile() {
236       creator.deleteJReleaseInfoFile();
237    }
238 
239    /***
240     * Utility method to check the JReleaseInfo file (for test purposes).
241     * @return file with JReleaseInfo
242     */
243    public File getJReleaseInfoFile() {
244       return creator.createJReleaseInfoFile();
245    }
246 
247 
248    /***
249     * Set method for isWithViewer flag
250     *
251     * @param isWithViewer flag
252     */
253    public void setWithViewer(boolean isWithViewer) {
254       this.isWithViewer = isWithViewer;
255    }
256 
257 
258    /***
259     * Utility method to check if a path is relative.
260     * 
261     * @param path
262     * @return true/false
263     */
264    protected boolean isRelativePath(String path) {
265       String pathN = path.replaceAll("////", "/");
266       File file = new File(path);
267       String absPath = file.getAbsolutePath().replaceAll("////", "/");
268       return !pathN.toLowerCase().equals(absPath.toLowerCase());
269    }
270 }