Search code examples
jakarta-eewebjbossejbear

Get context-root of WEB module in EJB


I have an EAR project with a WEB module and EJB module. This is from application.xml:

<module>
    <ejb>PersonEJB.jar</ejb>
</module>
<module>
    <web>
      <web-uri>PersonWeb.war</web-uri>
      <context-root>PersonWeb</context-root>
   </web>
</module>

I want to write a file from EJB into WEB project and I need to find the path of WEB project for this. First though is that I need to read context-root from application.xml in EJB but I don't know how to do this. If this is not the solution for my need please tell me another one.


Solution

  • Basically you want to have a way to specify a path to your EJB without hardcoding it in any way - the path will change depending on where the application is deployed after all.

    That means making it configurable, and there are several options to go to make that happen. Here are a handful.


    Use a database

    If you already have a database, you could add a table to it which holds a simple key-value pair and specify server-specific configuration data that way. In your application you would then simply load this data once on application startup, say in a @Singleton EJB.

    This method works, but having configuration data in the database is a bit of an ugly burden as it is very application specific. Databases tend to be used for more than only one application. I don't recommend this path, unless you already have such a table to begin with.


    Use a properties file

    Another way is to store it in a properties file. Most application servers have a specific spot where you can put instance-specific configuration files and be able to load them in your application. Since this in a JBoss environment, you can put the file in the server instance's conf directory. You can get the path to the conf directory programmatically like so:

    String pathToConf = System.getProperty("jboss.server.config.dir");
    

    This gives you the ability to put configuration files there and read them back in your application, again in for example a @Singleton EJB.

    You will find a more detailed answer here which is very copy-pastable:

    wildfly: reading properties from configuration directory

    I very much prefer this option. Files are also zero effort to secure on the filesystem, the operating system does the work for you.


    Use a Java property

    If it is just the one configuration property, you could just pass it to the server instance as a command line property if you know which file to modify or which environmental variable to set to be able to do this (if not, it will be documented). You might add a parameter

    -Dmyapp.upload.dir=/path/to/dir
    

    and then in your code get this property back using the same line of code as above.

        String pathToConf = System.getProperty("myapp.upload.dir");
    

    This is the least amount of effort development-wise. I'm not very fond of this option myself, as it involves changing server configuration which might get lost outside of your control, say during a patching round.