KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ant > jonasbase > JTask


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: JTask.java,v 1.5 2005/04/22 11:15:08 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.ant.jonasbase;
28
29 import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.FileNotFoundException JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.util.Properties JavaDoc;
36
37 import org.apache.tools.ant.BuildException;
38 import org.objectweb.jonas.ant.BootstrapTask;
39
40 /**
41  * Defines a common task
42  * @author Florent Benoit
43  */

44 public class JTask extends BootstrapTask implements BaseTaskItf {
45
46     /**
47      * Property separators (4 spaces)
48      */

49     public static final String JavaDoc SEPARATORS = " ";
50
51     /**
52      * configuration file used
53      */

54     private String JavaDoc configurationFile = null;
55
56     /**
57      * Information for the logger
58      */

59     private String JavaDoc logInfo = null;
60
61     /**
62      * Destination directory (JONAS_BASE)
63      */

64     private File JavaDoc destDir = null;
65
66     /**
67      * Sets the configuration file
68      * @param configurationFile The configurationFile to set.
69      */

70     public void setConfigurationFile(String JavaDoc configurationFile) {
71         this.configurationFile = configurationFile;
72     }
73
74     /**
75      * @param destDir The destDir to set.
76      */

77     public void setDestDir(File JavaDoc destDir) {
78         this.destDir = destDir;
79     }
80
81     /**
82      * Gets logger info (to be displayed)
83      * @return logger info
84      * @see org.objectweb.jonas.ant.jonasbase.BaseTaskItf#getLogInfo()
85      */

86     public String JavaDoc getLogInfo() {
87         return logInfo;
88     }
89
90     /**
91      * Set the info to be displayed by the logger
92      * @param logInfo information to be displayed
93      * @see org.objectweb.jonas.ant.jonasbase.BaseTaskItf#setLogInfo(java.lang.String)
94      */

95     public void setLogInfo(String JavaDoc logInfo) {
96         this.logInfo = logInfo;
97     }
98
99     /**
100      * Gets the destination directory
101      * @return the destination directory
102      */

103     public File JavaDoc getDestDir() {
104         return destDir;
105     }
106
107     /**
108      * Write properties object to a file with some logging info
109      * @param info header for logging
110      * @param props Properties to write
111      * @param f file for writing
112      */

113     protected void writePropsToFile(String JavaDoc info, Properties JavaDoc props, File JavaDoc f) {
114         OutputStream JavaDoc fOut = null;
115         try {
116             fOut = new FileOutputStream JavaDoc(f);
117         } catch (FileNotFoundException JavaDoc e) {
118             throw new BuildException(info + "File is invalid", e);
119         }
120
121         // Write properties to file
122
try {
123             props.store(fOut, "");
124             fOut.close();
125         } catch (IOException JavaDoc ioe) {
126             throw new BuildException(info + "Error while writing properties", ioe);
127         }
128
129     }
130
131     /**
132      * Add a value for a specific property in a configuration file.
133      * The separator uses between the property name and the property value is the default separator value.
134      * @param info text to be displayed for header
135      * @param confDir configuration directory (can be JONAS_BASE/conf)
136      * @param confFile configuration file (can be jonas.properties)
137      * @param property which must be found in confFile
138      * @param name value for the property to add
139      * @param add if true, add it, else replace
140      */

141     protected void changeValueForKey(String JavaDoc info, String JavaDoc confDir,
142             String JavaDoc confFile, String JavaDoc property, String JavaDoc name, boolean add) {
143         changeValueForKey(info, confDir, confFile, property, name, SEPARATORS, add);
144     }
145     /**
146      * Add a value for a specific property in a configuration file
147      * @param info text to be displayed for header
148      * @param confDir configuration directory (can be JONAS_BASE/conf)
149      * @param confFile configuration file (can be jonas.properties)
150      * @param property which must be found in confFile
151      * @param name value for the property to add
152      * @param separators separator using between the property name and the property value
153      * @param add if true, add it, else replace
154      */

155     protected void changeValueForKey(String JavaDoc info, String JavaDoc confDir,
156             String JavaDoc confFile, String JavaDoc property, String JavaDoc name, String JavaDoc separators, boolean add) {
157
158         // Read current value
159
Properties JavaDoc currentProps = new Properties JavaDoc();
160         File JavaDoc f = null;
161         try {
162             f = new File JavaDoc(confDir + File.separator + confFile);
163             currentProps.load(new FileInputStream JavaDoc(f));
164         } catch (Exception JavaDoc e) {
165             throw new BuildException(
166                     "Cannot load current properties for file '" + f + "'.", e);
167         }
168
169         String JavaDoc valueOfProperty = currentProps.getProperty(property);
170
171         // Now, add/replace mail value
172
JReplace propertyReplace = new JReplace();
173         propertyReplace.setProject(getProject());
174         propertyReplace.setConfigurationFile(confFile);
175         propertyReplace.setDestDir(new java.io.File JavaDoc(getDestDir().getPath()));
176         if (valueOfProperty == null || valueOfProperty.length() == 0) {
177             propertyReplace.setToken(property);
178             propertyReplace.setValue(property + separators + name);
179         } else if (!add) {
180             propertyReplace.setToken(property + separators + valueOfProperty);
181             propertyReplace.setValue(property + separators + name);
182         } else {
183             valueOfProperty = valueOfProperty.trim();
184             propertyReplace.setToken(property + separators + valueOfProperty);
185             valueOfProperty += ", " + name;
186             String JavaDoc replaceVal = property + separators + valueOfProperty;
187             replaceVal = replaceVal.trim();
188             propertyReplace.setValue(replaceVal);
189         }
190         if (add) {
191             log(info + "Adding '" + name + "' in " + confFile
192                     + " file to property '" + property + "'.");
193         } else {
194             log(info + "Replacing the property '" + property + "' in "
195                     + confFile + " file .");
196         }
197         propertyReplace.execute();
198     }
199
200     /**
201      * @return the configurationFile.
202      */

203     protected String JavaDoc getConfigurationFile() {
204         return configurationFile;
205     }
206 }
Popular Tags