Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load files into session bean

I have a java EE application EE5 EJB3. I develop using NetBeans 6.7 and GlassFish 2.x I need to have a configuration file (*.xsl, *.xml) that is deployment/client specific.

My questions are:

1) where do I put files that are external to the ear file?

2) How do I load the files into a session bean? can I use injection?

I manage to inject a @Resource for the file name using the ejb-jar.xml.

Many thanks in advance. G.

like image 549
Gadi Avatar asked Feb 27 '23 21:02

Gadi


2 Answers

I guess this is not what you are expecting but the right answer is that you shouldn't do it! According to the EJB specifications and more precisely the Programming Restrictions:

An enterprise bean must not use the java.io package to attempt to access files and directories in the file system.

And this statement is followed by this explanation:

The file system APIs are not well-suited for business components to access data. Business components should use a resource manager API, such as JDBC, to store data.

The reasons behind this statement are:

  1. Accessing the file system isn't transactional and would compromise component distributability.
  2. Accessing the file system from an EJB would compromise its deployability (the resource isn't under the EJB container's control and the EJB can't be moved easily in a cluster).
  3. Accessing the file system is a potential security hole.

Now that you know this, if you still want to do what you had in mind and if your EJB container doesn't restrict using classes from the java.io package, then I would put a read-only file on the classpath, preferably in a JAR, and access it using the getResource() or getResourceAsStream() methods of java.lang.Class. But really, you should keep the specification in mind, it's there to help you build portable applications.

like image 186
Pascal Thivent Avatar answered Mar 06 '23 17:03

Pascal Thivent


If you can assemble one EAR per target deployment (maybe maven profile can help in this area), then you can then load it like a resource.

Another option would be to have a look at J2EE Application Deployment Specification (JSR-88) to have one EAR with N deployment plan per environment.

You can also decide to store the file on the filesystem (even though it's prohibited). If you want the path to be in the ejb.xml then you need again to assemble or deploy the EAR in different ways - no big gain then. Another option would then to use a Glassfish Custom JNDI Resource to have the possibility to configure the path right from the admin console. The your app. can load the file according to the path that is configured.

like image 30
ewernli Avatar answered Mar 06 '23 16:03

ewernli