aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/taskdefs/optional/sos/SOS.java
blob: 03031d5edc3930468cb4bb45be3f889539c2ca64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.tools.ant.taskdefs.optional.sos;

import java.io.File;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Execute;
import org.apache.tools.ant.taskdefs.LogStreamHandler;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.util.FileUtils;

/**
 * A base class for creating tasks for executing commands on SourceOffSite.
 *
 *  These tasks were inspired by the VSS tasks.
 *
 */

public abstract class SOS extends Task implements SOSCmd {

    private static final int ERROR_EXIT_STATUS = 255;

    private String sosCmdDir = null;
    private String sosUsername = null;
    private String sosPassword = "";
    private String projectPath = null;
    private String vssServerPath = null;
    private String sosServerPath = null;
    private String sosHome = null;
    private String localPath = null;
    private String version = null;
    private String label = null;
    private String comment = null;
    private String filename = null;

    private boolean noCompress = false;
    private boolean noCache = false;
    private boolean recursive = false;
    private boolean verbose = false;

    // CheckStyle:VisibilityModifier OFF - bc
    /** Commandline to be executed. */
    protected Commandline commandLine;
    // CheckStyle:VisibilityModifier ON

    /**
     * Flag to disable the cache when set.
     * Required if SOSHOME is set as an environment variable.
     * Defaults to false.
     *
     * @param  nocache  True to disable caching.
     */
    public final void setNoCache(boolean nocache) {
        noCache = nocache;
    }

    /**
     * Flag to disable compression when set. Defaults to false.
     *
     * @param  nocompress  True to disable compression.
     */
    public final void setNoCompress(boolean nocompress) {
        noCompress = nocompress;
    }

    /**
     * The directory where soscmd(.exe) is located.
     * soscmd must be on the path if omitted.
     *
     * @param  dir  The new sosCmd value.
     */
    public final void setSosCmd(String dir) {
        sosCmdDir = FileUtils.translatePath(dir);
    }

    /**
     * The SourceSafe username.
     *
     * @param  username  The new username value.
     *
     * @ant.attribute group="required"
     */
    public final void setUsername(String username) {
        sosUsername = username;
    }

    /**
     * The SourceSafe password.
     *
     * @param  password  The new password value.
     */
    public final void setPassword(String password) {
        sosPassword = password;
    }

    /**
     * The SourceSafe project path.
     *
     * @param  projectpath  The new projectpath value.
     *
     * @ant.attribute group="required"
     */
    public final void setProjectPath(String projectpath) {
        if (projectpath.startsWith(SOSCmd.PROJECT_PREFIX)) {
            projectPath = projectpath;
        } else {
            projectPath = SOSCmd.PROJECT_PREFIX + projectpath;
        }
    }

    /**
     * The path to the location of the ss.ini file.
     *
     * @param  vssServerPath  The new vssServerPath value.
     *
     * @ant.attribute group="required"
     */
    public final void setVssServerPath(String vssServerPath) {
        this.vssServerPath = vssServerPath;
    }

    /**
     * Path to the SourceOffSite home directory.
     *
     * @param  sosHome  The new sosHome value.
     */
    public final void setSosHome(String sosHome) {
        this.sosHome = sosHome;
    }

    /**
     * The address and port of SourceOffSite Server,
     * for example 192.168.0.1:8888.
     *
     * @param  sosServerPath  The new sosServerPath value.
     *
     * @ant.attribute group="required"
     */
    public final void setSosServerPath(String sosServerPath) {
        this.sosServerPath = sosServerPath;
    }

    /**
     * Override the working directory and get to the specified path.
     *
     * @param  path  The new localPath value.
     */
    public final void setLocalPath(Path path) {
        localPath = path.toString();
    }

    /**
     * Enable verbose output. Defaults to false.
     *
     * @param  verbose  True for verbose output.
     */
    public void setVerbose(boolean verbose) {
        this.verbose = verbose;
    }

