/hudson-core/src/main/java/hudson/util/Service.java

http://github.com/hudson/hudson · Java · 115 lines · 71 code · 9 blank · 35 comment · 8 complexity · d7e16f48192055c185c78952b172d739 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
  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.util;
  25. import java.io.BufferedReader;
  26. import java.io.IOException;
  27. import java.io.InputStreamReader;
  28. import java.net.URL;
  29. import java.util.Collection;
  30. import java.util.Enumeration;
  31. import java.util.List;
  32. import java.util.ArrayList;
  33. import java.util.logging.Level;
  34. import java.util.logging.Logger;
  35. import static java.util.logging.Level.WARNING;
  36. /**
  37. * Load classes by looking up <tt>META-INF/services</tt>.
  38. *
  39. * @author Kohsuke Kawaguchi
  40. */
  41. public class Service {
  42. /**
  43. * Poorman's clone of JDK6 ServiceLoader.
  44. */
  45. public static <T> List<T> loadInstances(ClassLoader classLoader, Class<T> type) throws IOException {
  46. List<T> result = new ArrayList<T>();
  47. final Enumeration<URL> e = classLoader.getResources("META-INF/services/"+type.getName());
  48. while (e.hasMoreElements()) {
  49. URL url = e.nextElement();
  50. BufferedReader configFile = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8"));
  51. String line;
  52. while ((line = configFile.readLine()) != null) {
  53. line = line.trim();
  54. if (line.startsWith("#") || line.length()==0) continue;
  55. try {
  56. Class<?> t = classLoader.loadClass(line);
  57. if (!type.isAssignableFrom(t)) continue; // invalid type
  58. result.add(type.cast(t.newInstance()));
  59. } catch (ClassNotFoundException x) {
  60. LOGGER.log(WARNING,"Failed to load "+line,x);
  61. } catch (InstantiationException x) {
  62. LOGGER.log(WARNING,"Failed to load "+line,x);
  63. } catch (IllegalAccessException x) {
  64. LOGGER.log(WARNING,"Failed to load "+line,x);
  65. }
  66. }
  67. }
  68. return result;
  69. }
  70. /**
  71. * Look up <tt>META-INF/service/<i>SPICLASSNAME</i></tt> from the classloader
  72. * and all the discovered classes into the given collection.
  73. */
  74. public static <T> void load(Class<T> spi, ClassLoader cl, Collection<Class<? extends T>> result) {
  75. try {
  76. Enumeration<URL> e = cl.getResources("META-INF/services/" + spi.getName());
  77. while(e.hasMoreElements()) {
  78. BufferedReader r = null;
  79. URL url = e.nextElement();
  80. try {
  81. r = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8"));
  82. String line;
  83. while((line=r.readLine())!=null) {
  84. if(line.startsWith("#"))
  85. continue; // comment line
  86. line = line.trim();
  87. if(line.length()==0)
  88. continue; // empty line. ignore.
  89. try {
  90. result.add(cl.loadClass(line).asSubclass(spi));
  91. } catch (ClassNotFoundException x) {
  92. LOGGER.log(Level.WARNING, "Failed to load "+line, x);
  93. }
  94. }
  95. } catch (IOException x) {
  96. LOGGER.log(Level.WARNING, "Failed to load "+url, x);
  97. } finally {
  98. r.close();
  99. }
  100. }
  101. } catch (IOException x) {
  102. LOGGER.log(Level.WARNING, "Failed to look up service providers for "+spi, x);
  103. }
  104. }
  105. private static final Logger LOGGER = Logger.getLogger(Service.class.getName());
  106. }