1 package com.aragost.javahg.internals;
2
3 import com.google.common.io.Closeables;
4 import java.io.BufferedReader;
5 import java.io.InputStreamReader;
6 import java.net.URL;
7
8
9
10
11
12
13
14 public final class ServiceLoader
15 {
16
17 private static final String SERVICE_PREFIX = "META-INF/services/";
18
19
20
21
22
23
24
25
26
27
28
29 public static <S> S loadService( Class<S> serviceClass ) throws ServiceException {
30 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
31 if ( classLoader == null ){
32 classLoader = Utils.class.getClassLoader();
33 }
34 return loadService(classLoader, serviceClass);
35 }
36
37
38
39
40
41
42
43
44
45
46
47 public static <S> S loadService(ClassLoader classLoader, Class<S> serviceClass ) throws ServiceException{
48 S service = null;
49 String name = serviceClass.getName();
50 URL resource = classLoader.getResource(SERVICE_PREFIX.concat(name));
51 if ( resource != null ){
52 BufferedReader reader = null;
53 try {
54 reader = new BufferedReader(new InputStreamReader(resource.openStream(), "UTF-8"));
55 String line = reader.readLine();
56 while (line != null){
57 int comment = line.indexOf("#");
58 if ( comment >= 0 ){
59 line = line.substring(0, comment);
60 }
61 line = line.trim();
62 if ( line.length() > 0 ){
63 service = (S) Class.forName(line).newInstance();
64 break;
65 }
66 line = reader.readLine();
67 }
68 } catch ( Exception ex ){
69 throw new ServiceException(
70 "could not load service for class ".concat(name), ex);
71 } finally {
72 Closeables.closeQuietly(reader);
73 }
74 }
75 return service;
76 }
77 }