/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 javax.enterprise.inject.spi;

import javax.enterprise.inject.Instance;
import java.util.ServiceLoader;

/**
 * <p>Static helper class to access the {@link BeanManager}</p>
 *
 * <p>Usage:
 * <pre>
 * BeanManager bm = CDI.current().getBeanManager();
 * </pre>
 *
 *
 * @since 1.1
 */
public abstract class CDI<T> implements Instance<T>
{
    private static volatile CDIProvider PROVIDER;

    public static CDI<Object> current()
    {
        if (PROVIDER == null)
        {
            synchronized (CDI.class)
            {
                if (PROVIDER == null)
                {

                    CDIProvider highestProvider = null;
                    int ordinal = -1;

                    ServiceLoader<CDIProvider> cdiProviders = ServiceLoader.load(CDIProvider.class);
                    for (CDIProvider cdiProvider : cdiProviders)
                    {
                        if (highestProvider == null || cdiProvider.getPriority() > ordinal)
                        {
                            highestProvider = cdiProvider;
                        }
                    }

                    PROVIDER = highestProvider;
                }
            }
        }
        return PROVIDER.getCDI();
    }

    /**
     * <p>A container or an application can set this manually. If not
     * we will use the {@link java.util.ServiceLoader} and use the
     * first service we find.</p>
     *
     * TODO: clarify if this is per 'application' or general?
     *
     * @param provider to use
     */
    public static void setCDIProvider(CDIProvider provider)
    {
        if (provider == null)
        {
            PROVIDER = null;
        }
        else
        {
            PROVIDER = provider;
        }
    }

    public abstract BeanManager getBeanManager();
}

@@ -1980,32 +1980,176 @@
     %7D%0A        %7D%0A
+        if (PROVIDER == null)%0A        %7B%0A            throw new IllegalStateException(%22Unable to locate a CDIProvider implementation%22);%0A        %7D%0A
         return P
@@ -2588,23 +2588,71 @@
     
-PROVIDER =
+throw new IllegalStateException(%22CDIProvider must not be
  null
+%22)
 ;%0A  

Throw IllegalStateException when provider is null.