View Javadoc
1 /* 2 * PROJECT : DAR Runtime and Tools 3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org 4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE 5 * Version 2.1, February 1999 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 package org.enableit.db.darrt.ant; 22 23 import java.io.File; 24 import java.io.FileReader; 25 import java.io.FileWriter; 26 import java.io.IOException; 27 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.Project; 30 import org.apache.tools.ant.types.FileSet; 31 import org.apache.tools.ant.types.ZipFileSet; 32 import org.apache.tools.zip.ZipOutputStream; 33 import org.enableit.db.beans.Database; 34 import org.enableit.db.beans.Driver; 35 import org.enableit.db.beans.Provider; 36 import org.enableit.db.beans.Schema; 37 import org.enableit.db.beans.Table; 38 import org.enableit.db.darrt.DarHandler; 39 40 41 /*** 42 * Creates a DAR archive. 43 * 44 * @author Tim Stephenson 45 * 46 * @ant.task category="packaging" 47 */ 48 public class DarTask extends org.apache.tools.ant.taskdefs.Jar { 49 /*** 50 * CVS info ABOUT this class and its current version 51 */ 52 public static final String ABOUT = "$Id: DarTask.java,v 1.9 2004/03/20 05:22:18 tim Exp $"; 53 54 /*** 55 * Constituent parts of a schema, each must represent a table. 56 */ 57 private FileSet schemaFs; 58 59 /*** 60 * The JDBC driver's class name. 61 */ 62 protected String targetDriver; 63 64 /*** 65 * The database URL to connect to. 66 */ 67 protected String targetUrl; 68 69 /*** 70 * Username to use when connecting to the database. 71 */ 72 protected String targetUserid; 73 74 /*** 75 * Schema name to use when connecting to the database. 76 */ 77 private String targetSchemaName; 78 79 /*** 80 * Password to use when connecting to the database. 81 */ 82 protected String targetPassword = ""; 83 84 /*** 85 * Name of the file to use as the deployment descriptor. 86 * <p>This file will be save as <code>META-INF/schema.xml </code> 87 * within the archive.</p> 88 */ 89 private File deploymentDescriptor; 90 91 /*** 92 * Whether a deployment descriptor has been added. A DAR archive without 93 * a deployment descriptor is invalid. 94 */ 95 private boolean descriptorAdded; 96 97 /*** 98 * Default constructor. 99 */ 100 public DarTask() { 101 super(); 102 archiveType = "dar"; 103 emptyBehavior = "create"; 104 } 105 106 /*** 107 * Sets the JDBC driver to use in making a connection 108 */ 109 public void setTargetDriver(String driver) { 110 this.targetDriver = driver; 111 } 112 113 /*** 114 * Sets the JDBC url to connect to. 115 */ 116 public void setTargetUrl(String url) { 117 this.targetUrl = url; 118 } 119 120 /*** 121 * Sets the JDBC userid to use in making a connection 122 */ 123 public void setTargetUserid(String userid) { 124 this.targetUserid = userid; 125 } 126 127 /*** 128 * Sets the JDBC password to use in making a connection 129 */ 130 public void setTargetPassword(String password) { 131 this.targetPassword = password; 132 } 133 134 public void setTargetSchemaName(String targetSchemaName) { 135 this.targetSchemaName = targetSchemaName; 136 } 137 138 /*** 139 * Set the schema descriptor for this DAR file. 140 * @param descr File representing the descriptor. 141 */ 142 public void setSchemaxml(File descr) { 143 deploymentDescriptor = descr; 144 145 if (!deploymentDescriptor.exists()) { 146 throw new BuildException("Deployment descriptor: " 147 + deploymentDescriptor + " does not exist."); 148 } 149 150 // Create a ZipFileSet for this file, and pass it up. 151 ZipFileSet fs = new ZipFileSet(); 152 153 fs.setDir(new File(deploymentDescriptor.getParent())); 154 fs.setIncludes(deploymentDescriptor.getName()); 155 fs.setFullpath(DarHandler.SCHEMA_ENTRY); 156 super.addFileset(fs); 157 } 158 159 /*** 160 * Adds a zipfileset, specifying libraries to add to the DAR. 161 * <p>TODO: At this point there is no functionality defined for these 162 * libraries. It could be used in the future to specify jars to 163 * install in Java-enabled databases or to specify the darrt jars 164 * themselves.</p> 165 * @param fs FileSet to add to archive. 166 */ 167 public void addLib(ZipFileSet fs) { 168 // We just set the prefix for this fileset, and pass it up. 169 fs.setPrefix("META-INF/lib/"); 170 super.addFileset(fs); 171 } 172 173 /*** 174 * Adds a zipfileset, specifying the data files to add to the DAR. 175 * @param fs FileSet to add to archive. 176 */ 177 public void addData(ZipFileSet fs) { 178 // We just set the prefix for this fileset, and pass it up. 179 fs.setPrefix("data/"); 180 super.addFileset(fs); 181 } 182 183 /*** 184 * Adds a zipfileset, specifying the stored procedure code to add 185 * to the DAR. 186 * @param fs FileSet to add to archive. 187 */ 188 public void addProcedures(ZipFileSet fs) { 189 // We just set the prefix for this fileset, and pass it up. 190 fs.setPrefix("procedures/"); 191 super.addFileset(fs); 192 } 193 194 /*** 195 * Add schema information to the DAR. 196 * @param fs Specifies the schema in individual table chunks. 197 */ 198 public void addSchema(FileSet fs) { 199 schemaFs = fs; 200 } 201 202 /*** 203 * Method required by Ant archive-handling tasks. 204 * @param zOut Compressed output stream of containing the archive?? 205 * @throws IOException If there is a problem with file access. 206 * @throws BuildException If descriptor is missing. 207 */ 208 protected void initZipOutputStream(ZipOutputStream zOut) 209 throws IOException, BuildException { 210 // If no schemaxml file is specified, it's an error. 211 if ((deploymentDescriptor == null) && !isInUpdateMode()) { 212 throw new BuildException("schemaxml attribute is required", location); 213 } 214 215 super.initZipOutputStream(zOut); 216 } 217 218 /*** 219 * Method required by Ant archive-handling tasks. 220 * @param file The target file to write to ??. 221 * @param zOut Compressed output stream of containing the archive. 222 * @param vPath ?? . 223 * @throws IOException If there is a problem with file access. 224 */ 225 protected void zipFile(File file, ZipOutputStream zOut, String vPath) 226 throws IOException { 227 // If the file being added is META-INF/schema.xml, we warn if it's 228 // not the one specified in the "schemaxml" attribute - or if 229 // it's being added twice, meaning the same file is specified 230 // by the "schemaxml" attribute and in a <fileset> element. 231 if (vPath.equalsIgnoreCase("META-INF/schema.xml")) { 232 if ((deploymentDescriptor == null) 233 || !deploymentDescriptor.equals(file) 234 || descriptorAdded) { 235 log("Warning: selected " + archiveType 236 + " files include a META-INF/schema.xml" 237 + " which will be ignored " 238 + "(please use schemaxml attribute to " + archiveType 239 + " task)", Project.MSG_WARN); 240 } else { 241 super.zipFile(file, zOut, vPath); 242 descriptorAdded = true; 243 } 244 } else { 245 super.zipFile(file, zOut, vPath); 246 } 247 } 248 249 /*** 250 * Make sure we don't think we already have a schema.xml next 251 * time this task 252 * gets executed. 253 */ 254 protected void cleanUp() { 255 descriptorAdded = false; 256 super.cleanUp(); 257 } 258 259 public void execute() { 260 try { 261 // if have schema fileset need to merge the parts ... 262 if (schemaFs != null) { 263 Database db = null; 264 Schema schema = null; 265 266 if (deploymentDescriptor.exists() 267 && deploymentDescriptor.isFile()) { 268 log("Merging tables into schema", Project.MSG_VERBOSE); 269 db = Database.unmarshal(new FileReader(deploymentDescriptor)); 270 271 // TODO: How to determine schema to merge to if more than one? 272 schema = db.getSchema(0); 273 } else { 274 log("Creating schema from tables", Project.MSG_VERBOSE); 275 db = new Database(); 276 277 // TODO: dunno if putting this dummo data in is a problem, 278 // not very neat though... 279 Provider provider = new Provider(); 280 Driver driver = new Driver(); 281 282 driver.setClassName(this.targetDriver); 283 driver.setUrl(this.targetUrl); 284 driver.setUsername(this.targetUserid); 285 driver.setPassword(this.targetPassword); 286 provider.setDriver(driver); 287 db.setProvider(provider); 288 schema = new Schema(); 289 schema.setName(this.targetSchemaName); 290 db.addSchema(schema); 291 } 292 293 String[] tableDefs = schemaFs.getDirectoryScanner(project) 294 .getIncludedFiles(); 295 296 for (int i = 0; i < tableDefs.length; i++) { 297 if (!tableDefs[i].equals("schema.xml")) { 298 File file = new File(schemaFs.getDir(project), 299 tableDefs[i]); 300 301 log("Found table def: " + file, Project.MSG_VERBOSE); 302 schema.addTable(Table.unmarshal(new FileReader(file))); 303 } 304 } 305 306 // ... override schemaxml ... 307 File tempSchema = File.createTempFile("darrt", null); 308 FileWriter out = new FileWriter(tempSchema); 309 310 db.marshal(out); 311 out.close(); 312 setSchemaxml(tempSchema); 313 } 314 } catch (Exception e) { 315 throw new BuildException("Problem with schema creation: ", e); 316 } 317 318 // ... then carry on as normal 319 super.execute(); 320 } 321 }

This page was automatically generated by Maven