You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Niclas Hedhman <ni...@localbar.com> on 2000/06/22 10:51:36 UTC

[C2 Sitemap] Classloading comment

<map:generator
    type="dir"
    src="file:///home/mystuff/java/MyDirGenerator.class"
/>

from the proposed sitemap.

This is a really troublesome feature. None of the existing classloaders
can handle it, and if you make your own you will have a smaller headache
to figure out which part of the path that is the package name.

I therefor suggest another generic format,

<map:generator
    type="dir"
    src="class:///mypackage.DirGenerator"
    url="file:///home/niclas/java/"
/>

If 'url' attribute is not present, the system class loader is used,
otherwise, for each URL an URLClassloader (we are Java2, right?) is
instantiated and class is loaded.


Niclas

P.S.  Also note the 3 dashes for the class: protocol. In the URL spec,
it is said that after the 2 slashes follows host name. I can go with 0,
1 or 3 dashes, but not 2.



Re: [C2 Sitemap] Classloading comment

Posted by Stefano Mazzocchi <st...@apache.org>.
Niclas Hedhman wrote:
> 
> Stefano Mazzocchi wrote:
> 
> > Niclas Hedhman wrote:
> > >
> > > <map:generator
> > >     type="dir"
> > >     src="file:///home/mystuff/java/MyDirGenerator.class"
> > > />
> > >
> > > from the proposed sitemap.
> > >
> > > This is a really troublesome feature. None of the existing classloaders
> > > can handle it, and if you make your own you will have a smaller headache
> > > to figure out which part of the path that is the package name.
> >
> > ??? the package name is contained inside the class.
> 
> I am amazed!!!  Never knew that the following code actually works...
> 
> Sorry, for not figuring this out in advance.

Hey, no problem at all :)

-- 
Stefano Mazzocchi      One must still have chaos in oneself to be
                          able to give birth to a dancing star.
<st...@apache.org>                             Friedrich Nietzsche
--------------------------------------------------------------------
 Missed us in Orlando? Make it up with ApacheCON Europe in London!
------------------------- http://ApacheCon.Com ---------------------



Re: [C2 Sitemap] Classloading comment

Posted by Niclas Hedhman <ni...@localbar.com>.
Stefano Mazzocchi wrote:

> Niclas Hedhman wrote:
> >
> > <map:generator
> >     type="dir"
> >     src="file:///home/mystuff/java/MyDirGenerator.class"
> > />
> >
> > from the proposed sitemap.
> >
> > This is a really troublesome feature. None of the existing classloaders
> > can handle it, and if you make your own you will have a smaller headache
> > to figure out which part of the path that is the package name.
>
> ??? the package name is contained inside the class.

I am amazed!!!  Never knew that the following code actually works...

Sorry, for not figuring this out in advance.

Niclas

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class CocoonClassLoader extends ClassLoader
{
    public Class loadClassFromFile( String filename )
        throws IOException
    {
        FileInputStream fis = null;
        ByteArrayOutputStream baos = null;
        Class cls = null;
        try
        {
             fis = new FileInputStream(filename);
             baos = new ByteArrayOutputStream();
            int b;
            while( (b = fis.read() ) >= 0 )
                baos.write(b);
            byte[] bytes = baos.toByteArray();
            cls = defineClass( null, bytes, 0, bytes.length );
            resolveClass( cls );
        } finally
        {
            try
            {
                if( fis != null )
                    fis.close();
                if( baos != null )
                    baos.close();
            } catch( Exception f )
            {} // ignore
        }
        return cls;
    }

    public static void main( String[] args )
    {
        try
        {
            CocoonClassLoader cl = new CocoonClassLoader();
            Class cls = cl.loadClassFromFile( args[0] );
            System.out.println( cls.getName() );
        } catch( Exception e )
        {
            e.printStackTrace();
        }
    }
}



Re: [C2 Sitemap] Classloading comment

Posted by Stefano Mazzocchi <st...@apache.org>.
Niclas Hedhman wrote:
> 
> <map:generator
>     type="dir"
>     src="file:///home/mystuff/java/MyDirGenerator.class"
> />
> 
> from the proposed sitemap.
> 
> This is a really troublesome feature. None of the existing classloaders
> can handle it, and if you make your own you will have a smaller headache
> to figure out which part of the path that is the package name.

??? the package name is contained inside the class.
 
> I therefor suggest another generic format,
> 
> <map:generator
>     type="dir"
>     src="class:///mypackage.DirGenerator"
>     url="file:///home/niclas/java/"
> />
> 
> If 'url' attribute is not present, the system class loader is used,
> otherwise, for each URL an URLClassloader (we are Java2, right?) is
> instantiated and class is loaded.
> 
> Niclas
> 
> P.S.  Also note the 3 dashes for the class: protocol. In the URL spec,
> it is said that after the 2 slashes follows host name. I can go with 0,
> 1 or 3 dashes, but not 2.

You're right for this one.

-- 
Stefano Mazzocchi      One must still have chaos in oneself to be
                          able to give birth to a dancing star.
<st...@apache.org>                             Friedrich Nietzsche
--------------------------------------------------------------------
 Missed us in Orlando? Make it up with ApacheCON Europe in London!
------------------------- http://ApacheCon.Com ---------------------