1 | /*
|
---|
2 | * Copyright 2002-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.taskdefs.optional.sos;
|
---|
18 |
|
---|
19 | import java.io.File;
|
---|
20 | import org.apache.tools.ant.BuildException;
|
---|
21 | import org.apache.tools.ant.Project;
|
---|
22 | import org.apache.tools.ant.Task;
|
---|
23 | import org.apache.tools.ant.taskdefs.Execute;
|
---|
24 | import org.apache.tools.ant.taskdefs.LogStreamHandler;
|
---|
25 | import org.apache.tools.ant.types.Commandline;
|
---|
26 | import org.apache.tools.ant.types.Path;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * A base class for creating tasks for executing commands on SourceOffSite.
|
---|
30 | *
|
---|
31 | * These tasks were inspired by the VSS tasks
|
---|
32 | *
|
---|
33 | */
|
---|
34 |
|
---|
35 | public abstract class SOS extends Task implements SOSCmd {
|
---|
36 |
|
---|
37 | private String sosCmdDir = null;
|
---|
38 | private String sosUsername = null;
|
---|
39 | private String sosPassword = "";
|
---|
40 | private String projectPath = null;
|
---|
41 | private String vssServerPath = null;
|
---|
42 | private String sosServerPath = null;
|
---|
43 | private String sosHome = null;
|
---|
44 | private String localPath = null;
|
---|
45 | private String version = null;
|
---|
46 | private String label = null;
|
---|
47 | private String comment = null;
|
---|
48 | private String filename = null;
|
---|
49 |
|
---|
50 | private boolean noCompress = false;
|
---|
51 | private boolean noCache = false;
|
---|
52 | private boolean recursive = false;
|
---|
53 | private boolean verbose = false;
|
---|
54 |
|
---|
55 | /** Commandline to be executed */
|
---|
56 | protected Commandline commandLine;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Flag to disable the cache when set.
|
---|
60 | * Required if SOSHOME is set as an environment variable.
|
---|
61 | * Defaults to false.
|
---|
62 | *
|
---|
63 | * @param nocache True to disable caching.
|
---|
64 | */
|
---|
65 | public final void setNoCache(boolean nocache) {
|
---|
66 | noCache = nocache;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Flag to disable compression when set. Defaults to false.
|
---|
71 | *
|
---|
72 | * @param nocompress True to disable compression.
|
---|
73 | */
|
---|
74 | public final void setNoCompress(boolean nocompress) {
|
---|
75 | noCompress = nocompress;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * The directory where soscmd(.exe) is located.
|
---|
80 | * soscmd must be on the path if omitted.
|
---|
81 | *
|
---|
82 | * @param dir The new sosCmd value
|
---|
83 | */
|
---|
84 | public final void setSosCmd(String dir) {
|
---|
85 | sosCmdDir = Project.translatePath(dir);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * The SourceSafe username.
|
---|
90 | *
|
---|
91 | * @param username The new username value
|
---|
92 | *
|
---|
93 | * @ant.attribute group="required"
|
---|
94 | */
|
---|
95 | public final void setUsername(String username) {
|
---|
96 | sosUsername = username;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * The SourceSafe password.
|
---|
101 | *
|
---|
102 | * @param password The new password value
|
---|
103 | */
|
---|
104 | public final void setPassword(String password) {
|
---|
105 | sosPassword = password;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * The SourceSafe project path.
|
---|
110 | *
|
---|
111 | * @param projectpath The new projectpath value
|
---|
112 | *
|
---|
113 | * @ant.attribute group="required"
|
---|
114 | */
|
---|
115 | public final void setProjectPath(String projectpath) {
|
---|
116 | if (projectpath.startsWith(SOSCmd.PROJECT_PREFIX)) {
|
---|
117 | projectPath = projectpath;
|
---|
118 | } else {
|
---|
119 | projectPath = SOSCmd.PROJECT_PREFIX + projectpath;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * The path to the location of the ss.ini file.
|
---|
125 | *
|
---|
126 | * @param vssServerPath The new vssServerPath value
|
---|
127 | *
|
---|
128 | * @ant.attribute group="required"
|
---|
129 | */
|
---|
130 | public final void setVssServerPath(String vssServerPath) {
|
---|
131 | this.vssServerPath = vssServerPath;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Path to the SourceOffSite home directory.
|
---|
136 | *
|
---|
137 | * @param sosHome The new sosHome value
|
---|
138 | */
|
---|
139 | public final void setSosHome(String sosHome) {
|
---|
140 | this.sosHome = sosHome;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * The address and port of SourceOffSite Server,
|
---|
145 | * for example 192.168.0.1:8888.
|
---|
146 | *
|
---|
147 | * @param sosServerPath The new sosServerPath value
|
---|
148 | *
|
---|
149 | * @ant.attribute group="required"
|
---|
150 | */
|
---|
151 | public final void setSosServerPath(String sosServerPath) {
|
---|
152 | this.sosServerPath = sosServerPath;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Override the working directory and get to the specified path.
|
---|
157 | *
|
---|
158 | * @param path The new localPath value
|
---|
159 | */
|
---|
160 | public final void setLocalPath(Path path) {
|
---|
161 | localPath = path.toString();
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Enable verbose output. Defaults to false.
|
---|
166 | *
|
---|
167 | * @param verbose True for verbose output.
|
---|
168 | */
|
---|
169 | public void setVerbose(boolean verbose) {
|
---|
170 | this.verbose = verbose;
|
---|
171 | }
|
---|
172 |
|
---|
173 | // Special setters for the sub-classes
|
---|
174 |
|
---|
175 | protected void setInternalFilename(String file) {
|
---|
176 | filename = file;
|
---|
177 | }
|
---|
178 |
|
---|
179 | protected void setInternalRecursive(boolean recurse) {
|
---|
180 | recursive = recurse;
|
---|
181 | }
|
---|
182 |
|
---|
183 | protected void setInternalComment(String text) {
|
---|
184 | comment = text;
|
---|
185 | }
|
---|
186 |
|
---|
187 | protected void setInternalLabel(String text) {
|
---|
188 | label = text;
|
---|
189 | }
|
---|
190 |
|
---|
191 | protected void setInternalVersion(String text) {
|
---|
192 | version = text;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Get the executable to run. Add the path if it was specifed in the build file
|
---|
197 | *
|
---|
198 | * @return the executable to run
|
---|
199 | */
|
---|
200 | protected String getSosCommand() {
|
---|
201 | if (sosCmdDir == null) {
|
---|
202 | return COMMAND_SOS_EXE;
|
---|
203 | } else {
|
---|
204 | return sosCmdDir + File.separator + COMMAND_SOS_EXE;
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Get the comment
|
---|
210 | * @return if it was set, null if not
|
---|
211 | */
|
---|
212 | protected String getComment() {
|
---|
213 | return comment;
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Get the version
|
---|
218 | * @return if it was set, null if not
|
---|
219 | */
|
---|
220 | protected String getVersion() {
|
---|
221 | return version;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Get the label
|
---|
226 | * @return if it was set, null if not
|
---|
227 | */
|
---|
228 | protected String getLabel() {
|
---|
229 | return label;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Get the username
|
---|
234 | * @return if it was set, null if not
|
---|
235 | */
|
---|
236 | protected String getUsername() {
|
---|
237 | return sosUsername;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Get the password
|
---|
242 | * @return empty string if it wans't set
|
---|
243 | */
|
---|
244 | protected String getPassword() {
|
---|
245 | return sosPassword;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Get the project path
|
---|
250 | * @return if it was set, null if not
|
---|
251 | */
|
---|
252 | protected String getProjectPath() {
|
---|
253 | return projectPath;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Get the VSS server path
|
---|
258 | * @return if it was set, null if not
|
---|
259 | */
|
---|
260 | protected String getVssServerPath() {
|
---|
261 | return vssServerPath;
|
---|
262 | }
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Get the SOS home directory
|
---|
266 | * @return if it was set, null if not
|
---|
267 | */
|
---|
268 | protected String getSosHome() {
|
---|
269 | return sosHome;
|
---|
270 | }
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Get the SOS serve path
|
---|
274 | * @return if it was set, null if not
|
---|
275 | */
|
---|
276 | protected String getSosServerPath() {
|
---|
277 | return sosServerPath;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Get the filename to be acted upon
|
---|
282 | * @return if it was set, null if not
|
---|
283 | */
|
---|
284 | protected String getFilename() {
|
---|
285 | return filename;
|
---|
286 | }
|
---|
287 |
|
---|
288 | /**
|
---|
289 | * Get the NoCompress flag
|
---|
290 | *
|
---|
291 | * @return the 'nocompress' Flag if the attribute was 'true',
|
---|
292 | * otherwise an empty string
|
---|
293 | */
|
---|
294 | protected String getNoCompress() {
|
---|
295 | return noCompress ? FLAG_NO_COMPRESSION : "";
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * Get the NoCache flag
|
---|
300 | *
|
---|
301 | * @return the 'nocache' Flag if the attribute was 'true', otherwise an empty string
|
---|
302 | */
|
---|
303 | protected String getNoCache() {
|
---|
304 | return noCache ? FLAG_NO_CACHE : "";
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Get the 'verbose' Flag
|
---|
309 | *
|
---|
310 | * @return the 'verbose' Flag if the attribute was 'true', otherwise an empty string
|
---|
311 | */
|
---|
312 | protected String getVerbose() {
|
---|
313 | return verbose ? FLAG_VERBOSE : "";
|
---|
314 | }
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Get the 'recursive' Flag
|
---|
318 | *
|
---|
319 | * @return the 'recursive' Flag if the attribute was 'true', otherwise an empty string
|
---|
320 | */
|
---|
321 | protected String getRecursive() {
|
---|
322 | return recursive ? FLAG_RECURSION : "";
|
---|
323 | }
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Builds and returns the working directory.
|
---|
327 | * <p>
|
---|
328 | * The localpath is created if it didn't exist
|
---|
329 | *
|
---|
330 | * @return the absolute path of the working directory
|
---|
331 | */
|
---|
332 | protected String getLocalPath() {
|
---|
333 | if (localPath == null) {
|
---|
334 | return getProject().getBaseDir().getAbsolutePath();
|
---|
335 | } else {
|
---|
336 | // make sure localDir exists, create it if it doesn't
|
---|
337 | File dir = getProject().resolveFile(localPath);
|
---|
338 | if (!dir.exists()) {
|
---|
339 | boolean done = dir.mkdirs();
|
---|
340 | if (!done) {
|
---|
341 | String msg = "Directory " + localPath + " creation was not "
|
---|
342 | + "successful for an unknown reason";
|
---|
343 | throw new BuildException(msg, getLocation());
|
---|
344 | }
|
---|
345 | getProject().log("Created dir: " + dir.getAbsolutePath());
|
---|
346 | }
|
---|
347 | return dir.getAbsolutePath();
|
---|
348 | }
|
---|
349 | }
|
---|
350 |
|
---|
351 | /**
|
---|
352 | * Subclasses implement the logic required to construct the command line.
|
---|
353 | *
|
---|
354 | * @return The command line to execute.
|
---|
355 | */
|
---|
356 | abstract Commandline buildCmdLine();
|
---|
357 |
|
---|
358 |
|
---|
359 | /**
|
---|
360 | * Execute the created command line.
|
---|
361 | *
|
---|
362 | * @throws BuildException
|
---|
363 | */
|
---|
364 | public void execute()
|
---|
365 | throws BuildException {
|
---|
366 | int result = 0;
|
---|
367 | buildCmdLine();
|
---|
368 | result = run(commandLine);
|
---|
369 | if (result == 255) { // This is the exit status
|
---|
370 | String msg = "Failed executing: " + commandLine.toString();
|
---|
371 | throw new BuildException(msg, getLocation());
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | /**
|
---|
376 | * Execute the created command line.
|
---|
377 | *
|
---|
378 | * @param cmd The command line to run.
|
---|
379 | * @return int the exit code.
|
---|
380 | * @throws BuildException
|
---|
381 | */
|
---|
382 | protected int run(Commandline cmd) {
|
---|
383 | try {
|
---|
384 | Execute exe = new Execute(new LogStreamHandler(this,
|
---|
385 | Project.MSG_INFO,
|
---|
386 | Project.MSG_WARN));
|
---|
387 |
|
---|
388 | exe.setAntRun(getProject());
|
---|
389 | exe.setWorkingDirectory(getProject().getBaseDir());
|
---|
390 | exe.setCommandline(cmd.getCommandline());
|
---|
391 | exe.setVMLauncher(false); // Use the OS VM launcher so we get environment variables
|
---|
392 | return exe.execute();
|
---|
393 | } catch (java.io.IOException e) {
|
---|
394 | throw new BuildException(e, getLocation());
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | /** Sets the executable and add the required attributes to the command line. */
|
---|
399 | protected void getRequiredAttributes() {
|
---|
400 | // Get the path to the soscmd(.exe)
|
---|
401 | commandLine.setExecutable(getSosCommand());
|
---|
402 | // SOS server address is required
|
---|
403 | if (getSosServerPath() == null) {
|
---|
404 | throw new BuildException("sosserverpath attribute must be set!", getLocation());
|
---|
405 | }
|
---|
406 | commandLine.createArgument().setValue(FLAG_SOS_SERVER);
|
---|
407 | commandLine.createArgument().setValue(getSosServerPath());
|
---|
408 | // Login info is required
|
---|
409 | if (getUsername() == null) {
|
---|
410 | throw new BuildException("username attribute must be set!", getLocation());
|
---|
411 | }
|
---|
412 | commandLine.createArgument().setValue(FLAG_USERNAME);
|
---|
413 | commandLine.createArgument().setValue(getUsername());
|
---|
414 | // The SOS class knows that the SOS server needs the password flag,
|
---|
415 | // even if there is no password ,so we send a " "
|
---|
416 | commandLine.createArgument().setValue(FLAG_PASSWORD);
|
---|
417 | commandLine.createArgument().setValue(getPassword());
|
---|
418 | // VSS Info is required
|
---|
419 | if (getVssServerPath() == null) {
|
---|
420 | throw new BuildException("vssserverpath attribute must be set!", getLocation());
|
---|
421 | }
|
---|
422 | commandLine.createArgument().setValue(FLAG_VSS_SERVER);
|
---|
423 | commandLine.createArgument().setValue(getVssServerPath());
|
---|
424 | // VSS project is required
|
---|
425 | if (getProjectPath() == null) {
|
---|
426 | throw new BuildException("projectpath attribute must be set!", getLocation());
|
---|
427 | }
|
---|
428 | commandLine.createArgument().setValue(FLAG_PROJECT);
|
---|
429 | commandLine.createArgument().setValue(getProjectPath());
|
---|
430 | }
|
---|
431 |
|
---|
432 | /** Adds the optional attributes to the command line. */
|
---|
433 | protected void getOptionalAttributes() {
|
---|
434 | // -verbose
|
---|
435 | commandLine.createArgument().setValue(getVerbose());
|
---|
436 | // Disable Compression
|
---|
437 | commandLine.createArgument().setValue(getNoCompress());
|
---|
438 | // Path to the SourceOffSite home directory /home/user/.sos
|
---|
439 | if (getSosHome() == null) {
|
---|
440 | // If -soshome was not specified then we can look for nocache
|
---|
441 | commandLine.createArgument().setValue(getNoCache());
|
---|
442 | } else {
|
---|
443 | commandLine.createArgument().setValue(FLAG_SOS_HOME);
|
---|
444 | commandLine.createArgument().setValue(getSosHome());
|
---|
445 | }
|
---|
446 | //If a working directory was specified then add it to the command line
|
---|
447 | if (getLocalPath() != null) {
|
---|
448 | commandLine.createArgument().setValue(FLAG_WORKING_DIR);
|
---|
449 | commandLine.createArgument().setValue(getLocalPath());
|
---|
450 | }
|
---|
451 | }
|
---|
452 | }
|
---|