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