You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

TaskConfigurationChecker.java 3.6 kB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. /**
  22. * <p>Helper class for the check of the configuration of a given task.
  23. * This class provides methods for making assumptions about the task configuration.
  24. * After collecting all violations with <tt>assert*</tt> and <tt>fail</tt>
  25. * methods the <tt>checkErrors</tt> will throw a BuildException with all collected
  26. * messages or does nothing if there wasn't any error.</p>
  27. *
  28. * <p>Example:</p>
  29. *
  30. * <pre>
  31. * public class MyTask extends Task {
  32. * ...
  33. * public void execute() {
  34. * TaskConfigurationChecker checker = TaskConfigurationChecker(this);
  35. * checker.assertConfig(
  36. * srcdir != null,
  37. * "Attribute 'srcdir' must be set.
  38. * );
  39. * checker.assertConfig(
  40. * srcdir.exists(),
  41. * "Srcdir (" + srcdir + ") must exist."
  42. * );
  43. * if (someComplexCondition()) {
  44. * fail("Complex condition failed.");
  45. * }
  46. * checker.checkErrors();
  47. * }
  48. * }
  49. * </pre>
  50. *
  51. * @see <a href="http://martinfowler.com/eaaDev/Notification.html">Notification Pattern</a>
  52. */
  53. public class TaskConfigurationChecker {
  54. /** List of all collected error messages. */
  55. private List<String> errors = new ArrayList<>();
  56. /** Task for which the configuration should be checked. */
  57. private final Task task;
  58. /**
  59. * Constructor.
  60. * @param task which task should be checked
  61. */
  62. public TaskConfigurationChecker(Task task) {
  63. this.task = task;
  64. }
  65. /**
  66. * Asserts that a condition is true.
  67. * @param condition which condition to check
  68. * @param errormessage errormessage to throw if a condition failed
  69. */
  70. public void assertConfig(boolean condition, String errormessage) {
  71. if (!condition) {
  72. errors.add(errormessage);
  73. }
  74. }
  75. /**
  76. * Registers an error.
  77. * @param errormessage the message for the registered error
  78. */
  79. public void fail(String errormessage) {
  80. errors.add(errormessage);
  81. }
  82. /**
  83. * Checks if there are any collected errors and throws a BuildException
  84. * with all messages if there was one or more.
  85. * @throws BuildException if one or more errors were registered
  86. */
  87. public void checkErrors() throws BuildException {
  88. if (!errors.isEmpty()) {
  89. StringBuilder sb = new StringBuilder(String.format("Configuration error on <%s>:%n",
  90. task.getTaskName()));
  91. for (String msg : errors) {
  92. sb.append(String.format("- %s%n", msg));
  93. }
  94. throw new BuildException(sb.toString(), task.getLocation());
  95. }
  96. }
  97. }