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. /*
  18. * Portions of this software are based upon public domain software
  19. * originally written at the National Center for Supercomputing Applications,
  20. * University of Illinois, Urbana-Champaign.
  21. */
  22. package org.apache.tools.ant.taskdefs.optional.perforce;
  23. import org.apache.tools.ant.BuildException;
  24. import org.apache.tools.ant.Project;
  25. /**
  26. * Obtains or sets the value of a counter.
  27. *
  28. * <p> When used in its base form
  29. * (where only the counter name is provided), the counter value will be
  30. * printed to the output stream. When the value is provided, the counter
  31. * will be set to the value provided. When a property name is provided,
  32. * the property will be filled with the value of the counter. You may
  33. * not specify to both get and set the value of the counter in the same
  34. * Task.
  35. * </p>
  36. * <P>
  37. * The user performing this task must have Perforce "review" permissions
  38. * as defined by Perforce protections in order for this task to succeed.
  39. </P>
  40. * Example Usage:<br>
  41. * <p4counter name="${p4.counter}" property=${p4.change}"/>
  42. *
  43. * @ant.task category="scm"
  44. */
  45. public class P4Counter extends P4Base {
  46. /**
  47. * name of the counter
  48. */
  49. public String counter = null;
  50. /**
  51. * name of an optional property
  52. */
  53. public String property = null;
  54. /**
  55. * flag telling whether the value of the counter should be set
  56. */
  57. public boolean shouldSetValue = false;
  58. /**
  59. * flag telling whether a property should be set
  60. */
  61. public boolean shouldSetProperty = false;
  62. /**
  63. * new value for the counter
  64. */
  65. public int value = 0;
  66. /**
  67. * The name of the counter; required
  68. * @param counter name of the counter
  69. */
  70. public void setName(String counter) {
  71. this.counter = counter;
  72. }
  73. /**
  74. * The new value for the counter; optional.
  75. * @param value new value for the counter
  76. */
  77. public void setValue(int value) {
  78. this.value = value;
  79. shouldSetValue = true;
  80. }
  81. /**
  82. * A property to be set with the value of the counter
  83. * @param property the name of a property to set with the value
  84. * of the counter
  85. */
  86. public void setProperty(String property) {
  87. this.property = property;
  88. shouldSetProperty = true;
  89. }
  90. /**
  91. * again, properties are mutable in this tsk
  92. * @throws BuildException if the required parameters are not supplied.
  93. */
  94. public void execute() throws BuildException {
  95. if ((counter == null) || counter.length() == 0) {
  96. throw new BuildException("No counter specified to retrieve");
  97. }
  98. if (shouldSetValue && shouldSetProperty) {
  99. throw new BuildException("Cannot both set the value of the property and retrieve the "
  100. + "value of the property.");
  101. }
  102. String command = "counter " + P4CmdOpts + " " + counter;
  103. if (!shouldSetProperty) {
  104. // NOTE kirk@radik.com 04-April-2001 -- If you put in the -s, you
  105. // have to start running through regular expressions here. Much easier
  106. // to just not include the scripting information than to try to add it
  107. // and strip it later.
  108. command = "-s " + command;
  109. }
  110. if (shouldSetValue) {
  111. command += " " + value;
  112. }
  113. if (shouldSetProperty) {
  114. final Project myProj = getProject();
  115. P4Handler handler = new P4HandlerAdapter() {
  116. public void process(String line) {
  117. log("P4Counter retrieved line \"" + line + "\"", Project.MSG_VERBOSE);
  118. try {
  119. value = Integer.parseInt(line);
  120. myProj.setProperty(property, "" + value);
  121. } catch (NumberFormatException nfe) {
  122. throw new BuildException("Perforce error. "
  123. + "Could not retrieve counter value.");
  124. }
  125. }
  126. };
  127. execP4Command(command, handler);
  128. } else {
  129. execP4Command(command, new SimpleP4OutputHandler(this));
  130. }
  131. }
  132. }