cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Niclas Hedhman <nic...@localbar.com>
Subject Re: [C2 Sitemap] Classloading comment
Date Thu, 22 Jun 2000 14:12:18 GMT
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();
        }
    }
}



Mime
View raw message