/* Copyright 2004-2005 the original author or authors.
 *
 * 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.codehaus.groovy.grails.context.annotation;

import org.springframework.context.annotation.ComponentScanBeanDefinitionParser;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.AntPathMatcher;
import org.w3c.dom.Element;
import org.apache.commons.io.FilenameUtils;

/**
 * Extends Spring's default &lt;context:component-scan/&gt; element to ignore Groovy's
 * generated closure classes
 *
 * @author Graeme Rocher
 * @since 1.2
 */
public class ClosureClassIgnoringComponentScanBeanDefinitionParser extends ComponentScanBeanDefinitionParser{

    @Override
    protected ClassPathBeanDefinitionScanner configureScanner(ParserContext parserContext, Element element) {
        final ClassPathBeanDefinitionScanner scanner = super.configureScanner(parserContext, element);
        final PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(parserContext.getReaderContext().getResourceLoader());
        resourceResolver.setPathMatcher(new AntPathMatcher(){
            @Override
            public boolean match(String pattern, String path) {
                if(path.endsWith(".class")) {
                    String filename = FilenameUtils.getBaseName(path);
                    if(filename.indexOf("$")>-1) return false;
                }
                return super.match(pattern, path);    
            }
        });
        scanner.setResourceLoader(resourceResolver);
        return scanner;
    }

    class DelegatingResourceLoader implements ResourceLoader{
        private ResourceLoader delegate;

        DelegatingResourceLoader(ResourceLoader loader) {
            this.delegate = loader;
        }

        public Resource getResource(String location) {
            System.out.println("location = " + location);
            return delegate.getResource(location);
        }

        public ClassLoader getClassLoader() {
            return delegate.getClassLoader();
        }
    }
}

@@ -678,80 +678,206 @@
 ort 
-org.springframework.context.annotation.ComponentScanBeanDefinitionParser
+grails.util.BuildSettings;%0Aimport grails.util.BuildSettingsHolder;%0Aimport grails.util.Metadata;%0Aimport org.apache.commons.io.FilenameUtils;%0Aimport org.springframework.beans.factory.xml.ParserContext
 ;%0Aim
@@ -983,39 +983,105 @@
 ork.
-beans.factory.xml.ParserContext
+context.annotation.ComponentScanBeanDefinitionParser;%0Aimport org.springframework.core.io.Resource
 ;%0Aim
@@ -1133,53 +1133,8 @@
 er;%0A
-import org.springframework.core.io.Resource;%0A
 impo
@@ -1289,50 +1289,140 @@
 nt;%0A
+%0A
 import 
-org.apache.commons.io.FilenameUtils
+java.io.IOException;%0Aimport java.net.URL;%0Aimport java.util.Enumeration;%0Aimport java.util.LinkedHashSet;%0Aimport java.util.Set
 ;%0A%0A/
@@ -2086,16 +2086,1382 @@
 oader())
+ %7B%0A            @Override%0A            protected Resource%5B%5D findAllClassPathResources(String location) throws IOException %7B%0A                Set%3CResource%3E result = new LinkedHashSet%3CResource%3E(16);%0A%0A%0A                URL classesDir = null;%0A%0A                final boolean warDeployed = Metadata.getCurrent().isWarDeployed();%0A                if(!warDeployed) %7B%0A                    BuildSettings buildSettings = BuildSettingsHolder.getSettings();%0A                    classesDir = buildSettings.getClassesDir().toURI().toURL();%0A                %7D%0A%0A                // only scan classes from project classes directory%0A                String path = location;%0A                if (path.startsWith(%22/%22)) %7B%0A                    path = path.substring(1);%0A                %7D%0A                Enumeration resourceUrls = getClassLoader().getResources(path);%0A                while (resourceUrls.hasMoreElements()) %7B%0A                    URL url = (URL) resourceUrls.nextElement();%0A                    if(!warDeployed && classesDir!= null && url.equals(classesDir)) %7B%0A                        result.add(convertClassLoaderURL(url));%0A                    %7D%0A                    else %7B%0A                        result.add(convertClassLoaderURL(url));%0A                    %7D%0A%0A                %7D                %0A                return result.toArray(new Resource%5Bresult.size()%5D);%0A            %7D%0A        %7D
 ;%0A      

Fix for OutOfMemoryErrors by only getting Grails to scan the project classes directory and not all the others