Loading a Java Class file attached in a RT field

Guys,

I have a Java Client/Server application that needs to dynamically load a class from a Class file (or JAR file) attached in a RichText field on a NotesDocument.

Is it possible to user reflection in this case, and if so, how could I get the file to be loaded?

Thanks in advance

Subject: Loading a Java Class file attached in a RT field.

You need to create a custom classloader to make this happen.

Best thing you can do is google for “custom java classloader” and see what you get. I think sun has some good example code.

cheers,

Bram

Subject: RE: Loading a Java Class file attached in a RT field.

Hi Bram,

thanks for the tip.

I researched on that, and I could create a custom class loader. The code follows below…

Regards,

Ricardo.

import lotus.domino.*;

import java.io.*;

public class DominoAttachmentClassLoader extends ClassLoader

{

private RichTextItem item;

public DominoAttachmentClassLoader( Document doc, String itemname ) throws NotesException

{

  item = (RichTextItem)doc.getFirstItem( itemname );

}

public DominoAttachmentClassLoader( RichTextItem item ) throws NotesException

{

  this.item = (RichTextItem)item;

}

public Class findClass(String name)

{

  try

  {

    byte[] b = loadClassData(name);

    return defineClass(name, b, 0, b.length);

  }catch (NotesException n)

  {

    System.out.println(n.id + " " + n.text);

  }catch (Exception e)

  {

    e.printStackTrace();

  }

  return null;

}

private byte loadClassData( String filename ) throws NotesException, IOException

{

EmbeddedObject file = item.getEmbeddedObject( filename + ".class" );

int length = file.getFileSize();

byte[] bytes = new byte[length];



InputStream is = file.getInputStream();

int offset = 0;

int numRead = 0;

while (offset < bytes.length  && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {

    offset += numRead;

}



// Ensure all the bytes have been read in

if (offset < bytes.length) {

    throw new IOException("Could not completely read file "+file.getName());

}



// Close the input stream and return bytes

is.close();

return bytes;

}

}