PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
 24package hudson;
 25
 26import hudson.model.Hudson;
 27import hudson.tasks.UserNameResolver;
 28import hudson.util.CopyOnWriteList;
 29
 30import java.util.AbstractList;
 31import java.util.Iterator;
 32import java.util.List;
 33import java.util.Collection;
 34
 35/**
 36 * Compatibility layer for legacy manual registration of extension points.
 37 *
 38 * <p>
 39 * Instances of this class can be created statically as a singleton, but it provides the view
 40 * to {@link ExtensionList} of the current {@link Hudson}.
 41 * Write operations to this list will update the legacy instances on {@link ExtensionList}.
 42 *
 43 * <p>
 44 * Whereas we used to use some simple data structure to keep track of static singletons,
 45 * we can now use this instances, so that {@link ExtensionList} sees all the auto-registered
 46 * and manually registered instances.
 47 *
 48 * <p>
 49 * Similarly, the old list (such as {@link UserNameResolver#LIST} continues to show all
 50 * auto and manually registered instances, thus providing necessary bi-directional interoperability.
 51 *
 52 * @author Kohsuke Kawaguchi
 53 */
 54public class ExtensionListView {
 55    /**
 56     * Creates a plain {@link List} backed by the current {@link ExtensionList}.
 57     */
 58    public static <T> List<T> createList(final Class<T> type) {
 59        return new AbstractList<T>() {
 60            private ExtensionList<T> storage() {
 61                return Hudson.getInstance().getExtensionList(type);
 62            }
 63
 64            @Override
 65            public Iterator<T> iterator() {
 66                return storage().iterator();
 67            }
 68
 69            public T get(int index) {
 70                return storage().get(index);
 71            }
 72
 73            public int size() {
 74                return storage().size();
 75            }
 76
 77            @Override
 78            public boolean add(T t) {
 79                return storage().add(t);
 80            }
 81
 82            @Override
 83            public void add(int index, T t) {
 84                // index ignored
 85                storage().add(t);
 86            }
 87
 88            @Override
 89            public T remove(int index) {
 90                return storage().remove(index);
 91            }
 92
 93            @Override
 94            public boolean remove(Object o) {
 95                return storage().remove(o);
 96            }
 97        };
 98    }
 99
100    /**
101     * Creates a seriously hacked up {@link CopyOnWriteList} that acts as a view to the current {@link ExtensionList}.
102     */
103    public static <T> CopyOnWriteList<T> createCopyOnWriteList(final Class<T> type) {
104        return new CopyOnWriteList<T>() {
105            private ExtensionList<T> storage() {
106                return Hudson.getInstance().getExtensionList(type);
107            }
108
109            @Override
110            public void add(T t) {
111                storage().add(t);
112            }
113
114            @Override
115            public boolean remove(T t) {
116                return storage().remove(t);
117            }
118
119            @Override
120            public Iterator<T> iterator() {
121                return storage().iterator();
122            }
123
124            @Override
125            public void replaceBy(CopyOnWriteList<? extends T> that) {
126                throw new UnsupportedOperationException();
127            }
128
129            @Override
130            public void replaceBy(Collection<? extends T> that) {
131                throw new UnsupportedOperationException();
132            }
133
134            @Override
135            public void replaceBy(T... that) {
136                throw new UnsupportedOperationException();
137            }
138
139            @Override
140            public void clear() {
141                throw new UnsupportedOperationException();
142            }
143
144            @Override
145            public T[] toArray(T[] array) {
146                return storage().toArray(array);
147            }
148
149            @Override
150            public List<T> getView() {
151                return storage();
152            }
153
154            @Override
155            public void addAllTo(Collection<? super T> dst) {
156                dst.addAll(storage());
157            }
158
159            @Override
160            public boolean isEmpty() {
161                return storage().isEmpty();
162            }
163        };
164    }
165
166    // TODO: we need a few more types whose implementations get uglier
167}