1 25 26 package org.objectweb.jonas.ant.jonasbase; 27 28 import java.io.File ; 29 import java.util.Date ; 30 import java.util.Properties ; 31 32 import org.apache.tools.ant.BuildException; 33 import org.apache.tools.ant.taskdefs.Java; 34 35 import org.objectweb.jonas.ant.JOnASBaseTask; 36 37 41 public class JdbcRa extends JTask implements BaseTaskItf { 42 43 46 private static final String INFO = "[JdbcRa] "; 47 48 51 private static final String JONAS_JDBCDM = "JOnAS_jdbcDM"; 52 53 56 private static final String RACONFIG_CLASS = "org.objectweb.jonas_rar.raconfig.RAConfig"; 57 58 61 private static final String P6SPY_DRIVER = "com.p6spy.engine.spy.P6SpyDriver"; 62 63 66 private static final String REALDRIVER_PROPERTY = "realdriver"; 67 68 71 private String name = null; 72 73 76 private String mapperName = null; 77 78 81 private String user = null; 82 83 86 private String password = null; 87 88 91 private String url = null; 92 93 96 private String driverName = null; 97 98 101 private String realDriverName = null; 102 103 106 private String maxPoolSize = "100"; 107 108 111 private String jndiName = null; 112 113 116 private boolean autoload = true; 117 118 121 private boolean p6spy = false; 122 123 127 public void setName(String name) { 128 this.name = name; 129 } 130 131 135 public void setMapperName(String mapperName) { 136 this.mapperName = mapperName; 137 } 138 139 143 public void setUser(String user) { 144 this.user = user; 145 } 146 147 151 public void setPassword(String password) { 152 this.password = password; 153 } 154 155 159 public void setUrl(String url) { 160 this.url = url; 161 } 162 163 167 public void setMaxPoolSize(String maxPoolSize) { 168 this.maxPoolSize = maxPoolSize; 169 } 170 171 175 public void setDriverName(String driverName) { 176 this.driverName = driverName; 177 this.realDriverName = driverName; 178 } 179 180 184 public void setJndiName(String jndiName) { 185 this.jndiName = jndiName; 186 } 187 188 191 private void checkProperties() { 192 if (name == null) { 193 throw new BuildException(INFO + "Property 'name' is missing."); 194 } else if (mapperName == null) { 195 throw new BuildException(INFO + "Property 'mapperName' is missing."); 196 } else if (user == null) { 197 throw new BuildException(INFO + "Property 'user' is missing."); 198 } else if (password == null) { 199 throw new BuildException(INFO + "Property 'password' is missing."); 200 } else if (url == null) { 201 throw new BuildException(INFO + "Property 'url' is missing."); 202 } else if (driverName == null) { 203 throw new BuildException(INFO + "Property 'driverName' is missing."); 204 } else if (jndiName == null) { 205 throw new BuildException(INFO + "Property 'jndiName' is missing."); 206 } 207 } 208 209 213 private Properties getProperties() { 214 checkProperties(); 216 217 Properties props = new Properties (); 218 props.put("datasource.name", jndiName); 219 props.put("datasource.url", url); 220 if (p6spy) { 221 driverName = P6SPY_DRIVER; 222 } 223 props.put("datasource.classname", driverName); 224 props.put("datasource.username", user); 225 props.put("datasource.password", password); 226 props.put("datasource.mapper", mapperName); 227 props.put("jdbc.maxconpool", maxPoolSize); 228 return props; 229 } 230 231 234 public void execute() { 235 236 File tmpFile = new File (System.getProperty("java.io.tmpdir"), String.valueOf(new Date ().getTime())); 238 239 Properties props = getProperties(); 241 writePropsToFile(INFO, props, tmpFile); 242 243 245 String jRootRarsDir = getJonasRoot().getPath() + File.separator + "rars" + File.separator + "autoload"; 247 String jBaseRarsDir = getDestDir().getPath() + File.separator + "rars"; 248 if (isAutoload()) { 249 jBaseRarsDir += File.separator + "autoload"; 250 } 251 String jdbcDM = jRootRarsDir + File.separator + JONAS_JDBCDM; 252 String destRarFile = jBaseRarsDir + File.separator + name + ".rar"; 253 254 Java raConfigTask = getBootstraptask(""); 255 raConfigTask.clearArgs(); 256 raConfigTask.createArg().setValue(RACONFIG_CLASS); 257 raConfigTask.createArg().setValue("-dm"); 258 raConfigTask.createArg().setValue("-p"); 259 raConfigTask.createArg().setValue(tmpFile.getPath()); 260 raConfigTask.createArg().setValue(jdbcDM); 261 raConfigTask.createArg().setValue(destRarFile); 262 263 log(INFO + "Generating a rar file with name '" + name + "', mapperName '" + mapperName + "', user '" + user 264 + "', password '" + password + "', URL '" + url + "', driverName '" + driverName + "' and jndiName '" 265 + jndiName + "'..."); 266 267 try { 268 raConfigTask.execute(); 269 } catch (Exception rae) { 270 rae.printStackTrace(); 271 throw new BuildException(INFO + "Cannot make a resource adaptor on RAConfig: ", rae); 272 } finally { 273 tmpFile.delete(); 274 } 275 log(INFO + "Rar generated : '" + destRarFile + "'."); 276 277 if (p6spy) { 279 String jBaseConf = getJonasRoot().getPath() + File.separator + "conf"; 281 changeValueForKey(INFO, jBaseConf, JOnASBaseTask.P6SPY_CONF_FILE, 282 REALDRIVER_PROPERTY, realDriverName, "=", false); 283 } 284 } 285 286 290 public boolean isAutoload() { 291 return autoload; 292 } 293 294 298 public void setAutoload(boolean autoload) { 299 this.autoload = autoload; 300 } 301 302 306 public boolean isP6spy() { 307 return p6spy; 308 } 309 310 314 public void setP6spy(boolean p6spy) { 315 this.p6spy = p6spy; 316 } 317 } | Popular Tags |