Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a file from a different war which is in the same ear on a Java EE Server (JBoss)?

So following problem: We have a load balancer with n servers behind it. On each server there is a PDF project (war) installed in ear together with a pdf-builder project in the same earfile (different war). I now want to read a pdf from the other war.

The problem is: I don't know the IP address of the server I'm running on and using the dns lookup I might end on the different server through the loadbalancer. Furthermore there might be problems with the access / security constraints since the server itself is not a valid user who has rights to make accesses to the pdf file locations of the pdf war project.

Is there a way to obtain something like getResourceAsStream() which works across different modules / war files within an ear?

like image 600
Toskan Avatar asked May 15 '12 17:05

Toskan


People also ask

Can WAR files be nested into EAR files?

No, that is not valid. An EAR file may contain other module-level archives, such as WAR, EJB JARs, RARs, or library JARs, but it may not contain other EAR files.

Where are WAR files stored?

Static HTML files and JSP are stored at the top level of the WAR directory. The top-level directory contains the WEB-INF sub-directory which contains the following: Server-side classes (Servlets, JavaBean components and related Java class files) must be stored in the WEB-INF/classes directory.


2 Answers

You can move the .PDFs to a separate jar (ie pdf-builder.jar) and put it into the .EAR In the manifest of your .WAR (META-INF/MANIFEST.MF) put this line:

Class-Path: pdf-builder.jar

Now you can load the .PDFs using your class loader. This is J2EE compliant.

like image 100
Teg Avatar answered Nov 09 '22 12:11

Teg


You need to provide absolute path to the file in order to access a resource which is not in current class path.

you can call

serverletContext.getRealpath("/");
will give the the path till the server node (context). From there you can navigate to the file.

One more way is, Jboss uses (or set) several environment variables while it bootstraps. You can use those properties in order to get the current node (like 'default'). Following two properties will give you the absolute path to current server node.

System.getProperty("jboss.server.base.dir") + File.separator + System.getProperty("jboss.server.name");
(Please use System.getenv('varname') if getProperty don't work). From there you can navigate. Here is the complete list of system varibles that jboss uses.
like image 43
RP- Avatar answered Nov 09 '22 14:11

RP-