未验证 提交 a82a47a8 编写于 作者: J Jesse Glick 提交者: GitHub

Merge pull request #3191 from jglick/ServiceLoader

Deprecating Service in favor of ServiceLoader
...@@ -120,7 +120,6 @@ import java.util.HashSet; ...@@ -120,7 +120,6 @@ import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.ListIterator;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
...@@ -144,6 +143,7 @@ import hudson.util.FormValidation; ...@@ -144,6 +143,7 @@ import hudson.util.FormValidation;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.net.JarURLConnection; import java.net.JarURLConnection;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.ServiceLoader;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import static java.util.logging.Level.FINE; import static java.util.logging.Level.FINE;
...@@ -1195,7 +1195,9 @@ public abstract class PluginManager extends AbstractModelObject implements OnMas ...@@ -1195,7 +1195,9 @@ public abstract class PluginManager extends AbstractModelObject implements OnMas
/** /**
* Discover all the service provider implementations of the given class, * Discover all the service provider implementations of the given class,
* via <tt>META-INF/services</tt>. * via <tt>META-INF/services</tt>.
* @deprecated Use {@link ServiceLoader} instead, or (more commonly) {@link ExtensionList}.
*/ */
@Deprecated
public <T> Collection<Class<? extends T>> discover( Class<T> spi ) { public <T> Collection<Class<? extends T>> discover( Class<T> spi ) {
Set<Class<? extends T>> result = new HashSet<Class<? extends T>>(); Set<Class<? extends T>> result = new HashSet<Class<? extends T>>();
......
...@@ -17,7 +17,8 @@ import hudson.PluginManager; ...@@ -17,7 +17,8 @@ import hudson.PluginManager;
import jenkins.util.SystemProperties; import jenkins.util.SystemProperties;
import hudson.util.DirScanner; import hudson.util.DirScanner;
import hudson.util.FileVisitor; import hudson.util.FileVisitor;
import hudson.util.Service; import java.util.Iterator;
import java.util.ServiceLoader;
/** /**
* Strategy pattern of the various key decision making during the Jenkins initialization. * Strategy pattern of the various key decision making during the Jenkins initialization.
...@@ -113,11 +114,12 @@ public class InitStrategy { ...@@ -113,11 +114,12 @@ public class InitStrategy {
* Obtains the instance to be used. * Obtains the instance to be used.
*/ */
public static InitStrategy get(ClassLoader cl) throws IOException { public static InitStrategy get(ClassLoader cl) throws IOException {
List<InitStrategy> r = Service.loadInstances(cl, InitStrategy.class); Iterator<InitStrategy> it = ServiceLoader.load(InitStrategy.class, cl).iterator();
if (r.isEmpty()) return new InitStrategy(); // default if (!it.hasNext()) {
return new InitStrategy(); // default
InitStrategy s = r.get(0); }
LOGGER.fine("Using "+s+" as InitStrategy"); InitStrategy s = it.next();
LOGGER.log(Level.FINE, "Using {0} as InitStrategy", s);
return s; return s;
} }
......
...@@ -31,6 +31,7 @@ import java.util.Collection; ...@@ -31,6 +31,7 @@ import java.util.Collection;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.ServiceLoader;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
...@@ -39,11 +40,10 @@ import static java.util.logging.Level.WARNING; ...@@ -39,11 +40,10 @@ import static java.util.logging.Level.WARNING;
* Load classes by looking up <tt>META-INF/services</tt>. * Load classes by looking up <tt>META-INF/services</tt>.
* *
* @author Kohsuke Kawaguchi * @author Kohsuke Kawaguchi
* @deprecated use {@link ServiceLoader} instead.
*/ */
@Deprecated
public class Service { public class Service {
/**
* Poorman's clone of JDK6 ServiceLoader.
*/
public static <T> List<T> loadInstances(ClassLoader classLoader, Class<T> type) throws IOException { public static <T> List<T> loadInstances(ClassLoader classLoader, Class<T> type) throws IOException {
List<T> result = new ArrayList<T>(); List<T> result = new ArrayList<T>();
......
package jenkins; package jenkins;
import com.google.common.collect.Lists;
import jenkins.util.SystemProperties; import jenkins.util.SystemProperties;
import hudson.init.InitMilestone; import hudson.init.InitMilestone;
import hudson.init.InitReactorListener; import hudson.init.InitReactorListener;
import hudson.util.DaemonThreadFactory; import hudson.util.DaemonThreadFactory;
import hudson.util.NamingThreadFactory; import hudson.util.NamingThreadFactory;
import hudson.util.Service;
import jenkins.model.Configuration; import jenkins.model.Configuration;
import jenkins.model.Jenkins; import jenkins.model.Jenkins;
import org.jvnet.hudson.reactor.Milestone; import org.jvnet.hudson.reactor.Milestone;
...@@ -16,6 +16,7 @@ import org.jvnet.hudson.reactor.Task; ...@@ -16,6 +16,7 @@ import org.jvnet.hudson.reactor.Task;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.ServiceLoader;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
...@@ -59,7 +60,7 @@ public class InitReactorRunner { ...@@ -59,7 +60,7 @@ public class InitReactorRunner {
* As such there's no way for plugins to participate into this process. * As such there's no way for plugins to participate into this process.
*/ */
private ReactorListener buildReactorListener() throws IOException { private ReactorListener buildReactorListener() throws IOException {
List<ReactorListener> r = (List) Service.loadInstances(Thread.currentThread().getContextClassLoader(), InitReactorListener.class); List<ReactorListener> r = Lists.newArrayList(ServiceLoader.load(InitReactorListener.class, Thread.currentThread().getContextClassLoader()));
r.add(new ReactorListener() { r.add(new ReactorListener() {
final Level level = Level.parse( Configuration.getStringConfigParameter("initLogLevel", "FINE") ); final Level level = Level.parse( Configuration.getStringConfigParameter("initLogLevel", "FINE") );
public void onTaskStarted(Task t) { public void onTaskStarted(Task t) {
......
...@@ -4,7 +4,6 @@ import hudson.Extension; ...@@ -4,7 +4,6 @@ import hudson.Extension;
import hudson.Lookup; import hudson.Lookup;
import hudson.init.InitMilestone; import hudson.init.InitMilestone;
import hudson.util.Secret; import hudson.util.Secret;
import hudson.util.Service;
import jenkins.model.Jenkins; import jenkins.model.Jenkins;
import org.kohsuke.MetaInfServices; import org.kohsuke.MetaInfServices;
...@@ -12,7 +11,9 @@ import javax.annotation.CheckForNull; ...@@ -12,7 +11,9 @@ import javax.annotation.CheckForNull;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.List; import java.util.Iterator;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
...@@ -68,10 +69,11 @@ public abstract class ConfidentialStore { ...@@ -68,10 +69,11 @@ public abstract class ConfidentialStore {
ConfidentialStore cs = lookup.get(ConfidentialStore.class); ConfidentialStore cs = lookup.get(ConfidentialStore.class);
if (cs==null) { if (cs==null) {
try { try {
List<ConfidentialStore> r = (List) Service.loadInstances(ConfidentialStore.class.getClassLoader(), ConfidentialStore.class); Iterator<ConfidentialStore> it = ServiceLoader.load(ConfidentialStore.class, ConfidentialStore.class.getClassLoader()).iterator();
if (!r.isEmpty()) if (it.hasNext()) {
cs = r.get(0); cs = it.next();
} catch (IOException e) { }
} catch (ServiceConfigurationError e) {
LOGGER.log(Level.WARNING, "Failed to list up ConfidentialStore implementations",e); LOGGER.log(Level.WARNING, "Failed to list up ConfidentialStore implementations",e);
// fall through // fall through
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册