|
1 |
| package com.agical.rmock.core.find.iterator; |
|
2 |
| |
|
3 |
| import java.io.BufferedReader; |
|
4 |
| import java.io.IOException; |
|
5 |
| import java.io.InputStream; |
|
6 |
| import java.io.InputStreamReader; |
|
7 |
| import java.net.URL; |
|
8 |
| import java.util.Enumeration; |
|
9 |
| import java.util.Iterator; |
|
10 |
| import java.util.LinkedList; |
|
11 |
| import java.util.List; |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| public class Service { |
|
19 |
| public static final String SERVICE_LOCATION = "META-INF/services/"; |
|
20 |
| |
|
21 |
15
| public Iterator providers(Class clazz) {
|
|
22 |
15
| String resourceName = SERVICE_LOCATION + clazz.getName();
|
|
23 |
15
| List objects = new LinkedList();
|
|
24 |
15
| URL url = null;
|
|
25 |
15
| Enumeration enumeration = null;
|
|
26 |
15
| String tmp = null;
|
|
27 |
15
| String className = null;
|
|
28 |
15
| try {
|
|
29 |
15
| enumeration = this.getClass().getClassLoader().getResources( resourceName );
|
|
30 |
15
| while( enumeration.hasMoreElements() ) {
|
|
31 |
23
| url = (URL) enumeration.nextElement();
|
|
32 |
23
| InputStream inputStream = url.openStream();
|
|
33 |
23
| BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( inputStream ) );
|
|
34 |
?
| while( (tmp = bufferedReader.readLine() ) != null ) {
|
|
35 |
59
| int commentIndex = tmp.indexOf( '#' );
|
|
36 |
59
| commentIndex = commentIndex==-1 ? tmp.length() : commentIndex;
|
|
37 |
59
| className = tmp.substring( 0, commentIndex ).trim();
|
|
38 |
59
| if( className.length() == 0 ) {
|
|
39 |
9
| continue;
|
|
40 |
| } |
|
41 |
50
| Class tmpClass = Class.forName( className );
|
|
42 |
50
| objects.add( tmpClass.newInstance() );
|
|
43 |
| } |
|
44 |
| } |
|
45 |
| } catch (IOException e) { |
|
46 |
0
| throw new ServiceException( clazz, className, url, e );
|
|
47 |
| } catch (ClassNotFoundException e) { |
|
48 |
0
| throw new ServiceException( clazz, className, url, e );
|
|
49 |
| } catch (InstantiationException e) { |
|
50 |
0
| throw new ServiceException( clazz, className, url, e );
|
|
51 |
| } catch (IllegalAccessException e) { |
|
52 |
0
| throw new ServiceException( clazz, className, url, e );
|
|
53 |
| } |
|
54 |
15
| return objects.iterator();
|
|
55 |
| } |
|
56 |
| |
|
57 |
| |
|
58 |
| } |