001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JdbcRa.java 6618 2005-04-22 11:15:08Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.ant.jonasbase;
025:
026: import java.io.File;
027: import java.util.Date;
028: import java.util.Properties;
029:
030: import org.apache.tools.ant.BuildException;
031: import org.apache.tools.ant.taskdefs.Java;
032:
033: import org.objectweb.jonas.ant.JOnASBaseTask;
034:
035: /**
036: * Allow to create JDBC resource adaptors
037: * @author Florent Benoit
038: */
039: public class JdbcRa extends JTask implements BaseTaskItf {
040:
041: /**
042: * Info for the logger
043: */
044: private static final String INFO = "[JdbcRa] ";
045:
046: /**
047: * Name of the rar for JDBC_DM
048: */
049: private static final String JONAS_JDBCDM = "JOnAS_jdbcDM";
050:
051: /**
052: * RAConfig classname
053: */
054: private static final String RACONFIG_CLASS = "org.objectweb.jonas_rar.raconfig.RAConfig";
055:
056: /**
057: * P6Spy driver class name
058: */
059: private static final String P6SPY_DRIVER = "com.p6spy.engine.spy.P6SpyDriver";
060:
061: /**
062: * Name of the property of the real driver when using P6Spy tool
063: */
064: private static final String REALDRIVER_PROPERTY = "realdriver";
065:
066: /**
067: * Name of this JDBC Resource Adaptor
068: */
069: private String name = null;
070:
071: /**
072: * Mapper Name of this JDBC Resource Adaptor
073: */
074: private String mapperName = null;
075:
076: /**
077: * username of this JDBC Resource Adaptor
078: */
079: private String user = null;
080:
081: /**
082: * Password of this JDBC Resource Adaptor
083: */
084: private String password = null;
085:
086: /**
087: * URL of this JDBC Resource Adaptor
088: */
089: private String url = null;
090:
091: /**
092: * Driver Name of this JDBC Resource Adaptor (may be set to the P6Spy driver name)
093: */
094: private String driverName = null;
095:
096: /**
097: * Driver Name of this JDBC Resource Adaptor
098: */
099: private String realDriverName = null;
100:
101: /**
102: * Max Pool Size
103: */
104: private String maxPoolSize = "100";
105:
106: /**
107: * JNDI Name of this JDBC Resource Adaptor
108: */
109: private String jndiName = null;
110:
111: /**
112: * Copy to autoload dir or not
113: */
114: private boolean autoload = true;
115:
116: /**
117: * Using of the P6Spy tool or not
118: */
119: private boolean p6spy = false;
120:
121: /**
122: * Set the name of this JDBC Resource Adaptor
123: * @param name the name of this JDBC Resource Adaptor
124: */
125: public void setName(String name) {
126: this .name = name;
127: }
128:
129: /**
130: * Set the mapper name of this JDBC Resource Adaptor
131: * @param mapperName the mappername of this JDBC Resource Adaptor
132: */
133: public void setMapperName(String mapperName) {
134: this .mapperName = mapperName;
135: }
136:
137: /**
138: * Set the user of this JDBC Resource Adaptor
139: * @param user the user of this JDBC Resource Adaptor
140: */
141: public void setUser(String user) {
142: this .user = user;
143: }
144:
145: /**
146: * Set the password of this JDBC Resource Adaptor
147: * @param password the name of this JDBC Resource Adaptor
148: */
149: public void setPassword(String password) {
150: this .password = password;
151: }
152:
153: /**
154: * Set the url of this JDBC Resource Adaptor
155: * @param url the name of this JDBC Resource Adaptor
156: */
157: public void setUrl(String url) {
158: this .url = url;
159: }
160:
161: /**
162: * Set the max pool size of this JDBC Resource Adaptor Connection Pool
163: * @param maxPoolSize max pool size of connection
164: */
165: public void setMaxPoolSize(String maxPoolSize) {
166: this .maxPoolSize = maxPoolSize;
167: }
168:
169: /**
170: * Set the name of the driver of this JDBC Resource Adaptor
171: * @param driverName the name of the driver of this JDBC Resource Adaptor
172: */
173: public void setDriverName(String driverName) {
174: this .driverName = driverName;
175: this .realDriverName = driverName;
176: }
177:
178: /**
179: * Set the jndiName of this JDBC Resource Adaptor
180: * @param jndiName the jndiName of this JDBC Resource Adaptor
181: */
182: public void setJndiName(String jndiName) {
183: this .jndiName = jndiName;
184: }
185:
186: /**
187: * Check the properties
188: */
189: private void checkProperties() {
190: if (name == null) {
191: throw new BuildException(INFO
192: + "Property 'name' is missing.");
193: } else if (mapperName == null) {
194: throw new BuildException(INFO
195: + "Property 'mapperName' is missing.");
196: } else if (user == null) {
197: throw new BuildException(INFO
198: + "Property 'user' is missing.");
199: } else if (password == null) {
200: throw new BuildException(INFO
201: + "Property 'password' is missing.");
202: } else if (url == null) {
203: throw new BuildException(INFO
204: + "Property 'url' is missing.");
205: } else if (driverName == null) {
206: throw new BuildException(INFO
207: + "Property 'driverName' is missing.");
208: } else if (jndiName == null) {
209: throw new BuildException(INFO
210: + "Property 'jndiName' is missing.");
211: }
212: }
213:
214: /**
215: * Gets a Properties object for JDBC
216: * @return configured properties
217: */
218: private Properties getProperties() {
219: // Check properties
220: checkProperties();
221:
222: Properties props = new Properties();
223: props.put("datasource.name", jndiName);
224: props.put("datasource.url", url);
225: if (p6spy) {
226: driverName = P6SPY_DRIVER;
227: }
228: props.put("datasource.classname", driverName);
229: props.put("datasource.username", user);
230: props.put("datasource.password", password);
231: props.put("datasource.mapper", mapperName);
232: props.put("jdbc.maxconpool", maxPoolSize);
233: return props;
234: }
235:
236: /**
237: * Execute this task
238: */
239: public void execute() {
240:
241: // Build new temp file for making a resource adaptor
242: File tmpFile = new File(System.getProperty("java.io.tmpdir"),
243: String.valueOf(new Date().getTime()));
244:
245: // Write properties to a file
246: Properties props = getProperties();
247: writePropsToFile(INFO, props, tmpFile);
248:
249: // Build resource adapter for this configuration
250:
251: // args
252: String jRootRarsDir = getJonasRoot().getPath() + File.separator
253: + "rars" + File.separator + "autoload";
254: String jBaseRarsDir = getDestDir().getPath() + File.separator
255: + "rars";
256: if (isAutoload()) {
257: jBaseRarsDir += File.separator + "autoload";
258: }
259: String jdbcDM = jRootRarsDir + File.separator + JONAS_JDBCDM;
260: String destRarFile = jBaseRarsDir + File.separator + name
261: + ".rar";
262:
263: Java raConfigTask = getBootstraptask("");
264: raConfigTask.clearArgs();
265: raConfigTask.createArg().setValue(RACONFIG_CLASS);
266: raConfigTask.createArg().setValue("-dm");
267: raConfigTask.createArg().setValue("-p");
268: raConfigTask.createArg().setValue(tmpFile.getPath());
269: raConfigTask.createArg().setValue(jdbcDM);
270: raConfigTask.createArg().setValue(destRarFile);
271:
272: log(INFO + "Generating a rar file with name '" + name
273: + "', mapperName '" + mapperName + "', user '" + user
274: + "', password '" + password + "', URL '" + url
275: + "', driverName '" + driverName + "' and jndiName '"
276: + jndiName + "'...");
277:
278: try {
279: raConfigTask.execute();
280: } catch (Exception rae) {
281: rae.printStackTrace();
282: throw new BuildException(INFO
283: + "Cannot make a resource adaptor on RAConfig: ",
284: rae);
285: } finally {
286: tmpFile.delete();
287: }
288: log(INFO + "Rar generated : '" + destRarFile + "'.");
289:
290: // P6Spy tool Configuration
291: if (p6spy) {
292: // Set the realdriver property of the JONAS_BASE/conf/spy.properties P6Spy configuration file
293: String jBaseConf = getJonasRoot().getPath()
294: + File.separator + "conf";
295: changeValueForKey(INFO, jBaseConf,
296: JOnASBaseTask.P6SPY_CONF_FILE, REALDRIVER_PROPERTY,
297: realDriverName, "=", false);
298: }
299: }
300:
301: /**
302: * Copy rar to autoload or only in rars/ ?
303: * @return the autoload.
304: */
305: public boolean isAutoload() {
306: return autoload;
307: }
308:
309: /**
310: * opy rar to autoload or only in rars/ ?
311: * @param autoload true of false
312: */
313: public void setAutoload(boolean autoload) {
314: this .autoload = autoload;
315: }
316:
317: /**
318: * Configure the using of the P6Spy tool or not ?
319: * @return the p6spy
320: */
321: public boolean isP6spy() {
322: return p6spy;
323: }
324:
325: /**
326: * Configure the using of the P6Spy tool or not ?
327: * @param p6spy true or false
328: */
329: public void setP6spy(boolean p6spy) {
330: this.p6spy = p6spy;
331: }
332: }
|