JTask.java

00001 
00027 package org.objectweb.jonas.ant.jonasbase;
00028 
00029 import java.io.File;
00030 import java.io.FileInputStream;
00031 import java.io.FileNotFoundException;
00032 import java.io.FileOutputStream;
00033 import java.io.IOException;
00034 import java.io.OutputStream;
00035 import java.util.Properties;
00036 
00037 import org.apache.tools.ant.BuildException;
00038 import org.objectweb.jonas.ant.BootstrapTask;
00039 
00044 public class JTask extends BootstrapTask implements BaseTaskItf {
00045 
00049     public static final String SEPARATORS = "    ";
00050 
00054     private String configurationFile = null;
00055 
00059     private String logInfo = null;
00060 
00064     private File destDir = null;
00065 
00070     public void setConfigurationFile(String configurationFile) {
00071         this.configurationFile = configurationFile;
00072     }
00073 
00077     public void setDestDir(File destDir) {
00078         this.destDir = destDir;
00079     }
00080 
00086     public String getLogInfo() {
00087         return logInfo;
00088     }
00089 
00095     public void setLogInfo(String logInfo) {
00096         this.logInfo = logInfo;
00097     }
00098 
00103     public File getDestDir() {
00104         return destDir;
00105     }
00106 
00113     protected void writePropsToFile(String info, Properties props, File f) {
00114         OutputStream fOut = null;
00115         try {
00116             fOut = new FileOutputStream(f);
00117         } catch (FileNotFoundException e) {
00118             throw new BuildException(info + "File is invalid : "
00119                     + e.getMessage());
00120         }
00121 
00122         // Write properties to file
00123         try {
00124             props.store(fOut, "");
00125             fOut.close();
00126         } catch (IOException ioe) {
00127             throw new BuildException(info + "Error while writing properties : "
00128                     + ioe.getMessage());
00129         }
00130 
00131     }
00132 
00143     protected void changeValueForKey(String info, String confDir,
00144             String confFile, String property, String name, boolean add) {
00145         changeValueForKey(info, confDir, confFile, property, name, SEPARATORS, add);
00146     }
00157     protected void changeValueForKey(String info, String confDir,
00158             String confFile, String property, String name, String separators, boolean add) {
00159 
00160         // Read current value
00161         Properties currentProps = new Properties();
00162         File f = null;
00163         try {
00164             f = new File(confDir + File.separator + confFile);
00165             currentProps.load(new FileInputStream(f));
00166         } catch (Exception e) {
00167             throw new BuildException(
00168                     "Cannot load current properties for file '" + f + "'.");
00169         }
00170 
00171         String valueOfProperty = currentProps.getProperty(property);
00172 
00173         // Now, add/replace mail value
00174         JReplace propertyReplace = new JReplace();
00175         propertyReplace.setProject(getProject());
00176         propertyReplace.setConfigurationFile(confFile);
00177         propertyReplace.setDestDir(new java.io.File(getDestDir().getPath()));
00178         if (valueOfProperty == null || valueOfProperty.length() == 0 || !add) {
00179             propertyReplace.setToken(property + separators + valueOfProperty);
00180             propertyReplace.setValue(property + separators + name);
00181         } else {
00182             valueOfProperty = valueOfProperty.trim();
00183             propertyReplace.setToken(property + separators + valueOfProperty);
00184             valueOfProperty += ", " + name;
00185             String replaceVal = property + separators + valueOfProperty;
00186             replaceVal = replaceVal.trim();
00187             propertyReplace.setValue(replaceVal);
00188         }
00189         if (add) {
00190             log(info + "Adding '" + name + "' in " + confFile
00191                     + " file to property '" + property + "'.");
00192         } else {
00193             log(info + "Replacing the property '" + property + "' in "
00194                     + confFile + " file .");
00195         }
00196         propertyReplace.execute();
00197     }
00198 
00202     protected String getConfigurationFile() {
00203         return configurationFile;
00204     }
00205 }

Generated on Tue Feb 15 15:05:13 2005 for JOnAS by  doxygen 1.3.9.1