KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ant > jonasbase > JdbcRa


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JdbcRa.java,v 1.8 2005/04/22 11:15:08 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.ant.jonasbase;
27
28 import java.io.File JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Properties JavaDoc;
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 /**
38  * Allow to create JDBC resource adaptors
39  * @author Florent Benoit
40  */

41 public class JdbcRa extends JTask implements BaseTaskItf {
42
43     /**
44      * Info for the logger
45      */

46     private static final String JavaDoc INFO = "[JdbcRa] ";
47
48     /**
49      * Name of the rar for JDBC_DM
50      */

51     private static final String JavaDoc JONAS_JDBCDM = "JOnAS_jdbcDM";
52
53     /**
54      * RAConfig classname
55      */

56     private static final String JavaDoc RACONFIG_CLASS = "org.objectweb.jonas_rar.raconfig.RAConfig";
57
58     /**
59      * P6Spy driver class name
60      */

61     private static final String JavaDoc P6SPY_DRIVER = "com.p6spy.engine.spy.P6SpyDriver";
62
63     /**
64      * Name of the property of the real driver when using P6Spy tool
65      */

66     private static final String JavaDoc REALDRIVER_PROPERTY = "realdriver";
67
68     /**
69      * Name of this JDBC Resource Adaptor
70      */

71     private String JavaDoc name = null;
72
73     /**
74      * Mapper Name of this JDBC Resource Adaptor
75      */

76     private String JavaDoc mapperName = null;
77
78     /**
79      * username of this JDBC Resource Adaptor
80      */

81     private String JavaDoc user = null;
82
83     /**
84      * Password of this JDBC Resource Adaptor
85      */

86     private String JavaDoc password = null;
87
88     /**
89      * URL of this JDBC Resource Adaptor
90      */

91     private String JavaDoc url = null;
92
93     /**
94      * Driver Name of this JDBC Resource Adaptor (may be set to the P6Spy driver name)
95      */

96     private String JavaDoc driverName = null;
97
98     /**
99      * Driver Name of this JDBC Resource Adaptor
100      */

101     private String JavaDoc realDriverName = null;
102
103     /**
104      * Max Pool Size
105      */

106     private String JavaDoc maxPoolSize = "100";
107
108     /**
109      * JNDI Name of this JDBC Resource Adaptor
110      */

111     private String JavaDoc jndiName = null;
112
113     /**
114      * Copy to autoload dir or not
115      */

116     private boolean autoload = true;
117
118     /**
119      * Using of the P6Spy tool or not
120      */

121     private boolean p6spy = false;
122
123     /**
124      * Set the name of this JDBC Resource Adaptor
125      * @param name the name of this JDBC Resource Adaptor
126      */

127     public void setName(String JavaDoc name) {
128         this.name = name;
129     }
130
131     /**
132      * Set the mapper name of this JDBC Resource Adaptor
133      * @param mapperName the mappername of this JDBC Resource Adaptor
134      */

135     public void setMapperName(String JavaDoc mapperName) {
136         this.mapperName = mapperName;
137     }
138
139     /**
140      * Set the user of this JDBC Resource Adaptor
141      * @param user the user of this JDBC Resource Adaptor
142      */

143     public void setUser(String JavaDoc user) {
144         this.user = user;
145     }
146
147     /**
148      * Set the password of this JDBC Resource Adaptor
149      * @param password the name of this JDBC Resource Adaptor
150      */

151     public void setPassword(String JavaDoc password) {
152         this.password = password;
153     }
154
155     /**
156      * Set the url of this JDBC Resource Adaptor
157      * @param url the name of this JDBC Resource Adaptor
158      */

159     public void setUrl(String JavaDoc url) {
160         this.url = url;
161     }
162
163     /**
164      * Set the max pool size of this JDBC Resource Adaptor Connection Pool
165      * @param maxPoolSize max pool size of connection
166      */

167     public void setMaxPoolSize(String JavaDoc maxPoolSize) {
168         this.maxPoolSize = maxPoolSize;
169     }
170
171     /**
172      * Set the name of the driver of this JDBC Resource Adaptor
173      * @param driverName the name of the driver of this JDBC Resource Adaptor
174      */

175     public void setDriverName(String JavaDoc driverName) {
176         this.driverName = driverName;
177         this.realDriverName = driverName;
178     }
179
180     /**
181      * Set the jndiName of this JDBC Resource Adaptor
182      * @param jndiName the jndiName of this JDBC Resource Adaptor
183      */

184     public void setJndiName(String JavaDoc jndiName) {
185         this.jndiName = jndiName;
186     }
187
188     /**
189      * Check the properties
190      */

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     /**
210      * Gets a Properties object for JDBC
211      * @return configured properties
212      */

213     private Properties JavaDoc getProperties() {
214         // Check properties
215
checkProperties();
216
217         Properties JavaDoc props = new Properties JavaDoc();
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     /**
232      * Execute this task
233      */

234     public void execute() {
235
236         // Build new temp file for making a resource adaptor
237
File JavaDoc tmpFile = new File JavaDoc(System.getProperty("java.io.tmpdir"), String.valueOf(new Date JavaDoc().getTime()));
238
239         // Write properties to a file
240
Properties JavaDoc props = getProperties();
241         writePropsToFile(INFO, props, tmpFile);
242
243         // Build resource adapter for this configuration
244

245         // args
246
String JavaDoc jRootRarsDir = getJonasRoot().getPath() + File.separator + "rars" + File.separator + "autoload";
247         String JavaDoc jBaseRarsDir = getDestDir().getPath() + File.separator + "rars";
248         if (isAutoload()) {
249             jBaseRarsDir += File.separator + "autoload";
250         }
251         String JavaDoc jdbcDM = jRootRarsDir + File.separator + JONAS_JDBCDM;
252         String JavaDoc 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 JavaDoc 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         // P6Spy tool Configuration
278
if (p6spy) {
279             // Set the realdriver property of the JONAS_BASE/conf/spy.properties P6Spy configuration file
280
String JavaDoc jBaseConf = getJonasRoot().getPath() + File.separator + "conf";
281             changeValueForKey(INFO, jBaseConf, JOnASBaseTask.P6SPY_CONF_FILE,
282                     REALDRIVER_PROPERTY, realDriverName, "=", false);
283         }
284     }
285
286     /**
287      * Copy rar to autoload or only in rars/ ?
288      * @return the autoload.
289      */

290     public boolean isAutoload() {
291         return autoload;
292     }
293
294     /**
295      * opy rar to autoload or only in rars/ ?
296      * @param autoload true of false
297      */

298     public void setAutoload(boolean autoload) {
299         this.autoload = autoload;
300     }
301
302     /**
303      * Configure the using of the P6Spy tool or not ?
304      * @return the p6spy
305      */

306     public boolean isP6spy() {
307         return p6spy;
308     }
309
310     /**
311      * Configure the using of the P6Spy tool or not ?
312      * @param p6spy true or false
313      */

314     public void setP6spy(boolean p6spy) {
315         this.p6spy = p6spy;
316     }
317 }
Popular Tags