001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: JTask.java 6618 2005-04-22 11:15:08Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.ant.jonasbase;
026:
027: import java.io.File;
028: import java.io.FileInputStream;
029: import java.io.FileNotFoundException;
030: import java.io.FileOutputStream;
031: import java.io.IOException;
032: import java.io.OutputStream;
033: import java.util.Properties;
034:
035: import org.apache.tools.ant.BuildException;
036: import org.objectweb.jonas.ant.BootstrapTask;
037:
038: /**
039: * Defines a common task
040: * @author Florent Benoit
041: */
042: public class JTask extends BootstrapTask implements BaseTaskItf {
043:
044: /**
045: * Property separators (4 spaces)
046: */
047: public static final String SEPARATORS = " ";
048:
049: /**
050: * configuration file used
051: */
052: private String configurationFile = null;
053:
054: /**
055: * Information for the logger
056: */
057: private String logInfo = null;
058:
059: /**
060: * Destination directory (JONAS_BASE)
061: */
062: private File destDir = null;
063:
064: /**
065: * Sets the configuration file
066: * @param configurationFile The configurationFile to set.
067: */
068: public void setConfigurationFile(String configurationFile) {
069: this .configurationFile = configurationFile;
070: }
071:
072: /**
073: * @param destDir The destDir to set.
074: */
075: public void setDestDir(File destDir) {
076: this .destDir = destDir;
077: }
078:
079: /**
080: * Gets logger info (to be displayed)
081: * @return logger info
082: * @see org.objectweb.jonas.ant.jonasbase.BaseTaskItf#getLogInfo()
083: */
084: public String getLogInfo() {
085: return logInfo;
086: }
087:
088: /**
089: * Set the info to be displayed by the logger
090: * @param logInfo information to be displayed
091: * @see org.objectweb.jonas.ant.jonasbase.BaseTaskItf#setLogInfo(java.lang.String)
092: */
093: public void setLogInfo(String logInfo) {
094: this .logInfo = logInfo;
095: }
096:
097: /**
098: * Gets the destination directory
099: * @return the destination directory
100: */
101: public File getDestDir() {
102: return destDir;
103: }
104:
105: /**
106: * Write properties object to a file with some logging info
107: * @param info header for logging
108: * @param props Properties to write
109: * @param f file for writing
110: */
111: protected void writePropsToFile(String info, Properties props,
112: File f) {
113: OutputStream fOut = null;
114: try {
115: fOut = new FileOutputStream(f);
116: } catch (FileNotFoundException e) {
117: throw new BuildException(info + "File is invalid", e);
118: }
119:
120: // Write properties to file
121: try {
122: props.store(fOut, "");
123: fOut.close();
124: } catch (IOException ioe) {
125: throw new BuildException(info
126: + "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 info, String confDir,
142: String confFile, String property, String name, boolean add) {
143: changeValueForKey(info, confDir, confFile, property, name,
144: SEPARATORS, add);
145: }
146:
147: /**
148: * Add a value for a specific property in a configuration file
149: * @param info text to be displayed for header
150: * @param confDir configuration directory (can be JONAS_BASE/conf)
151: * @param confFile configuration file (can be jonas.properties)
152: * @param property which must be found in confFile
153: * @param name value for the property to add
154: * @param separators separator using between the property name and the property value
155: * @param add if true, add it, else replace
156: */
157: protected void changeValueForKey(String info, String confDir,
158: String confFile, String property, String name,
159: String separators, boolean add) {
160:
161: // Read current value
162: Properties currentProps = new Properties();
163: File f = null;
164: try {
165: f = new File(confDir + File.separator + confFile);
166: currentProps.load(new FileInputStream(f));
167: } catch (Exception e) {
168: throw new BuildException(
169: "Cannot load current properties for file '" + f
170: + "'.", e);
171: }
172:
173: String valueOfProperty = currentProps.getProperty(property);
174:
175: // Now, add/replace mail value
176: JReplace propertyReplace = new JReplace();
177: propertyReplace.setProject(getProject());
178: propertyReplace.setConfigurationFile(confFile);
179: propertyReplace.setDestDir(new java.io.File(getDestDir()
180: .getPath()));
181: if (valueOfProperty == null || valueOfProperty.length() == 0) {
182: propertyReplace.setToken(property);
183: propertyReplace.setValue(property + separators + name);
184: } else if (!add) {
185: propertyReplace.setToken(property + separators
186: + valueOfProperty);
187: propertyReplace.setValue(property + separators + name);
188: } else {
189: valueOfProperty = valueOfProperty.trim();
190: propertyReplace.setToken(property + separators
191: + valueOfProperty);
192: valueOfProperty += ", " + name;
193: String replaceVal = property + separators + valueOfProperty;
194: replaceVal = replaceVal.trim();
195: propertyReplace.setValue(replaceVal);
196: }
197: if (add) {
198: log(info + "Adding '" + name + "' in " + confFile
199: + " file to property '" + property + "'.");
200: } else {
201: log(info + "Replacing the property '" + property + "' in "
202: + confFile + " file .");
203: }
204: propertyReplace.execute();
205: }
206:
207: /**
208: * @return the configurationFile.
209: */
210: protected String getConfigurationFile() {
211: return configurationFile;
212: }
213: }
|