001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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:
019: package org.apache.tools.ant.taskdefs;
020:
021: import java.io.File;
022: import org.apache.tools.ant.BuildException;
023: import org.apache.tools.ant.Task;
024: import org.apache.tools.ant.Project;
025:
026: /**
027: * Creates a given directory.
028: * Creates a directory and any non-existent parent directories, when
029: * necessary
030: *
031: * @since Ant 1.1
032: *
033: * @ant.task category="filesystem"
034: */
035:
036: public class Mkdir extends Task {
037:
038: private static final int MKDIR_RETRY_SLEEP_MILLIS = 10;
039: /**
040: * our little directory
041: */
042: private File dir;
043:
044: /**
045: * create the directory and all parents
046: * @throws BuildException if dir is somehow invalid, or creation failed.
047: */
048: public void execute() throws BuildException {
049: if (dir == null) {
050: throw new BuildException("dir attribute is required",
051: getLocation());
052: }
053:
054: if (dir.isFile()) {
055: throw new BuildException(
056: "Unable to create directory as a file "
057: + "already exists with that name: "
058: + dir.getAbsolutePath());
059: }
060:
061: if (!dir.exists()) {
062: boolean result = mkdirs(dir);
063: if (!result) {
064: String msg = "Directory "
065: + dir.getAbsolutePath()
066: + " creation was not successful for an unknown reason";
067: throw new BuildException(msg, getLocation());
068: }
069: log("Created dir: " + dir.getAbsolutePath());
070: } else {
071: log("Skipping " + dir.getAbsolutePath()
072: + " because it already exists.",
073: Project.MSG_VERBOSE);
074: }
075: }
076:
077: /**
078: * the directory to create; required.
079: *
080: * @param dir the directory to be made.
081: */
082: public void setDir(File dir) {
083: this .dir = dir;
084: }
085:
086: /**
087: * Attempt to fix possible race condition when creating
088: * directories on WinXP. If the mkdirs does not work,
089: * wait a little and try again.
090: */
091: private boolean mkdirs(File f) {
092: if (!f.mkdirs()) {
093: try {
094: Thread.sleep(MKDIR_RETRY_SLEEP_MILLIS);
095: return f.mkdirs();
096: } catch (InterruptedException ex) {
097: return f.mkdirs();
098: }
099: }
100: return true;
101: }
102: }
|