01: /*
02: * This file is part of PFIXCORE.
03: *
04: * PFIXCORE is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * PFIXCORE is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public License
15: * along with PFIXCORE; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18:
19: package de.schlund.pfixxml.config;
20:
21: import java.io.File;
22: import java.io.FileInputStream;
23: import java.io.FileOutputStream;
24: import java.io.IOException;
25: import java.util.Hashtable;
26: import java.util.Properties;
27:
28: import org.apache.tools.ant.BuildException;
29: import org.apache.tools.ant.Task;
30:
31: public class BuildTimePropTask extends Task {
32: private File file;
33:
34: private String mode;
35:
36: private String machine;
37:
38: private String fqdn;
39:
40: private String uid;
41:
42: public void setFile(File file) {
43: this .file = file;
44: }
45:
46: public void setMode(String mode) {
47: this .mode = mode;
48: }
49:
50: public void setMachine(String machine) {
51: this .machine = machine;
52: }
53:
54: public void setFqdn(String fqdn) {
55: this .fqdn = fqdn;
56: }
57:
58: public void setUid(String uid) {
59: this .uid = uid;
60: }
61:
62: /*
63: * (non-Javadoc)
64: *
65: * @see org.apache.tools.ant.Task#execute()
66: */
67: @SuppressWarnings("unchecked")
68: public void execute() throws BuildException {
69: try {
70: Properties props = new Properties();
71: if (file.exists()) {
72: props.load(new FileInputStream(file));
73: } else {
74: file.createNewFile();
75: }
76:
77: props.setProperty("mode", mode);
78: props.setProperty("machine", machine);
79: props.setProperty("fqdn", fqdn);
80: props.setProperty("uid", uid);
81:
82: Hashtable<String, String> antProps = this .getProject()
83: .getProperties();
84: for (String key : antProps.keySet()) {
85: props
86: .setProperty("__antprop_" + key, antProps
87: .get(key));
88: }
89:
90: props.store(new FileOutputStream(file),
91: "Properties used at buildtime");
92: } catch (IOException e) {
93: throw new BuildException(
94: "Could not access property file \"" + file + "\"!",
95: e);
96: }
97: }
98:
99: }
|