~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/util/TGServiceReader.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.util;
 
2
 
 
3
import java.io.BufferedReader;
 
4
import java.io.IOException;
 
5
import java.io.InputStreamReader;
 
6
import java.io.UnsupportedEncodingException;
 
7
import java.net.URL;
 
8
import java.util.ArrayList;
 
9
import java.util.Collections;
 
10
import java.util.Enumeration;
 
11
import java.util.Iterator;
 
12
import java.util.List;
 
13
import java.util.NoSuchElementException;
 
14
 
 
15
public class TGServiceReader {
 
16
        
 
17
        private static final String SERVICE_PATH = new String("META-INF/services/");
 
18
        
 
19
        public static Iterator lookupProviders(Class spi){
 
20
                return TGServiceReader.lookupProviders(spi,TGClassLoader.instance().getClassLoader());
 
21
        }
 
22
        
 
23
        public static Iterator lookupProviders(Class spi,ClassLoader loader){
 
24
                try{
 
25
                        if (spi == null || loader == null){
 
26
                                throw new IllegalArgumentException();
 
27
                        }
 
28
                        return new IteratorImpl(spi,loader,loader.getResources(SERVICE_PATH + spi.getName()));
 
29
                }catch (IOException ioex){
 
30
                        return Collections.EMPTY_LIST.iterator();
 
31
                }
 
32
        }
 
33
        
 
34
        private static final class IteratorImpl implements Iterator{
 
35
                private Class spi;
 
36
                private ClassLoader loader;
 
37
                private Enumeration urls;
 
38
                private Iterator iterator;
 
39
                
 
40
                public IteratorImpl(Class spi,ClassLoader loader,Enumeration urls){
 
41
                        this.spi = spi;
 
42
                        this.loader = loader;
 
43
                        this.urls = urls;
 
44
                        this.initialize();
 
45
                }
 
46
                
 
47
                private void initialize(){
 
48
                        List providers = new ArrayList();
 
49
                        while (this.urls.hasMoreElements()) {
 
50
                                URL url = (URL) this.urls.nextElement();
 
51
                                try {
 
52
                                        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
 
53
                                        String line = null;
 
54
                                        while((line = reader.readLine()) != null){
 
55
                                                String provider = uncommentLine(line).trim();
 
56
                                                if(provider != null && provider.length() > 0){
 
57
                                                        providers.add(provider);
 
58
                                                }
 
59
                                        }
 
60
                                } catch (UnsupportedEncodingException e) {
 
61
                                        e.printStackTrace();
 
62
                                } catch (IOException e) {
 
63
                                        e.printStackTrace();
 
64
                                }
 
65
                        }
 
66
                        this.iterator = providers.iterator();
 
67
                }
 
68
                
 
69
                private String uncommentLine(String line){
 
70
                        int index = line.indexOf('#');
 
71
                        if(index >= 0){
 
72
                                return (line.substring(0,index));
 
73
                        }
 
74
                        return line;
 
75
                }
 
76
                
 
77
                public boolean hasNext() {
 
78
                        return (this.iterator != null && this.iterator.hasNext());
 
79
                }
 
80
                
 
81
                public Object next() {
 
82
                        if (!hasNext()){
 
83
                                throw new NoSuchElementException();
 
84
                        }
 
85
                        try {
 
86
                                Object provider = this.loader.loadClass( (String)this.iterator.next() ).newInstance();
 
87
                                if(this.spi.isInstance(provider)){
 
88
                                        return provider;
 
89
                                }
 
90
                        } catch (Throwable throwable) {
 
91
                                throwable.printStackTrace();
 
92
                        }
 
93
                        throw new NoSuchElementException();
 
94
                }
 
95
                
 
96
                public void remove() {
 
97
                        throw new UnsupportedOperationException();
 
98
                }
 
99
        }
 
100
}