diff --git a/core/src/main/java/hudson/PluginManager.java b/core/src/main/java/hudson/PluginManager.java
index c816805ba6f60b8b8961a36f65f1d06098e8512f..87153180f6bf887402f249352aaf26a3ded6be2d 100644
--- a/core/src/main/java/hudson/PluginManager.java
+++ b/core/src/main/java/hudson/PluginManager.java
@@ -120,7 +120,6 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
-import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
@@ -144,6 +143,7 @@ import hudson.util.FormValidation;
import java.io.ByteArrayInputStream;
import java.net.JarURLConnection;
import java.net.URLConnection;
+import java.util.ServiceLoader;
import java.util.jar.JarEntry;
import static java.util.logging.Level.FINE;
@@ -1195,7 +1195,9 @@ public abstract class PluginManager extends AbstractModelObject implements OnMas
/**
* Discover all the service provider implementations of the given class,
* via META-INF/services.
+ * @deprecated Use {@link ServiceLoader} instead, or (more commonly) {@link ExtensionList}.
*/
+ @Deprecated
public Collection> discover( Class spi ) {
Set> result = new HashSet>();
diff --git a/core/src/main/java/hudson/init/InitStrategy.java b/core/src/main/java/hudson/init/InitStrategy.java
index 0d1e037e6a6bc5cf939ea2da9bc42d5f058e5147..9d7d287c4310237183984b3287f7536867d76cde 100644
--- a/core/src/main/java/hudson/init/InitStrategy.java
+++ b/core/src/main/java/hudson/init/InitStrategy.java
@@ -17,7 +17,8 @@ import hudson.PluginManager;
import jenkins.util.SystemProperties;
import hudson.util.DirScanner;
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.
@@ -113,11 +114,12 @@ public class InitStrategy {
* Obtains the instance to be used.
*/
public static InitStrategy get(ClassLoader cl) throws IOException {
- List r = Service.loadInstances(cl, InitStrategy.class);
- if (r.isEmpty()) return new InitStrategy(); // default
-
- InitStrategy s = r.get(0);
- LOGGER.fine("Using "+s+" as InitStrategy");
+ Iterator it = ServiceLoader.load(InitStrategy.class, cl).iterator();
+ if (!it.hasNext()) {
+ return new InitStrategy(); // default
+ }
+ InitStrategy s = it.next();
+ LOGGER.log(Level.FINE, "Using {0} as InitStrategy", s);
return s;
}
diff --git a/core/src/main/java/hudson/util/Service.java b/core/src/main/java/hudson/util/Service.java
index 6275b07e3f9f1132e07a867a5f1c34ee483ffc39..a979324f33036cfb43a035bed8295e1ff753f077 100644
--- a/core/src/main/java/hudson/util/Service.java
+++ b/core/src/main/java/hudson/util/Service.java
@@ -31,6 +31,7 @@ import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.ArrayList;
+import java.util.ServiceLoader;
import java.util.logging.Level;
import java.util.logging.Logger;
import static java.util.logging.Level.WARNING;
@@ -39,11 +40,10 @@ import static java.util.logging.Level.WARNING;
* Load classes by looking up META-INF/services.
*
* @author Kohsuke Kawaguchi
+ * @deprecated use {@link ServiceLoader} instead.
*/
+@Deprecated
public class Service {
- /**
- * Poorman's clone of JDK6 ServiceLoader.
- */
public static List loadInstances(ClassLoader classLoader, Class type) throws IOException {
List result = new ArrayList();
diff --git a/core/src/main/java/jenkins/InitReactorRunner.java b/core/src/main/java/jenkins/InitReactorRunner.java
index c5c2bfb844484ba8bff5b6c0f76c082581fbda45..aa6ef83ab752d36d827f277f02a1c31ab6376419 100644
--- a/core/src/main/java/jenkins/InitReactorRunner.java
+++ b/core/src/main/java/jenkins/InitReactorRunner.java
@@ -1,11 +1,11 @@
package jenkins;
+import com.google.common.collect.Lists;
import jenkins.util.SystemProperties;
import hudson.init.InitMilestone;
import hudson.init.InitReactorListener;
import hudson.util.DaemonThreadFactory;
import hudson.util.NamingThreadFactory;
-import hudson.util.Service;
import jenkins.model.Configuration;
import jenkins.model.Jenkins;
import org.jvnet.hudson.reactor.Milestone;
@@ -16,6 +16,7 @@ import org.jvnet.hudson.reactor.Task;
import java.io.IOException;
import java.util.List;
+import java.util.ServiceLoader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
@@ -59,7 +60,7 @@ public class InitReactorRunner {
* As such there's no way for plugins to participate into this process.
*/
private ReactorListener buildReactorListener() throws IOException {
- List r = (List) Service.loadInstances(Thread.currentThread().getContextClassLoader(), InitReactorListener.class);
+ List r = Lists.newArrayList(ServiceLoader.load(InitReactorListener.class, Thread.currentThread().getContextClassLoader()));
r.add(new ReactorListener() {
final Level level = Level.parse( Configuration.getStringConfigParameter("initLogLevel", "FINE") );
public void onTaskStarted(Task t) {
diff --git a/core/src/main/java/jenkins/security/ConfidentialStore.java b/core/src/main/java/jenkins/security/ConfidentialStore.java
index 6e79d3d70a6dde0ede6f1e3e8564af750aeea0fe..1a8a152f82efe6c2ad61020ea12aa08d6299d2c7 100644
--- a/core/src/main/java/jenkins/security/ConfidentialStore.java
+++ b/core/src/main/java/jenkins/security/ConfidentialStore.java
@@ -4,7 +4,6 @@ import hudson.Extension;
import hudson.Lookup;
import hudson.init.InitMilestone;
import hudson.util.Secret;
-import hudson.util.Service;
import jenkins.model.Jenkins;
import org.kohsuke.MetaInfServices;
@@ -12,7 +11,9 @@ import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.IOException;
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.Logger;
@@ -68,10 +69,11 @@ public abstract class ConfidentialStore {
ConfidentialStore cs = lookup.get(ConfidentialStore.class);
if (cs==null) {
try {
- List r = (List) Service.loadInstances(ConfidentialStore.class.getClassLoader(), ConfidentialStore.class);
- if (!r.isEmpty())
- cs = r.get(0);
- } catch (IOException e) {
+ Iterator it = ServiceLoader.load(ConfidentialStore.class, ConfidentialStore.class.getClassLoader()).iterator();
+ if (it.hasNext()) {
+ cs = it.next();
+ }
+ } catch (ServiceConfigurationError e) {
LOGGER.log(Level.WARNING, "Failed to list up ConfidentialStore implementations",e);
// fall through
}