    // Special setters for the sub-classes

    /**
     * Set the file name.
     * @param file the filename to use.
     */
    protected void setInternalFilename(String file) {
        filename = file;
    }

    /**
     * Set the recursive flag.
     * @param recurse if true use the recursive flag on the command line.
     */
    protected void setInternalRecursive(boolean recurse) {
        recursive = recurse;
    }

    /**
     * Set the comment text.
     * @param text the comment text to use.
     */
    protected void setInternalComment(String text) {
        comment = text;
    }

    /**
     * Set the label.
     * @param text the label to use.
     */
    protected void setInternalLabel(String text) {
        label = text;
    }

    /**
     * Set the version.
     * @param text the version to use.
     */
    protected void setInternalVersion(String text) {
        version = text;
    }

    /**
     * Get the executable to run. Add the path if it was specified in the build file
     *
     * @return the executable to run.
     */
    protected String getSosCommand() {
        if (sosCmdDir == null) {
            return COMMAND_SOS_EXE;
        } else {
            return sosCmdDir + File.separator + COMMAND_SOS_EXE;
        }
    }

    /**
     * Get the comment
     * @return if it was set, null if not.
     */
    protected String getComment() {
        return comment;
    }

    /**
     * Get the version
     * @return if it was set, null if not.
     */
    protected String getVersion() {
        return version;
    }

    /**
     * Get the label
     * @return if it was set, null if not.
     */
    protected String getLabel() {
        return label;
    }

    /**
     * Get the username
     * @return if it was set, null if not.
     */
    protected String getUsername() {
        return sosUsername;
    }

    /**
     * Get the password
     * @return empty string if it wasn't set.
     */
    protected String getPassword() {
        return sosPassword;
    }

    /**
     * Get the project path
     * @return if it was set, null if not.
     */
    protected String getProjectPath() {
        return projectPath;
    }

    /**
     * Get the VSS server path
     * @return if it was set, null if not.
     */
    protected String getVssServerPath() {
        return vssServerPath;
    }

    /**
     * Get the SOS home directory.
     * @return if it was set, null if not.
     */
    protected String getSosHome() {
        return sosHome;
    }

    /**
     * Get the SOS serve path.
     * @return if it was set, null if not.
     */
    protected String getSosServerPath() {
        return sosServerPath;
    }

    /**
     * Get the filename to be acted upon.
     * @return if it was set, null if not.
     */
    protected String getFilename() {
        return filename;
    }

    /**
     * Get the NoCompress flag.
     *
     * @return the 'nocompress' Flag if the attribute was 'true',
     *         otherwise an empty string.
     */
    protected String getNoCompress() {
        return noCompress ? FLAG_NO_COMPRESSION : "";
    }

    /**
     * Get the NoCache flag.
     *
     * @return the 'nocache' Flag if the attribute was 'true', otherwise an empty string.
     */
    protected String getNoCache() {
        return noCache ? FLAG_NO_CACHE : "";
    }

    /**
     * Get the 'verbose' Flag.
     *
     * @return the 'verbose' Flag if the attribute was 'true', otherwise an empty string.
     */
    protected String getVerbose() {
        return verbose ? FLAG_VERBOSE : "";
    }

    /**
     * Get the 'recursive' Flag.
     *
     * @return the 'recursive' Flag if the attribute was 'true', otherwise an empty string.
     */
    protected String getRecursive() {
        return recursive ? FLAG_RECURSION : "";
    }

    /**
     * Builds and returns the working directory.
     * <p>
     * The localpath is created if it didn't exist.
     *
     * @return the absolute path of the working directory.
     */
    protected String getLocalPath() {
        if (localPath == null) {
            return getProject().getBaseDir().getAbsolutePath();
        } else {
            // make sure localDir exists, create it if it doesn't
            File dir = getProject().resolveFile(localPath);
            if (!dir.exists()) {
                boolean done = dir.mkdirs() || dir.isDirectory();
                if (!done) {
                    String msg = "Directory " + localPath + " creation was not "
                        + "successful for an unknown reason";
                    throw new BuildException(msg, getLocation());
                }
                getProject().log("Created dir: " + dir.getAbsolutePath());
            }
            return dir.getAbsolutePath();
        }
    }

