001: /**********************************************************************
002: Copyright (c) 2004 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: 2004 Andy Jefferson - added sysproperty and classpath input options
017: 2004 Andy Jefferson - changed to be derived from Java taskdef
018: 2004 Andy Jefferson - removed redundant methods. Changed to se fork=true always
019: ...
020: **********************************************************************/package org.jpox;
021:
022: import java.io.File;
023: import java.util.Vector;
024: import org.apache.tools.ant.BuildException;
025: import org.apache.tools.ant.DirectoryScanner;
026: import org.apache.tools.ant.Project;
027: import org.apache.tools.ant.taskdefs.Java;
028: import org.apache.tools.ant.types.FileSet;
029: import org.jpox.util.Localiser;
030:
031: /**
032: * SchemaTool Ant Task. Accepts the following parameters
033: * <UL>
034: * <LI><B>mode</B> Mode of operation (<B>"create"</B>, "delete", "validate", "dbinfo", "schemainfo").</LI>
035: * <LI><B>verbose</B> Verbose output.</LI>
036: * <LI><B>props</B> Name of a properties file for use in SchemaTool (PMF properties)</LI>
037: * <LI><B>ddlFile</B> Name of a file to output the DDL into</LI>
038: * <LI><B>completeDdl</B> Whether to output complete DDL (otherwise just the missing tables/constraints)</LI>
039: * <LI><B>persistenceUnit</B> Name of a persistence-unit to manage the schema for</LI>
040: * </UL>
041: *
042: * @version $Revision: 1.13 $
043: */
044: public class SchemaToolTask extends Java {
045: /** Localiser for messages. */
046: private static final Localiser LOCALISER = Localiser
047: .getInstance("org.jpox.Localisation");
048:
049: /** Operating mode */
050: private int runMode = SchemaTool.SCHEMATOOL_CREATE_MODE;
051:
052: /** Filesets of JDO files to be used in generating the schema. */
053: Vector filesets = new Vector();
054:
055: /**
056: * Constructor.
057: */
058: public SchemaToolTask() {
059: setClassname("org.jpox.SchemaTool");
060: setFork(true);
061: }
062:
063: /**
064: * Execute method, to execute the task.
065: * @throws BuildException if any error happens while running the task
066: **/
067: public void execute() throws BuildException {
068: if (runMode == SchemaTool.SCHEMATOOL_CREATE_MODE) {
069: createArg().setValue("-create");
070: } else if (runMode == SchemaTool.SCHEMATOOL_DELETE_MODE) {
071: createArg().setValue("-delete");
072: } else if (runMode == SchemaTool.SCHEMATOOL_VALIDATE_MODE) {
073: createArg().setValue("-validate");
074: } else if (runMode == SchemaTool.SCHEMATOOL_DATABASE_INFO_MODE) {
075: createArg().setValue("-dbinfo");
076: } else if (runMode == SchemaTool.SCHEMATOOL_SCHEMA_INFO_MODE) {
077: createArg().setValue("-schemainfo");
078: }
079:
080: File[] files = getFiles();
081: for (int i = 0; i < files.length; i++) {
082: createArg().setFile(files[i]);
083: }
084:
085: setFork(true);
086:
087: super .execute();
088: }
089:
090: /**
091: * Add a fileset. @see ant manual
092: * @param fs the FileSet
093: */
094: public void addFileSet(FileSet fs) {
095: filesets.addElement(fs);
096: }
097:
098: protected File[] getFiles() {
099: Vector v = new Vector();
100: final int size = filesets.size();
101: for (int i = 0; i < size; i++) {
102: FileSet fs = (FileSet) filesets.elementAt(i);
103: DirectoryScanner ds = fs.getDirectoryScanner(getProject());
104: ds.scan();
105: String[] f = ds.getIncludedFiles();
106: for (int j = 0; j < f.length; j++) {
107: String pathname = f[j];
108: File file = new File(ds.getBasedir(), pathname);
109: file = getProject().resolveFile(file.getPath());
110: v.addElement(file);
111: }
112: }
113: File[] files = new File[v.size()];
114: v.copyInto(files);
115: return files;
116: }
117:
118: /**
119: * set verbose
120: * @param verbose Whether to give verbose output
121: */
122: public void setVerbose(boolean verbose) {
123: if (verbose) {
124: createArg().setValue("-v");
125: log("JPOX SchemaTool verbose: " + verbose,
126: Project.MSG_VERBOSE);
127: }
128: }
129:
130: /**
131: * Get properties for the PMF from a file
132: * @param propsFileName Name of props file
133: */
134: public void setProps(String propsFileName) {
135: if (propsFileName != null && propsFileName.length() > 0) {
136: createArg().setLine("-props " + propsFileName);
137: log("JPOX SchemaTool props: " + propsFileName,
138: Project.MSG_VERBOSE);
139: }
140: }
141:
142: /**
143: * Set the file to output DDL to
144: * @param file Name of DDL file
145: */
146: public void setDdlFile(String file) {
147: if (file != null && file.length() > 0) {
148: createArg().setLine("-ddlFile " + file);
149: log("JPOX SchemaTool ddlFile: " + file, Project.MSG_VERBOSE);
150: }
151: }
152:
153: /**
154: * set complete DDL
155: * @param complete Whether to give complete DDL
156: */
157: public void setCompleteDdl(boolean complete) {
158: if (complete) {
159: createArg().setValue("-completeDdl");
160: log("JPOX SchemaTool completeDdl: " + complete,
161: Project.MSG_VERBOSE);
162: }
163: }
164:
165: /**
166: * Set the name of the persistence unit to manage
167: * @param unitName Name of persistence-unit
168: */
169: public void setPersistenceUnit(String unitName) {
170: if (unitName != null && unitName.length() > 0) {
171: createArg().setLine("-persistenceUnit " + unitName);
172: log("JPOX SchemaTool persistenceUnit: " + unitName,
173: Project.MSG_VERBOSE);
174: }
175: }
176:
177: /**
178: * Set the API Adapter
179: * @param api API Adapter
180: */
181: public void setApi(String api) {
182: if (api != null && api.length() > 0) {
183: createArg().setValue("-api");
184: createArg().setValue(api);
185: log("JPOX SchemaTool api: " + api, Project.MSG_VERBOSE);
186: }
187: }
188:
189: /**
190: * Sets the mode of operation.
191: * @param mode The mode of operation ("create", "delete", "validate", "dbinfo", "schemainfo")
192: */
193: public void setMode(String mode) {
194: if (mode == null) {
195: return;
196: }
197: if (mode.equalsIgnoreCase("create")) {
198: this .runMode = SchemaTool.SCHEMATOOL_CREATE_MODE;
199: } else if (mode.equalsIgnoreCase("delete")) {
200: this .runMode = SchemaTool.SCHEMATOOL_DELETE_MODE;
201: } else if (mode.equalsIgnoreCase("validate")) {
202: this .runMode = SchemaTool.SCHEMATOOL_VALIDATE_MODE;
203: } else if (mode.equalsIgnoreCase("dbinfo")) {
204: this .runMode = SchemaTool.SCHEMATOOL_DATABASE_INFO_MODE;
205: } else if (mode.equalsIgnoreCase("schemainfo")) {
206: this .runMode = SchemaTool.SCHEMATOOL_SCHEMA_INFO_MODE;
207: } else {
208: System.err.println(LOCALISER.msg("014036"));
209: }
210: }
211: }
|