Scripted Data Sources with BIRT
MyModel model = ...; IRunAndRenderTask task = engine.createRunAndRenderTask(design); Map parameters = new HashMap(); parameters.put("model", model); task.setParameterValues(parameters);
Cool. Now my model’s being passed to the engine. But wait. How does BIRT find my classes? I know it’s built on top of Eclipse which means the classpath is not as simple as you’d like. In some examples I found, you have to manually copy your compiled class files to a directory in one of the BIRT plugins. Uggh. This is a pain, but acceptable for deployment. But during development? I might as well go back to C++ if I want to waste that much time.
Some more searching dug up this FAQ question. It lists too yucky solutions and then has a tacked on snippet of code that looks promising.
config = new EngineConfig(); HashMap hm = config.getAppContext(); hm.put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY,MyClass.class.getClassLoader()); config.setAppContext(hm);
However, I guess I think this is for the version of BIRT I’m not using because it doesn’t work. Some more digging and I come up with this:
IRunAndRenderTask task = engine.createRunAndRenderTask(design); HashMap contextMap = new HashMap(); contextMap.put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY, MyModel.class.getClassLoader()); task.setAppContext(contextMap);
model = params["model"];
Nice. Now I write some more code to call some methods on the model and run it and… NullPointerException. This is where I was stuck for a little while. I thought that I had screwed up the class loader stuff or something so I fiddled with that stuff for a while. Eventually, when I was about to give up, I stumbled upon this bug report which, although it doesn’t mention a NullPointerException, does mention a “display name” which I had noticed references to while poking around in the debugger after my crash.
model = params["model"].value
and voila, I run it again and my report is generated as expected.
Hope this is helpful to someone else out there.