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