1. /*
  2. * Copyright 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.cvslib;
  18. import org.apache.tools.ant.taskdefs.AbstractCvsTask;
  19. import java.io.ByteArrayOutputStream;
  20. import java.util.StringTokenizer;
  21. /**
  22. * this task allows to find out the client and the server version of a
  23. * CVS installation
  24. *
  25. * example usage :
  26. * <cvsversion
  27. * cvsRoot=":pserver:anoncvs@cvs.apache.org:/home/cvspublic"
  28. * passfile="c:/programme/cygwin/home/antoine/.cvspass"
  29. * clientversionproperty="apacheclient"
  30. * serverversionproperty="apacheserver" />
  31. *
  32. * the task can be used also in the API by calling its execute method,
  33. * then calling getServerVersion and/or getClientVersion
  34. *
  35. * @ant.task category="scm"
  36. * @since ant 1.6.1
  37. */
  38. public class CvsVersion extends AbstractCvsTask {
  39. static final long VERSION_1_11_2 = 11102;
  40. static final long MULTIPLY = 100;
  41. private String clientVersion;
  42. private String serverVersion;
  43. private String clientVersionProperty;
  44. private String serverVersionProperty;
  45. /**
  46. * get the CVS client version
  47. * @return CVS client version
  48. */
  49. public String getClientVersion() {
  50. return clientVersion;
  51. }
  52. /**
  53. * get the CVS server version
  54. * @return CVS server version
  55. */
  56. public String getServerVersion() {
  57. return serverVersion;
  58. }
  59. /**
  60. * set a property where to store the CVS client version
  61. * @param clientVersionProperty property for CVS client version
  62. */
  63. public void setClientVersionProperty(String clientVersionProperty) {
  64. this.clientVersionProperty = clientVersionProperty;
  65. }
  66. /**
  67. * set a property where to store the CVS server version
  68. * @param serverVersionProperty property for CVS server version
  69. */
  70. public void setServerVersionProperty(String serverVersionProperty) {
  71. this.serverVersionProperty = serverVersionProperty;
  72. }
  73. /**
  74. * find out if the server version supports log with S option
  75. * @return boolean indicating if the server version supports log with S option
  76. */
  77. public boolean supportsCvsLogWithSOption() {
  78. if (serverVersion == null) {
  79. return false;
  80. }
  81. StringTokenizer mySt = new StringTokenizer(serverVersion, ".");
  82. long versionNumber;
  83. long counter = MULTIPLY * MULTIPLY;
  84. long version = 0;
  85. while (mySt.hasMoreTokens()) {
  86. String s = mySt.nextToken();
  87. int i = 0;
  88. for (i = 0; i < s.length(); i++) {
  89. if (!Character.isDigit(s.charAt(i))) {
  90. break;
  91. }
  92. }
  93. String s2 = s.substring(0, i);
  94. version = version + counter * Long.parseLong(s2);
  95. if (counter == 1) {
  96. break;
  97. }
  98. counter = counter / MULTIPLY;
  99. }
  100. return (version >= VERSION_1_11_2);
  101. }
  102. /**
  103. * the execute method running CvsVersion
  104. */
  105. public void execute() {
  106. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  107. this.setOutputStream(bos);
  108. ByteArrayOutputStream berr = new ByteArrayOutputStream();
  109. this.setErrorStream(berr);
  110. setCommand("version");
  111. super.execute();
  112. String output = bos.toString();
  113. StringTokenizer st = new StringTokenizer(output);
  114. boolean client = false;
  115. boolean server = false;
  116. boolean cvs = false;
  117. while (st.hasMoreTokens()) {
  118. String currentToken = st.nextToken();
  119. if (currentToken.equals("Client:")) {
  120. client = true;
  121. } else if (currentToken.equals("Server:")) {
  122. server = true;
  123. } else if (currentToken.equals("(CVS)")) {
  124. cvs = true;
  125. }
  126. if (client && cvs) {
  127. if (st.hasMoreTokens()) {
  128. clientVersion = st.nextToken();
  129. }
  130. client = false;
  131. cvs = false;
  132. } else if (server && cvs) {
  133. if (st.hasMoreTokens()) {
  134. serverVersion = st.nextToken();
  135. }
  136. server = false;
  137. cvs = false;
  138. }
  139. }
  140. if (clientVersionProperty != null) {
  141. getProject().setNewProperty(clientVersionProperty, clientVersion);
  142. }
  143. if (serverVersionProperty != null) {
  144. getProject().setNewProperty(serverVersionProperty, serverVersion);
  145. }
  146. }
  147. }