/**
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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.apache.geronimo.hook.equinox;

import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;

import org.apache.geronimo.hook.BundleHelper;
import org.apache.geronimo.hook.SharedLibraryRegistry;
import org.eclipse.osgi.framework.adaptor.BundleClassLoader;
import org.eclipse.osgi.framework.adaptor.BundleData;
import org.eclipse.osgi.framework.adaptor.ClassLoaderDelegateHook;
import org.osgi.framework.Bundle;

/**
 * @version $Rev$ $Date$
 */
public class SharedLibClassLoaderDelegateHook implements ClassLoaderDelegateHook {

    public Class<?> preFindClass(String name, BundleClassLoader classLoader, BundleData data) throws ClassNotFoundException {
        return null;
    }

    public Class<?> postFindClass(String name, BundleClassLoader classLoader, BundleData data) throws ClassNotFoundException {
        SharedLibraryRegistry sharedLibraryRegistry = BundleHelper.getSharedLibraryRegistry();
        if (sharedLibraryRegistry == null) {
            return null;
        }
        List<Bundle> dependentSharedLibBundles = sharedLibraryRegistry.getDependentSharedLibBundles(data.getBundleID());
        if (dependentSharedLibBundles != null) {
            for (Bundle sharedLibBundle : dependentSharedLibBundles) {
                try {
                    return sharedLibBundle.loadClass(name);
                } catch (ClassNotFoundException e) {
                }
            }
        }
        return null;
    }

    public URL preFindResource(String name, BundleClassLoader classLoader, BundleData data) throws FileNotFoundException {
        return null;
    }

    public URL postFindResource(String name, BundleClassLoader classLoader, BundleData data) throws FileNotFoundException {
        SharedLibraryRegistry sharedLibraryRegistry = BundleHelper.getSharedLibraryRegistry();
        if (sharedLibraryRegistry == null) {
            return null;
        }
        List<Bundle> dependentSharedLibBundles = sharedLibraryRegistry.getDependentSharedLibBundles(data.getBundleID());
        if (dependentSharedLibBundles != null) {
            for (Bundle sharedLibBundle : dependentSharedLibBundles) {
                URL url = sharedLibBundle.getResource(name);
                if (url != null) {
                    return url;
                }
            }
        }
        return null;
    }

    public Enumeration<URL> preFindResources(String name, BundleClassLoader classLoader, BundleData data) throws FileNotFoundException {
        return null;
    }

    public Enumeration<URL> postFindResources(String name, BundleClassLoader classLoader, BundleData data) throws FileNotFoundException {
        return null;
    }

    public String preFindLibrary(String name, BundleClassLoader classLoader, BundleData data) throws FileNotFoundException {
        return null;
    }

    public String postFindLibrary(String name, BundleClassLoader classLoader, BundleData data) {
        return null;
    }

    protected Class<?> loadClassFromSharedLib(String name, BundleClassLoader classLoader, BundleData data) {
        return null;
    }

}

@@ -905,67 +905,179 @@
 ava.
-net.URL;%0Aimport java.util.Enumeration;%0Aimport java.util.Lis
+io.IOException;%0Aimport java.net.URL;%0Aimport java.util.Collections;%0Aimport java.util.Enumeration;%0Aimport java.util.LinkedHashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0A
@@ -3609,35 +3609,1012 @@
 n %7B%0A        
-return null
+SharedLibraryRegistry sharedLibraryRegistry = BundleHelper.getSharedLibraryRegistry();%0A        if (sharedLibraryRegistry == null) %7B%0A            return null;%0A        %7D%0A        List%3CBundle%3E dependentSharedLibBundles = sharedLibraryRegistry.getDependentSharedLibBundles(data.getBundleID());%0A        if (dependentSharedLibBundles == null %7C%7C dependentSharedLibBundles.isEmpty()) %7B%0A            return null;%0A        %7D%0A        Set%3CURL%3E foundResources = new LinkedHashSet%3CURL%3E();%0A        for (Bundle sharedLibBundle : dependentSharedLibBundles) %7B%0A            try %7B%0A                Enumeration%3CURL%3E en = sharedLibBundle.getResources(name);%0A                if (en == null) %7B%0A                    continue;%0A                %7D%0A                while (en.hasMoreElements()) %7B%0A                    foundResources.add(en.nextElement());%0A                %7D%0A            %7D catch (IOException e) %7B%0A                //ignore this bundle%0A            %7D%0A        %7D%0A        return Collections.enumeration(foundResources)
 ;%0A    %7D%0A%0A   

Support getResources method for shared library