/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, Red Hat Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.as.weld;

import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.jboss.modules.Module;
import org.jboss.weld.resources.spi.ResourceLoader;

/**
 * A {@link ResourceLoader} that can load classes from a {@link Module}
 * <p>
 * Thread Safety: This class is thread safe, and does not require a happens before even between construction and usage
 *
 * @author Stuart Douglas
 *
 */
public class WeldModuleResourceLoader implements ResourceLoader {

    private final Module module;

    /**
     * Additional classes that have been added to the bean archive by the container or by a portable extension
     */
    private final Map<String, Class<?>> additionalClasses;

    public WeldModuleResourceLoader(Module module) {
        this.module = module;
        this.additionalClasses = new ConcurrentHashMap<String, Class<?>>();
    }

    /**
     * If the class name is found in addtionalClasses then return it.
     *
     * Otherwise the class will be loaded from the module ClassLoader
     */
    @Override
    public Class<?> classForName(String name) {
        if (additionalClasses.containsKey(name)) {
            return additionalClasses.get(name);
        }
        try {
            return module.getClassLoader().loadClass(name);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public void addAdditionalClass(Class<?> clazz) {
        this.additionalClasses.put(clazz.getName(), clazz);
    }

    /**
     * Loads a resource from the module class loader
     */
    @Override
    public URL getResource(String name) {
        return module.getClassLoader().getResource(name);
    }

    /**
     * Loads resources from the module class loader
     */
    @Override
    public Collection<URL> getResources(String name) {
        try {
            final HashSet<URL> resources = new HashSet<URL>();
            Enumeration<URL> urls = module.getClassLoader().getResources(name);
            while (urls.hasMoreElements()) {
                resources.add(urls.nextElement());
            }
            return resources;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void cleanup() {
        // nop
    }

}

@@ -1041,36 +1041,8 @@
 d;%0A%0A
-import java.io.IOException;%0A
 impo
@@ -1297,16 +1297,78 @@
 eLoader;
+%0Aimport org.jboss.weld.resources.spi.ResourceLoadingException;
 %0A%0A/**%0A *
@@ -2270,32 +2270,50 @@
 (String name) %7B%0A
+        try %7B%0A    
         if (addi
@@ -2343,24 +2343,28 @@
 ey(name)) %7B%0A
+    
             
@@ -2411,23 +2411,13 @@
     
-%7D%0A        try %7B
+    %7D
 %0A   
@@ -2494,21 +2494,8 @@
 ch (
-ClassNotFound
 Exce
@@ -2520,38 +2520,46 @@
      throw new R
-untime
+esourceLoading
 Exception(e);%0A  
@@ -2810,32 +2810,50 @@
 (String name) %7B%0A
+        try %7B%0A    
         return m
@@ -2890,24 +2890,117 @@
 urce(name);%0A
+        %7D catch (Exception e) %7B%0A            throw new ResourceLoadingException(e);%0A        %7D%0A
     %7D%0A%0A    /
@@ -3449,10 +3449,8 @@
 ch (
-IO
 Exce
@@ -3487,14 +3487,22 @@
 ew R
-untime
+esourceLoading
 Exce

Throw correct exception if resource loading fails