    /**
     * Subclasses implement the logic required to construct the command line.
     *
     * @return   The command line to execute.
     */
    abstract Commandline buildCmdLine();


    /**
     * Execute the created command line.
     *
     * @throws BuildException on error.
     */
    public void execute()
        throws BuildException {
        int result = 0;
        buildCmdLine();
        result = run(commandLine);
        if (result == ERROR_EXIT_STATUS) {  // This is the exit status
            String msg = "Failed executing: " + commandLine.toString();
            throw new BuildException(msg, getLocation());
        }
    }

    /**
     * Execute the created command line.
     *
     * @param  cmd              The command line to run.
     * @return                  int the exit code.
     * @throws  BuildException
     */
    protected int run(Commandline cmd) {
        try {
            Execute exe = new Execute(new LogStreamHandler(this,
                    Project.MSG_INFO,
                    Project.MSG_WARN));

            exe.setAntRun(getProject());
            exe.setWorkingDirectory(getProject().getBaseDir());
            exe.setCommandline(cmd.getCommandline());
            exe.setVMLauncher(false);  // Use the OS VM launcher so we get environment variables
            return exe.execute();
        } catch (java.io.IOException e) {
            throw new BuildException(e, getLocation());
        }
    }

    /** Sets the executable and add the required attributes to the command line. */
    protected void getRequiredAttributes() {
        // Get the path to the soscmd(.exe)
        commandLine.setExecutable(getSosCommand());
        // SOS server address is required
        if (getSosServerPath() == null) {
            throw new BuildException("sosserverpath attribute must be set!", getLocation());
        }
        commandLine.createArgument().setValue(FLAG_SOS_SERVER);
        commandLine.createArgument().setValue(getSosServerPath());
        // Login info is required
        if (getUsername() == null) {
            throw new BuildException("username attribute must be set!", getLocation());
        }
        commandLine.createArgument().setValue(FLAG_USERNAME);
        commandLine.createArgument().setValue(getUsername());
        // The SOS class knows that the SOS server needs the password flag,
        // even if there is no password ,so we send a " "
        commandLine.createArgument().setValue(FLAG_PASSWORD);
        commandLine.createArgument().setValue(getPassword());
        // VSS Info is required
        if (getVssServerPath() == null) {
            throw new BuildException("vssserverpath attribute must be set!", getLocation());
        }
        commandLine.createArgument().setValue(FLAG_VSS_SERVER);
        commandLine.createArgument().setValue(getVssServerPath());
        // VSS project is required
        if (getProjectPath() == null) {
            throw new BuildException("projectpath attribute must be set!", getLocation());
        }
        commandLine.createArgument().setValue(FLAG_PROJECT);
        commandLine.createArgument().setValue(getProjectPath());
    }

    /** Adds the optional attributes to the command line. */
    protected void getOptionalAttributes() {
        // -verbose
        commandLine.createArgument().setValue(getVerbose());
        // Disable Compression
        commandLine.createArgument().setValue(getNoCompress());
        // Path to the SourceOffSite home directory /home/user/.sos
        if (getSosHome() == null) {
            // If -soshome was not specified then we can look for nocache
            commandLine.createArgument().setValue(getNoCache());
        } else {
            commandLine.createArgument().setValue(FLAG_SOS_HOME);
            commandLine.createArgument().setValue(getSosHome());
        }
        //If a working directory was specified then add it to the command line
        if (getLocalPath() != null) {
            commandLine.createArgument().setValue(FLAG_WORKING_DIR);
            commandLine.createArgument().setValue(getLocalPath());
        }
    }
}