I just wanted to post this in case anyone else was interested and to draw any 
criticism about how this could mess something 
up or trigger armageddon. It allows a list of hivemodule.xml locations to be defined 
in a .property file.
I reason I wanted this was because I really wanted to split up the descriptors but 
still wanted all the code in the same
Eclipse project.

I realize now that I'm done that I could have used RegistryBuilder.processModule from 
code but I learned more about the
enemy..err..classloaders this way.

Also, I grabbed the latest cvs, but I can't find a maven target that actually compiles 
code. It will build dist jars but
they have no code...?

Take care,

John
package us.insurgent.hivemind;



import java.io.IOException;

import java.io.InputStream;

import java.net.URL;

import java.util.Enumeration;

import java.util.Iterator;

import java.util.Map;

import java.util.Properties;

import java.util.Vector;



import org.apache.commons.hivemind.impl.RegistryBuilder;



/**

 * 

 * Allows multiple hivemodule.xml files to be defined in

 * META-INF/hivemodulelist.properties so that the module definitions can have

 * different names and exist in different locations in the classpath.

 * <p>

 * The regular META-INF/hivemodule.xml files are still found by the parent

 * classload(s) and are not (hopefully) duplicated here. Hivemind will generate

 * a warning and pick one to use if it does.

 * <p>

 * hivemodulelist.properties: 

 * <pre>

 * core=META-INF/hivemodule-core.xml

 * module2=META-INF/hivemodule-module2.xml

 * module3=/com/example/module3/foomodule.xml

 * </pre>

 * <p>

 * The key names in the properties file are irrelevant but must be unique.

 * <p>

 * The modulename.properties file should be co-located with the modulename.xml.

 * Hivemind seems to find them OK.

 * <p>

 * Multiple META-INF/hivemodulelist.properties files in the classpath can be

 * found and will be processed.

 * <p>

 * Example usage:

 * <pre>

 *      ClassResolver resolver = new DefaultClassResolver(new 
HiveModuleListClassLoader());

 *      RegistryBuilder builder = new RegistryBuilder();

 *      ...

 * </pre>

 * 

 * @author John Rubier

 *  

 */

public class HiveModuleListClassLoader extends ClassLoader {



    ClassLoader _parent;



    public static final String HIVE_MODULE_LIST = "META-INF/hivemodulelist.properties";



    public HiveModuleListClassLoader() {

        super();

        _parent = super.getParent();



    }



    public HiveModuleListClassLoader(ClassLoader parent) {

        super(parent);

        _parent = parent;

        findResource("test");

    }



    /**

     * Finds and parses hivemodulelist.properties file for hivemodule locations

     * when called with

     * [EMAIL PROTECTED] org.apache.commons.hivemind.impl.RegistryBuilder#HIVE_MODULE}

     * 

     * @see java.lang.ClassLoader#findResources(java.lang.String)

     *  

     */

    protected Enumeration findResources(String name) throws IOException {

        if (_parent != null && name.equals(RegistryBuilder.HIVE_MODULE)) {

            Enumeration moduleLists = _parent.getResources(HIVE_MODULE_LIST);

            Vector modules = new Vector();

            while (moduleLists.hasMoreElements()) {

                URL descriptorURL = (URL) moduleLists.nextElement();

                InputStream stream = descriptorURL.openStream();

                Properties props = new Properties();

                props.load(stream);

                stream.close();

                for (Iterator iter = props.entrySet().iterator(); iter

                        .hasNext();) {

                    String element = (String) ((Map.Entry) iter.next())

                            .getValue();

                    URL module = _parent.getResource(element);

                    if (module != null) {

                        modules.add(module);

                    }

                }

            }

            return modules.elements();

        }

        return null;

    }

}


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to