KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > util > Service


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.util;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 /**
33  * This class handles looking up service providers on the class path.
34  * it implements the system described in:
35  *
36  * <a HREF='http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#Service Provider'> JAR
37  * File Specification Under Service Provider</a>. Note that this
38  * interface is very similar to the one they describe which seems to
39  * be missing in the JDK.
40  *
41  * @author <a HREF="mailto:Thomas.DeWeeese@Kodak.com">Thomas DeWeese</a>
42  * @version $Id: Service.java,v 1.7 2004/08/27 00:42:07 deweese Exp $ */

43 public class Service {
44
45     // Remember providers we have looked up before.
46
static HashMap JavaDoc providerMap = new HashMap JavaDoc();
47
48     /**
49      * Returns an iterator where each element should implement the
50      * interface (or subclass the baseclass) described by cls. The
51      * Classes are found by searching the classpath for service files
52      * named: 'META-INF/services/<fully qualified classname> that list
53      * fully qualifted classnames of classes that implement the
54      * service files classes interface. These classes must have
55      * default constructors.
56      *
57      * @param cls The class/interface to search for providers of.
58      */

59     public static synchronized Iterator JavaDoc providers(Class JavaDoc cls) {
60         String JavaDoc serviceFile = "META-INF/services/"+cls.getName();
61
62         // System.out.println("File: " + serviceFile);
63

64         List JavaDoc l = (List JavaDoc)providerMap.get(serviceFile);
65         if (l != null)
66             return l.iterator();
67
68         l = new ArrayList JavaDoc();
69         providerMap.put(serviceFile, l);
70
71         ClassLoader JavaDoc cl = null;
72         try {
73             cl = cls.getClassLoader();
74         } catch (SecurityException JavaDoc se) {
75             // Ooops! can't get his class loader.
76
}
77         // Can always request your own class loader. But it might be 'null'.
78
if (cl == null) cl = Service.class.getClassLoader();
79
80         // No class loader so we can't find 'serviceFile'.
81
if (cl == null) return l.iterator();
82
83         Enumeration JavaDoc e;
84         try {
85             e = cl.getResources(serviceFile);
86         } catch (IOException JavaDoc ioe) {
87             return l.iterator();
88         }
89
90         while (e.hasMoreElements()) {
91             try {
92                 URL JavaDoc u = (URL JavaDoc)e.nextElement();
93                 // System.out.println("URL: " + u);
94

95                 InputStream JavaDoc is = u.openStream();
96                 Reader JavaDoc r = new InputStreamReader JavaDoc(is, "UTF-8");
97                 BufferedReader JavaDoc br = new BufferedReader JavaDoc(r);
98
99                 String JavaDoc line = br.readLine();
100                 while (line != null) {
101                     try {
102                         // First strip any comment...
103
int idx = line.indexOf('#');
104                         if (idx != -1)
105                             line = line.substring(0, idx);
106
107                         // Trim whitespace.
108
line = line.trim();
109
110                         // If nothing left then loop around...
111
if (line.length() == 0) {
112                             line = br.readLine();
113                             continue;
114                         }
115                         // System.out.println("Line: " + line);
116

117                         // Try and load the class
118
Object JavaDoc obj = cl.loadClass(line).newInstance();
119                         // stick it into our vector...
120
l.add(obj);
121                     } catch (Exception JavaDoc ex) {
122                         // Just try the next line
123
}
124                     line = br.readLine();
125                 }
126             } catch (Exception JavaDoc ex) {
127                 // Just try the next file...
128
}
129         }
130         return l.iterator();
131     }
132 }
133
Popular Tags