KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > io > MkdirJob


1 package org.oddjob.io;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.Serializable JavaDoc;
6
7 import org.apache.commons.io.FileUtils;
8
9 /**
10  * @oddjob.description Make a directory, including any necessary but
11  * nonexistent parent directories. If there already exists a
12  * file with specified name or the directory cannot be created then
13  * an exception is flagged. If the directory exists alread it is left
14  * intact.
15  *
16  * @oddjob.example
17  *
18  * <pre>
19  * &lt;oddjob id="this"&gt;
20  * &lt;mkdir dir="${this.dir}/work/a/b/c"/&gt;
21  * &lt;/oddjob&gt;
22  * </pre>
23  */

24
25 public class MkdirJob implements Runnable JavaDoc, Serializable JavaDoc {
26     private static final long serialVersionUID = 20060117;
27
28     /**
29      * @oddjob.property
30      * @oddjob.description A name, can be any text.
31      * @oddjob.required No.
32      */

33     private String JavaDoc name;
34
35     /**
36      * @oddjob.property
37      * @oddjob.description The directory to create.
38      * @oddjob.required Yes.
39      */

40     private File JavaDoc dir;
41
42     /**
43      * Get the name.
44      *
45      * @return The name.
46      */

47     public String JavaDoc getName() {
48         return name;
49     }
50     
51     /**
52      * Set the name
53      *
54      * @param name The name.
55      */

56     public void setName(String JavaDoc name) {
57         this.name = name;
58     }
59     
60     /**
61      * Get the file.
62      *
63      * @return The file.
64      */

65     public File JavaDoc getDir() {
66         return dir;
67     }
68     
69     /**
70      * Set the file.
71      *
72      * @param The file.
73      */

74     public void setDir(File JavaDoc file) {
75         this.dir = file;
76     }
77     
78     /*
79      * (non-Javadoc)
80      * @see java.lang.Runnable#run()
81      */

82     public void run() {
83         if (dir == null) {
84             throw new IllegalStateException JavaDoc("File must be specified.");
85         }
86         try {
87             FileUtils.forceMkdir(dir);
88         } catch (IOException JavaDoc e) {
89             throw new RuntimeException JavaDoc(e);
90         }
91     }
92     
93     /*
94      * (non-Javadoc)
95      * @see java.lang.Object#toString()
96      */

97     public String JavaDoc toString() {
98         if (name ==null) {
99             return "Create a Directory";
100         }
101         return name;
102     }
103 }
104
Popular Tags