001    package org.apache.ojb.broker.ant;
002    
003    /* Copyright 2002-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.tools.ant.BuildException;
019    import org.apache.tools.ant.Task;
020    import org.apache.tools.ant.types.Path;
021    import org.apache.tools.ant.types.Reference;
022    import org.apache.tools.ant.taskdefs.Java;
023    
024    public class TorqueRepositoryGeneratorTask extends Task {
025    
026        private String inputFile;
027        private String outputFile;
028        private String database;
029    
030        // Optional parameters to the task
031        private Reference classpathRef = null;
032        private Path classpath = null;
033        private String indexTablespace = "";
034        private boolean useNativeIncrement = true;
035    
036        public void setInputFile(String inputFile) {
037            this.inputFile = inputFile;
038        }
039    
040        public String getInputFile() {
041            return this.inputFile;
042        }
043    
044        public void setOutputFile(String outputFile) {
045            this.outputFile = outputFile;
046        }
047    
048        public String getOutputFile() {
049            return this.outputFile;
050        }
051    
052        public void setDatabase(String database) {
053            this.database = database;
054        }
055    
056        public String getDatabase() {
057            return this.database;
058        }
059    
060        public void setIndexTablespace(String indexTablespace) {
061            this.indexTablespace = indexTablespace;
062        }
063    
064        public String getIndexTablespace() {
065            return this.indexTablespace;
066        }
067    
068        public void setUseNativeIncrement(String useNativeIncrement) {
069            if ("yes".equalsIgnoreCase(useNativeIncrement)) {
070                this.useNativeIncrement = true;
071            } else {
072                this.useNativeIncrement = false;
073            }
074        }
075    
076        public String getUseNativeIncrement() {
077            if (this.useNativeIncrement) {
078                return "yes";
079            } else {
080                return "no";
081            }
082        }
083    
084        public Path getClasspath() {
085            return this.classpath;
086        }
087    
088        public void setClasspath(Path path) {
089            this.classpath = path;
090        }
091    
092        public Path createClasspath() {
093            if (this.classpath == null) {
094                this.classpath = new Path(getProject());
095            }
096    
097            return this.classpath.createPath();
098        }
099    
100        public Reference getClasspathRef() {
101            return this.classpathRef;
102        }
103    
104        public void setClasspathRef(Reference classpathRef) {
105            this.classpathRef = classpathRef;
106        }
107    
108        public void execute() throws BuildException {
109            checkParameters();
110            Java javaTask = (Java)project.createTask("java");
111            javaTask.createArg().setLine(this.inputFile + " " +
112                                         this.outputFile + " " +
113                                         this.database + " " +
114                                         this.indexTablespace + " " +
115                                         this.useNativeIncrement);
116            javaTask.setFork(true);
117            javaTask.setClassname("org.apache.ojb.broker.metadata.torque.TorqueRepositoryGenerator");
118            if (this.classpathRef != null) {
119                javaTask.setClasspathRef(this.classpathRef);
120            }
121            if (this.classpath != null) {
122                javaTask.setClasspath(this.classpath);
123            }
124            javaTask.execute();
125        }
126    
127        protected boolean isEmpty(String string) {
128            return (string == null || string.trim().length() == 0);
129        }
130    
131        protected void checkParameters() throws BuildException {
132            StringBuffer errorMessageBuffer = new StringBuffer();
133    
134            if (isEmpty(this.database)) {
135                errorMessageBuffer.append("Database property not set.\n");
136            }
137    
138            if (isEmpty(this.inputFile)) {
139                errorMessageBuffer.append("Input file property not set.\n");
140            }
141    
142            if (isEmpty(this.outputFile)) {
143                errorMessageBuffer.append("Output file property not set.\n");
144            }
145    
146            if (errorMessageBuffer.toString().length() > 0) {
147                throw new BuildException(errorMessageBuffer.toString());
148            }
149        }
150    }