1 26 27 package org.objectweb.jonas.ant.jonasbase; 28 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.FileNotFoundException ; 32 import java.io.FileOutputStream ; 33 import java.io.IOException ; 34 import java.io.OutputStream ; 35 import java.util.Properties ; 36 37 import org.apache.tools.ant.BuildException; 38 import org.objectweb.jonas.ant.BootstrapTask; 39 40 44 public class JTask extends BootstrapTask implements BaseTaskItf { 45 46 49 public static final String SEPARATORS = " "; 50 51 54 private String configurationFile = null; 55 56 59 private String logInfo = null; 60 61 64 private File destDir = null; 65 66 70 public void setConfigurationFile(String configurationFile) { 71 this.configurationFile = configurationFile; 72 } 73 74 77 public void setDestDir(File destDir) { 78 this.destDir = destDir; 79 } 80 81 86 public String getLogInfo() { 87 return logInfo; 88 } 89 90 95 public void setLogInfo(String logInfo) { 96 this.logInfo = logInfo; 97 } 98 99 103 public File getDestDir() { 104 return destDir; 105 } 106 107 113 protected void writePropsToFile(String info, Properties props, File f) { 114 OutputStream fOut = null; 115 try { 116 fOut = new FileOutputStream (f); 117 } catch (FileNotFoundException e) { 118 throw new BuildException(info + "File is invalid", e); 119 } 120 121 try { 123 props.store(fOut, ""); 124 fOut.close(); 125 } catch (IOException ioe) { 126 throw new BuildException(info + "Error while writing properties", ioe); 127 } 128 129 } 130 131 141 protected void changeValueForKey(String info, String confDir, 142 String confFile, String property, String name, boolean add) { 143 changeValueForKey(info, confDir, confFile, property, name, SEPARATORS, add); 144 } 145 155 protected void changeValueForKey(String info, String confDir, 156 String confFile, String property, String name, String separators, boolean add) { 157 158 Properties currentProps = new Properties (); 160 File f = null; 161 try { 162 f = new File (confDir + File.separator + confFile); 163 currentProps.load(new FileInputStream (f)); 164 } catch (Exception e) { 165 throw new BuildException( 166 "Cannot load current properties for file '" + f + "'.", e); 167 } 168 169 String valueOfProperty = currentProps.getProperty(property); 170 171 JReplace propertyReplace = new JReplace(); 173 propertyReplace.setProject(getProject()); 174 propertyReplace.setConfigurationFile(confFile); 175 propertyReplace.setDestDir(new java.io.File (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 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 203 protected String getConfigurationFile() { 204 return configurationFile; 205 } 206 } | Popular Tags |