KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > sos > SOS


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 package org.apache.tools.ant.taskdefs.optional.sos;
19
20 import java.io.File JavaDoc;
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Project;
23 import org.apache.tools.ant.Task;
24 import org.apache.tools.ant.taskdefs.Execute;
25 import org.apache.tools.ant.taskdefs.LogStreamHandler;
26 import org.apache.tools.ant.types.Commandline;
27 import org.apache.tools.ant.types.Path;
28 import org.apache.tools.ant.util.FileUtils;
29
30 /**
31  * A base class for creating tasks for executing commands on SourceOffSite.
32  *
33  * These tasks were inspired by the VSS tasks.
34  *
35  */

36
37 public abstract class SOS extends Task implements SOSCmd {
38
39     private String JavaDoc sosCmdDir = null;
40     private String JavaDoc sosUsername = null;
41     private String JavaDoc sosPassword = "";
42     private String JavaDoc projectPath = null;
43     private String JavaDoc vssServerPath = null;
44     private String JavaDoc sosServerPath = null;
45     private String JavaDoc sosHome = null;
46     private String JavaDoc localPath = null;
47     private String JavaDoc version = null;
48     private String JavaDoc label = null;
49     private String JavaDoc comment = null;
50     private String JavaDoc filename = null;
51
52     private boolean noCompress = false;
53     private boolean noCache = false;
54     private boolean recursive = false;
55     private boolean verbose = false;
56
57     // CheckStyle:VisibilityModifier OFF - bc
58
/** Commandline to be executed. */
59     protected Commandline commandLine;
60     // CheckStyle:VisibilityModifier ON
61

62     /**
63      * Flag to disable the cache when set.
64      * Required if SOSHOME is set as an environment variable.
65      * Defaults to false.
66      *
67      * @param nocache True to disable caching.
68      */

69     public final void setNoCache(boolean nocache) {
70         noCache = nocache;
71     }
72
73     /**
74      * Flag to disable compression when set. Defaults to false.
75      *
76      * @param nocompress True to disable compression.
77      */

78     public final void setNoCompress(boolean nocompress) {
79         noCompress = nocompress;
80     }
81
82     /**
83      * The directory where soscmd(.exe) is located.
84      * soscmd must be on the path if omitted.
85      *
86      * @param dir The new sosCmd value.
87      */

88     public final void setSosCmd(String JavaDoc dir) {
89         sosCmdDir = FileUtils.translatePath(dir);
90     }
91
92     /**
93      * The SourceSafe username.
94      *
95      * @param username The new username value.
96      *
97      * @ant.attribute group="required"
98      */

99     public final void setUsername(String JavaDoc username) {
100         sosUsername = username;
101     }
102
103     /**
104      * The SourceSafe password.
105      *
106      * @param password The new password value.
107      */

108     public final void setPassword(String JavaDoc password) {
109         sosPassword = password;
110     }
111
112     /**
113      * The SourceSafe project path.
114      *
115      * @param projectpath The new projectpath value.
116      *
117      * @ant.attribute group="required"
118      */

119     public final void setProjectPath(String JavaDoc projectpath) {
120         if (projectpath.startsWith(SOSCmd.PROJECT_PREFIX)) {
121             projectPath = projectpath;
122         } else {
123             projectPath = SOSCmd.PROJECT_PREFIX + projectpath;
124         }
125     }
126
127     /**
128      * The path to the location of the ss.ini file.
129      *
130      * @param vssServerPath The new vssServerPath value.
131      *
132      * @ant.attribute group="required"
133      */

134     public final void setVssServerPath(String JavaDoc vssServerPath) {
135         this.vssServerPath = vssServerPath;
136     }
137
138     /**
139      * Path to the SourceOffSite home directory.
140      *
141      * @param sosHome The new sosHome value.
142      */

143     public final void setSosHome(String JavaDoc sosHome) {
144         this.sosHome = sosHome;
145     }
146
147     /**
148      * The address and port of SourceOffSite Server,
149      * for example 192.168.0.1:8888.
150      *
151      * @param sosServerPath The new sosServerPath value.
152      *
153      * @ant.attribute group="required"
154      */

155     public final void setSosServerPath(String JavaDoc sosServerPath) {
156         this.sosServerPath = sosServerPath;
157     }
158
159     /**
160      * Override the working directory and get to the specified path.
161      *
162      * @param path The new localPath value.
163      */

164     public final void setLocalPath(Path path) {
165         localPath = path.toString();
166     }
167
168     /**
169      * Enable verbose output. Defaults to false.
170      *
171      * @param verbose True for verbose output.
172      */

173     public void setVerbose(boolean verbose) {
174         this.verbose = verbose;
175     }
176
177     // Special setters for the sub-classes
178

179     /**
180      * Set the file name.
181      * @param file the filename to use.
182      */

183     protected void setInternalFilename(String JavaDoc file) {
184         filename = file;
185     }
186
187     /**
188      * Set the recursive flag.
189      * @param recurse if true use the recursive flag on the command line.
190      */

191     protected void setInternalRecursive(boolean recurse) {
192         recursive = recurse;
193     }
194
195     /**
196      * Set the comment text.
197      * @param text the comment text to use.
198      */

199     protected void setInternalComment(String JavaDoc text) {
200         comment = text;
201     }
202
203     /**
204      * Set the label.
205      * @param text the label to use.
206      */

207     protected void setInternalLabel(String JavaDoc text) {
208         label = text;
209     }
210
211     /**
212      * Set the version.
213      * @param text the version to use.
214      */

215     protected void setInternalVersion(String JavaDoc text) {
216         version = text;
217     }
218
219     /**
220      * Get the executable to run. Add the path if it was specifed in the build file
221      *
222      * @return the executable to run.
223      */

224     protected String JavaDoc getSosCommand() {
225         if (sosCmdDir == null) {
226             return COMMAND_SOS_EXE;
227         } else {
228             return sosCmdDir + File.separator + COMMAND_SOS_EXE;
229         }
230     }
231
232     /**
233      * Get the comment
234      * @return if it was set, null if not.
235      */

236     protected String JavaDoc getComment() {
237         return comment;
238     }
239
240     /**
241      * Get the version
242      * @return if it was set, null if not.
243      */

244     protected String JavaDoc getVersion() {
245         return version;
246     }
247
248     /**
249      * Get the label
250      * @return if it was set, null if not.
251      */

252     protected String JavaDoc getLabel() {
253         return label;
254     }
255
256     /**
257      * Get the username
258      * @return if it was set, null if not.
259      */

260     protected String JavaDoc getUsername() {
261         return sosUsername;
262     }
263
264     /**
265      * Get the password
266      * @return empty string if it wasn't set.
267      */

268     protected String JavaDoc getPassword() {
269         return sosPassword;
270     }
271
272     /**
273      * Get the project path
274      * @return if it was set, null if not.
275      */

276     protected String JavaDoc getProjectPath() {
277         return projectPath;
278     }
279
280     /**
281      * Get the VSS server path
282      * @return if it was set, null if not.
283      */

284     protected String JavaDoc getVssServerPath() {
285         return vssServerPath;
286     }
287
288     /**
289      * Get the SOS home directory.
290      * @return if it was set, null if not.
291      */

292     protected String JavaDoc getSosHome() {
293         return sosHome;
294     }
295
296     /**
297      * Get the SOS serve path.
298      * @return if it was set, null if not.
299      */

300     protected String JavaDoc getSosServerPath() {
301         return sosServerPath;
302     }
303
304     /**
305      * Get the filename to be acted upon.
306      * @return if it was set, null if not.
307      */

308     protected String JavaDoc getFilename() {
309         return filename;
310     }
311
312     /**
313      * Get the NoCompress flag.
314      *
315      * @return the 'nocompress' Flag if the attribute was 'true',
316      * otherwise an empty string.
317      */

318     protected String JavaDoc getNoCompress() {
319         return noCompress ? FLAG_NO_COMPRESSION : "";
320     }
321
322     /**
323      * Get the NoCache flag.
324      *
325      * @return the 'nocache' Flag if the attribute was 'true', otherwise an empty string.
326      */

327     protected String JavaDoc getNoCache() {
328         return noCache ? FLAG_NO_CACHE : "";
329     }
330
331     /**
332      * Get the 'verbose' Flag.
333      *
334      * @return the 'verbose' Flag if the attribute was 'true', otherwise an empty string.
335      */

336     protected String JavaDoc getVerbose() {
337         return verbose ? FLAG_VERBOSE : "";
338     }
339
340     /**
341      * Get the 'recursive' Flag.
342      *
343      * @return the 'recursive' Flag if the attribute was 'true', otherwise an empty string.
344      */

345     protected String JavaDoc getRecursive() {
346         return recursive ? FLAG_RECURSION : "";
347     }
348
349     /**
350      * Builds and returns the working directory.
351      * <p>
352      * The localpath is created if it didn't exist.
353      *
354      * @return the absolute path of the working directory.
355      */

356     protected String JavaDoc getLocalPath() {
357         if (localPath == null) {
358             return getProject().getBaseDir().getAbsolutePath();
359         } else {
360             // make sure localDir exists, create it if it doesn't
361
File JavaDoc dir = getProject().resolveFile(localPath);
362             if (!dir.exists()) {
363                 boolean done = dir.mkdirs();
364                 if (!done) {
365                     String JavaDoc msg = "Directory " + localPath + " creation was not "
366                         + "successful for an unknown reason";
367                     throw new BuildException(msg, getLocation());
368                 }
369                 getProject().log("Created dir: " + dir.getAbsolutePath());
370             }
371             return dir.getAbsolutePath();
372         }
373     }
374
375     /**
376      * Subclasses implement the logic required to construct the command line.
377      *
378      * @return The command line to execute.
379      */

380     abstract Commandline buildCmdLine();
381
382
383     /**
384      * Execute the created command line.
385      *
386      * @throws BuildException on error.
387      */

388     public void execute()
389         throws BuildException {
390         int result = 0;
391         buildCmdLine();
392         result = run(commandLine);
393         if (result == 255) { // This is the exit status
394
String JavaDoc msg = "Failed executing: " + commandLine.toString();
395             throw new BuildException(msg, getLocation());
396         }
397     }
398
399     /**
400      * Execute the created command line.
401      *
402      * @param cmd The command line to run.
403      * @return int the exit code.
404      * @throws BuildException
405      */

406     protected int run(Commandline cmd) {
407         try {
408             Execute exe = new Execute(new LogStreamHandler(this,
409                     Project.MSG_INFO,
410                     Project.MSG_WARN));
411
412             exe.setAntRun(getProject());
413             exe.setWorkingDirectory(getProject().getBaseDir());
414             exe.setCommandline(cmd.getCommandline());
415             exe.setVMLauncher(false); // Use the OS VM launcher so we get environment variables
416
return exe.execute();
417         } catch (java.io.IOException JavaDoc e) {
418             throw new BuildException(e, getLocation());
419         }
420     }
421
422     /** Sets the executable and add the required attributes to the command line. */
423     protected void getRequiredAttributes() {
424         // Get the path to the soscmd(.exe)
425
commandLine.setExecutable(getSosCommand());
426         // SOS server address is required
427
if (getSosServerPath() == null) {
428             throw new BuildException("sosserverpath attribute must be set!", getLocation());
429         }
430         commandLine.createArgument().setValue(FLAG_SOS_SERVER);
431         commandLine.createArgument().setValue(getSosServerPath());
432         // Login info is required
433
if (getUsername() == null) {
434             throw new BuildException("username attribute must be set!", getLocation());
435         }
436         commandLine.createArgument().setValue(FLAG_USERNAME);
437         commandLine.createArgument().setValue(getUsername());
438         // The SOS class knows that the SOS server needs the password flag,
439
// even if there is no password ,so we send a " "
440
commandLine.createArgument().setValue(FLAG_PASSWORD);
441         commandLine.createArgument().setValue(getPassword());
442         // VSS Info is required
443
if (getVssServerPath() == null) {
444             throw new BuildException("vssserverpath attribute must be set!", getLocation());
445         }
446         commandLine.createArgument().setValue(FLAG_VSS_SERVER);
447         commandLine.createArgument().setValue(getVssServerPath());
448         // VSS project is required
449
if (getProjectPath() == null) {
450             throw new BuildException("projectpath attribute must be set!", getLocation());
451         }
452         commandLine.createArgument().setValue(FLAG_PROJECT);
453         commandLine.createArgument().setValue(getProjectPath());
454     }
455
456     /** Adds the optional attributes to the command line. */
457     protected void getOptionalAttributes() {
458         // -verbose
459
commandLine.createArgument().setValue(getVerbose());
460         // Disable Compression
461
commandLine.createArgument().setValue(getNoCompress());
462         // Path to the SourceOffSite home directory /home/user/.sos
463
if (getSosHome() == null) {
464             // If -soshome was not specified then we can look for nocache
465
commandLine.createArgument().setValue(getNoCache());
466         } else {
467             commandLine.createArgument().setValue(FLAG_SOS_HOME);
468             commandLine.createArgument().setValue(getSosHome());
469         }
470         //If a working directory was specified then add it to the command line
471
if (getLocalPath() != null) {
472             commandLine.createArgument().setValue(FLAG_WORKING_DIR);
473             commandLine.createArgument().setValue(getLocalPath());
474         }
475     }
476 }
477
Popular Tags