You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Service.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 1999-2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.util;
  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.io.Reader;
  23. import java.util.Enumeration;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.Map;
  27. //code stolen from org.apache.batik.util and modified slightly
  28. //does what sun.misc.Service probably does, but it cannot be relied on.
  29. //hopefully will be part of standard jdk sometime.
  30. /**
  31. * This class loads services present in the class path.
  32. */
  33. public class Service {
  34. private static Map providerMap = new java.util.Hashtable();
  35. public static synchronized Iterator providers(Class cls) {
  36. ClassLoader cl = cls.getClassLoader();
  37. // null if loaded by bootstrap class loader
  38. if (cl == null) {
  39. cl = ClassLoader.getSystemClassLoader();
  40. }
  41. String serviceFile = "META-INF/services/" + cls.getName();
  42. // log.debug("File: " + serviceFile);
  43. List lst = (List)providerMap.get(serviceFile);
  44. if (lst != null) {
  45. return lst.iterator();
  46. }
  47. lst = new java.util.Vector();
  48. providerMap.put(serviceFile, lst);
  49. Enumeration e;
  50. try {
  51. e = cl.getResources(serviceFile);
  52. } catch (IOException ioe) {
  53. return lst.iterator();
  54. }
  55. while (e.hasMoreElements()) {
  56. try {
  57. java.net.URL u = (java.net.URL)e.nextElement();
  58. //log.debug("URL: " + u);
  59. InputStream is = u.openStream();
  60. Reader r = new InputStreamReader(is, "UTF-8");
  61. BufferedReader br = new BufferedReader(r);
  62. String line = br.readLine();
  63. while (line != null) {
  64. try {
  65. // First strip any comment...
  66. int idx = line.indexOf('#');
  67. if (idx != -1) {
  68. line = line.substring(0, idx);
  69. }
  70. // Trim whitespace.
  71. line = line.trim();
  72. // If nothing left then loop around...
  73. if (line.length() == 0) {
  74. line = br.readLine();
  75. continue;
  76. }
  77. // log.debug("Line: " + line);
  78. // Try and load the class
  79. // Object obj = cl.loadClass(line).newInstance();
  80. // stick it into our vector...
  81. lst.add(line);
  82. } catch (Exception ex) {
  83. // Just try the next line
  84. }
  85. line = br.readLine();
  86. }
  87. } catch (Exception ex) {
  88. // Just try the next file...
  89. }
  90. }
  91. return lst.iterator();
  92. }
  93. }