001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.starteam;
019:
020: import com.starbase.starteam.BuildNumber;
021: import com.starbase.starteam.Server;
022: import com.starbase.starteam.StarTeamFinder;
023: import com.starbase.starteam.TypeNames;
024: import com.starbase.starteam.User;
025: import com.starbase.starteam.View;
026: import java.util.StringTokenizer;
027: import org.apache.tools.ant.BuildException;
028: import org.apache.tools.ant.Project;
029: import org.apache.tools.ant.Task;
030:
031: /**
032: * Common super class for all StarTeam tasks.
033: * At this level of the hierarchy we are concerned only with obtaining a
034: * connection to the StarTeam server. The subclass <code>TreeBasedTask</code>,
035: * also abstract defines the tree-walking behavior common to many subtasks.
036: *
037: * @see TreeBasedTask
038: * @version 1.1
039: */
040:
041: public abstract class StarTeamTask extends Task {
042:
043: // ATTRIBUTES
044:
045: /**
046: * The username of the connection
047: */
048: private String userName;
049:
050: /**
051: * The username of the connection
052: */
053: private String password;
054:
055: /**
056: * name of Starteam server to connect to
057: */
058: private String servername;
059:
060: /**
061: * port of Starteam server to connect to
062: */
063: private String serverport;
064:
065: /**
066: * name of Starteam project to connect to
067: */
068: private String projectname;
069:
070: /**
071: * name of Starteam view to connect to
072: */
073: private String viewname;
074:
075: /**
076: *The starteam server through which all activities will be done.
077: */
078: private Server server = null;
079:
080: private void logStarteamVersion() {
081: log("StarTeam version: " + BuildNumber.getDisplayString(),
082: Project.MSG_VERBOSE);
083: }
084:
085: /////////////////////////////////////////////////////////
086: // GET/SET methods.
087: // Setters, of course are where ant user passes in values.
088: /////////////////////////////////////////////////////////
089:
090: /**
091: * Set the name of StarTeamServer;
092: * required if <tt>URL</tt> is not set.
093: * @param servername a <code>String</code> value
094: * @see #setURL(String)
095: */
096: public final void setServername(String servername) {
097: this .servername = servername;
098: }
099:
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: * Set the server name, server port,
174: * project name and project folder in one shot;
175: * optional, but the server connection must be specified somehow.
176: *
177: * @param url a <code>String</code> of the form
178: * "servername:portnum/project/view"
179: * @see #setServername(String)
180: * @see #setServerport(String)
181: * @see #setProjectname(String)
182: * @see #setViewname(String)
183: */
184: public final void setURL(String url) {
185: StringTokenizer t = new StringTokenizer(url, "/");
186: if (t.hasMoreTokens()) {
187: String unpw = t.nextToken();
188: int pos = unpw.indexOf(":");
189: if (pos > 0) {
190: this .servername = unpw.substring(0, pos);
191: this .serverport = unpw.substring(pos + 1);
192: if (t.hasMoreTokens()) {
193: this .projectname = t.nextToken();
194: if (t.hasMoreTokens()) {
195: this .viewname = t.nextToken();
196: }
197: }
198: }
199: }
200: }
201:
202: /**
203: * convenience method returns whole URL at once
204: * returns
205: * as a single string
206: */
207: /**
208: * a convenience method which returns the whole StarTeam
209: * connection information as a single URL string of
210: *
211: * @return a <code>String</code> of the form
212: * "servername:portnum/project/view"
213: * @see #getServername()
214: * @see #getServerport()
215: * @see #getProjectname()
216: * @see #getViewname()
217: */
218: public final String getURL() {
219: return this .servername + ":" + this .serverport + "/"
220: + this .projectname + "/"
221: + ((null == this .viewname) ? "" : this .viewname);
222: }
223:
224: /**
225: * returns an URL string useful for interacting with many StarTeamFinder
226: * methods.
227: *
228: * @return the URL string for this task.
229: */
230: protected final String getViewURL() {
231: return getUserName() + ":" + getPassword() + "@" + getURL();
232: }
233:
234: /**
235: * set the name of the StarTeam user, needed for the connection
236: *
237: * @param userName name of the user to be logged in
238: */
239: public final void setUserName(String userName) {
240: this .userName = userName;
241: }
242:
243: /**
244: * returns the name of the StarTeam user
245: *
246: * @return the name of the StarTeam user
247: */
248: public final String getUserName() {
249: return this .userName;
250: }
251:
252: /**
253: * set the password to be used for login; required.
254: *
255: * @param password the password to be used for login
256: */
257: public final void setPassword(String password) {
258: this .password = password;
259: }
260:
261: /**
262: * returns the password used for login
263: *
264: * @return the password used for login
265: */
266: public final String getPassword() {
267: return this .password;
268: }
269:
270: /**
271: * returns a reference to the server which may be used for informational
272: * purposes by subclasses.
273: *
274: * @return a reference to the server
275: */
276: protected final Server getServer() {
277: return this .server;
278: }
279:
280: /**
281: * disconnects from the StarTeam server. Should be called from the
282: * finally clause of every StarTeamTask-based execute method.
283: */
284: protected final void disconnectFromServer() {
285: if (null != this .server) {
286: this .server.disconnect();
287: log("successful disconnect from StarTeam Server "
288: + servername, Project.MSG_VERBOSE);
289: }
290: }
291:
292: /**
293: * returns a list of TypeNames known to the server.
294: *
295: * @return a reference to the server's TypeNames
296: */
297: protected final TypeNames getTypeNames() {
298: return this .server.getTypeNames();
299: }
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: * @throws BuildException on error
308: */
309: protected abstract View createSnapshotView(View rawview)
310: throws BuildException;
311:
312: /**
313: * All subclasses will call on this method to open the view needed for
314: * processing. This method also saves a reference to the
315: * <code>Server</code> that may be accessed for information at various
316: * points in the process.
317: *
318: * @return the <code>View</code> that will be used for processing.
319: * @see #createSnapshotView(View)
320: * @see #getServer()
321: * @throws BuildException on error
322: */
323: protected View openView() throws BuildException {
324:
325: logStarteamVersion();
326: View view = null;
327: try {
328: view = StarTeamFinder.openView(getViewURL());
329: } catch (Exception e) {
330: throw new BuildException(
331: "Failed to connect to " + getURL(), e);
332: }
333:
334: if (null == view) {
335: throw new BuildException("Cannot find view" + getURL()
336: + " in repository()");
337: }
338:
339: View snapshot = createSnapshotView(view);
340: log("Connected to StarTeam view " + getURL(),
341: Project.MSG_VERBOSE);
342: this .server = snapshot.getServer();
343: return snapshot;
344: }
345:
346: /**
347: * Returns the name of the user with the supplied ID or a blank string
348: * if user not found.
349: *
350: * @param userID a user's ID
351: * @return the name of the user with ID userID
352: */
353: protected final String getUserName(int userID) {
354: User u = this .server.getUser(userID);
355: if (null == u) {
356: return "";
357: }
358: return u.getName();
359: }
360:
361: }
|