You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Norman Franke <no...@myasd.com> on 2012/08/16 00:33:24 UTC

T5 Auto Binding of DAOs

Something similar was asked in another thread, but I figured others may find this helpful.

I wanted to automatically bind my interfaces and implementations of my numerous DAOs in my AppModule. It's pretty easy, so here is what I idd. This is called from the bind static method. Obviously you'd need to change the package names. My naming convention is to put the implementation packages in a sub ".impl" package, e.g. com.myasd.db.dao.impl in my case. Classes then are suffixed with "Impl" to make Eclipse's package auto-complete suggestions work better. I've been using this for a few years, at lest.

    @SuppressWarnings({ "unchecked", "rawtypes" })
    static void bindDAOs(ServiceBinder binder) {
        try {
            ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
            List<Class<?>> daos = PackageEnumerator.getClassesForPackage("com.myasd.db.dao");
            for (Class<?> dao : daos) {
                String pkg = dao.getPackage().getName();
                String cls = dao.getSimpleName();
                try {
                    Class impl = contextClassLoader.loadClass(pkg+".impl."+cls+"Impl");
                    binder.bind(dao, impl).scope(ScopeConstants.PERTHREAD);
                } catch (ClassNotFoundException e) {
                    // Ignore, we just won't bind that class.
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


Helper method borrowed from who-knows-where:

    public static List<Class<?>> getClassesForPackage(String packageName) throws ClassNotFoundException {
        // This will hold a list of directories matching the pckgname.
        // There may be more than one if a package is split over multiple
        // jars/paths
        List<Class<?>> classes = new ArrayList<Class<?>>();
        ArrayList<File> directories = new ArrayList<File>();
        try {
            ClassLoader cld = Thread.currentThread().getContextClassLoader();
            if (cld == null) {
                throw new ClassNotFoundException("Can't get class loader.");
            }
            // Ask for all resources for the path
            Enumeration<URL> resources = cld.getResources(packageName.replace('.', '/'));
            while (resources.hasMoreElements()) {
                URL res = resources.nextElement();
                if (res.getProtocol().equalsIgnoreCase("jar")) {
                    JarURLConnection conn = (JarURLConnection) res.openConnection();
                    JarFile jar = conn.getJarFile();
                    for (JarEntry e : Collections.list(jar.entries())) {

                        if (e.getName().startsWith(packageName.replace('.', '/')) && e.getName().endsWith(".class")
                                && !e.getName().contains("$")) {
                            String className = e.getName().replace("/", ".").substring(0, e.getName().length() - 6);
                            System.out.println(className);
                            classes.add(Class.forName(className));
                        }
                    }
                } else
                    directories.add(new File(URLDecoder.decode(res.getPath(), "UTF-8")));
            }
        } catch (NullPointerException x) {
            throw new ClassNotFoundException(packageName + " does not appear to be "
                    + "a valid package (Null pointer exception)");
        } catch (UnsupportedEncodingException encex) {
            throw new ClassNotFoundException(packageName + " does not appear to be "
                    + "a valid package (Unsupported encoding)");
        } catch (IOException ioex) {
            throw new ClassNotFoundException("IOException was thrown when trying " + "to get all resources for "
                    + packageName);
        }

        // For every directory identified capture all the .class files
        for (File directory : directories) {
            if (directory.exists()) {
                // Get the list of the files contained in the package
                String[] files = directory.list();
                for (String file : files) {
                    // we are only interested in .class files
                    if (file.endsWith(".class")) {
                        // removes the .class extension
                        classes.add(Class.forName(packageName + '.' + file.substring(0, file.length() - 6)));
                    }
                }
            } else {
                throw new ClassNotFoundException(packageName + " (" + directory.getPath()
                        + ") does not appear to be a valid package");
            }
        }
        return classes;
    }

Norman Franke
Answering Service for Directors, Inc.
www.myasd.com




Re: T5 Auto Binding of DAOs

Posted by Dmitry Gusev <dm...@gmail.com>.
There's a typo in the code snippet, should be:

String className = interfacesPackage.getName() + ".impl." +
intf.getSimpleName() + "Impl";

We need just package name, not its toString() presentation which is
"package com.x.y.z"

On Thu, Aug 16, 2012 at 11:34 AM, Dmitry Gusev <dm...@gmail.com>wrote:

> Thanks for sharing, Norman!
>
> Here's mine implementation:
>
>     public static void bind(ServiceBinder binder) throwsClassNotFoundException
>
>     {
>
>         autobindServices(binder, ProjectDAO.class.getPackage());
>
>         autobindServices(binder, ProjectManager.class.getPackage());
>
>
>
>         ...
>
>     }
>
>
>     @SuppressWarnings({"unchecked", "rawtypes"})
>
>     private static void autobindServices(ServiceBinder binder, Package
> interfacesPackage) throws ClassNotFoundException
>
>     {
>
>             List<Class<?>> interfaces =
> Utils.getClassesForPackage(interfacesPackage.getName());
>
>             for (Class intf : interfaces) {
>
>                 String className = interfacesPackage + ".impl." +
> intf.getSimpleName() + "Impl";
>
>                 try {
>
>                     Class impl = Class.forName(className);
>
>                     binder.bind(intf, impl);
>
>                 }
>
>                 catch (ClassNotFoundException e) {
>
>                     logger.warn("Class not found during autobinding: {}",
> className);
>
>                 }
>
>             }
>
>     }
>
>
> On Thu, Aug 16, 2012 at 2:33 AM, Norman Franke <no...@myasd.com> wrote:
>
>> Something similar was asked in another thread, but I figured others may
>> find this helpful.
>>
>> I wanted to automatically bind my interfaces and implementations of my
>> numerous DAOs in my AppModule. It's pretty easy, so here is what I idd.
>> This is called from the bind static method. Obviously you'd need to change
>> the package names. My naming convention is to put the implementation
>> packages in a sub ".impl" package, e.g. com.myasd.db.dao.impl in my case.
>> Classes then are suffixed with "Impl" to make Eclipse's package
>> auto-complete suggestions work better. I've been using this for a few
>> years, at lest.
>>
>>     @SuppressWarnings({ "unchecked", "rawtypes" })
>>     static void bindDAOs(ServiceBinder binder) {
>>         try {
>>             ClassLoader contextClassLoader =
>> Thread.currentThread().getContextClassLoader();
>>             List<Class<?>> daos =
>> PackageEnumerator.getClassesForPackage("com.myasd.db.dao");
>>             for (Class<?> dao : daos) {
>>                 String pkg = dao.getPackage().getName();
>>                 String cls = dao.getSimpleName();
>>                 try {
>>                     Class impl =
>> contextClassLoader.loadClass(pkg+".impl."+cls+"Impl");
>>                     binder.bind(dao,
>> impl).scope(ScopeConstants.PERTHREAD);
>>                 } catch (ClassNotFoundException e) {
>>                     // Ignore, we just won't bind that class.
>>                 }
>>             }
>>         } catch (ClassNotFoundException e) {
>>             e.printStackTrace();
>>         }
>>     }
>>
>>
>> Helper method borrowed from who-knows-where:
>>
>>     public static List<Class<?>> getClassesForPackage(String packageName)
>> throws ClassNotFoundException {
>>         // This will hold a list of directories matching the pckgname.
>>         // There may be more than one if a package is split over multiple
>>         // jars/paths
>>         List<Class<?>> classes = new ArrayList<Class<?>>();
>>         ArrayList<File> directories = new ArrayList<File>();
>>         try {
>>             ClassLoader cld =
>> Thread.currentThread().getContextClassLoader();
>>             if (cld == null) {
>>                 throw new ClassNotFoundException("Can't get class
>> loader.");
>>             }
>>             // Ask for all resources for the path
>>             Enumeration<URL> resources =
>> cld.getResources(packageName.replace('.', '/'));
>>             while (resources.hasMoreElements()) {
>>                 URL res = resources.nextElement();
>>                 if (res.getProtocol().equalsIgnoreCase("jar")) {
>>                     JarURLConnection conn = (JarURLConnection)
>> res.openConnection();
>>                     JarFile jar = conn.getJarFile();
>>                     for (JarEntry e : Collections.list(jar.entries())) {
>>
>>                         if
>> (e.getName().startsWith(packageName.replace('.', '/')) &&
>> e.getName().endsWith(".class")
>>                                 && !e.getName().contains("$")) {
>>                             String className = e.getName().replace("/",
>> ".").substring(0, e.getName().length() - 6);
>>                             System.out.println(className);
>>                             classes.add(Class.forName(className));
>>                         }
>>                     }
>>                 } else
>>                     directories.add(new
>> File(URLDecoder.decode(res.getPath(), "UTF-8")));
>>             }
>>         } catch (NullPointerException x) {
>>             throw new ClassNotFoundException(packageName + " does not
>> appear to be "
>>                     + "a valid package (Null pointer exception)");
>>         } catch (UnsupportedEncodingException encex) {
>>             throw new ClassNotFoundException(packageName + " does not
>> appear to be "
>>                     + "a valid package (Unsupported encoding)");
>>         } catch (IOException ioex) {
>>             throw new ClassNotFoundException("IOException was thrown when
>> trying " + "to get all resources for "
>>                     + packageName);
>>         }
>>
>>         // For every directory identified capture all the .class files
>>         for (File directory : directories) {
>>             if (directory.exists()) {
>>                 // Get the list of the files contained in the package
>>                 String[] files = directory.list();
>>                 for (String file : files) {
>>                     // we are only interested in .class files
>>                     if (file.endsWith(".class")) {
>>                         // removes the .class extension
>>                         classes.add(Class.forName(packageName + '.' +
>> file.substring(0, file.length() - 6)));
>>                     }
>>                 }
>>             } else {
>>                 throw new ClassNotFoundException(packageName + " (" +
>> directory.getPath()
>>                         + ") does not appear to be a valid package");
>>             }
>>         }
>>         return classes;
>>     }
>>
>> Norman Franke
>> Answering Service for Directors, Inc.
>> www.myasd.com
>>
>>
>>
>>
>
>
> --
> Dmitry Gusev
>
> AnjLab Team
> http://anjlab.com
>



-- 
Dmitry Gusev

AnjLab Team
http://anjlab.com

Re: T5 Auto Binding of DAOs

Posted by Dmitry Gusev <dm...@gmail.com>.
Thanks for sharing, Norman!

Here's mine implementation:

    public static void bind(ServiceBinder binder) throwsClassNotFoundException

    {

        autobindServices(binder, ProjectDAO.class.getPackage());

        autobindServices(binder, ProjectManager.class.getPackage());



        ...

    }


    @SuppressWarnings({"unchecked", "rawtypes"})

    private static void autobindServices(ServiceBinder binder, Package
interfacesPackage) throws ClassNotFoundException

    {

            List<Class<?>> interfaces =
Utils.getClassesForPackage(interfacesPackage.getName());

            for (Class intf : interfaces) {

                String className = interfacesPackage + ".impl." +
intf.getSimpleName() + "Impl";

                try {

                    Class impl = Class.forName(className);

                    binder.bind(intf, impl);

                }

                catch (ClassNotFoundException e) {

                    logger.warn("Class not found during autobinding: {}",
className);

                }

            }

    }


On Thu, Aug 16, 2012 at 2:33 AM, Norman Franke <no...@myasd.com> wrote:

> Something similar was asked in another thread, but I figured others may
> find this helpful.
>
> I wanted to automatically bind my interfaces and implementations of my
> numerous DAOs in my AppModule. It's pretty easy, so here is what I idd.
> This is called from the bind static method. Obviously you'd need to change
> the package names. My naming convention is to put the implementation
> packages in a sub ".impl" package, e.g. com.myasd.db.dao.impl in my case.
> Classes then are suffixed with "Impl" to make Eclipse's package
> auto-complete suggestions work better. I've been using this for a few
> years, at lest.
>
>     @SuppressWarnings({ "unchecked", "rawtypes" })
>     static void bindDAOs(ServiceBinder binder) {
>         try {
>             ClassLoader contextClassLoader =
> Thread.currentThread().getContextClassLoader();
>             List<Class<?>> daos =
> PackageEnumerator.getClassesForPackage("com.myasd.db.dao");
>             for (Class<?> dao : daos) {
>                 String pkg = dao.getPackage().getName();
>                 String cls = dao.getSimpleName();
>                 try {
>                     Class impl =
> contextClassLoader.loadClass(pkg+".impl."+cls+"Impl");
>                     binder.bind(dao, impl).scope(ScopeConstants.PERTHREAD);
>                 } catch (ClassNotFoundException e) {
>                     // Ignore, we just won't bind that class.
>                 }
>             }
>         } catch (ClassNotFoundException e) {
>             e.printStackTrace();
>         }
>     }
>
>
> Helper method borrowed from who-knows-where:
>
>     public static List<Class<?>> getClassesForPackage(String packageName)
> throws ClassNotFoundException {
>         // This will hold a list of directories matching the pckgname.
>         // There may be more than one if a package is split over multiple
>         // jars/paths
>         List<Class<?>> classes = new ArrayList<Class<?>>();
>         ArrayList<File> directories = new ArrayList<File>();
>         try {
>             ClassLoader cld =
> Thread.currentThread().getContextClassLoader();
>             if (cld == null) {
>                 throw new ClassNotFoundException("Can't get class
> loader.");
>             }
>             // Ask for all resources for the path
>             Enumeration<URL> resources =
> cld.getResources(packageName.replace('.', '/'));
>             while (resources.hasMoreElements()) {
>                 URL res = resources.nextElement();
>                 if (res.getProtocol().equalsIgnoreCase("jar")) {
>                     JarURLConnection conn = (JarURLConnection)
> res.openConnection();
>                     JarFile jar = conn.getJarFile();
>                     for (JarEntry e : Collections.list(jar.entries())) {
>
>                         if
> (e.getName().startsWith(packageName.replace('.', '/')) &&
> e.getName().endsWith(".class")
>                                 && !e.getName().contains("$")) {
>                             String className = e.getName().replace("/",
> ".").substring(0, e.getName().length() - 6);
>                             System.out.println(className);
>                             classes.add(Class.forName(className));
>                         }
>                     }
>                 } else
>                     directories.add(new
> File(URLDecoder.decode(res.getPath(), "UTF-8")));
>             }
>         } catch (NullPointerException x) {
>             throw new ClassNotFoundException(packageName + " does not
> appear to be "
>                     + "a valid package (Null pointer exception)");
>         } catch (UnsupportedEncodingException encex) {
>             throw new ClassNotFoundException(packageName + " does not
> appear to be "
>                     + "a valid package (Unsupported encoding)");
>         } catch (IOException ioex) {
>             throw new ClassNotFoundException("IOException was thrown when
> trying " + "to get all resources for "
>                     + packageName);
>         }
>
>         // For every directory identified capture all the .class files
>         for (File directory : directories) {
>             if (directory.exists()) {
>                 // Get the list of the files contained in the package
>                 String[] files = directory.list();
>                 for (String file : files) {
>                     // we are only interested in .class files
>                     if (file.endsWith(".class")) {
>                         // removes the .class extension
>                         classes.add(Class.forName(packageName + '.' +
> file.substring(0, file.length() - 6)));
>                     }
>                 }
>             } else {
>                 throw new ClassNotFoundException(packageName + " (" +
> directory.getPath()
>                         + ") does not appear to be a valid package");
>             }
>         }
>         return classes;
>     }
>
> Norman Franke
> Answering Service for Directors, Inc.
> www.myasd.com
>
>
>
>


-- 
Dmitry Gusev

AnjLab Team
http://anjlab.com

Re: T5 Auto Binding of DAOs

Posted by Norman Franke <no...@myasd.com>.
It was an internal class when I first started, and I generally won't use classes in internal packages. I see it's been moved, so that may very well be a better idea.

Norman Franke
Answering Service for Directors, Inc.
www.myasd.com



On Aug 15, 2012, at 6:43 PM, Thiago H de Paula Figueiredo wrote:

> On Wed, 15 Aug 2012 19:33:24 -0300, Norman Franke <no...@myasd.com> wrote:
> 
>> Helper method borrowed from who-knows-where:
>> 
>>    public static List<Class<?>> getClassesForPackage(String packageName) throws ClassNotFoundException {
> 
> Have you taken a look at Tapestry's ClassNameLocator and ClassNameLocatorImpl? When I need something like that, that's what I've used.
> 
> -- 
> Thiago H. de Paula Figueiredo
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


Re: T5 Auto Binding of DAOs

Posted by Muhammad Gelbana <m....@gmail.com>.
I remember tapestry that by searching for implementation classes in the
same package by the same name but suffixed by "Impl". Like "MySvc" as in
interface and "MySvcImpl" as the implementation.

1. When I wan'ted to extract that piece of info from the docs I couldn't
find it ! Is it deprecated ?!
2. The only advantage from the codes listed in this thread is placing the
implementation classes in a sub-package called "impl", correct ?

On Thu, Aug 16, 2012 at 1:51 PM, Thiago H de Paula Figueiredo <
thiagohp@gmail.com> wrote:

> On Thu, 16 Aug 2012 03:48:17 -0300, Dmitry Gusev <dm...@gmail.com>
> wrote:
>
>  Hi,
>>
>
> Hi!
>
>
>  correct me if I wrong, but you can't get instance of ClassNameLocator in
>> module's bind method, right? Since the registry is not started yet.
>>
>
> Nope, but maybe Tapestry-IoC could provide it anyway. JIRA please?
>
>
> --
> Thiago H. de Paula Figueiredo
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.**apache.org<us...@tapestry.apache.org>
>
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
*Regards,*
*Muhammad Gelbana
Java Developer*

Re: T5 Auto Binding of DAOs

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Thu, 16 Aug 2012 03:48:17 -0300, Dmitry Gusev <dm...@gmail.com>  
wrote:

> Hi,

Hi!

> correct me if I wrong, but you can't get instance of ClassNameLocator in
> module's bind method, right? Since the registry is not started yet.

Nope, but maybe Tapestry-IoC could provide it anyway. JIRA please?

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5 Auto Binding of DAOs

Posted by Dmitry Gusev <dm...@gmail.com>.
Hi,

correct me if I wrong, but you can't get instance of ClassNameLocator in
module's bind method, right?
Since the registry is not started yet.

On Thu, Aug 16, 2012 at 2:43 AM, Thiago H de Paula Figueiredo <
thiagohp@gmail.com> wrote:

> On Wed, 15 Aug 2012 19:33:24 -0300, Norman Franke <no...@myasd.com>
> wrote:
>
>  Helper method borrowed from who-knows-where:
>>
>>     public static List<Class<?>> getClassesForPackage(String packageName)
>> throws ClassNotFoundException {
>>
>
> Have you taken a look at Tapestry's ClassNameLocator and
> ClassNameLocatorImpl? When I need something like that, that's what I've
> used.
>
> --
> Thiago H. de Paula Figueiredo
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.**apache.org<us...@tapestry.apache.org>
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Dmitry Gusev

AnjLab Team
http://anjlab.com

Re: T5 Auto Binding of DAOs

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Wed, 15 Aug 2012 19:33:24 -0300, Norman Franke <no...@myasd.com> wrote:

> Helper method borrowed from who-knows-where:
>
>     public static List<Class<?>> getClassesForPackage(String  
> packageName) throws ClassNotFoundException {

Have you taken a look at Tapestry's ClassNameLocator and  
ClassNameLocatorImpl? When I need something like that, that's what I've  
used.

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5 Auto Binding of DAOs

Posted by Michael Prescott <mi...@gmail.com>.
I might.. yes.  I don't necessarily need a lot of content, but a paragraph
and a few code snippets for each service would be a fantastic jump-off
point.

One side effect of all Tapestry's injection, reflection-based config and
event binding, and literate APIs, is that the explorability suffers a bit.
 I can't rely on my IDE auto-completing the relevant events on an
interface, I've got to find some relevant documentation.

It's a very powerful thing to know what all your choices are within the IDE.

Michael

 On 15 August 2012 22:52, Thiago H de Paula Figueiredo
<th...@gmail.com>wrote:

> On Wed, 15 Aug 2012 19:47:50 -0300, Michael Prescott <
> michael.r.prescott@gmail.com> wrote:
>
>  That's very nifty, Norman.  This is a simultaneously inspiring and
>> depressing reminder that despite having used Tapestry since version 3, I
>> still feel like a total noob. :-)  I'd love to see a blog series on
>> "Services you never knew about and fun things to do with them!"
>>
>
> Would you guys buy a book with this kind of stuff?
>
>
> --
> Thiago H. de Paula Figueiredo
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.**apache.org<us...@tapestry.apache.org>
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: T5 Auto Binding of DAOs

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Wed, 15 Aug 2012 19:47:50 -0300, Michael Prescott  
<mi...@gmail.com> wrote:

> That's very nifty, Norman.  This is a simultaneously inspiring and
> depressing reminder that despite having used Tapestry since version 3, I
> still feel like a total noob. :-)  I'd love to see a blog series on
> "Services you never knew about and fun things to do with them!"

Would you guys buy a book with this kind of stuff?

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5 Auto Binding of DAOs

Posted by Michael Prescott <mi...@gmail.com>.
That's very nifty, Norman.  This is a simultaneously inspiring and
depressing reminder that despite having used Tapestry since version 3, I
still feel like a total noob. :-)  I'd love to see a blog series on
"Services you never knew about and fun things to do with them!"

Michael

On 15 August 2012 18:33, Norman Franke <no...@myasd.com> wrote:

> Something similar was asked in another thread, but I figured others may
> find this helpful.
>
> I wanted to automatically bind my interfaces and implementations of my
> numerous DAOs in my AppModule. It's pretty easy, so here is what I idd.
> This is called from the bind static method. Obviously you'd need to change
> the package names. My naming convention is to put the implementation
> packages in a sub ".impl" package, e.g. com.myasd.db.dao.impl in my case.
> Classes then are suffixed with "Impl" to make Eclipse's package
> auto-complete suggestions work better. I've been using this for a few
> years, at lest.
>
>     @SuppressWarnings({ "unchecked", "rawtypes" })
>     static void bindDAOs(ServiceBinder binder) {
>         try {
>             ClassLoader contextClassLoader =
> Thread.currentThread().getContextClassLoader();
>             List<Class<?>> daos =
> PackageEnumerator.getClassesForPackage("com.myasd.db.dao");
>             for (Class<?> dao : daos) {
>                 String pkg = dao.getPackage().getName();
>                 String cls = dao.getSimpleName();
>                 try {
>                     Class impl =
> contextClassLoader.loadClass(pkg+".impl."+cls+"Impl");
>                     binder.bind(dao, impl).scope(ScopeConstants.PERTHREAD);
>                 } catch (ClassNotFoundException e) {
>                     // Ignore, we just won't bind that class.
>                 }
>             }
>         } catch (ClassNotFoundException e) {
>             e.printStackTrace();
>         }
>     }
>
>
> Helper method borrowed from who-knows-where:
>
>     public static List<Class<?>> getClassesForPackage(String packageName)
> throws ClassNotFoundException {
>         // This will hold a list of directories matching the pckgname.
>         // There may be more than one if a package is split over multiple
>         // jars/paths
>         List<Class<?>> classes = new ArrayList<Class<?>>();
>         ArrayList<File> directories = new ArrayList<File>();
>         try {
>             ClassLoader cld =
> Thread.currentThread().getContextClassLoader();
>             if (cld == null) {
>                 throw new ClassNotFoundException("Can't get class
> loader.");
>             }
>             // Ask for all resources for the path
>             Enumeration<URL> resources =
> cld.getResources(packageName.replace('.', '/'));
>             while (resources.hasMoreElements()) {
>                 URL res = resources.nextElement();
>                 if (res.getProtocol().equalsIgnoreCase("jar")) {
>                     JarURLConnection conn = (JarURLConnection)
> res.openConnection();
>                     JarFile jar = conn.getJarFile();
>                     for (JarEntry e : Collections.list(jar.entries())) {
>
>                         if
> (e.getName().startsWith(packageName.replace('.', '/')) &&
> e.getName().endsWith(".class")
>                                 && !e.getName().contains("$")) {
>                             String className = e.getName().replace("/",
> ".").substring(0, e.getName().length() - 6);
>                             System.out.println(className);
>                             classes.add(Class.forName(className));
>                         }
>                     }
>                 } else
>                     directories.add(new
> File(URLDecoder.decode(res.getPath(), "UTF-8")));
>             }
>         } catch (NullPointerException x) {
>             throw new ClassNotFoundException(packageName + " does not
> appear to be "
>                     + "a valid package (Null pointer exception)");
>         } catch (UnsupportedEncodingException encex) {
>             throw new ClassNotFoundException(packageName + " does not
> appear to be "
>                     + "a valid package (Unsupported encoding)");
>         } catch (IOException ioex) {
>             throw new ClassNotFoundException("IOException was thrown when
> trying " + "to get all resources for "
>                     + packageName);
>         }
>
>         // For every directory identified capture all the .class files
>         for (File directory : directories) {
>             if (directory.exists()) {
>                 // Get the list of the files contained in the package
>                 String[] files = directory.list();
>                 for (String file : files) {
>                     // we are only interested in .class files
>                     if (file.endsWith(".class")) {
>                         // removes the .class extension
>                         classes.add(Class.forName(packageName + '.' +
> file.substring(0, file.length() - 6)));
>                     }
>                 }
>             } else {
>                 throw new ClassNotFoundException(packageName + " (" +
> directory.getPath()
>                         + ") does not appear to be a valid package");
>             }
>         }
>         return classes;
>     }
>
> Norman Franke
> Answering Service for Directors, Inc.
> www.myasd.com
>
>
>
>