package com.cedarsoftware.ncube.util;

import groovy.lang.GroovyClassLoader;

import java.net.URL;
import java.util.regex.Pattern;

/**
 * Created by Kenny on 7/12/2014.
 */
public class CdnClassLoader extends GroovyClassLoader
{
    Pattern GroovyExpression = Pattern.compile("^\\/dyn\\/([^\\/]+)\\/(.*)$");



    private boolean _preventRemoteBeanInfo;
    private boolean _preventRemoteCusomizer;
    /**
     * creates a GroovyClassLoader using the given ClassLoader as parent
     */
    public CdnClassLoader(ClassLoader loader, boolean preventRemoteBeanInfo, boolean preventRemoteCusomizer) {
        super(loader, null);
        _preventRemoteBeanInfo = preventRemoteBeanInfo;
        _preventRemoteCusomizer = preventRemoteCusomizer;
    }

    /**
     * Finds and loads the class with the specified name from the URL search
     * path. Any URLs referring to JAR files are loaded and opened as needed
     * until the class is found.
     *
     * @param name the name of the class
     * @return the resulting class
     * @exception ClassNotFoundException if the class could not be found,
     *            or if the loader is closed.
     */
    protected Class<?> findClass(final String name)
            throws ClassNotFoundException
    {
        if (_preventRemoteBeanInfo)
        {
           if (name.endsWith("BeanInfo"))
            {
                return super.getParent().loadClass(name);
            }
        }

        if (_preventRemoteCusomizer)
        {
            if (name.endsWith("Customizer"))
            {
                return super.getParent().loadClass(name);
            }
        }

        if (name.startsWith("java.lang") ||
            name.startsWith("java.io") ||
            name.startsWith("java.net") ||
            name.startsWith("java.util") ||
            name.startsWith("groovy.util") ||
            name.startsWith("groovy.lang")) {

            return super.getParent().loadClass(name);
        }

        if (name.endsWith("NCubeGroovyExpression")) {
            return super.getParent().loadClass(name);
        }
        return super.findClass(name);
    }

    public URL getResource(String name) {
        if (name.endsWith("NCubeGroovyExpression.groovy")) {
            return null;
        }

        if (_preventRemoteBeanInfo)
        {
            if (name.endsWith("BeanInfo.groovy"))
            {
                return null;
            }
        }

        if (_preventRemoteCusomizer)
        {
            if (name.endsWith("Customizer.groovy"))
            {
                return null;
            }
        }

        return super.getResource(name);
    }



}

@@ -223,16 +223,18 @@
 oader%0A%7B%0A
+//
     Patt
@@ -277,35 +277,29 @@
 le(%22
-%5E%5C%5C/dyn%5C%5C/(%5B%5E%5C%5C/%5D+)%5C%5C/(.*)$
+patternize this later
 %22);%0A

remove unused pattern until pattern is established