001: /*
002:
003: ============================================================================
004: The Apache Software License, Version 1.1
005: ============================================================================
006:
007: Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
008:
009: Redistribution and use in source and binary forms, with or without modifica-
010: tion, are permitted provided that the following conditions are met:
011:
012: 1. Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014:
015: 2. Redistributions in binary form must reproduce the above copyright notice,
016: this list of conditions and the following disclaimer in the documentation
017: and/or other materials provided with the distribution.
018:
019: 3. The end-user documentation included with the redistribution, if any, must
020: include the following acknowledgment: "This product includes software
021: developed by the Apache Software Foundation (http://www.apache.org/)."
022: Alternately, this acknowledgment may appear in the software itself, if
023: and wherever such third-party acknowledgments normally appear.
024:
025: 4. The names "Batik" and "Apache Software Foundation" must not be
026: used to endorse or promote products derived from this software without
027: prior written permission. For written permission, please contact
028: apache@apache.org.
029:
030: 5. Products derived from this software may not be called "Apache", nor may
031: "Apache" appear in their name, without prior written permission of the
032: Apache Software Foundation.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
035: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
036: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
037: APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
038: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
039: DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
040: OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
041: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
043: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: This software consists of voluntary contributions made by many individuals
046: on behalf of the Apache Software Foundation. For more information on the
047: Apache Software Foundation, please see <http://www.apache.org/>.
048:
049: */
050:
051: package org.apache.batik.util;
052:
053: import java.io.BufferedReader;
054: import java.io.IOException;
055: import java.io.InputStream;
056: import java.io.InputStreamReader;
057: import java.io.Reader;
058: import java.net.URL;
059: import java.util.Enumeration;
060: import java.util.HashMap;
061: import java.util.Iterator;
062: import java.util.Vector;
063:
064: /**
065: * This class handles looking up service providers on the class path.
066: * it implements the system described in:
067: *
068: * <a href='http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#Service Provider'> JAR
069: * File Specification Under Service Provider</a>. Note that this
070: * interface is very similar to the one they describe which seems to
071: * be missing in the JDK.
072: *
073: * @author <a href="mailto:Thomas.DeWeeese@Kodak.com">Thomas DeWeese</a>
074: * @version $Id$ */
075: public class Service {
076:
077: // Remember providers we have looked up before.
078: static HashMap providerMap = new HashMap();
079:
080: /**
081: * Returns an iterator where each element should implement the
082: * interface (or subclass the baseclass) described by cls. The
083: * Classes are found by searching the classpath for service files
084: * named: 'META-INF/services/<fully qualified classname> that list
085: * fully qualifted classnames of classes that implement the
086: * service files classes interface. These classes must have
087: * default constructors.
088: *
089: * @param cls The class/interface to search for providers of.
090: */
091: public static synchronized Iterator providers(Class cls) {
092: ClassLoader cl = null;
093: try {
094: cl = cls.getClassLoader();
095: } catch (SecurityException se) {
096: // Ooops! can't get his class loader.
097: }
098: // Can always request your own class loader. But it might be 'null'.
099: if (cl == null)
100: cl = Service.class.getClassLoader();
101:
102: String serviceFile = "META-INF/services/" + cls.getName();
103:
104: // System.out.println("File: " + serviceFile);
105:
106: Vector v = (Vector) providerMap.get(serviceFile);
107: if (v != null)
108: return v.iterator();
109:
110: v = new Vector();
111: providerMap.put(serviceFile, v);
112: // No class loader so we can't find 'serviceFile'.
113: if (cl == null)
114: return v.iterator();
115:
116: Enumeration e;
117: try {
118: e = cl.getResources(serviceFile);
119: } catch (IOException ioe) {
120: return v.iterator();
121: }
122:
123: while (e.hasMoreElements()) {
124: try {
125: URL u = (URL) e.nextElement();
126: // System.out.println("URL: " + u);
127:
128: InputStream is = u.openStream();
129: Reader r = new InputStreamReader(is, "UTF-8");
130: BufferedReader br = new BufferedReader(r);
131:
132: String line = br.readLine();
133: while (line != null) {
134: try {
135: // First strip any comment...
136: int idx = line.indexOf('#');
137: if (idx != -1)
138: line = line.substring(0, idx);
139:
140: // Trim whitespace.
141: line = line.trim();
142:
143: // If nothing left then loop around...
144: if (line.length() == 0) {
145: line = br.readLine();
146: continue;
147: }
148: // System.out.println("Line: " + line);
149:
150: // Try and load the class
151: Object obj = cl.loadClass(line).newInstance();
152: // stick it into our vector...
153: v.add(obj);
154: } catch (Exception ex) {
155: // Just try the next line
156: }
157: line = br.readLine();
158: }
159: } catch (Exception ex) {
160: // Just try the next file...
161: }
162: }
163: return v.iterator();
164: }
165: }
|