PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/scm/RepositoryBrowsers.java

http://github.com/hudson/hudson
Java | 90 lines | 33 code | 9 blank | 48 comment | 5 complexity | c137d6324c19518aac428f4e01d6a519 MD5 | raw file
 1/*
 2 * The MIT License
 3 * 
 4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Daniel Dyer, Stephen Connolly
 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.scm;
25
26import hudson.model.Descriptor;
27import hudson.model.Descriptor.FormException;
28import hudson.util.DescriptorList;
29import hudson.Extension;
30import org.kohsuke.stapler.StaplerRequest;
31
32import java.util.ArrayList;
33import java.util.List;
34
35import net.sf.json.JSONObject;
36
37/**
38 * List of all installed {@link RepositoryBrowsers}.
39 *
40 * @author Kohsuke Kawaguchi
41 */
42public class RepositoryBrowsers {
43    /**
44     * List of all installed {@link RepositoryBrowsers}.
45     *
46     * @deprecated as of 1.286.
47     *      Use {@link RepositoryBrowser#all()} for read access and {@link Extension} for registration.
48     */
49    public static final List<Descriptor<RepositoryBrowser<?>>> LIST = new DescriptorList<RepositoryBrowser<?>>((Class)RepositoryBrowser.class);
50
51    /**
52     * Only returns those {@link RepositoryBrowser} descriptors that extend from the given type.
53     */
54    public static List<Descriptor<RepositoryBrowser<?>>> filter(Class<? extends RepositoryBrowser> t) {
55        List<Descriptor<RepositoryBrowser<?>>> r = new ArrayList<Descriptor<RepositoryBrowser<?>>>();
56        for (Descriptor<RepositoryBrowser<?>> d : RepositoryBrowser.all())
57            if(d.isSubTypeOf(t))
58                r.add(d);
59        return r;
60    }
61
62    /**
63     * Creates an instance of {@link RepositoryBrowser} from a form submission.
64     *
65     * @deprecated since 2008-06-19.
66     *      Use {@link #createInstance(Class, StaplerRequest, JSONObject, String)}.
67     */
68    public static <T extends RepositoryBrowser>
69    T createInstance(Class<T> type, StaplerRequest req, String fieldName) throws FormException {
70        List<Descriptor<RepositoryBrowser<?>>> list = filter(type);
71        String value = req.getParameter(fieldName);
72        if(value==null || value.equals("auto"))
73            return null;
74
75        return type.cast(list.get(Integer.parseInt(value)).newInstance(req,null/*TODO*/));
76    }
77
78    /**
79     * Creates an instance of {@link RepositoryBrowser} from a form submission.
80     *
81     * @since 1.227
82     */
83    public static <T extends RepositoryBrowser>
84    T createInstance(Class<T> type, StaplerRequest req, JSONObject parent, String fieldName) throws FormException {
85        JSONObject o = (JSONObject)parent.get(fieldName);
86        if(o==null) return null;
87
88        return req.bindJSON(type,o);
89    }
90}