/*
 * Copyright 2017 The OpenTracing Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.opentracing.contrib.tracerresolver;

import io.opentracing.Tracer;

import java.util.ServiceLoader;

/**
 * {@code TracerResolver} API definition looks for one or more registered {@link TracerResolver} implementations
 * using the {@link ServiceLoader}.
 * <p>
 * If no {@link TracerResolver} implementations are found, the {@link #resolveTracer()} method will fallback to
 * {@link ServiceLoader} lookup of the {@link Tracer} service itself.
 */
public abstract class TracerResolver {
    private static final ServiceLoader<TracerResolver> RESOLVERS = ServiceLoader.load(TracerResolver.class);
    private static final ServiceLoader<Tracer> FALLBACK = ServiceLoader.load(Tracer.class);

    private static Tracer resolved = null; // [ST] I'm still in doubt whether the resolved result should be cached or not!

    /**
     * Resolves the {@link Tracer} implementation.
     *
     * @return The resolved Tracer or {@code null} if none was resolved.
     */
    protected abstract Tracer resolve();

    /**
     * Detects all {@link TracerResolver} service implementations and attempts to resolve a {@link Tracer}.
     * <p>
     * If there are more than one resolver, the first non-<code>null</code> resolved tracer is returned.
     *
     * @return The resolved Tracer or {@code null} if none was resolved.
     */
    public static Tracer resolveTracer() {
        if (resolved == null) {
            // TODO error handling?
            for (TracerResolver resolver : RESOLVERS) {
                Tracer tracer = resolver.resolve();
                if (tracer != null) {
                    resolved = tracer;
                    return resolved;
                }
            }
            for (Tracer tracer : FALLBACK) {
                if (tracer != null) {
                    resolved = tracer;
                    break;
                }
            }
        }
        return resolved;
    }

    public static void reset() {
        RESOLVERS.reload();
        FALLBACK.reload();
        resolved = null;
    }

}

@@ -713,16 +713,81 @@
 eLoader;
+%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;
 %0A%0A/**%0A *
@@ -1165,16 +1165,107 @@
 olver %7B%0A
+    private static final Logger LOGGER = Logger.getLogger(TracerResolver.class.getName());%0A
     priv
@@ -1462,132 +1462,8 @@
 );%0A%0A
-    private static Tracer resolved = null; // %5BST%5D I'm still in doubt whether the resolved result should be cached or not!%0A%0A
     
@@ -2014,80 +2014,8 @@
 ) %7B%0A
-        if (resolved == null) %7B%0A            // TODO error handling?%0A    
     
@@ -2058,24 +2058,42 @@
 ESOLVERS) %7B%0A
+            try %7B%0A
             
@@ -2190,33 +2190,72 @@
         
-resolved =
+LOGGER.log(Level.FINER, %22Resolved tracer: %7B0%7D.%22,
  tracer
+)
 ;%0A      
@@ -2271,32 +2271,30 @@
      return 
-resolved
+tracer
 ;%0A          
@@ -2306,37 +2306,206 @@
  %7D%0A            %7D
-%0A    
+ catch (RuntimeException rte) %7B%0A                LOGGER.log(Level.WARNING, %22Error resolving tracer using %22 + resolver + %22: %22 + rte.getMessage(), rte);%0A            %7D%0A        %7D%0A
         for (Tra
@@ -2525,28 +2525,24 @@
 FALLBACK) %7B%0A
-    
             
@@ -2583,29 +2583,64 @@
     
-    resolved =
+LOGGER.log(Level.FINER, %22Resolved tracer: %7B0%7D.%22,
  tracer
+)
 ;%0A  
@@ -2657,17 +2657,21 @@
     
-   
- break
+return tracer
 ;%0A  
@@ -2668,32 +2668,38 @@
 tracer;%0A        
+    %7D%0A
         %7D%0A      
@@ -2704,46 +2704,191 @@
     
-    %7D%0A    
+LOGGER.log(Level.FINEST, %22No tracer was resolved.%22);%0A        return null;%0A
     %7D%0A
+%0A
     
-    return
+/**%0A     * Reloads the lazily found %7B@linkplain TracerResolver resolvers%7D and the fallback
  resolve
 d;%0A 
@@ -2887,17 +2887,18 @@
 olve
-d;
+r.
 %0A    
-%7D%0A
+ */
 %0A   
@@ -2923,11 +2923,12 @@
 d re
-set
+load
 () %7B
@@ -2995,23 +2995,59 @@
     
-resolved = null
+LOGGER.log(Level.FINER, %22Resolvers were reloaded.%22)
 ;%0A  

Remove resolved Tracer caching + add errorhandling and logging.