KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antmod > scm > ScmTask


1 package org.antmod.scm;
2
3 import java.io.File JavaDoc;
4 import java.text.ParseException JavaDoc;
5
6 import org.apache.tools.ant.BuildException;
7 import org.apache.tools.ant.Project;
8 import org.apache.tools.ant.Task;
9 import org.apache.tools.ant.types.EnumeratedAttribute;
10
11 /**
12  * Ant task for using Antmod's scm mechanism.
13  *
14  * @author Klaas Waslander
15  */

16 public class ScmTask extends Task {
17     private String JavaDoc dest;
18     
19     // either url or repos needs to be set
20
private String JavaDoc url;
21     private String JavaDoc urlProperty;
22     private String JavaDoc repos;
23     
24     private Command command;
25     private String JavaDoc revision;
26     private String JavaDoc module;
27     private boolean quiet;
28     private String JavaDoc message;
29     private String JavaDoc property;
30
31
32     public void execute() throws BuildException {
33         if (url == null && urlProperty == null && repos == null) {
34             throw new BuildException("Either 'url', 'urlProperty' or 'repos' attribute has to be set for the ScmTask.");
35         }
36
37         try {
38             // get scm system using either url or repos
39
ScmSystem scm;
40             if (this.urlProperty != null) {
41                 this.url = getProject().getProperty(this.urlProperty);
42                 try {
43                     scm = ScmSystemFactory.getScmSystemByUrl(this.url);
44                 } catch (IllegalArgumentException JavaDoc iae) {
45                     throw new BuildException("Antmod property '" + this.urlProperty + "' not configured correctly -> " + iae.getMessage());
46                 }
47             } else if (this.url != null) {
48                 scm = ScmSystemFactory.getScmSystemByUrl(this.url);
49             } else {
50                 scm = ScmSystemFactory.getScmSystemByName(this.repos);
51             }
52
53             // if module unknown, initialize it with the optionally known module inside the url
54
if (this.module == null) {
55                 this.module = scm.getUrl().getModule();
56             }
57
58             // perform the requested scm command
59
String JavaDoc cmd = this.command.getValue();
60             if (cmd.equals(Command.UPDATE)) {
61                 scm.doUpdate(new File JavaDoc(this.dest), new ScmVersion(module, revision));
62                 
63             } else if (cmd.equals(Command.CHECKOUT_OR_UPDATE)) {
64                 if (this.module == null) {
65                     throw new BuildException("No modulename specified in SCM url property '" + this.urlProperty + "' (currently set to '" + this.url + "')");
66                 }
67                 scm.doCheckoutOrUpdate(this.module, new File JavaDoc(this.dest), new ScmVersion(module, revision), this.quiet);
68                 
69             } else if (cmd.equals(Command.ADD)) {
70                 boolean recursiveAdd = true;
71                 scm.doAdd(new File JavaDoc(this.dest, this.module), recursiveAdd);
72                 
73             } else if (cmd.equals(Command.COMMIT)) {
74                 scm.doCommit(new File JavaDoc(this.dest), this.message);
75                 
76             } else if (cmd.equals(Command.GETURLMODULE)) {
77                 if (this.property == null) {
78                     throw new BuildException("Cannot get module of URL ('geturlmodule' command) without 'property' attribute set.");
79                 }
80                 if (scm.getUrl().getModule() != null) {
81                     getProject().setProperty(this.property, scm.getUrl().getModule());
82                 }
83
84             } else if (cmd.equals(Command.GETURLBASE)) {
85                 if (this.property == null) {
86                     throw new BuildException("Cannot get base of URL ('geturlbase' command) without 'property' attribute set.");
87                 }
88                 getProject().setProperty(this.property, scm.getUrl().getUrlStringNoModule());
89
90             } else if (cmd.equals(Command.GETREPOSURL)) {
91                 if (this.property == null) {
92                     throw new BuildException("Cannot get URL of a repository ('getreposurl' command) without 'property' attribute set.");
93                 }
94                 getProject().setProperty(this.property, scm.getUrl().getUrlString());
95
96             } else if (cmd.equals(Command.GETREVISION)) {
97                 if (this.property == null) {
98                     throw new BuildException("Cannot get revision of dest attribute ('getrevision' command) without 'property' attribute set.");
99                 }
100                 getProject().setProperty(this.property, scm.getRevisionNumber(new File JavaDoc(this.dest)));
101
102             } else if (cmd.equals(Command.ISUPTODATE)) {
103                 if (this.property == null) {
104                     throw new BuildException("Cannot check up-to-date-ness without 'property' attribute set.");
105                 }
106                 getProject().setProperty(this.property, String.valueOf(scm.isUpToDate(new File JavaDoc(this.dest))));
107             }
108
109             if (!quiet) {
110                 if (scm.getStandardOutput() != null) {
111                     log(scm.getStandardOutput(), Project.MSG_WARN);
112                 }
113             }
114
115         } catch (IllegalArgumentException JavaDoc e) {
116             throw new BuildException(e);
117         } catch (ParseException JavaDoc e) {
118             throw new BuildException(e);
119         }
120     }
121
122     /**
123      * @return
124      */

125     public Command getCommand() {
126         return command;
127     }
128
129     /**
130      * @return
131      */

132     public String JavaDoc getDest() {
133         return dest;
134     }
135
136     /**
137      * @return
138      */

139     public String JavaDoc getUrl() {
140         return url;
141     }
142
143     /**
144      * @param string
145      */

146     public void setCommand(Command cmd) {
147         this.command = cmd;
148     }
149
150     /**
151      * @param string
152      */

153     public void setDest(String JavaDoc string) {
154         dest = string;
155     }
156
157     public void setUrl(String JavaDoc string) {
158         url = string;
159     }
160
161     public void setUrlProperty(String JavaDoc string) {
162         this.urlProperty = string;
163     }
164
165     public void setRepos(String JavaDoc repos) {
166         this.repos = repos;
167     }
168     
169     public void setRevision(String JavaDoc rev) {
170         if (rev != null) {
171             rev = rev.trim();
172         }
173         this.revision = rev;
174     }
175     
176     public void setModule(String JavaDoc module) {
177         if (module != null) {
178             module = module.trim();
179         }
180         this.module = module;
181     }
182     
183     public void setQuiet(boolean quiet) {
184         this.quiet = quiet;
185     }
186     
187     public void setMessage(String JavaDoc message) {
188         this.message = message;
189     }
190     
191     public void setProperty(String JavaDoc property) {
192         this.property = property;
193     }
194
195
196     public static class Command extends EnumeratedAttribute {
197
198         public static String JavaDoc UPDATE = "update";
199         public static String JavaDoc CHECKOUT_OR_UPDATE = "checkoutOrUpdate";
200         public static String JavaDoc ADD = "add";
201         public static String JavaDoc COMMIT = "commit";
202         public static String JavaDoc GETURLMODULE = "geturlmodule";
203         public static String JavaDoc GETURLBASE = "geturlbase";
204         public static String JavaDoc GETREPOSURL = "getreposurl";
205         public static String JavaDoc GETREVISION = "getrevision";
206         public static String JavaDoc ISUPTODATE = "ISUPTODATE";
207         
208         private static String JavaDoc[] values =
209                 new String JavaDoc[]{
210                     UPDATE,
211                     CHECKOUT_OR_UPDATE,
212                     ADD,
213                     COMMIT,
214                     GETURLMODULE,
215                     GETURLBASE,
216                     GETREPOSURL,
217                     GETREVISION,
218                     ISUPTODATE
219                 };
220
221         public String JavaDoc[] getValues() {
222             return Command.values;
223         }
224
225     }
226 }
227
Popular Tags