00001
00026 package org.objectweb.jonas.ant.jonasbase;
00027
00028 import java.io.File;
00029 import java.util.Date;
00030 import java.util.Properties;
00031
00032 import org.apache.tools.ant.BuildException;
00033 import org.apache.tools.ant.taskdefs.Java;
00034
00035 import org.objectweb.jonas.ant.JOnASBaseTask;
00036
00041 public class JdbcRa extends JTask implements BaseTaskItf {
00042
00046 private static final String INFO = "[JdbcRa] ";
00047
00051 private static final String JONAS_JDBCDM = "JOnAS_jdbcDM";
00052
00056 private static final String RACONFIG_CLASS = "org.objectweb.jonas_rar.raconfig.RAConfig";
00057
00061 private static final String P6SPY_DRIVER = "com.p6spy.engine.spy.P6SpyDriver";
00062
00066 private static final String REALDRIVER_PROPERTY = "realdriver";
00067
00071 private String name = null;
00072
00076 private String mapperName = null;
00077
00081 private String user = null;
00082
00086 private String password = null;
00087
00091 private String url = null;
00092
00096 private String driverName = null;
00097
00101 private String realDriverName = null;
00102
00106 private String jndiName = null;
00107
00111 private boolean autoload = true;
00112
00116 private boolean p6spy = false;
00117
00122 public void setName(String name) {
00123 this.name = name;
00124 }
00125
00130 public void setMapperName(String mapperName) {
00131 this.mapperName = mapperName;
00132 }
00133
00138 public void setUser(String user) {
00139 this.user = user;
00140 }
00141
00146 public void setPassword(String password) {
00147 this.password = password;
00148 }
00149
00154 public void setUrl(String url) {
00155 this.url = url;
00156 }
00157
00162 public void setDriverName(String driverName) {
00163 this.driverName = driverName;
00164 this.realDriverName = driverName;
00165 }
00166
00171 public void setJndiName(String jndiName) {
00172 this.jndiName = jndiName;
00173 }
00174
00178 private void checkProperties() {
00179 if (name == null) {
00180 throw new BuildException(INFO + "Property 'name' is missing.");
00181 } else if (mapperName == null) {
00182 throw new BuildException(INFO + "Property 'mapperName' is missing.");
00183 } else if (user == null) {
00184 throw new BuildException(INFO + "Property 'user' is missing.");
00185 } else if (password == null) {
00186 throw new BuildException(INFO + "Property 'password' is missing.");
00187 } else if (url == null) {
00188 throw new BuildException(INFO + "Property 'url' is missing.");
00189 } else if (driverName == null) {
00190 throw new BuildException(INFO + "Property 'driverName' is missing.");
00191 } else if (jndiName == null) {
00192 throw new BuildException(INFO + "Property 'jndiName' is missing.");
00193 }
00194 }
00195
00200 private Properties getProperties() {
00201
00202 checkProperties();
00203
00204 Properties props = new Properties();
00205 props.put("datasource.name", jndiName);
00206 props.put("datasource.url", url);
00207 if (p6spy) {
00208 driverName = P6SPY_DRIVER;
00209 }
00210 props.put("datasource.classname", driverName);
00211 props.put("datasource.username", user);
00212 props.put("datasource.password", password);
00213 props.put("datasource.mapper", mapperName);
00214 return props;
00215 }
00216
00220 public void execute() {
00221
00222
00223 File tmpFile = new File(System.getProperty("java.io.tmpdir"), String.valueOf(new Date().getTime()));
00224
00225
00226 Properties props = getProperties();
00227 writePropsToFile(INFO, props, tmpFile);
00228
00229
00230
00231
00232 String jRootRarsDir = getJonasRoot().getPath() + File.separator + "rars" + File.separator + "autoload";
00233 String jBaseRarsDir = getDestDir().getPath() + File.separator + "rars";
00234 if (isAutoload()) {
00235 jBaseRarsDir += File.separator + "autoload";
00236 }
00237 String jdbcDM = jRootRarsDir + File.separator + JONAS_JDBCDM;
00238 String destRarFile = jBaseRarsDir + File.separator + name + ".rar";
00239
00240 Java raConfigTask = getBootstraptask("");
00241 raConfigTask.clearArgs();
00242 raConfigTask.createArg().setValue(RACONFIG_CLASS);
00243 raConfigTask.createArg().setValue("-dm");
00244 raConfigTask.createArg().setValue("-p");
00245 raConfigTask.createArg().setValue(tmpFile.getPath());
00246 raConfigTask.createArg().setValue(jdbcDM);
00247 raConfigTask.createArg().setValue(destRarFile);
00248
00249 log(INFO + "Generating a rar file with name '" + name + "', mapperName '" + mapperName + "', user '" + user
00250 + "', password '" + password + "', URL '" + url + "', driverName '" + driverName + "' and jndiName '"
00251 + jndiName + "'...");
00252
00253 try {
00254 raConfigTask.execute();
00255 } catch (Exception rae) {
00256 rae.printStackTrace();
00257 throw new BuildException(INFO + "Cannot make a resource adaptor on RAConfig: " + rae.getMessage());
00258 } finally {
00259 tmpFile.delete();
00260 }
00261 log(INFO + "Rar generated : '" + destRarFile + "'.");
00262
00263
00264 if (p6spy) {
00265
00266 String jBaseConf = getJonasRoot().getPath() + File.separator + "conf";
00267 changeValueForKey(INFO, jBaseConf, JOnASBaseTask.P6SPY_CONF_FILE,
00268 REALDRIVER_PROPERTY, realDriverName, "=", false);
00269 }
00270 }
00271
00276 public boolean isAutoload() {
00277 return autoload;
00278 }
00279
00284 public void setAutoload(boolean autoload) {
00285 this.autoload = autoload;
00286 }
00287
00292 public boolean isP6spy() {
00293 return p6spy;
00294 }
00295
00300 public void setP6spy(boolean p6spy) {
00301 this.p6spy = p6spy;
00302 }
00303 }