/*
 * JBoss, Home of Professional Open Source
 * Copyright 2014, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * 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 org.jboss.weld.bootstrap;

import java.util.HashSet;
import java.util.Set;

import javax.annotation.Priority;

import org.jboss.weld.bootstrap.api.Service;
import org.jboss.weld.bootstrap.api.ServiceRegistry;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.bootstrap.spi.Deployment;
import org.jboss.weld.bootstrap.spi.Metadata;
import org.jboss.weld.resources.DefaultResourceLoader;
import org.jboss.weld.resources.WeldClassLoaderResourceLoader;
import org.jboss.weld.resources.spi.ResourceLoader;
import org.jboss.weld.transaction.spi.TransactionServices;
import org.jboss.weld.util.ServiceLoader;
import org.jboss.weld.util.Services;

/**
 * Loads {@link Service} implementations using {@link ServiceLoader}. This can be used e.g. by a JTA library
 * in weld-servlet environment to provide {@link TransactionServices} implementation which would not otherwise
 * be provided by weld-servlet. Deployment classloaders, TCCL and Weld's classloader are used to discover service
 * implementations. Service implementation may specify their priority using {@link Priority}. Services with higher
 * priority have precedence. Services that do not specify priority have the default priority of 4500.
 * See https://issues.jboss.org/browse/WELD-1495 for details.
 *
 * @author Jozef Hartinger
 *
 */
class AdditionalServiceLoader {

    private final Deployment deployment;

    AdditionalServiceLoader(Deployment deployment) {
        this.deployment = deployment;
    }

    /**
     * Discovers additional services using {@link ServiceLoader} and appends them to the given service registry.
     * @param registry the given service registry
     */
    void loadAdditionalServices(ServiceRegistry registry) {
        for (ResourceLoader loader : getResourceLoaders()) {
            for (Metadata<Service> metadata : ServiceLoader.load(Service.class, loader)) {
                Service service = metadata.getValue();
                for (Class<? extends Service> serviceInterface : Services.identifyServiceInterfaces(service.getClass(), new HashSet<>())) {
                    Services.put(registry, serviceInterface, service);
                }
            }
        }
    }

    private Set<ResourceLoader> getResourceLoaders() {
        Set<ResourceLoader> resourceLoaders = new HashSet<>();
        for (BeanDeploymentArchive archive : deployment.getBeanDeploymentArchives()) {
            ResourceLoader resourceLoader = archive.getServices().get(ResourceLoader.class);
            if (resourceLoader != null) {
                resourceLoaders.add(resourceLoader);
            }
        }
        resourceLoaders.add(WeldClassLoaderResourceLoader.INSTANCE);
        resourceLoaders.add(DefaultResourceLoader.INSTANCE);
        return resourceLoaders;
    }
}

@@ -1313,67 +1313,8 @@
 er;%0A
-import org.jboss.weld.transaction.spi.TransactionServices;%0A
 impo
@@ -1462,16 +1462,29 @@
 Loader%7D.
+%0A *%0A * %3Cp%3E%0A *
  This ca
@@ -1514,19 +1514,16 @@
  library
-%0A *
  in weld
@@ -1561,16 +1561,47 @@
  %7B@link 
+org.jboss.weld.transaction.spi.
 Transact
@@ -1633,16 +1633,19 @@
 on which
+%0A *
  would n
@@ -1656,19 +1656,16 @@
 therwise
-%0A *
  be prov
@@ -1767,19 +1767,16 @@
  service
-%0A *
  impleme
@@ -1784,16 +1784,299 @@
 tations.
+ If a%0A * service provider is loadable from multiple classloaders (most likely in a Java SE application) the service class will be instantiated multiple times but only%0A * the first instance will be used (see %7B@link Services#put(ServiceRegistry, Class, Service)%7D).%0A * %3C/p%3E%0A *%0A * %3Cp%3E%0A *
  Service
@@ -2162,19 +2162,16 @@
 h higher
-%0A *
  priorit
@@ -2217,16 +2217,19 @@
  specify
+%0A *
  priorit
@@ -2268,20 +2268,186 @@
 500.%0A * 
-See 
+%3C/p%3E%0A *%0A * %3Cp%3E%0A * Note that the loaded services are registered globally, i.e. are accessible from each bean manager in the application.%0A * %3C/p%3E%0A *%0A * %3Cp%3E%0A * See %3Ca href=%22
 https://
@@ -2479,16 +2479,31 @@
 ELD-1495
+%22%3EWELD-1495%3C/a%3E
  for det
@@ -2508,16 +2508,24 @@
 etails.%0A
+ * %3C/p%3E%0A
  *%0A * @a
@@ -2546,19 +2546,16 @@
 rtinger%0A
- *%0A
  */%0Aclas

Update AdditionalServiceLoader javadoc.