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.

PropertyHelperTask.java 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.taskdefs;
  19. import java.util.ArrayList;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import org.apache.tools.ant.BuildException;
  23. import org.apache.tools.ant.MagicNames;
  24. import org.apache.tools.ant.Project;
  25. import org.apache.tools.ant.PropertyHelper;
  26. import org.apache.tools.ant.Task;
  27. /**
  28. * This task is designed to allow the user to install a different
  29. * PropertyHelper on the current Project. This task also allows the
  30. * installation of PropertyHelper delegates on either the newly installed
  31. * or existing PropertyHelper.
  32. * @since Ant 1.8
  33. */
  34. public class PropertyHelperTask extends Task {
  35. /**
  36. * Nested delegate for refid usage.
  37. */
  38. public final class DelegateElement {
  39. private String refid;
  40. private DelegateElement() {
  41. }
  42. /**
  43. * Get the refid.
  44. * @return String
  45. */
  46. public String getRefid() {
  47. return refid;
  48. }
  49. /**
  50. * Set the refid.
  51. * @param refid the String to set
  52. */
  53. public void setRefid(String refid) {
  54. this.refid = refid;
  55. }
  56. private PropertyHelper.Delegate resolve() {
  57. if (refid == null) {
  58. throw new BuildException("refid required for generic delegate");
  59. }
  60. return (PropertyHelper.Delegate) getProject().getReference(refid);
  61. }
  62. }
  63. private PropertyHelper propertyHelper;
  64. private List delegates;
  65. /**
  66. * Add a new PropertyHelper to be set on the Project.
  67. * @param propertyHelper the PropertyHelper to set.
  68. */
  69. public synchronized void addConfigured(PropertyHelper propertyHelper) {
  70. if (this.propertyHelper != null) {
  71. throw new BuildException("Only one PropertyHelper can be installed");
  72. }
  73. this.propertyHelper = propertyHelper;
  74. }
  75. /**
  76. * Add a PropertyHelper delegate to the existing or new PropertyHelper.
  77. * @param delegate the delegate to add.
  78. */
  79. public synchronized void addConfigured(PropertyHelper.Delegate delegate) {
  80. getAddDelegateList().add(delegate);
  81. }
  82. /**
  83. * Add a nested <delegate refid="foo" /> element.
  84. * @return DelegateElement
  85. */
  86. public DelegateElement createDelegate() {
  87. DelegateElement result = new DelegateElement();
  88. getAddDelegateList().add(result);
  89. return result;
  90. }
  91. /**
  92. * Execute the task.
  93. * @throws BuildException on error.
  94. */
  95. public void execute() throws BuildException {
  96. if (getProject() == null) {
  97. throw new BuildException("Project instance not set");
  98. }
  99. if (propertyHelper == null && delegates == null) {
  100. throw new BuildException("Either a new PropertyHelper"
  101. + " or one or more PropertyHelper delegates are required");
  102. }
  103. PropertyHelper ph = propertyHelper;
  104. if (ph == null) {
  105. ph = PropertyHelper.getPropertyHelper(getProject());
  106. } else {
  107. ph = propertyHelper;
  108. }
  109. synchronized (ph) {
  110. if (delegates != null) {
  111. for (Iterator iter = delegates.iterator(); iter.hasNext();) {
  112. Object o = iter.next();
  113. PropertyHelper.Delegate delegate = o instanceof DelegateElement
  114. ? ((DelegateElement) o).resolve() : (PropertyHelper.Delegate) o;
  115. log("Adding PropertyHelper delegate " + delegate, Project.MSG_DEBUG);
  116. ph.add(delegate);
  117. }
  118. }
  119. }
  120. if (propertyHelper != null) {
  121. log("Installing PropertyHelper " + propertyHelper, Project.MSG_DEBUG);
  122. // TODO copy existing properties to new PH?
  123. getProject().addReference(MagicNames.REFID_PROPERTY_HELPER, propertyHelper);
  124. }
  125. }
  126. private synchronized List getAddDelegateList() {
  127. if (delegates == null) {
  128. delegates = new ArrayList();
  129. }
  130. return delegates;
  131. }
  132. }