KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > starteam > StarTeamTask


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.starteam;
19
20 import com.starbase.starteam.BuildNumber;
21 import com.starbase.starteam.Server;
22 import com.starbase.starteam.StarTeamFinder;
23 import com.starbase.starteam.TypeNames;
24 import com.starbase.starteam.User;
25 import com.starbase.starteam.View;
26 import java.util.StringTokenizer JavaDoc;
27 import org.apache.tools.ant.BuildException;
28 import org.apache.tools.ant.Project;
29 import org.apache.tools.ant.Task;
30
31 /**
32  * Common super class for all StarTeam tasks.
33  * At this level of the hierarchy we are concerned only with obtaining a
34  * connection to the StarTeam server. The subclass <code>TreeBasedTask</code>,
35  * also abstract defines the tree-walking behavior common to many subtasks.
36  *
37  * @see TreeBasedTask
38  * @version 1.1
39  */

40
41 public abstract class StarTeamTask extends Task {
42
43     // ATTRIBUTES
44

45     /**
46      * The username of the connection
47      */

48     private String JavaDoc userName;
49
50     /**
51      * The username of the connection
52      */

53     private String JavaDoc password;
54
55     /**
56      * name of Starteam server to connect to
57      */

58     private String JavaDoc servername;
59
60     /**
61      * port of Starteam server to connect to
62      */

63     private String JavaDoc serverport;
64
65     /**
66      * name of Starteam project to connect to
67      */

68     private String JavaDoc projectname;
69
70     /**
71      * name of Starteam view to connect to
72      */

73     private String JavaDoc viewname;
74
75     /**
76      *The starteam server through which all activities will be done.
77      */

78     private Server server = null;
79
80     private void logStarteamVersion() {
81         log("StarTeam version: "
82             + BuildNumber.getDisplayString(), Project.MSG_VERBOSE);
83     }
84
85
86     /////////////////////////////////////////////////////////
87
// GET/SET methods.
88
// Setters, of course are where ant user passes in values.
89
/////////////////////////////////////////////////////////
90

91     /**
92      * Set the name of StarTeamServer;
93      * required if <tt>URL</tt> is not set.
94      * @param servername a <code>String</code> value
95      * @see #setURL(String)
96      */

97     public final void setServername(String JavaDoc servername) {
98         this.servername = servername;
99     }
100
101     /**
102      * returns the name of the StarTeamServer
103      *
104      * @return the name of the StarTeam server
105      * @see #getURL()
106      */

107     public final String JavaDoc getServername() {
108         return this.servername;
109     }
110
111     /**
112      * set the port number of the StarTeam connection;
113      * required if <tt>URL</tt> is not set.
114      * @param serverport port number to be set
115      * @see #setURL(String)
116      */

117     public final void setServerport(String JavaDoc serverport) {
118         this.serverport = serverport;
119     }
120
121     /**
122      * returns the port number of the StarTeam connection
123      *
124      * @return the port number of the StarTeam connection
125      * @see #getURL()
126      */

127     public final String JavaDoc getServerport() {
128         return this.serverport;
129     }
130
131     /**
132      * set the name of the StarTeam project to be acted on;
133      * required if <tt>URL</tt> is not set.
134      *
135      * @param projectname the name of the StarTeam project to be acted on
136      * @see #setURL(String)
137      */

138     public final void setProjectname(String JavaDoc projectname) {
139         this.projectname = projectname;
140     }
141
142     /**
143      * returns the name of the StarTeam project to be acted on
144      *
145      * @return the name of the StarTeam project to be acted on
146      * @see #getURL()
147      */

148     public final String JavaDoc getProjectname() {
149         return this.projectname;
150     }
151
152     /**
153      * set the name of the StarTeam view to be acted on;
154      * required if <tt>URL</tt> is not set.
155      *
156      * @param viewname the name of the StarTeam view to be acted on
157      * @see #setURL(String)
158      */

159     public final void setViewname(String JavaDoc viewname) {
160         this.viewname = viewname;
161     }
162
163     /**
164      * returns the name of the StarTeam view to be acted on
165      *
166      * @return the name of the StarTeam view to be acted on
167      * @see #getURL()
168      */

169     public final String JavaDoc getViewname() {
170         return this.viewname;
171     }
172
173
174     /**
175      * Set the server name, server port,
176      * project name and project folder in one shot;
177      * optional, but the server connection must be specified somehow.
178      *
179      * @param url a <code>String</code> of the form
180      * "servername:portnum/project/view"
181      * @see #setServername(String)
182      * @see #setServerport(String)
183      * @see #setProjectname(String)
184      * @see #setViewname(String)
185      */

186     public final void setURL(String JavaDoc url) {
187         StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(url, "/");
188         if (t.hasMoreTokens()) {
189             String JavaDoc unpw = t.nextToken();
190             int pos = unpw.indexOf(":");
191             if (pos > 0) {
192                 this.servername = unpw.substring(0, pos);
193                 this.serverport = unpw.substring(pos + 1);
194                 if (t.hasMoreTokens()) {
195                     this.projectname = t.nextToken();
196                     if (t.hasMoreTokens()) {
197                         this.viewname = t.nextToken();
198                     }
199                 }
200             }
201         }
202     }
203
204     /**
205      * convenience method returns whole URL at once
206      * returns
207      * as a single string
208      */

209     /**
210      * a convenience method which returns the whole StarTeam
211      * connection information as a single URL string of
212      *
213      * @return a <code>String</code> of the form
214      * "servername:portnum/project/view"
215      * @see #getServername()
216      * @see #getServerport()
217      * @see #getProjectname()
218      * @see #getViewname()
219      */

220     public final String JavaDoc getURL() {
221         return this.servername + ":"
222             + this.serverport + "/"
223             + this.projectname + "/"
224             + ((null == this.viewname) ? "" : this.viewname);
225     }
226
227     /**
228      * returns an URL string useful for interacting with many StarTeamFinder
229      * methods.
230      *
231      * @return the URL string for this task.
232      */

233     protected final String JavaDoc getViewURL() {
234         return getUserName() + ":" + getPassword() + "@" + getURL();
235     }
236     /**
237      * set the name of the StarTeam user, needed for the connection
238      *
239      * @param userName name of the user to be logged in
240      */

241     public final void setUserName(String JavaDoc userName) {
242         this.userName = userName;
243     }
244
245     /**
246      * returns the name of the StarTeam user
247      *
248      * @return the name of the StarTeam user
249      */

250     public final String JavaDoc getUserName() {
251         return this.userName;
252     }
253
254     /**
255      * set the password to be used for login; required.
256      *
257      * @param password the password to be used for login
258      */

259     public final void setPassword(String JavaDoc password) {
260         this.password = password;
261     }
262
263     /**
264      * returns the password used for login
265      *
266      * @return the password used for login
267      */

268     public final String JavaDoc getPassword() {
269         return this.password;
270     }
271
272     /**
273      * returns a reference to the server which may be used for informational
274      * purposes by subclasses.
275      *
276      * @return a reference to the server
277      */

278     protected final Server getServer() {
279         return this.server;
280     }
281
282     /**
283      * disconnects from the StarTeam server. Should be called from the
284      * finally clause of every StarTeamTask-based execute method.
285      */

286     protected final void disconnectFromServer() {
287         if (null != this.server) {
288             this.server.disconnect();
289             log("successful disconnect from StarTeam Server " + servername,
290                 Project.MSG_VERBOSE);
291         }
292     }
293
294     /**
295      * returns a list of TypeNames known to the server.
296      *
297      * @return a reference to the server's TypeNames
298      */

299     protected final TypeNames getTypeNames() {
300         return this.server.getTypeNames();
301     }
302     /**
303      * Derived classes must override <code>createSnapshotView</code>
304      * defining the kind of configured view appropriate to its task.
305      *
306      * @param rawview the unconfigured <code>View</code>
307      * @return the snapshot <code>View</code> appropriately configured.
308      * @throws BuildException on error
309      */

310     protected abstract View createSnapshotView(View rawview)
311     throws BuildException;
312
313     /**
314      * All subclasses will call on this method to open the view needed for
315      * processing. This method also saves a reference to the
316      * <code>Server</code> that may be accessed for information at various
317      * points in the process.
318      *
319      * @return the <code>View</code> that will be used for processing.
320      * @see #createSnapshotView(View)
321      * @see #getServer()
322      * @throws BuildException on error
323      */

324     protected View openView() throws BuildException {
325
326         logStarteamVersion();
327         View view = null;
328         try {
329             view = StarTeamFinder.openView(getViewURL());
330         } catch (Exception JavaDoc e) {
331             throw new BuildException(
332                 "Failed to connect to " + getURL(), e);
333         }
334
335         if (null == view) {
336             throw new BuildException("Cannot find view" + getURL()
337                 + " in repository()");
338         }
339
340         View snapshot = createSnapshotView(view);
341         log("Connected to StarTeam view " + getURL(),
342             Project.MSG_VERBOSE);
343         this.server = snapshot.getServer();
344         return snapshot;
345     }
346
347     /**
348      * Returns the name of the user with the supplied ID or a blank string
349      * if user not found.
350      *
351      * @param userID a user's ID
352      * @return the name of the user with ID userID
353      */

354     protected final String JavaDoc getUserName(int userID) {
355         User u = this.server.getUser(userID);
356         if (null == u) {
357             return "";
358         }
359         return u.getName();
360     }
361
362 }
363
Popular Tags