/hudson-core/src/main/java/hudson/ExtensionListView.java

http://github.com/hudson/hudson · Java · 167 lines · 94 code · 23 blank · 50 comment · 0 complexity · 94ec264872d2df5d691fffb39df591c8 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson;
  25. import hudson.model.Hudson;
  26. import hudson.tasks.UserNameResolver;
  27. import hudson.util.CopyOnWriteList;
  28. import java.util.AbstractList;
  29. import java.util.Iterator;
  30. import java.util.List;
  31. import java.util.Collection;
  32. /**
  33. * Compatibility layer for legacy manual registration of extension points.
  34. *
  35. * <p>
  36. * Instances of this class can be created statically as a singleton, but it provides the view
  37. * to {@link ExtensionList} of the current {@link Hudson}.
  38. * Write operations to this list will update the legacy instances on {@link ExtensionList}.
  39. *
  40. * <p>
  41. * Whereas we used to use some simple data structure to keep track of static singletons,
  42. * we can now use this instances, so that {@link ExtensionList} sees all the auto-registered
  43. * and manually registered instances.
  44. *
  45. * <p>
  46. * Similarly, the old list (such as {@link UserNameResolver#LIST} continues to show all
  47. * auto and manually registered instances, thus providing necessary bi-directional interoperability.
  48. *
  49. * @author Kohsuke Kawaguchi
  50. */
  51. public class ExtensionListView {
  52. /**
  53. * Creates a plain {@link List} backed by the current {@link ExtensionList}.
  54. */
  55. public static <T> List<T> createList(final Class<T> type) {
  56. return new AbstractList<T>() {
  57. private ExtensionList<T> storage() {
  58. return Hudson.getInstance().getExtensionList(type);
  59. }
  60. @Override
  61. public Iterator<T> iterator() {
  62. return storage().iterator();
  63. }
  64. public T get(int index) {
  65. return storage().get(index);
  66. }
  67. public int size() {
  68. return storage().size();
  69. }
  70. @Override
  71. public boolean add(T t) {
  72. return storage().add(t);
  73. }
  74. @Override
  75. public void add(int index, T t) {
  76. // index ignored
  77. storage().add(t);
  78. }
  79. @Override
  80. public T remove(int index) {
  81. return storage().remove(index);
  82. }
  83. @Override
  84. public boolean remove(Object o) {
  85. return storage().remove(o);
  86. }
  87. };
  88. }
  89. /**
  90. * Creates a seriously hacked up {@link CopyOnWriteList} that acts as a view to the current {@link ExtensionList}.
  91. */
  92. public static <T> CopyOnWriteList<T> createCopyOnWriteList(final Class<T> type) {
  93. return new CopyOnWriteList<T>() {
  94. private ExtensionList<T> storage() {
  95. return Hudson.getInstance().getExtensionList(type);
  96. }
  97. @Override
  98. public void add(T t) {
  99. storage().add(t);
  100. }
  101. @Override
  102. public boolean remove(T t) {
  103. return storage().remove(t);
  104. }
  105. @Override
  106. public Iterator<T> iterator() {
  107. return storage().iterator();
  108. }
  109. @Override
  110. public void replaceBy(CopyOnWriteList<? extends T> that) {
  111. throw new UnsupportedOperationException();
  112. }
  113. @Override
  114. public void replaceBy(Collection<? extends T> that) {
  115. throw new UnsupportedOperationException();
  116. }
  117. @Override
  118. public void replaceBy(T... that) {
  119. throw new UnsupportedOperationException();
  120. }
  121. @Override
  122. public void clear() {
  123. throw new UnsupportedOperationException();
  124. }
  125. @Override
  126. public T[] toArray(T[] array) {
  127. return storage().toArray(array);
  128. }
  129. @Override
  130. public List<T> getView() {
  131. return storage();
  132. }
  133. @Override
  134. public void addAllTo(Collection<? super T> dst) {
  135. dst.addAll(storage());
  136. }
  137. @Override
  138. public boolean isEmpty() {
  139. return storage().isEmpty();
  140. }
  141. };
  142. }
  143. // TODO: we need a few more types whose implementations get uglier
  144. }