|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ServiceFinder.java | 75% | 96,9% | 100% | 90,2% |
|
1 | /* | |
2 | * Copyright 2005 [ini4j] Development Team | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | package org.ini4j; | |
18 | ||
19 | import java.io.BufferedReader; | |
20 | import java.io.InputStream; | |
21 | import java.io.InputStreamReader; | |
22 | ||
23 | /** | |
24 | * JDK JAR Services API alapú service kereső osztály. | |
25 | * | |
26 | * @author Szkiba Iván | |
27 | * @version $Name: v0_2_6 $ | |
28 | */ | |
29 | class ServiceFinder | |
30 | { | |
31 | /** | |
32 | * Service osztály nevének keresése | |
33 | * | |
34 | * a JDK JAR specifikációban definiált <B>Services API</B>-nak | |
35 | * megfelelően service osztály keresés.</p><p> | |
36 | * Az implementáló osztály név keresése a <CODE>serviceId</CODE> nevű | |
37 | * system property vizsgálatával kezdődik. Amennyiben nincs ilyen | |
38 | * property, úgy a keresés a | |
39 | * <CODE>/META-INF/services/<I>serviceId</I></CODE> nevű file tartalmával | |
40 | * folytatódik. Amennyiben nincs ilyen nevű file, úgy a paraméterként átadott | |
41 | * <CODE>defaultService</CODE> lesz az osztály neve.</p><p> | |
42 | * @param serviceId keresett osztály/service neve | |
43 | * @param defaultService alapértelmezett implementáló osztály neve | |
44 | * @throws IllegalArgumentException keresési vagy példányosítási hiba esetén | |
45 | * @return a keresett osztály neve | |
46 | */ | |
47 | 41 | protected static String findServiceClassName(String serviceId, String defaultService) |
48 | throws IllegalArgumentException | |
49 | { | |
50 | 41 | if (defaultService == null) |
51 | { | |
52 | 1 | throw new IllegalArgumentException("Provider for " + serviceId + " cannot be found"); |
53 | } | |
54 | ||
55 | 40 | ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); |
56 | 40 | String serviceClassName = null; |
57 | ||
58 | // Use the system property first | |
59 | 40 | try |
60 | { | |
61 | 40 | String systemProp = System.getProperty(serviceId); |
62 | ||
63 | 40 | if (systemProp != null) |
64 | { | |
65 | 9 | serviceClassName = systemProp; |
66 | } | |
67 | } | |
68 | catch (SecurityException x) | |
69 | { | |
70 | ; | |
71 | } | |
72 | ||
73 | 40 | if (serviceClassName == null) |
74 | { | |
75 | 31 | String servicePath = "META-INF/services/" + serviceId; |
76 | ||
77 | // try to find services in CLASSPATH | |
78 | 31 | try |
79 | { | |
80 | 31 | InputStream is = null; |
81 | ||
82 | 31 | if (classLoader == null) |
83 | { | |
84 | 0 | is = ClassLoader.getSystemResourceAsStream(servicePath); |
85 | } | |
86 | else | |
87 | { | |
88 | 31 | is = classLoader.getResourceAsStream(servicePath); |
89 | } | |
90 | ||
91 | 31 | if (is != null) |
92 | { | |
93 | 3 | BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8")); |
94 | ||
95 | 3 | String line = rd.readLine(); |
96 | 3 | rd.close(); |
97 | ||
98 | ? | if ((line != null) && !"".equals(line = line.trim()) ) |
99 | { | |
100 | 1 | serviceClassName = line.split("\\s|#")[0]; |
101 | } | |
102 | } | |
103 | } | |
104 | catch (Exception x) | |
105 | { | |
106 | ; | |
107 | } | |
108 | } | |
109 | ||
110 | 40 | if (serviceClassName == null) |
111 | { | |
112 | 30 | serviceClassName = defaultService; |
113 | } | |
114 | ||
115 | 40 | return serviceClassName; |
116 | } | |
117 | ||
118 | /** | |
119 | * Service osztály keresés | |
120 | * | |
121 | * a JDK JAR specifikációban definiált <B>Services API</B>-nak | |
122 | * megfelelően service osztály keresés.</p><p> | |
123 | * Az implementáló osztály név keresése a <CODE>serviceId</CODE> nevű | |
124 | * system property vizsgálatával kezdődik. Amennyiben nincs ilyen | |
125 | * property, úgy a keresés a | |
126 | * <CODE>/META-INF/services/<I>serviceId</I></CODE> nevű file tartalmával | |
127 | * folytatódik. Amennyiben nincs ilyen nevű file, úgy a paraméterként átadott | |
128 | * <CODE>defaultService</CODE> lesz az osztály neve.</p><p> | |
129 | * @param serviceId keresett osztály/service neve | |
130 | * @param defaultService alapértelmezett implementáló osztály neve | |
131 | * @throws IllegalArgumentException keresési vagy példányosítási hiba esetén | |
132 | * @return a keresett osztály objektum | |
133 | */ | |
134 | 35 | protected static Class findServiceClass(String serviceId, String defaultService) |
135 | throws IllegalArgumentException | |
136 | { | |
137 | 35 | ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); |
138 | 35 | String serviceClassName = findServiceClassName(serviceId, defaultService); |
139 | ||
140 | 35 | try |
141 | { | |
142 | 35 | return (classLoader == null) ? Class.forName(serviceClassName) : classLoader.loadClass(serviceClassName); |
143 | } | |
144 | catch (ClassNotFoundException x) | |
145 | { | |
146 | 1 | throw (IllegalArgumentException) new IllegalArgumentException("Provider " + serviceClassName + " not found").initCause(x); |
147 | } | |
148 | } | |
149 | ||
150 | /** | |
151 | * Service objektum keresés és példányosítás | |
152 | * | |
153 | * a JDK JAR specifikációban definiált <B>Services API</B>-nak | |
154 | * megfelelően service osztály keresés, majd pedig példány képzés a context | |
155 | * ClassLoader segítségével.</p><p> | |
156 | * Az implementáló osztály név keresése a <CODE>serviceId</CODE> nevű | |
157 | * system property vizsgálatával kezdődik. Amennyiben nincs ilyen | |
158 | * property, úgy a keresés a | |
159 | * <CODE>/META-INF/services/<I>serviceId</I></CODE> nevű file tartalmával | |
160 | * folytatódik. Amennyiben nincs ilyen nevű file, úgy a paraméterként átadott | |
161 | * <CODE>defaultService</CODE> lesz az osztály neve.</p><p> | |
162 | * A fenti keresést követően történik a példány képzés. A visszatérési | |
163 | * érték mindig egy valódi objektum, lévén minden hiba exception-t generál. | |
164 | * @param serviceId keresett osztály/service neve | |
165 | * @param defaultService alapértelmezett implementáló osztály neve | |
166 | * @throws IllegalArgumentException keresési vagy példányosítási hiba esetén | |
167 | * @return a keresett osztály implementáló objektum | |
168 | */ | |
169 | 34 | protected static Object findService(String serviceId, String defaultService) |
170 | throws IllegalArgumentException | |
171 | { | |
172 | 34 | try |
173 | { | |
174 | 34 | return findServiceClass(serviceId, defaultService).newInstance(); |
175 | } | |
176 | catch (Exception x) | |
177 | { | |
178 | 1 | throw (IllegalArgumentException) new IllegalArgumentException("Provider " + serviceId + " could not be instantiated: " + x).initCause(x); |
179 | } | |
180 | } | |
181 | } |
|