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 | ****************************************************************/ |
---|
19 | package org.apache.james.mime4j.dom; |
---|
20 | |
---|
21 | import java.io.BufferedReader; |
---|
22 | import java.io.IOException; |
---|
23 | import java.io.InputStream; |
---|
24 | import java.io.InputStreamReader; |
---|
25 | import java.net.URL; |
---|
26 | import 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 | */ |
---|
33 | class 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 | } |
---|