File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\Service.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.net.URL;
30 import java.util.Collection;
31 import java.util.Enumeration;
32 import java.util.List;
33 import java.util.ArrayList;
34 import java.util.logging.Level;
35 import java.util.logging.Logger;
36 import static java.util.logging.Level.WARNING;
37
38 /**
39 * Load classes by looking up <tt>META-INF/services</tt>.
40 *
41 * @author Kohsuke Kawaguchi
42 */
43 public class Service {
44 /**
45 * Poorman's clone of JDK6 ServiceLoader.
46 */
47 public static <T> List<T> loadInstances(ClassLoader classLoader, Class<T> type) throws IOException {
48 List<T> result = new ArrayList<T>();
49
50 final Enumeration<URL> e = classLoader.getResources("META-INF/services/"+type.getName());
51 while (e.hasMoreElements()) {
52 URL url = e.nextElement();
53 BufferedReader configFile = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8"));
54 try {
55 String line;
56 while ((line = configFile.readLine()) != null) {
57 line = line.trim();
58 if (line.startsWith("#") || line.length()==0) continue;
59
60 try {
61 Class<?> t = classLoader.loadClass(line);
62 if (!type.isAssignableFrom(t)) continue; // invalid type
63
64 result.add(type.cast(t.newInstance()));
65 } catch (ClassNotFoundException x) {
66 LOGGER.log(WARNING,"Failed to load "+line,x);
67 } catch (InstantiationException x) {
68 LOGGER.log(WARNING,"Failed to load "+line,x);
69 } catch (IllegalAccessException x) {
70 LOGGER.log(WARNING,"Failed to load "+line,x);
71 }
72 }
73 } finally {
74 configFile.close();
75 }
76 }
77
78 return result;
79 }
80
81 /**
82 * Look up <tt>META-INF/service/<i>SPICLASSNAME</i></tt> from the classloader
83 * and all the discovered classes into the given collection.
84 */
85 public static <T> void load(Class<T> spi, ClassLoader cl, Collection<Class<? extends T>> result) {
86 try {
87 Enumeration<URL> e = cl.getResources("META-INF/services/" + spi.getName());
88 while(e.hasMoreElements()) {
89 BufferedReader r = null;
90 URL url = e.nextElement();
91 try {
92 r = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8"));
93 String line;
94 while((line=r.readLine())!=null) {
95 if(line.startsWith("#"))
96 continue; // comment line
97 line = line.trim();
98 if(line.length()==0)
99 continue; // empty line. ignore.
100
101 try {
102 result.add(cl.loadClass(line).asSubclass(spi));
103 } catch (ClassNotFoundException x) {
104 LOGGER.log(Level.WARNING, "Failed to load "+line, x);
105 }
106 }
107 } catch (IOException x) {
108 LOGGER.log(Level.WARNING, "Failed to load "+url, x);
109 } finally {
110 r.close();
111 }
112 }
113 } catch (IOException x) {
114 LOGGER.log(Level.WARNING, "Failed to look up service providers for "+spi, x);
115 }
116 }
117
118 private static final Logger LOGGER = Logger.getLogger(Service.class.getName());
119 }
120