001: /*
002: * This file is part of "SnipSnap Radeox Rendering Engine".
003: *
004: * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
005: * All Rights Reserved.
006: *
007: * Please visit http://radeox.org/ for updates and contact.
008: *
009: * --LICENSE NOTICE--
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * --LICENSE NOTICE--
022: */
023:
024: package org.radeox.util;
025:
026: import java.io.BufferedReader;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.io.InputStreamReader;
030: import java.io.Reader;
031: import java.net.URL;
032: import java.util.ArrayList;
033: import java.util.Enumeration;
034: import java.util.HashMap;
035: import java.util.Iterator;
036: import java.util.List;
037:
038: /**
039: * After the Service class from Sun and the Apache project. With help from
040: * FrŽdŽric Miserey.
041: *
042: * @credits FrŽdŽric Miserey, Joseph Oettinger
043: * @author Matthias L. Jugel
044: * @version $id$
045: */
046: public class Service {
047:
048: static HashMap services = new HashMap();
049:
050: public static synchronized Iterator providerClasses(Class cls) {
051: return providers(cls, false);
052: }
053:
054: public static synchronized Iterator providers(Class cls) {
055: return providers(cls, true);
056: }
057:
058: public static synchronized Iterator providers(Class cls,
059: boolean instantiate) {
060: ClassLoader classLoader = cls.getClassLoader();
061: String providerFile = "META-INF/services/" + cls.getName();
062:
063: // check whether we already loaded the provider classes
064: List providers = (List) services.get(providerFile);
065: if (providers != null) {
066: return providers.iterator();
067: }
068:
069: // create new list of providers
070: providers = new ArrayList();
071: services.put(providerFile, providers);
072:
073: try {
074: Enumeration providerFiles = classLoader
075: .getResources(providerFile);
076:
077: if (providerFiles.hasMoreElements()) {
078: // cycle through the provider files and load classes
079: while (providerFiles.hasMoreElements()) {
080: try {
081: URL url = (URL) providerFiles.nextElement();
082: Reader reader = new InputStreamReader(url
083: .openStream(), "UTF-8");
084: if (instantiate) {
085: loadResource(reader, classLoader, providers);
086: } else {
087: loadClasses(reader, classLoader, providers);
088: }
089: } catch (Exception ex) {
090: // ex.printStackTrace();
091: // Just try the next file...
092: }
093: }
094: } else {
095: // Workaround for broken classloaders, e.g. Orion
096: InputStream is = classLoader
097: .getResourceAsStream(providerFile);
098: if (is == null) {
099: providerFile = providerFile.substring(providerFile
100: .lastIndexOf('.') + 1);
101: is = classLoader.getResourceAsStream(providerFile);
102: }
103: if (is != null) {
104: Reader reader = new InputStreamReader(is, "UTF-8");
105: loadResource(reader, classLoader, providers);
106: }
107: }
108: } catch (IOException ioe) {
109: // ioe.printStackTrace();
110: // ignore exception
111: }
112: return providers.iterator();
113: }
114:
115: private static List loadClasses(Reader input,
116: ClassLoader classLoader, List classes) throws IOException {
117: BufferedReader reader = new BufferedReader(input);
118:
119: String line = reader.readLine();
120: while (line != null) {
121: try {
122: // First strip any comment...
123: int idx = line.indexOf('#');
124: if (idx != -1) {
125: line = line.substring(0, idx);
126: }
127:
128: // Trim whitespace.
129: line = line.trim();
130:
131: // load class if a line was left
132: if (line.length() > 0) {
133: // Try and load the class
134: classes.add(classLoader.loadClass(line));
135: }
136: } catch (Exception ex) {
137: // ex.printStackTrace();
138: // Just try the next line
139: }
140: line = reader.readLine();
141: }
142: return classes;
143: }
144:
145: private static void loadResource(Reader input,
146: ClassLoader classLoader, List providers) throws IOException {
147: List classes = new ArrayList();
148: loadClasses(input, classLoader, classes);
149: Iterator iterator = classes.iterator();
150: while (iterator.hasNext()) {
151: Class klass = (Class) iterator.next();
152: try {
153: Object obj = klass.newInstance();
154: // stick it into our vector...
155: providers.add(obj);
156: } catch (InstantiationException e) {
157: e.printStackTrace();
158: } catch (IllegalAccessException e) {
159: e.printStackTrace();
160: }
161: // Logger.debug("Service: loaded "+ obj.getClass().getName());
162: }
163: }
164: }
|