/*******************************************************************************
 * 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.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 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) {
        super(delegateLoader);
        aHelperContext = aContext;
        generatedClasses = new HashMap();
    }

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

        // To maximize performance, check the generated classes first
        javaClass = (Class) generatedClasses.get(className);
        if (javaClass != null) {
            return javaClass;
        }

        // Not found, so now check classpath
        try {
            javaClass = getParent().loadClass(className);
        } catch (ClassNotFoundException e) {
            throw e;
        } catch (NoClassDefFoundError error) {
            throw error;
        }
        return javaClass;
    }
    
    public Class loadClass(String className, SDOType type) throws ClassNotFoundException {
        Class javaClass = null;

        // To maximize performance, check the generated classes first
        javaClass = (Class) generatedClasses.get(className);
        if (javaClass != null) {
            return javaClass;
        }

        try {
            javaClass = getParent().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;
            }
        }
        if(!type.isDataType() && null != aHelperContext && null != aHelperContext.getTypeHelper()) {
            ((SDOTypeHelper) aHelperContext.getTypeHelper()).getImplClassesToSDOType().put(javaClass, type);
        }
        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;
    }

}
@@ -1067,16 +1067,31 @@
 vate Map
+%3CString, Class%3E
  generat
@@ -1556,116 +1556,8 @@
 ss =
- null;%0D%0A%0D%0A        // To maximize performance, check the generated classes first%0D%0A        javaClass = (Class)
  gen
@@ -1663,26 +1663,24 @@
      %7D%0D%0A
-%0D%0A
         
 // Not f
@@ -1675,311 +1675,57 @@
     
-// Not found, so now check classpath%0D%0A        try %7B%0D%0A            javaClass = getParent().loadClass(className);%0D%0A        %7D catch (ClassNotFoundException e) %7B%0D%0A            throw e;%0D%0A        %7D catch (NoClassDefFoundError error) %7B%0D%0A            throw error;%0D%0A        %7D%0D%0A        return javaClass;%0D%0A    %7D%0D%0A    
+return getParent().loadClass(className);%0D%0A    %7D%0D%0A
 %0D%0A  
@@ -1818,43 +1818,8 @@
  %7B%0D%0A
-        Class javaClass = null;%0D%0A%0D%0A
     
@@ -1885,32 +1885,38 @@
  first%0D%0A        
+Class 
 javaClass = (Cla
@@ -1910,24 +1910,16 @@
 aClass =
- (Class)
  generat

Fix for bug #Bug 69513: Code Clean Up - SDOClassLoader