source: contrib/MailArchiver/sources/vendor/mime4j/apache-mime4j-0.7-SNAPSHOT-20110327.010440-17/dom/src/main/java/org/apache/james/mime4j/dom/ServiceLoader.java @ 6785

Revision 6785, 3.7 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
1/****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one   *
3 * or more contributor license agreements.  See the NOTICE file *
4 * distributed with this work for additional information        *
5 * regarding copyright ownership.  The ASF licenses this file   *
6 * to you under the Apache License, Version 2.0 (the            *
7 * "License"); you may not use this file except in compliance   *
8 * with the License.  You may obtain a copy of the License at   *
9 *                                                              *
10 *   http://www.apache.org/licenses/LICENSE-2.0                 *
11 *                                                              *
12 * Unless required by applicable law or agreed to in writing,   *
13 * software distributed under the License is distributed on an  *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15 * KIND, either express or implied.  See the License for the    *
16 * specific language governing permissions and limitations      *
17 * under the License.                                           *
18 ****************************************************************/
19package org.apache.james.mime4j.dom;
20
21import java.io.BufferedReader;
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.InputStreamReader;
25import java.net.URL;
26import java.util.Enumeration;
27
28/**
29 * Utility class to load Service Providers (SPI).
30 * This will deprecated as soon as mime4j will be upgraded to Java6
31 * as Java6 has javax.util.ServiceLoader as a core class.
32 */
33class ServiceLoader {
34
35    private ServiceLoader() {
36    }
37
38    /**
39     * Loads a Service Provider for the given interface/class (SPI).
40     */
41    static <T> T load(Class<T> spiClass) {
42        String spiResURI = "META-INF/services/" + spiClass.getName();
43        ClassLoader classLoader = spiClass.getClassLoader();
44        try {
45             Enumeration<URL> resources = classLoader.getResources(spiResURI);
46             while (resources.hasMoreElements()) {
47                 URL resource = resources.nextElement();
48                 InputStream instream = resource.openStream();
49                 try {
50                     BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
51                     String line;
52                     while ((line = reader.readLine()) != null) {
53                        line = line.trim();
54                         int cmtIdx = line.indexOf('#');
55                         if (cmtIdx != -1) {
56                             line = line.substring(0, cmtIdx);
57                             line = line.trim();
58                         }
59
60                         if (line.length() == 0) {
61                             continue;
62                         }
63
64                         Class<?> implClass = classLoader.loadClass(line);
65                        if (spiClass.isAssignableFrom(implClass)) {
66                            Object impl = implClass.newInstance();
67                            return spiClass.cast(impl);
68                        }
69                    }
70                     reader.close();
71                 } finally {
72                     instream.close();
73                }
74            }
75             return null;
76         } catch (IOException ex) {
77             throw new ServiceLoaderException(ex);
78         } catch (ClassNotFoundException ex) {
79             throw new ServiceLoaderException("Unknown SPI class '"
80                     + spiClass.getName() + "'", ex);
81         } catch (IllegalAccessException ex) {
82             // Not visible
83             return null;
84         } catch (InstantiationException ex) {
85             throw new ServiceLoaderException("SPI class '"
86                     + spiClass.getName() + "' cannot be instantiated", ex);
87        }
88    }
89
90}
Note: See TracBrowser for help on using the repository browser.