/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, 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.environment.se.discovery.url;

import java.io.IOException;
import java.net.URL;
import java.util.Collection;

import org.jboss.weld.resources.spi.ResourceLoader;
import org.jboss.weld.resources.spi.ResourceLoadingException;
import org.jboss.weld.util.collections.EnumerationList;

/**
 * A simple resource loader.
 * 
 * Uses {@link WeldSEResourceLoader}'s classloader if the Thread Context 
 * Classloader isn't available
 * 
 * @author Pete Muir
 *
 */
public class WeldSEResourceLoader implements ResourceLoader
{
   
   public Class<?> classForName(String name)
   {
      
      try
      {
         if (Thread.currentThread().getContextClassLoader() != null)
         {
            return Thread.currentThread().getContextClassLoader().loadClass(name);
         }
         else
         {
            return Class.forName(name);
         }
      }
      catch (ClassNotFoundException e)
      {
         throw new ResourceLoadingException(e);
      }
      catch (NoClassDefFoundError e)
      {
         throw new ResourceLoadingException(e);
      }
   }
   
   public URL getResource(String name)
   {
      if (Thread.currentThread().getContextClassLoader() != null)
      {
         return Thread.currentThread().getContextClassLoader().getResource(name);
      }
      else
      {
         return getClass().getResource(name);
      }
   }
   
   public Collection<URL> getResources(String name)
   {
      try
      {
         if (Thread.currentThread().getContextClassLoader() != null)
         {
            return new EnumerationList<URL>(Thread.currentThread().getContextClassLoader().getResources(name));
         }
         else
         {
            return new EnumerationList<URL>(getClass().getClassLoader().getResources(name));
         }
      }
      catch (IOException e)
      {
         throw new ResourceLoadingException(e);
      }
   }
   
   public void cleanup() {}
   
}

@@ -1073,16 +1073,67 @@
 ionList;
+%0Aimport org.jboss.weld.util.reflection.Reflections;
 %0A%0A/**%0A *
@@ -1433,32 +1433,64 @@
     try%0A      %7B%0A
+         Class%3C?%3E clazz = null;%0A
          if (Thr
@@ -1557,38 +1557,39 @@
   %7B%0A            
-return
+clazz =
  Thread.currentT
@@ -1689,46 +1689,685 @@
     
-return Class.forName(name);%0A         %7D
+clazz = Class.forName(name);%0A         %7D%0A         // if the class relies on optional dependencies that are not present%0A         // then a CNFE can be thrown later in the deployment process when the%0A         // Introspector is inspecting the class. We call getMethods, getFields%0A         // and getConstructors now over the whole type heirachey to force %0A         // these errors to occur early%0A         Class%3C?%3E obj = clazz;%0A         while (obj != null && obj != Object.class)%0A         %7B%0A            obj.getDeclaredConstructors();%0A            obj.getDeclaredFields();%0A            obj.getDeclaredMethods();%0A            obj = obj.getSuperclass();%0A         %7D%0A         return clazz;
 %0A   

improve the way weld-se handles optional dependencies