users@glassfish.java.net

External Resources

From: Daniel Cavalcanti <dhcavalcanti_at_gmail.com>
Date: Tue, 27 Feb 2007 16:52:03 -0500

Hello.
What's the best way to reference an external resource, e.g. a file, from an
EJB in a portable way.
Right now, I have the following solution:

@Stateless()
public class MyBean implements MyLocalBean {

  @Resource(name="file-name")
  prvate String fileName;

  @PostConstruct()
  private void init() {
    File file = new File(fileName);
    // process file ...
  }

}

and in ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar
    version = "3.0"
    xmlns = "http://java.sun.com/xml/ns/javaee"
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
    <enterprise-beans>
        <session>
            <ejb-name>MyBean</ejb-name>
            <env-entry>
                <env-entry-name>file-name</env-entry-name>
                <env-entry-type>java.lang.String</env-entry-type>
                <env-entry-value>the actual file name</env-entry-value>
            </env-entry>
        </session>
    </enterprise-beans>
</ejb-jar>

Now, if I have 50 beans that use that same file, I have to repeat this step
for all of them.
Worse yet, if I change the file name, I have to update the 50 entries in
ejb-jar.xml.

Is there a better way of doing this? Could you give a code sniplet example?

thanks,
Daniel.