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.

LocalPropertyStack.java 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.property;
  19. import java.util.LinkedList;
  20. import java.util.Map;
  21. import java.util.concurrent.ConcurrentHashMap;
  22. import org.apache.tools.ant.PropertyHelper;
  23. /**
  24. * A stack of local property maps.
  25. * There is a map for each scope (target, sequential, macro).
  26. * @since Ant 1.8.0
  27. */
  28. public class LocalPropertyStack {
  29. private final LinkedList<Map<String, Object>> stack = new LinkedList<Map<String, Object>>();
  30. private final Object LOCK = new Object();
  31. // --------------------------------------------------
  32. //
  33. // Local property adding and scoping
  34. //
  35. // --------------------------------------------------
  36. /**
  37. * Add a local property.
  38. * @param property the name of the local property.
  39. */
  40. public void addLocal(String property) {
  41. synchronized (LOCK) {
  42. Map<String, Object> map = stack.peek();
  43. if (map != null) {
  44. map.put(property, NullReturn.NULL);
  45. }
  46. }
  47. }
  48. /**
  49. * Enter the local scope.
  50. */
  51. public void enterScope() {
  52. synchronized (LOCK) {
  53. stack.addFirst(new ConcurrentHashMap<String, Object>());
  54. }
  55. }
  56. /**
  57. * Exit the local scope.
  58. */
  59. public void exitScope() {
  60. synchronized (LOCK) {
  61. stack.removeFirst().clear();
  62. }
  63. }
  64. // --------------------------------------------------
  65. //
  66. // Copy - used in parallel to make a new stack
  67. //
  68. // --------------------------------------------------
  69. /**
  70. * Copy the stack for a parallel thread.
  71. * @return a copy.
  72. */
  73. public LocalPropertyStack copy() {
  74. synchronized (LOCK) {
  75. LocalPropertyStack ret = new LocalPropertyStack();
  76. ret.stack.addAll(stack);
  77. return ret;
  78. }
  79. }
  80. // --------------------------------------------------
  81. //
  82. // PropertyHelper delegate methods
  83. //
  84. // --------------------------------------------------
  85. /**
  86. * Evaluate a property.
  87. * @param property the property's String "identifier".
  88. * @param helper the invoking PropertyHelper.
  89. * @return Object value.
  90. */
  91. public Object evaluate(String property, PropertyHelper helper) {
  92. synchronized (LOCK) {
  93. for (Map<String, Object> map : stack) {
  94. Object ret = map.get(property);
  95. if (ret != null) {
  96. return ret;
  97. }
  98. }
  99. }
  100. return null;
  101. }
  102. /**
  103. * Set a *new" property.
  104. * @param property the property's String "identifier".
  105. * @param value the value to set.
  106. * @param propertyHelper the invoking PropertyHelper.
  107. * @return true if this entity 'owns' the property.
  108. */
  109. public boolean setNew(
  110. String property, Object value, PropertyHelper propertyHelper) {
  111. Map<String, Object> map = getMapForProperty(property);
  112. if (map == null) {
  113. return false;
  114. }
  115. Object currValue = map.get(property);
  116. if (currValue == NullReturn.NULL) {
  117. map.put(property, value);
  118. }
  119. return true;
  120. }
  121. /**
  122. * Set a property.
  123. * @param property the property's String "identifier".
  124. * @param value the value to set.
  125. * @param propertyHelper the invoking PropertyHelper.
  126. * @return true if this entity 'owns' the property.
  127. */
  128. public boolean set(String property, Object value, PropertyHelper propertyHelper) {
  129. Map<String, Object> map = getMapForProperty(property);
  130. if (map == null) {
  131. return false;
  132. }
  133. map.put(property, value);
  134. return true;
  135. }
  136. private Map<String, Object> getMapForProperty(String property) {
  137. synchronized (LOCK) {
  138. for (Map<String, Object> map : stack) {
  139. if (map.get(property) != null) {
  140. return map;
  141. }
  142. }
  143. }
  144. return null;
  145. }
  146. }