Skip to content

Commit bd1b08c

Browse files
committed
Do not cache the value obtained by ServiceLoader, because that value depends on the context class loader.
Caching a JVM-wide value is deprecated. See `ServiceLoader.load(Class)` javadoc for explanation. #241
1 parent a7bf0ea commit bd1b08c

2 files changed

Lines changed: 2 additions & 50 deletions

File tree

src/main/java/javax/measure/spi/ServiceProvider.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,7 @@ private Stream<ServiceProvider> stream() {
306306
*/
307307
public static final List<ServiceProvider> available() {
308308
ArrayList<ServiceProvider> providers = new Selector(null).stream().collect(Collectors.toCollection(ArrayList::new));
309-
/*
310-
* Get the current service provider. If no provider has been set yet, set it now for
311-
* consistency with the contract saying that the first item is the current provider.
312-
*/
313-
ServiceProvider first = current.get();
314-
if (first == null && !providers.isEmpty()) {
315-
first = setDefault(providers.get(0));
316-
}
309+
final ServiceProvider first = current.get();
317310
/*
318311
* Make sure that 'first' is the first item in the 'providers' list. If that item appears
319312
* somewhere else, we have to remove the second occurrence for avoiding duplicated elements.
@@ -396,27 +389,10 @@ public static final ServiceProvider current() {
396389
} else {
397390
throw new IllegalStateException("No Measurement ServiceProvider found.");
398391
}
399-
p = setDefault(p);
400392
}
401393
return p;
402394
}
403395

404-
/**
405-
* Sets the given provider as the current one if and only if no other provider are currently set.
406-
* If another provider is already set, that other provider is returned.
407-
*
408-
* @param provider the provider to set by default if no other provider is currently set.
409-
* @return the current provider, which is the specified {@code provider} if no other provider
410-
* was set before this method call.
411-
*/
412-
private static ServiceProvider setDefault(ServiceProvider provider) {
413-
while (!current.compareAndSet(null, provider)) {
414-
final ServiceProvider c = current.get();
415-
if (c != null) return c;
416-
}
417-
return provider;
418-
}
419-
420396
/**
421397
* Replaces the current {@link ServiceProvider}.
422398
*

src/main/jdk9/javax/measure/spi/ServiceProvider.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,7 @@ private Stream<ServiceProvider> stream() {
317317
*/
318318
public static final List<ServiceProvider> available() {
319319
ArrayList<ServiceProvider> providers = new Selector(null).stream().collect(Collectors.toCollection(ArrayList::new));
320-
/*
321-
* Get the current service provider. If no provider has been set yet, set it now for
322-
* consistency with the contract saying that the first item is the current provider.
323-
*/
324-
ServiceProvider first = current.get();
325-
if (first == null && !providers.isEmpty()) {
326-
first = setDefault(providers.get(0));
327-
}
320+
final ServiceProvider first = current.get();
328321
/*
329322
* Make sure that 'first' is the first item in the 'providers' list. If that item appears
330323
* somewhere else, we have to remove the second occurrence for avoiding duplicated elements.
@@ -407,27 +400,10 @@ public static final ServiceProvider current() {
407400
} else {
408401
throw new IllegalStateException("No Measurement ServiceProvider found.");
409402
}
410-
p = setDefault(p);
411403
}
412404
return p;
413405
}
414406

415-
/**
416-
* Sets the given provider as the current one if and only if no other provider are currently set.
417-
* If another provider is already set, that other provider is returned.
418-
*
419-
* @param provider the provider to set by default if no other provider is currently set.
420-
* @return the current provider, which is the specified {@code provider} if no other provider
421-
* was set before this method call.
422-
*/
423-
private static ServiceProvider setDefault(ServiceProvider provider) {
424-
while (!current.compareAndSet(null, provider)) {
425-
final ServiceProvider c = current.get();
426-
if (c != null) return c;
427-
}
428-
return provider;
429-
}
430-
431407
/**
432408
* Replaces the current {@link ServiceProvider}.
433409
*

0 commit comments

Comments
 (0)