KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Mkdir


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.File JavaDoc;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Task;
24 import org.apache.tools.ant.Project;
25
26 /**
27  * Creates a given directory.
28  * Creates a directory and any non-existent parent directories, when
29  * necessary
30  *
31  * @since Ant 1.1
32  *
33  * @ant.task category="filesystem"
34  */

35
36 public class Mkdir extends Task {
37
38     private static final int MKDIR_RETRY_SLEEP_MILLIS = 10;
39     /**
40      * our little directory
41      */

42     private File JavaDoc dir;
43
44     /**
45      * create the directory and all parents
46      * @throws BuildException if dir is somehow invalid, or creation failed.
47      */

48     public void execute() throws BuildException {
49         if (dir == null) {
50             throw new BuildException("dir attribute is required", getLocation());
51         }
52
53         if (dir.isFile()) {
54             throw new BuildException("Unable to create directory as a file "
55                                      + "already exists with that name: "
56                                      + dir.getAbsolutePath());
57         }
58
59         if (!dir.exists()) {
60             boolean result = mkdirs(dir);
61             if (!result) {
62                 String JavaDoc msg = "Directory " + dir.getAbsolutePath()
63                     + " creation was not successful for an unknown reason";
64                 throw new BuildException(msg, getLocation());
65             }
66             log("Created dir: " + dir.getAbsolutePath());
67         } else {
68             log("Skipping " + dir.getAbsolutePath()
69                 + " because it already exists.", Project.MSG_VERBOSE);
70         }
71     }
72
73     /**
74      * the directory to create; required.
75      *
76      * @param dir the directory to be made.
77      */

78     public void setDir(File JavaDoc dir) {
79         this.dir = dir;
80     }
81     /**
82      * Attempt to fix possible race condition when creating
83      * directories on WinXP. If the mkdirs does not work,
84      * wait a little and try again.
85      */

86     private boolean mkdirs(File JavaDoc f) {
87         if (!f.mkdirs()) {
88             try {
89                 Thread.sleep(MKDIR_RETRY_SLEEP_MILLIS);
90                 return f.mkdirs();
91             } catch (InterruptedException JavaDoc ex) {
92                 return f.mkdirs();
93             }
94         }
95         return true;
96     }
97 }
98
99
Popular Tags