001: package org.apache.ojb.broker.util.dbhandling;
002:
003: /* Copyright 2004-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 java.io.IOException;
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.StringTokenizer;
022:
023: import org.apache.ojb.broker.PBKey;
024: import org.apache.ojb.broker.PersistenceBrokerFactory;
025: import org.apache.ojb.broker.util.ClassHelper;
026: import org.apache.ojb.broker.metadata.ConnectionRepository;
027: import org.apache.ojb.broker.metadata.MetadataManager;
028: import org.apache.tools.ant.*;
029: import org.apache.tools.ant.types.FileSet;
030:
031: /**
032: * Ant task for performing basic db setup functions.
033: *
034: * @author Thomas Dudziak
035: */
036: public class DBHandlingTask extends Task {
037: /** The name of the known db handlings */
038: private static final String HANDLING_TORQUE = "torque";
039: /** The commands */
040: private static final String COMMAND_CREATE = "create";
041: private static final String COMMAND_INIT = "init";
042:
043: /** The name of the db handling to use */
044: private String _handling = HANDLING_TORQUE;
045: /** The path to the properties file */
046: private String _propertiesFile = null;
047: /** The alias of the jdbc connection to use (empty = default connection) */
048: private String _jcdAlias = null;
049: /** The working directory */
050: private String _workDir = null;
051: /** The input files */
052: private ArrayList _fileSets = new ArrayList();
053: /** The commands to perform */
054: private String _commands = "";
055:
056: /**
057: * Sets the name of the handling to use.
058: *
059: * @param name The short name of the handling
060: */
061: public void setHandling(String name) {
062: _handling = (name == null ? HANDLING_TORQUE : name
063: .toLowerCase());
064: }
065:
066: /**
067: * Returns the name of the handling that is used.
068: *
069: * @return The short name of the handling
070: */
071: public String getHandling() {
072: return _handling;
073: }
074:
075: /**
076: * Sets the properties file (OJB.properties).
077: *
078: * @param path The path to the properties file
079: */
080: public void setPropertiesFile(String path) {
081: _propertiesFile = path;
082: }
083:
084: /**
085: * Returns the properties file.
086: *
087: * @return The path to the properties file
088: */
089: public String getPropertiesFile() {
090: return _propertiesFile;
091: }
092:
093: /**
094: * Sets the alias of the jdbc connection to use.
095: *
096: * @param alias The alias of the connection
097: */
098: public void setJcdAlias(String alias) {
099: _jcdAlias = alias;
100: }
101:
102: /**
103: * Returns the alias of the jdbc connection.
104: *
105: * @return The alias
106: */
107: public String getJcdAlias() {
108: return _jcdAlias;
109: }
110:
111: /**
112: * Sets the working directory. If none is given, then the system's temporary directory is used.
113: *
114: * @param dir The working directory
115: */
116: public void setWorkDir(String dir) {
117: _workDir = dir;
118: }
119:
120: /**
121: * Returns the working directory.
122: *
123: * @return The working directory
124: */
125: public String getWorkDir() {
126: return _workDir;
127: }
128:
129: /**
130: * Adds a fileset.
131: *
132: * @param fileset The additional input files
133: */
134: public void addFileset(FileSet fileset) {
135: _fileSets.add(fileset);
136: }
137:
138: /**
139: * Sets the list of commands to perform.
140: *
141: * @param listOfCommands The comma-separated list of commands
142: */
143: public void setCommands(String listOfCommands) {
144: _commands = listOfCommands;
145: }
146:
147: /**
148: * Returns the list of commands.
149: *
150: * @return The comma-separated list of commands
151: */
152: public String getCommands() {
153: return _commands;
154: }
155:
156: /* (non-Javadoc)
157: * @see org.apache.tools.ant.Task#execute()
158: */
159: public void execute() throws BuildException {
160: if ((_commands == null) || (_commands.length() == 0)) {
161: return;
162: }
163:
164: DBHandling handling = createDBHandling();
165:
166: try {
167: if ((_workDir != null) && (_workDir.length() > 0)) {
168: handling.setWorkDir(_workDir);
169: System.setProperty("user.dir", _workDir);
170: }
171: for (Iterator it = _fileSets.iterator(); it.hasNext();) {
172: addIncludes(handling, (FileSet) it.next());
173: }
174:
175: if ((_propertiesFile != null)
176: && (_propertiesFile.length() > 0)) {
177: System.setProperty("OJB.properties", _propertiesFile);
178: }
179:
180: ConnectionRepository connRep = MetadataManager
181: .getInstance().connectionRepository();
182: PBKey pbKey = null;
183:
184: if ((_jcdAlias == null) || (_jcdAlias.length() == 0)) {
185: pbKey = PersistenceBrokerFactory.getDefaultKey();
186: } else {
187: pbKey = connRep.getStandardPBKeyForJcdAlias(_jcdAlias);
188: if (pbKey == null) {
189: throw new BuildException("Undefined jcdAlias "
190: + _jcdAlias);
191: }
192: }
193: handling.setConnection(connRep.getDescriptor(pbKey));
194:
195: String command;
196:
197: for (StringTokenizer tokenizer = new StringTokenizer(
198: _commands, ","); tokenizer.hasMoreTokens();) {
199: command = tokenizer.nextToken().toLowerCase().trim();
200: if (COMMAND_CREATE.equals(command)) {
201: handling.createDB();
202: } else if (COMMAND_INIT.equals(command)) {
203: handling.initDB();
204: } else {
205: throw new BuildException("Unknown command "
206: + command);
207: }
208: }
209: } catch (Exception ex) {
210: throw new BuildException(ex);
211: }
212: }
213:
214: /**
215: * Creates a db handling object.
216: *
217: * @return The db handling object
218: * @throws BuildException If the handling is invalid
219: */
220: private DBHandling createDBHandling() throws BuildException {
221: if ((_handling == null) || (_handling.length() == 0)) {
222: throw new BuildException("No handling specified");
223: }
224: try {
225: String className = "org.apache.ojb.broker.platforms."
226: + Character.toTitleCase(_handling.charAt(0))
227: + _handling.substring(1) + "DBHandling";
228: Class handlingClass = ClassHelper.getClass(className);
229:
230: return (DBHandling) handlingClass.newInstance();
231: } catch (Exception ex) {
232: throw new BuildException("Invalid handling '" + _handling
233: + "' specified");
234: }
235: }
236:
237: /**
238: * Adds the includes of the fileset to the handling.
239: *
240: * @param handling The handling
241: * @param fileSet The fileset
242: */
243: private void addIncludes(DBHandling handling, FileSet fileSet)
244: throws BuildException {
245: DirectoryScanner scanner = fileSet
246: .getDirectoryScanner(getProject());
247: String[] files = scanner.getIncludedFiles();
248: StringBuffer includes = new StringBuffer();
249:
250: for (int idx = 0; idx < files.length; idx++) {
251: if (idx > 0) {
252: includes.append(",");
253: }
254: includes.append(files[idx]);
255: }
256: try {
257: handling.addDBDefinitionFiles(fileSet.getDir(getProject())
258: .getAbsolutePath(), includes.toString());
259: } catch (IOException ex) {
260: throw new BuildException(ex);
261: }
262: }
263: }
|