1 | /*
|
---|
2 | * Copyright 2001-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.starteam;
|
---|
18 |
|
---|
19 | import com.starbase.starteam.BuildNumber;
|
---|
20 | import com.starbase.starteam.Server;
|
---|
21 | import com.starbase.starteam.StarTeamFinder;
|
---|
22 | import com.starbase.starteam.TypeNames;
|
---|
23 | import com.starbase.starteam.User;
|
---|
24 | import com.starbase.starteam.View;
|
---|
25 | import java.util.StringTokenizer;
|
---|
26 | import org.apache.tools.ant.BuildException;
|
---|
27 | import org.apache.tools.ant.Project;
|
---|
28 | import org.apache.tools.ant.Task;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Common super class for all StarTeam tasks.
|
---|
32 | * At this level of the hierarchy we are concerned only with obtaining a
|
---|
33 | * connection to the StarTeam server. The subclass <code>TreeBasedTask</code>,
|
---|
34 | * also abstract defines the tree-walking behavior common to many subtasks.
|
---|
35 | *
|
---|
36 | * @see TreeBasedTask
|
---|
37 | * @version 1.1
|
---|
38 | */
|
---|
39 |
|
---|
40 | public abstract class StarTeamTask extends Task {
|
---|
41 |
|
---|
42 | // ATTRIBUTES
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * The username of the connection
|
---|
46 | */
|
---|
47 | private String userName;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * The username of the connection
|
---|
51 | */
|
---|
52 | private String password;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * name of Starteam server to connect to
|
---|
56 | */
|
---|
57 | private String servername;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * port of Starteam server to connect to
|
---|
61 | */
|
---|
62 | private String serverport;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * name of Starteam project to connect to
|
---|
66 | */
|
---|
67 | private String projectname;
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * name of Starteam view to connect to
|
---|
71 | */
|
---|
72 | private String viewname;
|
---|
73 |
|
---|
74 | /**
|
---|
75 | *The starteam server through which all activities will be done.
|
---|
76 | */
|
---|
77 | private Server server = null;
|
---|
78 |
|
---|
79 | private void logStarteamVersion() {
|
---|
80 | log("StarTeam version: "
|
---|
81 | + BuildNumber.getDisplayString(), Project.MSG_VERBOSE);
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | /////////////////////////////////////////////////////////
|
---|
86 | // GET/SET methods.
|
---|
87 | // Setters, of course are where ant user passes in values.
|
---|
88 | /////////////////////////////////////////////////////////
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Set the name of StarTeamServer;
|
---|
92 | * required if <tt>URL</tt> is not set.
|
---|
93 | * @param servername a <code>String</code> value
|
---|
94 | * @see #setURL(String)
|
---|
95 | */
|
---|
96 | public final void setServername(String servername) {
|
---|
97 | this.servername = servername;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * returns the name of the StarTeamServer
|
---|
102 | *
|
---|
103 | * @return the name of the StarTeam server
|
---|
104 | * @see #getURL()
|
---|
105 | */
|
---|
106 | public final String getServername() {
|
---|
107 | return this.servername;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * set the port number of the StarTeam connection;
|
---|
112 | * required if <tt>URL</tt> is not set.
|
---|
113 | * @param serverport port number to be set
|
---|
114 | * @see #setURL(String)
|
---|
115 | */
|
---|
116 | public final void setServerport(String serverport) {
|
---|
117 | this.serverport = serverport;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * returns the port number of the StarTeam connection
|
---|
122 | *
|
---|
123 | * @return the port number of the StarTeam connection
|
---|
124 | * @see #getURL()
|
---|
125 | */
|
---|
126 | public final String getServerport() {
|
---|
127 | return this.serverport;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * set the name of the StarTeam project to be acted on;
|
---|
132 | * required if <tt>URL</tt> is not set.
|
---|
133 | *
|
---|
134 | * @param projectname the name of the StarTeam project to be acted on
|
---|
135 | * @see #setURL(String)
|
---|
136 | */
|
---|
137 | public final void setProjectname(String projectname) {
|
---|
138 | this.projectname = projectname;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * returns the name of the StarTeam project to be acted on
|
---|
143 | *
|
---|
144 | * @return the name of the StarTeam project to be acted on
|
---|
145 | * @see #getURL()
|
---|
146 | */
|
---|
147 | public final String getProjectname() {
|
---|
148 | return this.projectname;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * set the name of the StarTeam view to be acted on;
|
---|
153 | * required if <tt>URL</tt> is not set.
|
---|
154 | *
|
---|
155 | * @param viewname the name of the StarTeam view to be acted on
|
---|
156 | * @see #setURL(String)
|
---|
157 | */
|
---|
158 | public final void setViewname(String viewname) {
|
---|
159 | this.viewname = viewname;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * returns the name of the StarTeam view to be acted on
|
---|
164 | *
|
---|
165 | * @return the name of the StarTeam view to be acted on
|
---|
166 | * @see #getURL()
|
---|
167 | */
|
---|
168 | public final String getViewname() {
|
---|
169 | return this.viewname;
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Set the server name, server port,
|
---|
175 | * project name and project folder in one shot;
|
---|
176 | * optional, but the server connection must be specified somehow.
|
---|
177 | *
|
---|
178 | * @param url a <code>String</code> of the form
|
---|
179 | * "servername:portnum/project/view"
|
---|
180 | * @see #setServername(String)
|
---|
181 | * @see #setServerport(String)
|
---|
182 | * @see #setProjectname(String)
|
---|
183 | * @see #setViewname(String)
|
---|
184 | */
|
---|
185 | public final void setURL(String url) {
|
---|
186 | StringTokenizer t = new StringTokenizer(url, "/");
|
---|
187 | if (t.hasMoreTokens()) {
|
---|
188 | String unpw = t.nextToken();
|
---|
189 | int pos = unpw.indexOf(":");
|
---|
190 | if (pos > 0) {
|
---|
191 | this.servername = unpw.substring(0, pos);
|
---|
192 | this.serverport = unpw.substring(pos + 1);
|
---|
193 | if (t.hasMoreTokens()) {
|
---|
194 | this.projectname = t.nextToken();
|
---|
195 | if (t.hasMoreTokens()) {
|
---|
196 | this.viewname = t.nextToken();
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * convenience method returns whole URL at once
|
---|
205 | * returns
|
---|
206 | * as a single string
|
---|
207 | */
|
---|
208 | /**
|
---|
209 | * a convenience method which returns the whole StarTeam
|
---|
210 | * connection information as a single URL string of
|
---|
211 | *
|
---|
212 | * @return a <code>String</code> of the form
|
---|
213 | * "servername:portnum/project/view"
|
---|
214 | * @see #getServername()
|
---|
215 | * @see #getServerport()
|
---|
216 | * @see #getProjectname()
|
---|
217 | * @see #getViewname()
|
---|
218 | */
|
---|
219 | public final String getURL() {
|
---|
220 | return this.servername + ":"
|
---|
221 | + this.serverport + "/"
|
---|
222 | + this.projectname + "/"
|
---|
223 | + ((null == this.viewname) ? "" : this.viewname);
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * returns an URL string useful for interacting with many StarTeamFinder
|
---|
228 | * methods.
|
---|
229 | *
|
---|
230 | * @return the URL string for this task.
|
---|
231 | */
|
---|
232 | protected final String getViewURL() {
|
---|
233 | return getUserName() + ":" + getPassword() + "@" + getURL();
|
---|
234 | }
|
---|
235 | /**
|
---|
236 | * set the name of the StarTeam user, needed for the connection
|
---|
237 | *
|
---|
238 | * @param userName name of the user to be logged in
|
---|
239 | */
|
---|
240 | public final void setUserName(String userName) {
|
---|
241 | this.userName = userName;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * returns the name of the StarTeam user
|
---|
246 | *
|
---|
247 | * @return the name of the StarTeam user
|
---|
248 | */
|
---|
249 | public final String getUserName() {
|
---|
250 | return this.userName;
|
---|
251 | }
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * set the password to be used for login; required.
|
---|
255 | *
|
---|
256 | * @param password the password to be used for login
|
---|
257 | */
|
---|
258 | public final void setPassword(String password) {
|
---|
259 | this.password = password;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * returns the password used for login
|
---|
264 | *
|
---|
265 | * @return the password used for login
|
---|
266 | */
|
---|
267 | public final String getPassword() {
|
---|
268 | return this.password;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * returns a reference to the server which may be used for informational
|
---|
273 | * purposes by subclasses.
|
---|
274 | *
|
---|
275 | * @return a reference to the server
|
---|
276 | */
|
---|
277 | protected final Server getServer() {
|
---|
278 | return this.server;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * disconnects from the StarTeam server. Should be called from the
|
---|
283 | * finally clause of every StarTeamTask-based execute method.
|
---|
284 | */
|
---|
285 | protected final void disconnectFromServer() {
|
---|
286 | if (null != this.server) {
|
---|
287 | this.server.disconnect();
|
---|
288 | log("successful disconnect from StarTeam Server " + servername,
|
---|
289 | Project.MSG_VERBOSE);
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * returns a list of TypeNames known to the server.
|
---|
295 | *
|
---|
296 | * @return a reference to the server's TypeNames
|
---|
297 | */
|
---|
298 | protected final TypeNames getTypeNames() {
|
---|
299 | return this.server.getTypeNames();
|
---|
300 | }
|
---|
301 | /**
|
---|
302 | * Derived classes must override <code>createSnapshotView</code>
|
---|
303 | * defining the kind of configured view appropriate to its task.
|
---|
304 | *
|
---|
305 | * @param rawview the unconfigured <code>View</code>
|
---|
306 | * @return the snapshot <code>View</code> appropriately configured.
|
---|
307 | */
|
---|
308 | protected abstract View createSnapshotView(View rawview)
|
---|
309 | throws BuildException;
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * All subclasses will call on this method to open the view needed for
|
---|
313 | * processing. This method also saves a reference to the
|
---|
314 | * <code>Server</code> that may be accessed for information at various
|
---|
315 | * points in the process.
|
---|
316 | *
|
---|
317 | * @return the <code>View</code> that will be used for processing.
|
---|
318 | * @see #createSnapshotView(View)
|
---|
319 | * @see #getServer()
|
---|
320 | */
|
---|
321 | protected View openView() throws BuildException {
|
---|
322 |
|
---|
323 | logStarteamVersion();
|
---|
324 | View view = null;
|
---|
325 | try {
|
---|
326 | view = StarTeamFinder.openView(getViewURL());
|
---|
327 | } catch (Exception e) {
|
---|
328 | throw new BuildException(
|
---|
329 | "Failed to connect to " + getURL(), e);
|
---|
330 | }
|
---|
331 |
|
---|
332 | if (null == view) {
|
---|
333 | throw new BuildException("Cannot find view" + getURL()
|
---|
334 | + " in repository()");
|
---|
335 | }
|
---|
336 |
|
---|
337 | View snapshot = createSnapshotView(view);
|
---|
338 | log("Connected to StarTeam view " + getURL(),
|
---|
339 | Project.MSG_VERBOSE);
|
---|
340 | this.server = snapshot.getServer();
|
---|
341 | return snapshot;
|
---|
342 | }
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * Returns the name of the user with the supplied ID or a blank string
|
---|
346 | * if user not found.
|
---|
347 | *
|
---|
348 | * @param userID a user's ID
|
---|
349 | * @return the name of the user with ID userID
|
---|
350 | */
|
---|
351 | protected final String getUserName(int userID) {
|
---|
352 | User u = this.server.getUser(userID);
|
---|
353 | if (null == u) {
|
---|
354 | return "";
|
---|
355 | }
|
---|
356 | return u.getName();
|
---|
357 | }
|
---|
358 |
|
---|
359 | }
|
---|
360 |
|
---|
361 |
|
---|
362 |
|
---|
363 |
|
---|
364 |
|
---|
365 |
|
---|
366 |
|
---|
367 |
|
---|
368 |
|
---|
369 |
|
---|
370 |
|
---|
371 |
|
---|