Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2010 Tristan Tarrant Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package net.dataforte.commons.resources;
 
 import java.net.URL;
 import java.util.List;
 
A utility class which scans the classpath looking for Service Provider configuration files following the standard Service Provider pattern specified in the JAR File Specification.

Author(s):
Tristan Tarrant
 
 public class ServiceFinder {
 	private static Logger log = LoggerFactory.getLogger(ServiceFinder.class);
 	private static final Pattern nonCommentPattern = Pattern.compile("^([^#]+)");


Scans the classpath for Service Provider files for the specified class. Returns a list of SPI instances. This method use the Thread's Context ClassLoader

Parameters:
<T>
clazz
Returns:
 
 	public static <T> List<Class<? extends T>> findServices(Class<T> clazz) {
 		return findServices(clazz, Thread.currentThread().getContextClassLoader());
 	}

Scans the classpath for Service Provider files for the specified class. Returns a list of SPI instances

Parameters:
<T>
clazz
classLoader the classloader to use
Returns:
 
 	public static <T> List<Class<? extends T>> findServices(Class<T> clazzClassLoader classLoader) {
 		List<Class<? extends T>> spis = new ArrayList<Class<? extends T>>();
 
 		Enumeration<?> resources;
 
 		try {
 			resources = classLoader.getResources("META-INF/services/" + clazz.getName());
 		} catch (IOException e) {
 			.error("Could not load service files for interface " + clazz.getName(), e);
 			return spis;
 		}
 
 		while (resources.hasMoreElements()) {
 			URL url = (URLresources.nextElement();
 			try {
 				String line;
 				while ((line = reader.readLine()) != null) {
 					line = line.trim();
 					if (m.find()) {
 						String spiClassName = m.group().trim();
 						try {
 							Class<?> spiClass = Class.forName(spiClassNametrueclassLoader);
 							// Try to instantiate the SPI to detect any missing classes / dependencies
 							spiClass.asSubclass(clazz).newInstance();
 							spis.add(spiClass.asSubclass(clazz));
 						} catch (NoClassDefFoundError ncdfe) {
 							.info("Could not instantiate SPI class " + spiClassName + " because of missing dependency " + ncdfe.getMessage());
 						} catch (ClassCastException cce) {
 							.warn("Skipping SPI class " + spiClassName + " as it does not implement interface " + clazz.getName());
 						} catch (ClassNotFoundException cnfe) {
 							.warn("Could not find SPI class " + spiClassName);
 						} catch (InstantiationException e) {
 							.warn("Could not instantiate SPI class " + spiClassName);
catch (IllegalAccessException e) {
							.warn("Could not instantiate SPI class " + spiClassName);
						}
					}
				}
				reader.close();
catch (IOException ioe) {
				.warn("Error while reading SPIs from " + url.toString(), ioe);
			}
		}
		return spis;
	}
New to GrepCode? Check out our FAQ X