/*******************************************************************************
 * Copyright (c) 1998, 2008 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 
 * which accompanies this distribution. 
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at 
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Oracle - initial API and implementation from Oracle TopLink
 ******************************************************************************/ 
package org.eclipse.persistence.sdo.helper;

import commonj.sdo.helper.HelperContext;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.persistence.sdo.SDOType;

/**
 * <p><b>Purpose</b>: A custom classloader used to dynamically create classes as needed.
 */
public class SDOClassLoader extends ClassLoader {
    private ClassLoader delegateLoader;
    private Map generatedClasses;

    // hold the context containing all helpers so that we can preserve inter-helper relationships
    private HelperContext aHelperContext;

    public SDOClassLoader(ClassLoader delegateLoader, HelperContext aContext) {
        aHelperContext = aContext;
        this.delegateLoader = delegateLoader;
        generatedClasses = new HashMap();
    }

    public Class loadClass(String className) throws ClassNotFoundException {
        Class javaClass = null;
        try {
            javaClass = delegateLoader.loadClass(className);
        } catch (ClassNotFoundException e) {
            javaClass = (Class)generatedClasses.get(className);
            if (javaClass != null) {
                return javaClass;
            }
            throw e;
        } catch (NoClassDefFoundError error) {
            javaClass = (Class)generatedClasses.get(className);
            if (javaClass == null) {
                throw error;
            }
        }
        return javaClass;
    }

    public Class loadClass(String className, SDOType type) throws ClassNotFoundException {
        Class javaClass = null;

        try {
            javaClass = delegateLoader.loadClass(className);
        } catch (ClassNotFoundException e) {
            javaClass = createGeneric(className, type);
            if (javaClass == null) {
                throw e;
            }
        } catch (NoClassDefFoundError error) {
            javaClass = createGeneric(className, type);
            if (javaClass == null) {
                throw error;
            }
        }

        return javaClass;
    }

    public Class createGeneric(String className, SDOType type) {
        Class javaClass = (Class)generatedClasses.get(className);
        if (javaClass != null) {
            return javaClass;
        }

        if (className == null) {
            return null;
        }

        DynamicClassWriter dcWriter = new DynamicClassWriter(className, type, aHelperContext);

        byte[] bytes = dcWriter.createClass();

        javaClass = defineClass(className, bytes, 0, bytes.length);
        generatedClasses.put(className, javaClass);
        return javaClass;
    }

    public void setDelegateLoader(ClassLoader delegateLoader) {
        this.delegateLoader = delegateLoader;
    }

    public ClassLoader getDelegateLoader() {
        return delegateLoader;
    }

    public URL getResource(String name) {
        return delegateLoader.getResource(name);
    }

    public InputStream getResourceAsStream(String name) {
        return delegateLoader.getResourceAsStream(name);
    }

    public Enumeration getResources(String name) throws IOException {
        return delegateLoader.getResources(name);
    }
}
@@ -1714,135 +1714,81 @@
 l;%0D%0A
-        try %7B%0D%0A            javaClass = delegateLoader.loadClass(className);%0D%0A        %7D catch (ClassNotFoundException e) %7B%0D%0A    
+%0D%0A        // To maximize performance, check the generated classes first%0D%0A
     
@@ -1802,32 +1802,33 @@
 aClass = (Class)
+ 
 generatedClasses
@@ -1845,36 +1845,32 @@
 Name);%0D%0A        
-    
 if (javaClass !=
@@ -1883,36 +1883,32 @@
  %7B%0D%0A            
-    
 return javaClass
@@ -1910,37 +1910,35 @@
 Class;%0D%0A        
-    %7D
+%7D%0D%0A
 %0D%0A            th
@@ -1935,201 +1935,237 @@
     
-    throw e;%0D%0A        %7D catch (NoClassDefFoundError error) %7B%0D%0A            javaClass = (Class)generatedClasses.get(className);%0D%0A            if (javaClass == null) %7B%0D%0A                throw
+// Not found, so now check classpath%0D%0A        try %7B%0D%0A            javaClass = delegateLoader.loadClass(className);%0D%0A        %7D catch (ClassNotFoundException e) %7B%0D%0A            throw e;%0D%0A        %7D catch (NoClassDefFoundError
  error
-;
+) %7B
 %0D%0A  
@@ -2166,33 +2166,44 @@
  %7B%0D%0A            
-%7D
+throw error;
 %0D%0A        %7D%0D%0A   
@@ -2225,32 +2225,36 @@
 aClass;%0D%0A    %7D%0D%0A
+    
 %0D%0A    public Cla
@@ -2358,32 +2358,243 @@
 lass = null;%0D%0A%0D%0A
+        // To maximize performance, check the generated classes first%0D%0A        javaClass = (Class) generatedClasses.get(className);%0D%0A        if (javaClass != null) %7B%0D%0A            return javaClass;%0D%0A        %7D%0D%0A%0D%0A
         try %7B%0D%0A 
@@ -3027,34 +3027,32 @@
   %7D%0D%0A        %7D%0D%0A
-%0D%0A
         return j

Fix for bug 233756