summaryrefslogtreecommitdiff
path: root/src/main/org/apache/tools/ant/taskdefs/optional/starteam/StarTeamTask.java
blob: 47c6eaf38482f0d7f40da79fdd4c4e020acd67e3 (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
/*
 * Copyright  2001-2006 The Apache Software Foundation
 *
 *  Licensed 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.starteam;

import com.starbase.starteam.BuildNumber;
import com.starbase.starteam.Server;
import com.starbase.starteam.StarTeamFinder;
import com.starbase.starteam.TypeNames;
import com.starbase.starteam.User;
import com.starbase.starteam.View;
import java.util.StringTokenizer;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;

/**
 * Common super class for all StarTeam tasks.
 * At this level of the hierarchy we are concerned only with obtaining a
 * connection to the StarTeam server.  The subclass <code>TreeBasedTask</code>,
 * also abstract defines the tree-walking behavior common to many subtasks.
 *
 * @see TreeBasedTask
 * @version 1.1
 */

public abstract class StarTeamTask extends Task {

    // ATTRIBUTES

    /**
     * The username of the connection
     */
    private String userName;

    /**
     * The username of the connection
     */
    private String password;

    /**
     * name of Starteam server to connect to
     */
    private String servername;

    /**
     * port of Starteam server to connect to
     */
    private String serverport;

    /**
     * name of Starteam project to connect to
     */
    private String projectname;

    /**
     * name of Starteam view to connect to
     */
    private String viewname;

    /**
     *The starteam server through which all activities will be done.
     */
    private Server server = null;

    private void logStarteamVersion() {
        log("StarTeam version: "
            + BuildNumber.getDisplayString(), Project.MSG_VERBOSE);
    }


    /////////////////////////////////////////////////////////
    // GET/SET methods.
    // Setters, of course are where ant user passes in values.
    /////////////////////////////////////////////////////////

    /**
     * Set the name of StarTeamServer;
     * required if <tt>URL</tt> is not set.
     * @param servername a <code>String</code> value
     * @see #setURL(String)
     */
    public final void setServername(String servername) {
        this.servername = servername;
    }

    /**
     * returns the name of the StarTeamServer
     *
     * @return the name of the StarTeam server
     * @see #getURL()
     */
    public final String getServername() {
        return this.servername;
    }

    /**
     * set the port number of the StarTeam connection;
     * required if <tt>URL</tt> is not set.
     * @param serverport port number to be set
     * @see #setURL(String)
     */
    public final void setServerport(String serverport) {
        this.serverport = serverport;
    }

    /**
     * returns the port number of the StarTeam connection
     *
     * @return the port number of the StarTeam connection
     * @see #getURL()
     */
    public final String getServerport() {
        return this.serverport;
    }

    /**
     * set the name of the StarTeam project to be acted on;
     * required if <tt>URL</tt> is not set.
     *
     * @param projectname the name of the StarTeam project to be acted on
     * @see #setURL(String)
     */
    public final void setProjectname(String projectname) {
        this.projectname = projectname;
    }

    /**
     * returns the name of the StarTeam project to be acted on
     *
     * @return the name of the StarTeam project to be acted on
     * @see #getURL()
     */
    public final String getProjectname() {
        return this.projectname;
    }

    /**
     * set the name of the StarTeam view to be acted on;
     * required if <tt>URL</tt> is not set.
     *
     * @param viewname the name of the StarTeam view to be acted on
     * @see #setURL(String)
     */
    public final void setViewname(String viewname) {
        this.viewname = viewname;
    }

    /**
     * returns the name of the StarTeam view to be acted on
     *
     * @return the name of the StarTeam view to be acted on
     * @see #getURL()
     */
    public final String getViewname() {
        return this.viewname;
    }


    /**
     * Set the server name, server port,
     * project name and project folder in one shot;
     * optional, but the server connection must be specified somehow.
     *
     * @param url a <code>String</code> of the form
     *             "servername:portnum/project/view"
     * @see #setServername(String)
     * @see #setServerport(String)
     * @see #setProjectname(String)
     * @see #setViewname(String)
     */
    public final void setURL(String url) {
        StringTokenizer t = new StringTokenizer(url, "/");
        if (t.hasMoreTokens()) {
            String unpw = t.nextToken();
            int pos = unpw.indexOf(":");
            if (pos > 0) {
                this.servername = unpw.substring(0, pos);
                this.serverport = unpw.substring(pos + 1);
                if (t.hasMoreTokens()) {
                    this.projectname = t.nextToken();
                    if (t.hasMoreTokens()) {
                        this.viewname = t.nextToken();
                    }
                }
            }
        }
    }

    /**
     * convenience method returns whole URL at once
     * returns
     * as a single string
     */
    /**
     * a convenience method which returns the whole StarTeam
     * connection information as a single URL string of
     *
     * @return a <code>String</code> of the form
     *         "servername:portnum/project/view"
     * @see #getServername()
     * @see #getServerport()
     * @see #getProjectname()
     * @see #getViewname()
     */
    public final String getURL() {
        return this.servername + ":"
            + this.serverport + "/"
            + this.projectname + "/"
            + ((null == this.viewname) ? "" : this.viewname);
    }

    /**
     * returns an URL string useful for interacting with many StarTeamFinder
     * methods.
     *
     * @return the URL string for this task.
     */
    protected final String getViewURL() {
        return getUserName() + ":" + getPassword() + "@" + getURL();
    }
    /**
     * set the name of the StarTeam user, needed for the connection
     *
     * @param userName name of the user to be logged in
     */
    public final void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * returns the name of the StarTeam user
     *
     * @return the name of the StarTeam user
     */
    public final String getUserName() {
        return this.userName;
    }

    /**
     * set the password to be used for login; required.
     *
     * @param password the password to be used for login
     */
    public final void setPassword(String password) {
        this.password = password;
    }

    /**
     * returns the password used for login
     *
     * @return the password used for login
     */
    public final String getPassword() {
        return this.password;
    }

    /**
     * returns a reference to the server which may be used for informational
     * purposes by subclasses.
     *
     * @return a reference to the server
     */
    protected final Server getServer() {
        return this.server;
    }

    /**
     * disconnects from the StarTeam server.  Should be called from the
     * finally clause of every StarTeamTask-based execute method.
     */
    protected final void disconnectFromServer() {
        if (null != this.server) {
            this.server.disconnect();
            log("successful disconnect from StarTeam Server " + servername,
                Project.MSG_VERBOSE);
        }
    }

    /**
     * returns a list of TypeNames known to the server.
     *
     * @return a reference to the server's TypeNames
     */
    protected final TypeNames getTypeNames() {
        return this.server.getTypeNames();
    }
    /**
     * Derived classes must override <code>createSnapshotView</code>
     * defining the kind of configured view appropriate to its task.
     *
     * @param rawview the unconfigured <code>View</code>
     * @return the snapshot <code>View</code> appropriately configured.
     * @throws BuildException on error
     */
    protected abstract View createSnapshotView(View rawview)
    throws BuildException;

    /**
     * All subclasses will call on this method to open the view needed for
     * processing.  This method also saves a reference to the
     * <code>Server</code> that may be accessed for information at various
     * points in the process.
     *
     * @return the <code>View</code> that will be used for processing.
     * @see #createSnapshotView(View)
     * @see #getServer()
     * @throws BuildException on error
     */
    protected View openView() throws BuildException {

        logStarteamVersion();
        View view = null;
        try {
            view = StarTeamFinder.openView(getViewURL());
        } catch (Exception e) {
            throw new BuildException(
                "Failed to connect to " + getURL(), e);
        }

        if (null == view) {
            throw new BuildException("Cannot find view" + getURL()
                + " in repository()");
        }

        View snapshot = createSnapshotView(view);
        log("Connected to StarTeam view " + getURL(),
            Project.MSG_VERBOSE);
        this.server = snapshot.getServer();
        return snapshot;
    }

    /**
     * Returns the name of the user with the supplied ID or a blank string
     * if user not found.
     *
     * @param userID a user's ID
     * @return the name of the user with ID userID
     */
    protected final String getUserName(int userID) {
        User u = this.server.getUser(userID);
        if (null == u) {
            return "";
        }
        return u.getName();
    }

}