/**
 * Copyright (c) 2017 Contributors to the Eclipse Foundation
 * <p>
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 org.eclipse.microprofile.openapi.spi;

import org.eclipse.microprofile.openapi.models.Constructible;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ServiceLoader;

public abstract class OASFactoryResolver {

    private static volatile OASFactoryResolver instance = null;

    public abstract <T extends Constructible> T createObject(Class<T> clazz);

    public static OASFactoryResolver instance() {
        if (instance == null) {
            synchronized (OASFactoryResolver.class) {
                if (instance != null) {
                    return instance;
                }

                ClassLoader cl = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
                    @Override
                    public ClassLoader run() {
                        return Thread.currentThread().getContextClassLoader();
                    }
                });
                if (cl == null) {
                    cl = OASFactoryResolver.class.getClassLoader();
                }

                OASFactoryResolver newInstance = loadSpi(cl);

                if (newInstance == null) {
                    throw new IllegalStateException("No OASFactoryResolver implementation found!");
                }

                instance = newInstance;
            }
        }

        return instance;
    }

    private static OASFactoryResolver loadSpi(ClassLoader cl) {
        if (cl == null) {
            return null;
        }

        OASFactoryResolver instance = loadSpi(cl.getParent());

        if (instance == null) {
            ServiceLoader<OASFactoryResolver> sl = ServiceLoader.load(OASFactoryResolver.class, cl);
            for (OASFactoryResolver spi : sl) {
                if (instance != null) {
                    throw new IllegalStateException("Multiple OASFactoryResolver implementations found: " + spi.getClass().getName() + " and "
                            + instance.getClass().getName());
                }
                else {
                    instance = spi;
                }
            }
        }
        return instance;
    }

    /**
     * Set the instance. It is used by OSGi environment while service loader pattern is not supported.
     *
     * @param factory set the instance.
     */
    public static void setInstance(OASFactoryResolver factory) {
        instance = factory;
    }
}

@@ -854,195 +854,1106 @@
 r;%0A%0A
-public abstract class OASFactoryResolver %7B%0A%0A    private static volatile OASFactoryResolver instance = null;%0A%0A    public abstract %3CT extends Constructible%3E T createObject(Class%3CT%3E clazz);%0A
+/**%0A * This class is not intended to be used by end-users. It should%0A * be used by vendors to set their implementation of OASFactoryResolver.%0A *%0A * %3Cbr%3E%3Cbr%3EService provider for OASFactoryResolver. The implementation registers%0A * itself via the %7B@link java.util.ServiceLoader%7D mechanism or by manually%0A * setting their implementation using the setInstance method.%0A *%0A */%0Apublic abstract class OASFactoryResolver %7B%0A%0A    private static volatile OASFactoryResolver instance = null;%0A%0A    /**%0A     * Create a new instance of a constructible element from the OpenAPI model tree.%0A     * %0A     * @param %3CT%3E describes the type parameter%0A     * @param clazz represents a model which extends the org.eclipse.microprofile.openapi.models.Constructible interface%0A%0A     * @return a new instance of the requested model%0A     */%0A    public abstract %3CT extends Constructible%3E T createObject(Class%3CT%3E clazz);%0A%0A    /**%0A     * Creates an OASFactoryResolver object.%0A     * Only used internally from within %7B@link org.eclipse.microprofile.openapi.OASFactory%7D%0A     * %0A     * @return an instance of OASFactoryResolver%0A     */
 %0A   

Document SPI class OASFactoryResolver