Federico
Federico

Reputation: 551

Open a File within an EJB

I know that it is in general not allowed to access local Files (with java.io) within an EJB: nevertheless, I have an EJB which has to open an Excel File and edit it using the apache.poi library. If I do something like this:

@Stateless
public class MyEJB {

public void editExcel(){
...
InputStream in =  MyEJB.class.getClassLoader().getResourceAsStream("/xls/ExcelFile.xls"); 

final Workbook generatedExcel = new XLSTransformer().transformXLS(in, beans);
...

} }

The filesystem structure:

MyEar.ear
--my-ejb.jar
---com
-----company
-------ejbs
----------MyEJB.class
---xls
-----ExcelFile.xls

In this case I will get an Instance of ZipFile$ZipFileInputStream (private inner class of ZipFileInputStream) and XLSTransformer will throw an IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream"), as it expects an InputStream representing ExcelFile.xls and gets instead a stream representing the whole my-ejb.jar.

My questions: do you know how to solve this situation? What is the best practice for accessing file within EJBs?

Thanks a lot!

Upvotes: 4

Views: 1568

Answers (1)

Big Bad Baerni
Big Bad Baerni

Reputation: 996

I also had to return a modified XLS template out of a EE container once. Though I always look at such resources not as a part of the WAR/EAR deployment but as a configurable entity managed by customers.

So, one simple solution might be to save your template in a configuration directory, and provide it's path & name trough JNDI to your application.

BUT

You might also use JNDI for representing a more complex datatype representing an excel file, so your EJB code stays free of direct file operations.

You may find some pointers (for JBoss JNDI AS) here: http://middlewaremagic.com/jboss/?p=1690

Upvotes: 2

Related Questions